Beispiel #1
0
  public void restart(int siteId) throws Exception {
    EnvironmentConfig ec = makeBasicConfig();

    int p = config.getMyPort(siteId);
    ReplicationManagerSiteConfig dbsite = new ReplicationManagerSiteConfig("localhost", p);
    dbsite.setLocalSite(true);
    dbsite.setLegacy(true);
    ec.addReplicationManagerSite(dbsite);

    p = config.getOtherPort(siteId);
    dbsite = new ReplicationManagerSiteConfig("localhost", p);
    dbsite.setLegacy(true);
    ec.addReplicationManagerSite(dbsite);

    MyEventHandler monitor = new MyEventHandler();
    ec.setEventHandler(monitor);
    File clientDir = new File(config.getBaseDir(), "dir" + siteId);
    assertTrue(clientDir.exists());
    Environment client = new Environment(clientDir, ec);
    client.setReplicationConfig(ReplicationConfig.STRICT_2SITE, false);

    envs[siteId] = client;
    monitors[siteId] = monitor;
    // we want to make sure we don't retry from here after the
    // initial failure, because we want to make the old master
    // connect to us.
    client.setReplicationTimeout(ReplicationTimeoutType.CONNECTION_RETRY, Integer.MAX_VALUE);
    client.replicationManagerStart(3, ReplicationManagerStartPolicy.REP_CLIENT);
  }
  /** Creates the environment and runs a transaction */
  public static void main(String[] argv) throws Exception {

    String dir = "./tmp";

    // environment is transactional
    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setTransactional(true);
    envConfig.setInitializeCache(true);
    envConfig.setInitializeLocking(true);
    if (create) {
      envConfig.setAllowCreate(true);
    }
    Environment env = new Environment(new File(dir), envConfig);

    // create the application and run a transaction
    HelloDatabaseWorld worker = new HelloDatabaseWorld(env);
    TransactionRunner runner = new TransactionRunner(env);
    try {
      // open and access the database within a transaction
      runner.run(worker);
    } finally {
      // close the database outside the transaction
      worker.close();
    }
  }
Beispiel #3
0
  public void joinExistingClient(int site, boolean useHB) throws Exception {
    EnvironmentConfig ec = makeBasicConfig();

    int p = config.getMyPort(site);
    ReplicationManagerSiteConfig dbsite = new ReplicationManagerSiteConfig("localhost", p);
    dbsite.setLocalSite(true);
    dbsite.setLegacy(true);
    ec.addReplicationManagerSite(dbsite);

    p = config.getOtherPort(site);
    dbsite = new ReplicationManagerSiteConfig("localhost", p);
    dbsite.setLegacy(true);
    ec.addReplicationManagerSite(dbsite);

    MyEventHandler monitor = new MyEventHandler();
    monitors[site] = monitor;
    ec.setEventHandler(monitor);
    File clientDir = new File(config.getBaseDir(), "dir" + site);
    assertTrue(clientDir.exists());
    Environment client = new Environment(clientDir, ec);
    client.setReplicationConfig(ReplicationConfig.STRICT_2SITE, false);

    if (useHB) {
      client.setReplicationTimeout(ReplicationTimeoutType.HEARTBEAT_SEND, 3000000);
      client.setReplicationTimeout(ReplicationTimeoutType.HEARTBEAT_MONITOR, 6000000);
    }

    envs[site] = client;
    client.setReplicationTimeout(ReplicationTimeoutType.CONNECTION_RETRY, 1000000); // be impatient
    client.replicationManagerStart(3, ReplicationManagerStartPolicy.REP_CLIENT);
    monitor.await();

    assertTrue(client.getReplicationStats(StatsConfig.DEFAULT).getStartupComplete());
  }
Beispiel #4
0
  private static EnvironmentConfig newEnvConfig() {

    EnvironmentConfig config = new EnvironmentConfig();
    config.setTxnNoSync(Boolean.getBoolean(SharedTestUtils.NO_SYNC));
    if (DbCompat.MEMORY_SUBSYSTEM) {
      DbCompat.setInitializeCache(config, true);
    }
    return config;
  }
  private EventExampleDPL(File envHome) throws DatabaseException, FileNotFoundException {

    /* Open a transactional Berkeley DB engine environment. */
    System.out.println("-> Creating a BDB environment");
    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setAllowCreate(true);
    envConfig.setTransactional(true);
    envConfig.setInitializeCache(true);
    envConfig.setInitializeLocking(true);
    env = new Environment(envHome, envConfig);

    /* Initialize the data access object. */
    init();
    cal = Calendar.getInstance();
  }
