Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*-
   * See the file LICENSE for redistribution information.
   *
   * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
   *
   */
  
  package com.sleepycat.je.rep.util;
  
 import java.util.Set;
 
DbGroupAdmin supplies the functionality of the administrative class ReplicationGroupAdmin in a convenient command line utility. For example, it can be used to display replication group information, or to remove a node from the replication group.

Note: This utility does not handle security and authorization. It is left to the user to ensure that the utility is invoked with proper authorization.

See main(java.lang.String[]) for a full description of the command line arguments.

 
 public class DbGroupAdmin {
 
     enum Command { DUMP, REMOVE, TRANSFER_MASTER, UPDATE_ADDRESS };
 
     private String groupName;
     private Set<InetSocketAddresshelperSockets;
     private String nodeName;
     private String newHostName;
     private int newPort;
     private String timeout;
     private boolean forceFlag;
     private ReplicationGroupAdmin groupAdmin;
     private final ArrayList<Commandactions = new ArrayList<Command>();
 
     private static final String usageString =
         "Usage: " + CmdUtil.getJavaCommand(DbGroupAdmin.class) + "\n" +
         "  -groupName <group name>   # name of replication group\n" +
         "  -helperHosts <host:port>  # identifier for one or more members\n" +
         "                            # of the replication group which can\n"+
         "                            # be contacted for group information,\n"+
         "                            # in this format:\n" +
         "                            # hostname[:port][,hostname[:port]]\n" +
         "  -dumpGroup                # dump group information\n" +
         "  -removeMember <node name> # node to be removed\n" +
         "  -updateAddress <node name> <new host:port>\n" +
         "                            # update the network address for a\n " +
         "                            # specified node.  The node should not\n" +
         "                            # be alive when updating the address\n" +
         "  -transferMaster [-force] <node1,node2,...> <timeout>\n" +
         "                            # transfer master role to one of the\n" +
         "                            # specified nodes.";

    
Usage:
 java {com.sleepycat.je.rep.util.DbGroupAdmin |
       -jar je-<version>.jar DbGroupAdmin}
   -groupName <group name>  # name of replication group
   -helperHosts <host:port> # identifier for one or more members
                            # of the replication group which can be
                            # contacted for group information, in
                            # this format:
                            # hostname[:port][,hostname[:port]]*
   -dumpGroup               # dump group information
   -removeMember <node name># node to be removed
   -updateAddress <node name> <new host:port>
                            # update the network address for a specified
                            # node. The node should not be alive when
                            # updating address
   -transferMaster [-force] <node1,node2,...> <timeout>                         
 
 
     public static void main(String... args)
         throws Exception {
 
         DbGroupAdmin admin = new DbGroupAdmin();
         admin.parseArgs(args);
         admin.run();
     }

    
Print usage information for this utility.

Parameters:
message
    private void printUsage(String msg) {
        if (msg != null) {
            ..println(msg);
        }
        ..println();
        System.exit(-1);
    }


    
Parse the command line parameters.

Parameters:
argv Input command line parameters.
    private void parseArgs(String argv[]) {
        int argc = 0;
        int nArgs = argv.length;
        if (nArgs == 0) {
            printUsage(null);
            System.exit(0);
        }
        while (argc < nArgs) {
            String thisArg = argv[argc++];
            if (thisArg.equals("-groupName")) {
                if (argc < nArgs) {
                     = argv[argc++];
                } else {
                    printUsage("-groupName requires an argument");
                }
            } else if (thisArg.equals("-helperHosts")) {
                if (argc < nArgs) {
                     = HostPortPair.getSockets(argv[argc++]);
                } else {
                    printUsage("-helperHosts requires an argument");
                }
            } else if (thisArg.equals("-dumpGroup")) {
                .add(.);
            } else if (thisArg.equals("-removeMember")) {
                if (argc < nArgs) {
                     = argv[argc++];
                    .add(.);
                } else {
                    printUsage("-removeMember requires an argument");
                }
            } else if (thisArg.equals("-updateAddress")) {
                if (argc < nArgs) {
                     = argv[argc++];
                    if (argc < nArgs) {
                        String hostPort = argv[argc++];
                        int index = hostPort.indexOf(":");
                        if (index < 0) {
                            printUsage("Host port pair format must be " +
                                       "<host name>:<port number>");
                        }
                         = hostPort.substring(0, index);
                         = Integer.parseInt
                            (hostPort.substring(index + 1, hostPort.length()));
                    } else {
                        printUsage("-updateAddress requires a " +
                                   "<host name>:<port number> argument");
                    }
                    .add(.);
                } else {
                    printUsage
                        ("-updateAddress requires the node name argument");
                }
            } else if (thisArg.equals("-transferMaster")) {
                // TODO: it wouldn't be too hard to allow "-force" as a
                // node name.
                // 
                if (argc < nArgs && "-force".equals(argv[argc])) {
                     = true;
                    argc++;
                }
                if (argc + 1 < nArgs) {
                     = argv[argc++];
                    /*
                     * Allow either
                     *     -transferMaster mercury,venus 900 ms
                     * or
                     *     -transferMaster mercury,venus "900 ms"
                     */
                    if (argc + 1 < nArgs && argv[argc + 1].charAt(0) != '-') {
                         = argv[argc] + " " + argv[argc + 1];
                        argc += 2;
                    } else {
                         = argv[argc++];
                    }
                    .add(.);
                } else {
                    printUsage
                        ("-transferMaster requires at least two arguments");
                }
            } else {
                printUsage(thisArg + " is not a valid argument");
            }
        }
    }
    /* Execute commands */
    private void run()
        throws Exception {
        createGroupAdmin();
        if (.size() == 0) {
            return;
        }
        for (Command action : ) {
            /* Dump the group information. */
            if (action == .) {
                dumpGroup();
            }
            /* Remove a member. */
            if (action == .) {
                removeMember();
            }
            /* Transfer the current mastership to a specified node. */
            if (action == .) {
                transferMaster();
            }
            /* Update the network address of a specified node. */
            if (action == .) {
                updateAddress();
            }
        }
    }
    private DbGroupAdmin() {
    }

    
Create a DbGroupAdmin instance for programmatic use.

Parameters:
groupName replication group name
helperSockets set of host and port pairs for group members which can be queried to obtain group information.
    public DbGroupAdmin(String groupName,
                        Set<InetSocketAddresshelperSockets) {
        this. = groupName;
        this. = helperSockets;
        createGroupAdmin();
    }
    /* Create the ReplicationGroupAdmin object. */
    private void createGroupAdmin() {
        if ( == null) {
            printUsage("Group name must be specified");
        }
        if (( == null) || (.size() == 0)) {
            printUsage("Host and ports of helper nodes must be specified");
        }
    }

    
Display group information. Lists all members and the group master. Can be used when reviewing the group configuration.
    public void dumpGroup() {
    }

    
Remove a node from the replication group. Once removed, a node cannot be added again to the group under the same node name.

Parameters:
name name of the node to be removed
See also:
ReplicationGroupAdmin.removeMember(java.lang.String)
    public void removeMember(String name) {
        if (name == null) {
            printUsage("Node name must be specified");
        }
        .removeMember(name);
    }

    
Update the network address for a specified node. When updating the address of a node, the node cannot be alive. See ReplicationGroupAdmin.updateAddress(java.lang.String,java.lang.String,int) for more information.

Parameters:
nodeName the name of the node whose address will be updated
newHostName the new host name of the node
newPort the new port number of the node
    public void updateAddress(String nodeName,
                              String newHostName,
                              int newPort) {
        if (nodeName == null || newHostName == null) {
            printUsage("Node name and new host name must be specified");
        }
        if (newPort <= 0) {
            printUsage("Port of the new network address must be specified");
        }
        .updateAddress(nodeNamenewHostNamenewPort);
    }

    
Transfers the master role from the current master to one of the electable replicas specified in the argument list.

Parameters:
nodeList comma-separated list of nodes
timeout in same form as accepted by duration config params
See also:
com.sleepycat.je.rep.ReplicatedEnvironment.transferMaster(java.util.Set,int,java.util.concurrent.TimeUnit)
    public void transferMaster(String nodeListString timeout) {
        String result =
            .transferMaster(parseNodes(nodeList),
                                      PropUtil.parseDuration(timeout),
                                      .,
                                      );
        ..println("The new master is: " + result);
    }
    private Set<StringparseNodes(String nodes) {
        if (nodes == null) {
            throw new IllegalArgumentException("node list may not be null");
        }
        StringTokenizer st = new StringTokenizer(nodes",");
        Set<Stringset = new HashSet<String>();
        while (st.hasMoreElements()) {
            set.add(st.nextToken());
        }
        return set;
    }
    /*
     * This method presents group information in a user friendly way. Internal
     * fields are hidden.
     */
    private String getFormattedOutput() {
        StringBuilder sb = new StringBuilder();
        RepGroupImpl repGroupImpl = .getGroup().getRepGroupImpl();
        /* Get the master node name. */
        String masterName = .getMasterNodeName();
        /* Get the electable nodes information. */
        sb.append("\nGroup: " + repGroupImpl.getName() + "\n");
        sb.append("Electable Members:\n");
        Set<RepNodeImplnodes = repGroupImpl.getAllElectableMembers();
        if (nodes.size() == 0) {
            sb.append("    No electable members\n");
        } else {
            for (RepNodeImpl node : nodes) {
                String type =
                    masterName.equals(node.getName()) ? "master, " : "";
                sb.append("    " + node.getName() + " (" + type +
                          node.getHostName() + ":" + node.getPort() + ", " +
                          node.getBarrierState() + ")\n");
            }
        }
        /* Get the monitors information. */
        sb.append("\nMonitor Members:\n");
        nodes = repGroupImpl.getMonitorNodes();
        if (nodes.size() == 0) {
            sb.append("    No monitors\n");
        } else {
            for (RepNodeImpl node : nodes) {
                sb.append("    " + node.getName() + " (" + node.getHostName() +
                          ":" + node.getPort() + ")\n");
            }
        }
        return sb.toString();
    }
New to GrepCode? Check out our FAQ X