Esempio n. 1
0
  public int init(RepConfig config) throws DatabaseException {
    int ret = 0;
    appConfig = config;
    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setErrorStream(System.err);
    envConfig.setErrorPrefix(RepConfig.progname);

    envConfig.setReplicationManagerLocalSite(appConfig.getThisHost());
    for (RepRemoteHost host = appConfig.getFirstOtherHost();
        host != null;
        host = appConfig.getNextOtherHost()) {
      envConfig.replicationManagerAddRemoteSite(host.getAddress(), host.isPeer());
    }
    if (appConfig.totalSites > 0) envConfig.setReplicationNumSites(appConfig.totalSites);

    /*
     * Set replication group election priority for this environment.
     * An election first selects the site with the most recent log
     * records as the new master.  If multiple sites have the most
     * recent log records, the site with the highest priority value
     * is selected as master.
     */
    envConfig.setReplicationPriority(appConfig.priority);

    envConfig.setCacheSize(RepConfig.CACHESIZE);
    envConfig.setTxnNoSync(true);

    envConfig.setEventHandler(new RepQuoteEventHandler());

    /*
     * Set the policy that determines how master and client sites
     * handle acknowledgement of replication messages needed for
     * permanent records.  The default policy of "quorum" requires only
     * a quorum of electable peers sufficient to ensure a permanent
     * record remains durable if an election is held.  The "all" option
     * requires all clients to acknowledge a permanent replication
     * message instead.
     */
    envConfig.setReplicationManagerAckPolicy(appConfig.ackPolicy);

    /*
     * Set the threshold for the minimum and maximum time the client
     * waits before requesting retransmission of a missing message.
     * Base these values on the performance and load characteristics
     * of the master and client host platforms as well as the round
     * trip message time.
     */
    envConfig.setReplicationRequestMin(20000);
    envConfig.setReplicationRequestMax(500000);

    /*
     * Configure deadlock detection to ensure that any deadlocks
     * are broken by having one of the conflicting lock requests
     * rejected. DB_LOCK_DEFAULT uses the lock policy specified
     * at environment creation time or DB_LOCK_RANDOM if none was
     * specified.
     */
    envConfig.setLockDetectMode(LockDetectMode.DEFAULT);

    envConfig.setAllowCreate(true);
    envConfig.setRunRecovery(true);
    envConfig.setThreaded(true);
    envConfig.setInitializeReplication(true);
    envConfig.setInitializeLocking(true);
    envConfig.setInitializeLogging(true);
    envConfig.setInitializeCache(true);
    envConfig.setTransactional(true);
    envConfig.setVerboseReplication(appConfig.verbose);
    try {
      dbenv = new RepQuoteEnvironment(appConfig.getHome(), envConfig);
    } catch (FileNotFoundException e) {
      System.err.println("FileNotFound exception: " + e);
      System.err.println("Ensure that the environment directory is pre-created.");
      ret = 1;
    }

    if (appConfig.bulk) dbenv.setReplicationConfig(ReplicationConfig.BULK, true);

    /*
     * Configure heartbeat timeouts so that repmgr monitors the
     * health of the TCP connection.  Master sites broadcast a heartbeat
     * at the frequency specified by the DB_REP_HEARTBEAT_SEND timeout.
     * Client sites wait for message activity the length of the
     * DB_REP_HEARTBEAT_MONITOR timeout before concluding that the
     * connection to the master is lost.  The DB_REP_HEARTBEAT_MONITOR
     * timeout should be longer than the DB_REP_HEARTBEAT_SEND timeout.
     */
    dbenv.setReplicationTimeout(ReplicationTimeoutType.HEARTBEAT_SEND, 5000000);
    dbenv.setReplicationTimeout(ReplicationTimeoutType.HEARTBEAT_MONITOR, 10000000);

    /* The following base replication features may also be useful to your
     * application. See Berkeley DB documentation for more details.
     *   - Master leases: Provide stricter consistency for data reads
     *     on a master site.
     *   - Timeouts: Customize the amount of time Berkeley DB waits
     *     for such things as an election to be concluded or a master
     *     lease to be granted.
     *   - Delayed client synchronization: Manage the master site's
     *     resources by spreading out resource-intensive client
     *     synchronizations.
     *   - Blocked client operations: Return immediately with an error
     *     instead of waiting indefinitely if a client operation is
     *     blocked by an ongoing client synchronization.
     *
     * The following repmgr features may also be useful to your
     * application.  See Berkeley DB documentation for more details.
     *  - Two-site strict majority rule - In a two-site replication
     *    group, require both sites to be available to elect a new
     *    master.
     *  - Timeouts - Customize the amount of time repmgr waits
     *    for such things as waiting for acknowledgements or attempting
     *    to reconnect to other sites.
     *  - Site list - return a list of sites currently known to repmgr.
     */

    /* Start checkpoint and log archive support threads. */
    ckpThr = new CheckpointThread(dbenv);
    ckpThr.start();
    lgaThr = new LogArchiveThread(dbenv, envConfig);
    lgaThr.start();

    /* Start replication manager. */
    dbenv.replicationManagerStart(3, appConfig.startPolicy);

    return ret;
  }