Beispiel #6
0
  public Environment open(String testName, boolean create) throws IOException, DatabaseException {

    config.setAllowCreate(create);
    /* OLDEST deadlock detection on DB matches the use of timeouts on JE.*/
    DbCompat.setLockDetectModeOldest(config);
    File dir = getDirectory(testName, create);
    return newEnvironment(dir, config);
  }
Beispiel #7
0
  @Test
  public void test1() throws DatabaseException, FileNotFoundException {
    EnvironmentConfig envc = new EnvironmentConfig();
    envc.setAllowCreate(true);
    envc.setInitializeCache(true);
    envc.setVerbose(VerboseConfig.DEADLOCK, true);
    envc.setVerbose(VerboseConfig.FILEOPS, true);
    envc.setVerbose(VerboseConfig.FILEOPS_ALL, true);
    envc.setVerbose(VerboseConfig.RECOVERY, true);
    envc.setVerbose(VerboseConfig.REGISTER, true);
    envc.setVerbose(VerboseConfig.REPLICATION, true);
    envc.setVerbose(VerboseConfig.WAITSFOR, true);
    envc.setMessageStream(new FileOutputStream(new File("messages.txt")));
    Environment db_env = new Environment(TestUtils.BASETEST_DBFILE, envc);

    new File("messages.txt").delete();
  }
Beispiel #8
0
  /*
   * Tests for old (now deprecated) API.
   */
  @Test
  public void test2() throws DatabaseException, FileNotFoundException {
    EnvironmentConfig envc = new EnvironmentConfig();
    envc.setAllowCreate(true);
    envc.setInitializeCache(true);
    envc.setVerboseDeadlock(true);
    envc.setVerboseRecovery(true);
    envc.setVerboseRegister(true);
    envc.setVerboseReplication(true);
    envc.setVerboseWaitsFor(true);
    envc.setMessageStream(new FileOutputStream(new File("messages.txt")));
    Environment db_env = new Environment(TestUtils.BASETEST_DBFILE, envc);

    new File("messages.txt").delete();
  }
Beispiel #9
0
  static {
    EnvironmentConfig config;

    config = newEnvConfig();
    BDB = new TestEnv("bdb", config);

    if (DbCompat.CDB) {
      config = newEnvConfig();
      DbCompat.setInitializeCDB(config, true);
      CDB = new TestEnv("cdb", config);
    } else {
      CDB = null;
    }

    config = newEnvConfig();
    config.setTransactional(true);
    DbCompat.setInitializeLocking(config, true);
    TXN = new TestEnv("txn", config);
  }
Beispiel #10
0
 @Test
 public void testInitialMutexes() throws DatabaseException, FileNotFoundException {
   EnvironmentConfig envc = new EnvironmentConfig();
   envc.setAllowCreate(true);
   envc.setInitializeCache(true);
   envc.setInitialMutexes(1024);
   assertEquals(envc.getInitialMutexes(), 1024);
   Environment dbEnv = new Environment(TestUtils.BASETEST_DBFILE, envc);
   assertEquals(envc.getInitialMutexes(), 1024);
 }
Beispiel #11
0
 void copyConfig(EnvironmentConfig copyToConfig) {
   DbCompat.setInitializeCache(copyToConfig, DbCompat.getInitializeCache(config));
   DbCompat.setInitializeLocking(copyToConfig, DbCompat.getInitializeLocking(config));
   DbCompat.setInitializeCDB(copyToConfig, DbCompat.getInitializeCDB(config));
   copyToConfig.setTransactional(config.getTransactional());
 }
