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.utilint;
  
  import java.io.File;
 import java.util.Date;
 

Hidden:
DbNullNode is an internal debugging aid that simply starts up a node as part of a group. It's primarily useful for diagnosing node start up bugs, since it enables you to start up the nodes in a group if you have the associated log files.

If the environment does not exist, and the -createEnv is specified it will create the environment (with the aid of helpers), using network restore to initialize it. This behavior can be used to advantage at a Replica node if the log files have been damaged for example and the Replica cannot proceed. It effectively restores the environment for the Replica, which can then proceed with the restored files using the actual application.

Similarly, if a node does not exist in the environment and -createNode is specified, it will create a new node in either the existing environment, or if -createEnv is specified in a new environment.

Note that the hostnames mentioned in the member database must be resolveable before the group can start up. The hostnames can be obtained by running DbDumpGroup. The hostnames can be "faked" on Linux, by adding appropriate entries to /etc/hosts or cnames to the DNS.

If the hosts all use the same port, you will need to make provisions for virtual ip addresses that are then associated with the host. On Linux this can be done by adding an entry of the following type for each virtual ip in /etc/network/interfaces:

iface eth0:1 inet static address 192.168.1.201 netmask 255.255.255.0 gateway 192.168.2.1 hwaddress ether XX:YY:ZZ:AA:BB:CC

