/** Opens the database and creates the Map. */
  private void open() throws Exception {

    // use a generic database configuration
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    if (create) {
      dbConfig.setAllowCreate(true);
      dbConfig.setType(DatabaseType.BTREE);
    }

    // catalog is needed for serial bindings (java serialization)
    Database catalogDb = env.openDatabase(null, "catalog", null, dbConfig);
    catalog = new StoredClassCatalog(catalogDb);

    // use Integer tuple binding for key entries
    TupleBinding keyBinding = TupleBinding.getPrimitiveBinding(Integer.class);

    // use String serial binding for data entries
    SerialBinding dataBinding = new SerialBinding(catalog, String.class);

    this.db = env.openDatabase(null, "helloworld", null, dbConfig);

    // create a map view of the database
    this.map = new StoredSortedMap(db, keyBinding, dataBinding, true);
  }
Esempio n. 2
0
  private Database openDb(String file) throws Exception {

    DatabaseConfig config = new DatabaseConfig();
    DbCompat.setTypeBtree(config);
    config.setTransactional(testEnv.isTxnMode());
    config.setAllowCreate(true);

    return DbCompat.testOpenDatabase(env, null, file, null, config);
  }
Esempio n. 3
0
    public void run() {
      double gtps, itps;
      int n, ret;
      long start_time, end_time;

      //
      // Open the database files.
      //
      int err;
      try {
        DatabaseConfig config = new DatabaseConfig();
        config.setTransactional(true);
        adb = dbenv.openDatabase(null, "account", null, config);
        bdb = dbenv.openDatabase(null, "branch", null, config);
        tdb = dbenv.openDatabase(null, "teller", null, config);
        hdb = dbenv.openDatabase(null, "history", null, config);
      } catch (DatabaseException dbe) {
        TpcbExample.errExit(dbe, "Open of db files failed");
      } catch (FileNotFoundException fnfe) {
        TpcbExample.errExit(fnfe, "Open of db files failed, missing file");
      }

      start_time = (new Date()).getTime();
      for (txns = n = ntxns, failed = 0; n-- > 0; ) if ((ret = txn()) != 0) failed++;
      end_time = (new Date()).getTime();
      if (end_time == start_time) end_time++;

      System.out.println(
          getName()
              + ": "
              + (long) txns
              + " txns: "
              + failed
              + " failed, "
              + TpcbExample.showRounded((txns - failed) / (double) (end_time - start_time), 2)
              + " TPS");

      try {
        adb.close();
        bdb.close();
        tdb.close();
        hdb.close();
      } catch (DatabaseException dbe2) {
        TpcbExample.errExit(dbe2, "Close of db files failed");
      }
    }
Esempio n. 4
0
  public int doloop() throws DatabaseException {
    Database db = null;

    for (; ; ) {
      if (db == null) {
        DatabaseConfig dbconf = new DatabaseConfig();
        dbconf.setType(DatabaseType.BTREE);
        if (dbenv.getIsMaster()) {
          /*
           * Open database allowing create only if this is a master
           * database.  A client database uses polling to attempt
           * to open the database without allowing create until
           * it is successful.
           *
           * This polling logic for allowing create can be
           * simplified under some circumstances.  For example, if
           * the application can be sure a database is already
           * there, it would never need to open it allowing create.
           */
          dbconf.setAllowCreate(true);
        }
        dbconf.setTransactional(true);

        try {
          db = dbenv.openDatabase(null, RepConfig.progname, null, dbconf);
        } catch (java.io.FileNotFoundException e) {
          System.err.println("no stock database available yet.");
          if (db != null) {
            db.close(true);
            db = null;
          }
          try {
            Thread.sleep(RepConfig.SLEEPTIME);
          } catch (InterruptedException ie) {
          }
          continue;
        }
      }

      BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

      /* Listen for input, and add it to the database. */
      System.out.print("QUOTESERVER");
      if (!dbenv.getIsMaster()) System.out.print("(read-only)");
      System.out.print("> ");
      System.out.flush();
      String nextline = null;
      try {
        nextline = stdin.readLine();
      } catch (IOException ioe) {
        System.err.println("Unable to get data from stdin");
        break;
      }
      String[] words = nextline.split("\\s");

      /* A blank line causes the DB to be dumped to stdout. */
      if (words.length == 0 || (words.length == 1 && words[0].length() == 0)) {
        try {
          if (dbenv.getInClientSync())
            System.err.println("Cannot read data during client initialization - please try again.");
          else printStocks(db);
        } catch (DeadlockException de) {
          continue;
        } catch (DatabaseException e) {
          /*
           * This could be DB_REP_HANDLE_DEAD, which
           * should close the database and continue.
           */
          System.err.println("Got db exception reading replication" + "DB: " + e);
          System.err.println(
              "Expected if it was due to a dead "
                  + "replication handle, otherwise an unexpected error.");
          db.close(true); /* Close no sync. */
          db = null;
          continue;
        }
        continue;
      }

      if (words.length == 1
          && (words[0].compareToIgnoreCase("quit") == 0
              || words[0].compareToIgnoreCase("exit") == 0)) {
        dbenv.setAppFinished(true);
        break;
      } else if (words.length != 2) {
        System.err.println("Format: TICKER VALUE");
        continue;
      }

      if (!dbenv.getIsMaster()) {
        System.err.println("Can't update client.");
        continue;
      }

      DatabaseEntry key = new DatabaseEntry(words[0].getBytes());
      DatabaseEntry data = new DatabaseEntry(words[1].getBytes());

      db.put(null, key, data);
    }
    if (db != null) db.close(true);
    return 0;
  }
  /** 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);
  }
Esempio n. 6
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();
  }