Beispiel #12
0
  public void run() {
    String homedirName = baseDirName + threadNumber;
    TestUtils.removeDir(homedirName);

    try {
      homedir = new File(homedirName);
      homedir.mkdir();
    } catch (Exception e) {
      TestUtils.DEBUGOUT(
          2, "Warning: initialization had a problem creating a clean directory.\n" + e);
    }
    try {
      homedir = new File(homedirName);
    } catch (NullPointerException npe) {
      // can't really happen :)
    }

    TestUtils.DEBUGOUT(1, "Creating worker: " + threadNumber);

    envConfig = new EnvironmentConfig();
    envConfig.setErrorStream(TestUtils.getErrorStream());
    envConfig.setErrorPrefix("RepmgrElectionTest test(" + threadNumber + ")");
    envConfig.setAllowCreate(true);
    envConfig.setRunRecovery(true);
    envConfig.setThreaded(true);
    envConfig.setInitializeLocking(true);
    envConfig.setInitializeLogging(true);
    envConfig.setInitializeCache(true);
    envConfig.setTransactional(true);
    envConfig.setTxnNoSync(true);
    envConfig.setInitializeReplication(true);
    envConfig.setVerboseReplication(false);

    ReplicationManagerSiteConfig localConfig =
        new ReplicationManagerSiteConfig(address, basePort + threadNumber);
    localConfig.setLocalSite(true);
    envConfig.addReplicationManagerSite(localConfig);

    envConfig.setReplicationPriority(priorities[threadNumber]);
    envConfig.setEventHandler(this);
    envConfig.setReplicationManagerAckPolicy(ReplicationManagerAckPolicy.ALL);

    if (masterThreadIndex >= 0) {
      // If we already have the master, then set it as the bootstrap helper,
      // otherwise, set local site as new master.
      ReplicationManagerSiteConfig remoteConfig =
          new ReplicationManagerSiteConfig(address, basePort + masterThreadIndex);
      remoteConfig.setBootstrapHelper(true);
      envConfig.addReplicationManagerSite(remoteConfig);
    }

    try {
      dbenv = new Environment(homedir, envConfig);

    } catch (FileNotFoundException e) {
      fail("Unexpected FNFE in standard environment creation." + e);
    } catch (DatabaseException dbe) {
      fail("Unexpected database exception came from environment create." + dbe);
    }

    try {
      // If we do not have master, then set local site as new master.
      if (masterThreadIndex == -1)
        dbenv.replicationManagerStart(NUM_WORKER_THREADS, ReplicationManagerStartPolicy.REP_MASTER);
      else
        dbenv.replicationManagerStart(NUM_WORKER_THREADS, ReplicationManagerStartPolicy.REP_CLIENT);
    } catch (DatabaseException dbe) {
      fail("Unexpected database exception came from replicationManagerStart." + dbe);
    }

    TestUtils.DEBUGOUT(1, "Started replication site: " + threadNumber);
    lastSiteStarted = true;

    try {
      java.lang.Thread.sleep(1000 * (1 + threadNumber));
    } catch (InterruptedException ie) {
    }

    if (masterThreadIndex != -1) {
      // Wait for "Start-up done" for each client, then add next client.
      ReplicationStats rs = null;
      int i = 0;
      do {
        try {
          java.lang.Thread.sleep(2000);
        } catch (InterruptedException e) {
        }

        try {
          rs = dbenv.getReplicationStats(StatsConfig.DEFAULT);
        } catch (DatabaseException dbe) {
          dbe.printStackTrace();
          fail("Unexpected database exception came from getReplicationStats." + dbe);
        }
      } while (!rs.getStartupComplete() && i++ < maxLoopWait);
      assertTrue(rs.getStartupComplete());
    }
  }
  public TpcbExample(
      File home,
      int accounts,
      int branches,
      int tellers,
      int history,
      int cachesize,
      boolean noSync)
      throws DatabaseException, FileNotFoundException {

    this.accounts = accounts;
    this.branches = branches;
    this.tellers = tellers;
    this.history = history;

    EnvironmentConfig config = new EnvironmentConfig();
    config.setErrorStream(System.err);
    config.setErrorPrefix(progname);
    config.setLockDetectMode(LockDetectMode.DEFAULT);
    config.setCacheSize(cachesize == 0 ? 4 * 1024 * 1024 : cachesize);
    config.setTxnNoSync(noSync);
    config.setLockDetectMode(LockDetectMode.DEFAULT);
    config.setAllowCreate(true);

    config.setInitializeCache(true);
    config.setTransactional(true);
    config.setInitializeLocking(true);
    config.setInitializeLogging(true);

    dbenv = new Environment(home, config);
  }