Use eth0:2, eth0:3, etc for each new virtual ip that is needed. Also, substitute the real NIC address for XX:YY:ZZ:AA:BB:CC in the iface stanza. Be careful when modifying your machine's configuration information since it can have unintended side-effects. So back up the above config files before making any changes.

 
 public class DbNullNode {
     private static final String USAGE =
 
         "usage: " + CmdUtil.getJavaCommand(DbNullNode.class) + "\n" +
         "       -h <env home dir>\n" +
         "       -nodeName <nodeName>\n" +
         "       [-groupName <groupName>]\n" +
         "       [-hostPort <hostPort>]\n" +
         "       [-createEnv]\n" +
         "       [-createNode]\n" +
         "       [-helpers <hostPort>,<hostPort> ...]\n" +
         "       [-designatedPrimary] \n" +
         "       [-electableGroupSize] <groupSize> \n";
 
     private File envHome;
     private String nodeName;
     private String hostPort;
     private String groupName;
     public  String helpers;
 
     /* Permit creation of a new environment. */
     private boolean createNode = false;
     private boolean createEnv = false;
 
     private final int pollIntervalMs = 60*1000;
 
     private boolean designatedPrimary = false;
 
     private int electableGroupSize = 0;
 
     public static void main(String[] argv) {
 
        DbNullNode runAction = new DbNullNode();
        runAction.parseArgs(argv);
        try {
            runAction.run();
            System.exit(0);
        } catch (Throwable e) {
            e.printStackTrace(.);
            System.exit(1);
        }
    }
    private void parseArgs(String argv[]) {
        int argc = 0;
        int nArgs = argv.length;
        if (nArgs < 4) {
            printUsage(null);
            System.exit(0);
        }
        while (argc < nArgs) {
            String thisArg = argv[argc++];
            if (thisArg.equals("-h")) {
                if (argc < nArgs) {
                     = new File(argv[argc++]);
                } else {
                    printUsage("-h requires an argument");
                }
            } else if (thisArg.equals("-nodeName")) {
                if (argc < nArgs) {
                     = argv[argc++];
                } else {
                    printUsage("-nodeName requires an argument");
                }
            } else if (thisArg.equals("-hostPort")) {
                if (argc < nArgs) {
                     = argv[argc++];
                } else {
                    printUsage("-hostPort requires an argument");
                }
            } else if (thisArg.equals("-groupName")) {
                if (argc < nArgs) {
                     = argv[argc++];
                } else {
                    printUsage("-groupName requires an argument");
                }
            } else if (thisArg.equals("-createNode")) {
                  = true;
            } else if (thisArg.equals("-createEnv")) {
                  = true;
            } else if (thisArg.equals("-helpers")) {
                if (argc < nArgs) {
                     = argv[argc++];
                } else {
                    printUsage("-helpers requires an argument");
                }
            } else if ("-designatedPrimary".equals(thisArg)) {
                 = true ;
            }  else if ("-electableGroupSize".equals(thisArg)) {
                if (argc < nArgs) {
                     = Integer.parseInt(argv[argc++]);
                } else {
                    printUsage("-electableGroupSize requires a group size " +
                    		"argument");
                }
            } else {
                printUsage(thisArg + " is not a valid argument");
            }
        }
        if () {
            /* Verify that the create arguments are all specified. */
            if (( == null) ||
                ( == null) ||
                ( == null) ||
                (  == null)) {
                printUsage("groupName, nodeName, nodeHost and helpers " +
                           "must all be specified when using -createNode");
            }
        }
        if () {
            if (( == null) || (  == null)) {
                printUsage("groupName and helpers " +
                    "must all be specified when using -createEnv");
            }
        }
    }
    private void run() {
        while (true) {
            try {
                checkParameters();
                openAndIdle();
                break;
            }  catch (InsufficientLogException ile) {
                ..println("Restoring environment:" + );
                NetworkRestore networkRestore = new NetworkRestore();
                NetworkRestoreConfig config = new NetworkRestoreConfig();
                networkRestore.execute(ileconfig);
                ..println("Restored environment:" + );
                 = false/* The environment has been created. */
                continue;
            }
        }
    }
    /*
     * Verifies that the parameters supplied on the command line are consistent
     * with any stored state in the environment.
     */
    private void checkParameters() {
        try {
            if (!.exists()) {
                printUsage("Directory:" +  + " does not exist.");
            }
            final RepGroupImpl group = RepGroupDB.getGroup();
            if () {
                printUsage("Environment exists:" +  +
                           "but -createEnv was specified.");
            }
            final RepNodeImpl node = group.getNode();
            if (node == null) {
                if (! ) {
                    printUsage("The node:" +  +
                               " is not a member of the group:" + group +
                               ". Use -createNode to create a new one.");
                }
            } else {
                /* Node exists, check arguments if any. */
                if ( == null) {
                     = group.getName();
                } else if (!.equals(group.getName())) {
                    printUsage("-groupname:" +  +
                               ", does not match the name:" + group.getName() +
                               " in the environment.");
                }
                if ( == null) {
                     = node.getHostPortPair();
                } else if (!.equals(node.getHostPortPair())) {
                    ..println("-hostPort:" +  +
                                       ", does not match the hostPort:" +
                                       node.getHostPortPair() +
                                       " in the environment. " +
                                       "Continuing ...");
                }
            }
        } catch (EnvironmentNotFoundException enf) {
            if (!) {
                printUsage("No existing environment:" +  +
                           ". Use -createEnv to create one");
            }
        }
    }

    
Opens a replicated environment and idles in a loop printing out its state periodically.

Throws:
com.sleepycat.je.rep.InsufficientLogException so that the log files can be restored if necessary
    private void openAndIdle()
        throws InsufficientLogException {
        ReplicationConfig repConfig = new ReplicationConfig();
        repConfig.setNodeName();
        repConfig.setGroupName();
        repConfig.setNodeHostPort();
        repConfig.setDesignatedPrimary();
        if ( != null) {
            repConfig.setHelperHosts();
        }
        EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setTransactional(true);
        envConfig.setAllowCreate();
        ReplicatedEnvironment repEnv =
            new ReplicatedEnvironment(repConfigenvConfig);
        RepNode repNode = RepInternal.getRepImpl(repEnv).getRepNode();
        ..println("Handle created:" + repEnv +
                           "  Node idling indefinitely...");
        try {
            while (true) {
                ..println(new Date() +
                                   " State:" + repEnv.getState() + " " +
                                   " VLSN range:" +
                                   repNode.getVLSNIndex().getRange() +
                                   repNode.dumpState());
                Thread.sleep();
            }
        } catch (InterruptedException e) {
            ..println("Exiting");
        }
    }
    private void printUsage(String msg) {
        if (msg != null) {
            ..println(msg);
        }
        ..println();
        System.exit(-1);
    }
New to GrepCode? Check out our FAQ X