Esempio n. 2
0
  public static void main(String[] argv) throws Exception {
    RepConfig config = new RepConfig();
    boolean isPeer;
    String tmpHost;
    int tmpPort = 0;

    /*  Extract the command line parameters. */
    for (int i = 0; i < argv.length; i++) {
      isPeer = false;
      if (argv[i].compareTo("-a") == 0) {
        if (i == argv.length - 1) usage();
        i++;
        if (argv[i].equals("all")) config.ackPolicy = ReplicationManagerAckPolicy.ALL;
        else if (!argv[i].equals("quorum")) usage();
      } else if (argv[i].compareTo("-b") == 0) config.bulk = true;
      else if (argv[i].compareTo("-C") == 0) {
        config.startPolicy = ReplicationManagerStartPolicy.REP_CLIENT;
      } else if (argv[i].compareTo("-h") == 0) {
        if (i == argv.length - 1) usage();
        /* home - a string arg. */
        i++;
        config.home = argv[i];
      } else if (argv[i].compareTo("-l") == 0) {
        if (i == argv.length - 1) usage();
        /* "local" should be host:port. */
        i++;
        String[] words = argv[i].split(":");
        if (words.length != 2) {
          System.err.println("Invalid host specification host:port needed.");
          usage();
        }
        try {
          tmpPort = Integer.parseInt(words[1]);
        } catch (NumberFormatException nfe) {
          System.err.println("Invalid host specification, " + "could not parse port number.");
          usage();
        }
        config.setThisHost(words[0], tmpPort);
      } else if (argv[i].compareTo("-M") == 0) {
        config.startPolicy = ReplicationManagerStartPolicy.REP_MASTER;
      } else if (argv[i].compareTo("-n") == 0) {
        if (i == argv.length - 1) usage();
        i++;
        config.totalSites = Integer.parseInt(argv[i]);
      } else if (argv[i].compareTo("-p") == 0) {
        if (i == argv.length - 1) usage();
        i++;
        config.priority = Integer.parseInt(argv[i]);
      } else if (argv[i].compareTo("-R") == 0 || argv[i].compareTo("-r") == 0) {
        if (i == argv.length - 1) usage();
        if (argv[i].equals("-R")) isPeer = true;
        i++;
        String[] words = argv[i].split(":");
        if (words.length != 2) {
          System.err.println("Invalid host specification host:port needed.");
          usage();
        }
        try {
          tmpPort = Integer.parseInt(words[1]);
        } catch (NumberFormatException nfe) {
          System.err.println("Invalid host specification, " + "could not parse port number.");
          usage();
        }
        config.addOtherHost(words[0], tmpPort, isPeer);
      } else if (argv[i].compareTo("-v") == 0) {
        config.verbose = true;
      } else {
        System.err.println("Unrecognized option: " + argv[i]);
        usage();
      }
    }

    /* Error check command line. */
    if ((!config.gotListenAddress()) || config.home.length() == 0) usage();

    RepQuoteExample runner = null;
    try {
      runner = new RepQuoteExample();
      runner.init(config);

      /* Sleep to give ourselves time to find a master. */
      // try {
      //    Thread.sleep(5000);
      // } catch (InterruptedException e) {}

      runner.doloop();
      runner.terminate();
    } catch (DatabaseException dbe) {
      System.err.println("Caught an exception during " + "initialization or processing: " + dbe);
      if (runner != null) runner.terminate();
    }
  } /* End main. */