Beispiel #14
0
 private EnvironmentConfig makeBasicConfig() {
   EnvironmentConfig ec = new EnvironmentConfig();
   ec.setAllowCreate(true);
   ec.setInitializeCache(true);
   ec.setInitializeLocking(true);
   ec.setInitializeLogging(true);
   ec.setInitializeReplication(true);
   ec.setTransactional(true);
   ec.setReplicationManagerAckPolicy(ReplicationManagerAckPolicy.ALL);
   ec.setRunRecovery(true);
   ec.setThreaded(true);
   if (Boolean.getBoolean("VERB_REPLICATION")) ec.setVerbose(VerboseConfig.REPLICATION, true);
   return (ec);
 }
  /** Open all storage containers, indices, and catalogs. */
  public SampleDatabase(String homeDirectory) throws DatabaseException, FileNotFoundException {

    // Open the Berkeley DB environment in transactional mode.
    //
    System.out.println("Opening environment in: " + homeDirectory);
    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setTransactional(true);
    envConfig.setAllowCreate(true);
    envConfig.setInitializeCache(true);
    envConfig.setInitializeLocking(true);
    env = new Environment(new File(homeDirectory), envConfig);

    // Set the Berkeley DB config for opening all stores.
    //
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);
    dbConfig.setType(DatabaseType.BTREE);

    // Create the Serial class catalog.  This holds the serialized class
    // format for all database records of serial format.
    //
    Database catalogDb = env.openDatabase(null, CLASS_CATALOG, null, dbConfig);
    javaCatalog = new StoredClassCatalog(catalogDb);

    // Open the Berkeley DB database for the part, supplier and shipment
    // stores.  The stores are opened with no duplicate keys allowed.
    //
    partDb = env.openDatabase(null, PART_STORE, null, dbConfig);

    supplierDb = env.openDatabase(null, SUPPLIER_STORE, null, dbConfig);

    shipmentDb = env.openDatabase(null, SHIPMENT_STORE, null, dbConfig);

    // Open the SecondaryDatabase for the city index of the supplier store,
    // and for the part and supplier indices of the shipment store.
    // Duplicate keys are allowed since more than one supplier may be in
    // the same city, and more than one shipment may exist for the same
    // supplier or part.  A foreign key constraint is defined for the
    // supplier and part indices to ensure that a shipment only refers to
    // existing part and supplier keys.  The CASCADE delete action means
    // that shipments will be deleted if their associated part or supplier
    // is deleted.
    //
    SecondaryConfig secConfig = new SecondaryConfig();
    secConfig.setTransactional(true);
    secConfig.setAllowCreate(true);
    secConfig.setType(DatabaseType.BTREE);
    secConfig.setSortedDuplicates(true);

    secConfig.setKeyCreator(
        new SupplierByCityKeyCreator(
            javaCatalog, SupplierKey.class, SupplierData.class, String.class));
    supplierByCityDb =
        env.openSecondaryDatabase(null, SUPPLIER_CITY_INDEX, null, supplierDb, secConfig);

    secConfig.setForeignKeyDatabase(partDb);
    secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE);
    secConfig.setKeyCreator(
        new ShipmentByPartKeyCreator(
            javaCatalog, ShipmentKey.class, ShipmentData.class, PartKey.class));
    shipmentByPartDb =
        env.openSecondaryDatabase(null, SHIPMENT_PART_INDEX, null, shipmentDb, secConfig);

    secConfig.setForeignKeyDatabase(supplierDb);
    secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.CASCADE);
    secConfig.setKeyCreator(
        new ShipmentBySupplierKeyCreator(
            javaCatalog, ShipmentKey.class, ShipmentData.class, SupplierKey.class));
    shipmentBySupplierDb =
        env.openSecondaryDatabase(null, SHIPMENT_SUPPLIER_INDEX, null, shipmentDb, secConfig);
  }
Beispiel #16
0
  @Test
  public void testRegionMemoryInitialSize() throws DatabaseException, FileNotFoundException {
    /* Create an environment. */
    EnvironmentConfig envc = new EnvironmentConfig();
    envc.setAllowCreate(true);
    envc.setInitializeCache(true);
    envc.setRegionMemoryInitialSize(RegionResourceType.LOCK, 1024);
    envc.setRegionMemoryInitialSize(RegionResourceType.LOCK_OBJECT, 1024);
    envc.setRegionMemoryInitialSize(RegionResourceType.LOCKER, 1024);
    envc.setRegionMemoryInitialSize(RegionResourceType.LOG_ID, 1024);
    envc.setRegionMemoryInitialSize(RegionResourceType.TRANSACTION, 1024);
    envc.setRegionMemoryInitialSize(RegionResourceType.THREAD, 1024);

    /* Check to see that they "stuck" before opening env. */
    assertEquals(envc.getRegionMemoryInitialSize(RegionResourceType.LOCK), 1024);
    assertEquals(envc.getRegionMemoryInitialSize(RegionResourceType.LOCK_OBJECT), 1024);
    assertEquals(envc.getRegionMemoryInitialSize(RegionResourceType.LOCKER), 1024);
    assertEquals(envc.getRegionMemoryInitialSize(RegionResourceType.LOG_ID), 1024);
    assertEquals(envc.getRegionMemoryInitialSize(RegionResourceType.TRANSACTION), 1024);
    assertEquals(envc.getRegionMemoryInitialSize(RegionResourceType.THREAD), 1024);

    Environment dbEnv = new Environment(TestUtils.BASETEST_DBFILE, envc);

    /* Check again after opnening. */
    assertEquals(envc.getRegionMemoryInitialSize(RegionResourceType.LOCK), 1024);
    assertEquals(envc.getRegionMemoryInitialSize(RegionResourceType.LOCK_OBJECT), 1024);
    assertEquals(envc.getRegionMemoryInitialSize(RegionResourceType.LOCKER), 1024);
    assertEquals(envc.getRegionMemoryInitialSize(RegionResourceType.LOG_ID), 1024);
    assertEquals(envc.getRegionMemoryInitialSize(RegionResourceType.TRANSACTION), 1024);
    assertEquals(envc.getRegionMemoryInitialSize(RegionResourceType.THREAD), 1024);
  }
Beispiel #17
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;
  }
Beispiel #18
0
  @Test
  public void testDraining() throws Exception {
    EnvironmentConfig masterConfig = makeBasicConfig();
    masterConfig.setReplicationLimit(100000000);
    ReplicationManagerSiteConfig site = new ReplicationManagerSiteConfig("localhost", masterPort);
    site.setLocalSite(true);
    site.setLegacy(true);
    masterConfig.addReplicationManagerSite(site);

    site = new ReplicationManagerSiteConfig("localhost", clientPort);
    site.setLegacy(true);
    masterConfig.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", client2Port);
    site.setLegacy(true);
    masterConfig.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", client3Port);
    site.setLegacy(true);
    masterConfig.addReplicationManagerSite(site);

    Environment master = new Environment(mkdir("master"), masterConfig);
    setTimeouts(master);
    // Prevent connection retries, so that all connections
    // originate from clients
    master.setReplicationTimeout(ReplicationTimeoutType.CONNECTION_RETRY, Integer.MAX_VALUE);
    master.replicationManagerStart(2, ReplicationManagerStartPolicy.REP_MASTER);

    DatabaseConfig dc = new DatabaseConfig();
    dc.setTransactional(true);
    dc.setAllowCreate(true);
    dc.setType(DatabaseType.BTREE);
    dc.setPageSize(4096);
    Database db = master.openDatabase(null, "test.db", null, dc);

    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();
    value.setData(data);

    for (int i = 0; ((BtreeStats) db.getStats(null, null)).getPageCount() < 500; i++) {
      String k = "The record number is: " + i;
      key.setData(k.getBytes());
      db.put(null, key, value);
    }

    // tell fiddler to stop reading once it sees a PAGE message
    Socket s = new Socket("localhost", mgrPort);
    OutputStreamWriter w = new OutputStreamWriter(s.getOutputStream());

    String path1 = "{" + masterPort + "," + clientPort + "}"; // looks like {6000,6001}
    w.write("{init," + path1 + ",page_clog}\r\n");
    w.flush();
    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    br.readLine();
    assertEquals("ok", br.readLine());
    // create client
    //
    EnvironmentConfig ec = makeBasicConfig();
    site = new ReplicationManagerSiteConfig("localhost", clientPort);
    site.setLocalSite(true);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", masterPort);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", client2Port);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", client3Port);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    Environment client = new Environment(mkdir("client"), ec);
    setTimeouts(client);
    client.replicationManagerStart(1, ReplicationManagerStartPolicy.REP_CLIENT);

    // wait til it gets stuck
    Thread.sleep(5000); // FIXME

    // Do the same for another client, because the master has 2
    // msg processing threads.  (It's no longer possible to
    // configure just 1.)
    String path2 = "{" + masterPort + "," + client2Port + "}";
    w.write("{init," + path2 + ",page_clog}\r\n");
    w.flush();
    br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    br.readLine();
    assertEquals("ok", br.readLine());

    ec = makeBasicConfig();
    site = new ReplicationManagerSiteConfig("localhost", client2Port);
    site.setLocalSite(true);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", masterPort);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", clientPort);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", client3Port);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    Environment client2 = new Environment(mkdir("client2"), ec);
    setTimeouts(client2);
    client2.replicationManagerStart(1, ReplicationManagerStartPolicy.REP_CLIENT);

    // wait til it gets stuck
    Thread.sleep(5000);

    // With the connection stuck, the master cannot write out log
    // records for new "live" transactions.  Knowing we didn't
    // write the record, we should not bother waiting for an ack
    // that cannot possibly arrive; so we should simply return
    // quickly.  The duration should be very quick, but anything
    // less than the ack timeout indicates correct behavior (in
    // case this test runs on a slow, overloaded system).
    //
    long startTime = System.currentTimeMillis();
    key.setData("one extra record".getBytes());
    db.put(null, key, value);
    long duration = System.currentTimeMillis() - startTime;
    assertTrue("txn duration: " + duration, duration < 29000);
    System.out.println("txn duration: " + duration);
    db.close();

    // Tell fiddler to close the connections.  That should trigger
    // us to abandon the timeout.  Then create another client and
    // see that it can complete its internal init quickly.  Since
    // we have limited threads at the master, this demonstrates
    // that they were abandoned.
    //
    path1 = "{" + clientPort + "," + masterPort + "}"; // looks like {6001,6000}
    w.write("{" + path1 + ",shutdown}\r\n");
    w.flush();
    assertEquals("ok", br.readLine());
    path2 = "{" + client2Port + "," + masterPort + "}"; // looks like {6001,6000}
    w.write("{" + path2 + ",shutdown}\r\n");
    w.flush();
    assertEquals("ok", br.readLine());

    ec = makeBasicConfig();
    site = new ReplicationManagerSiteConfig("localhost", client3Port);
    site.setLocalSite(true);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", masterPort);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", clientPort);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", client2Port);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);

    EventHandler clientMonitor = new EventHandler();
    ec.setEventHandler(clientMonitor);
    Environment client3 = new Environment(mkdir("client3"), ec);
    setTimeouts(client3);
    startTime = System.currentTimeMillis();
    client3.replicationManagerStart(2, ReplicationManagerStartPolicy.REP_CLIENT);
    clientMonitor.await();
    duration = System.currentTimeMillis() - startTime;
    assertTrue("sync duration: " + duration, duration < 20000); // 20 seconds should be plenty

    client3.close();
    master.close();

    w.write("shutdown\r\n");
    w.flush();
    assertEquals("ok", br.readLine());
    s.close();
  }
Beispiel #19
0
  public boolean isTxnMode() {

    return config.getTransactional();
  }
  /** @throws FileNotFoundException from DB core. */
  void openEnv() throws FileNotFoundException, DatabaseException {

    EnvironmentConfig config = TestEnv.TXN.getConfig();
    config.setAllowCreate(true);
    env = new Environment(envHome, config);
  }
Beispiel #21
0
 public static EnvironmentConfig makeBasicConfig() {
   EnvironmentConfig ec = new EnvironmentConfig();
   ec.setAllowCreate(true);
   ec.setInitializeCache(true);
   ec.setInitializeLocking(true);
   ec.setInitializeLogging(true);
   ec.setInitializeReplication(true);
   ec.setTransactional(true);
   ec.setThreaded(true);
   ec.setReplicationInMemory(true);
   ec.setCacheSize(256 * 1024 * 1024);
   if (Boolean.getBoolean("VERB_REPLICATION")) ec.setVerbose(VerboseConfig.REPLICATION, true);
   return (ec);
 }