/** Test exclusive creation. */
  @Test
  public void testExclusive() throws Throwable {

    try {
      EnvironmentConfig envConfig = TestUtils.initEnvConfig();

      /*
       * Make sure that the database keeps its own copy of the
       * configuration object.
       */
      envConfig.setAllowCreate(true);
      env = create(envHome, envConfig);
      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setAllowCreate(true);
      dbConfig.setExclusiveCreate(true);

      /* Should succeed and create the database. */
      Database dbA = env.openDatabase(null, "foo", dbConfig);
      dbA.close();

      /* Should not succeed, because the database exists. */
      try {
        env.openDatabase(null, "foo", dbConfig);
        fail("Database already exists");
      } catch (DatabaseException e) {
      }
      close(env);
    } catch (Throwable t) {
      t.printStackTrace();
      throw t;
    }
  }
    public boolean setup(boolean readOnly) {
      boolean open = false;
      try {
        EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setReadOnly(readOnly);
        envConfig.setAllowCreate(!readOnly);

        env = new Environment(envHome, envConfig);

        StoreConfig storeConfig = new StoreConfig();
        storeConfig.setReadOnly(readOnly);
        storeConfig.setAllowCreate(!readOnly);

        store = new EntityStore(env, "EntityStore", storeConfig);

        objectBySid = store.getPrimaryIndex(String.class, TestObject.class);

        open = true;
      } catch (EnvironmentLockedException e) {
      } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
      }

      return open;
    }
示例#3
0
 private void setEnvironment() {
   envLocation = ConfigManager.getDbDir();
   envConfig = new EnvironmentConfig();
   envConfig.setAllowCreate(true);
   envConfig.setLocking(false);
   myDbEnvironment = new Environment(new File(envLocation), envConfig);
 }
示例#4
0
 public void addEnvironmentVariablesToEnvironment(
     String environmentName, String variableName, String variableValue)
     throws NoSuchEnvironmentException {
   CruiseConfig config = loadForEdit();
   EnvironmentConfig env =
       config.getEnvironments().named(new CaseInsensitiveString(environmentName));
   env.addEnvironmentVariable(variableName, variableValue);
   writeConfigFile(config);
 }
  /* Test mutable and persistent configurations. */
  @Test
  public void testPersistentAndMutableConfigs() throws Exception {

    final String dbName = "foo";

    EnvironmentConfig envConfig = TestUtils.initEnvConfig();
    envConfig.setAllowCreate(true);
    envConfig.setTransactional(true);
    env = create(envHome, envConfig);

    DbConfigManager configMgr = DbInternal.getEnvironmentImpl(env).getConfigManager();
    int defaultNodeMaxEntries = configMgr.getInt(EnvironmentParams.NODE_MAX);
    int defaultNodeDupTreeMaxEntries = configMgr.getInt(EnvironmentParams.NODE_MAX_DUPTREE);

    /* Check the default node max entries setting. */
    assertEquals(defaultNodeMaxEntries, 128);
    assertEquals(defaultNodeDupTreeMaxEntries, 128);

    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setAllowCreate(true);
    dbConfig.setTransactional(true);

    /* Do updates on each persistent and mutable config. */

    /* Check whether BtreeComparator setting is persisted. */
    dbConfig.setOverrideBtreeComparator(true);
    dbConfig.setBtreeComparator(TestComparator.class);
    DatabaseConfig newConfig = setAndGetDbConfig(env, dbConfig, dbName);
    assertTrue(newConfig.getBtreeComparator() instanceof TestComparator);

    /* Check whether DuplicateComparator setting is persisted. */
    dbConfig.setOverrideDuplicateComparator(true);
    dbConfig.setDuplicateComparator(new TestSerialComparator());
    newConfig = setAndGetDbConfig(env, dbConfig, dbName);
    assertTrue(newConfig.getDuplicateComparator() instanceof TestSerialComparator);

    /* Check whether KeyPrefixing setting is persisted. */
    dbConfig.setKeyPrefixing(true);
    newConfig = setAndGetDbConfig(env, dbConfig, dbName);
    assertTrue(newConfig.getKeyPrefixing());

    /* Check whether NodeMaxEntries setting is persisted. */
    dbConfig.setNodeMaxEntries(512);
    newConfig = setAndGetDbConfig(env, dbConfig, dbName);
    assertTrue(newConfig.getNodeMaxEntries() == 512);

    close(env);
  }
示例#6
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();
  }
示例#7
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();
  }
示例#8
0
文件: Bdb.java 项目: jefron-ap/TTK
  private Bdb(boolean readOnly, File directory) throws IOException {
    try {
      directory.mkdirs();
      EnvironmentConfig envConfig = new EnvironmentConfig();
      envConfig.setSharedCache(true);
      envConfig.setReadOnly(readOnly);
      envConfig.setAllowCreate(!readOnly);
      /*
       * int primeForLockTable = SieveForPrimeNumbers.largestPrime(
       * Runtime.getRuntime().availableProcessors() - 1);
       *
       * envConfig.setConfigParam("je.lock.nLockTables", Integer.toString(primeForLockTable));
       * envConfig.setConfigParam("je.log.faultReadSize", "4096");
       *
       */

      bdbEnv = new Environment(directory, envConfig);
    } catch (EnvironmentLockedException e) {
      throw new IOException(e);
    } catch (DatabaseException e) {
      throw new IOException(e);
    }
  }
  /**
   * Test that we can retrieve a database configuration and that it clones its configuration
   * appropriately.
   */
  @Test
  public void testConfig() throws Throwable {

    try {
      EnvironmentConfig envConfig = TestUtils.initEnvConfig();
      envConfig.setAllowCreate(true);
      env = create(envHome, envConfig);

      /*
       * Make sure that the database keeps its own copy of the
       * configuration object.
       */
      DatabaseConfig dbConfigA = new DatabaseConfig();
      dbConfigA.setAllowCreate(true);
      Database dbA = env.openDatabase(null, "foo", dbConfigA);

      /* Change the original dbConfig */
      dbConfigA.setAllowCreate(false);
      DatabaseConfig getConfig1 = dbA.getConfig();
      assertEquals(true, getConfig1.getAllowCreate());
      assertEquals(false, getConfig1.getSortedDuplicates());

      /*
       * Change the retrieved config, ought to have no effect on what the
       * Database is storing.
       */
      getConfig1.setSortedDuplicates(true);
      DatabaseConfig getConfig2 = dbA.getConfig();
      assertEquals(false, getConfig2.getSortedDuplicates());

      dbA.close();
      close(env);
    } catch (Throwable t) {
      t.printStackTrace();
      throw t;
    }
  }
示例#10
0
 @Test
 public void shouldUpdateExistingEnvironment() throws Exception {
   BasicEnvironmentConfig uat = environmentConfig("uat");
   goConfigService.addPipeline(
       PipelineConfigMother.createPipelineConfig("foo", "dev", "job"), "foo-grp");
   goConfigService.addPipeline(
       PipelineConfigMother.createPipelineConfig("bar", "dev", "job"), "foo-grp");
   Username user = Username.ANONYMOUS;
   agentConfigService.addAgent(new AgentConfig("uuid-1", "host-1", "192.168.1.2"), user);
   agentConfigService.addAgent(new AgentConfig("uuid-2", "host-2", "192.168.1.3"), user);
   uat.addPipeline(new CaseInsensitiveString("foo"));
   uat.addAgent("uuid-2");
   uat.addEnvironmentVariable("env-one", "ONE");
   uat.addEnvironmentVariable("env-two", "TWO");
   goConfigService.addEnvironment(new BasicEnvironmentConfig(new CaseInsensitiveString("dev")));
   goConfigService.addEnvironment(new BasicEnvironmentConfig(new CaseInsensitiveString("qa")));
   goConfigService.addEnvironment(uat);
   goConfigService.addEnvironment(
       new BasicEnvironmentConfig(new CaseInsensitiveString("acceptance")));
   goConfigService.addEnvironment(
       new BasicEnvironmentConfig(new CaseInsensitiveString("function_testing")));
   EnvironmentConfig newUat = new BasicEnvironmentConfig(new CaseInsensitiveString("prod"));
   newUat.addPipeline(new CaseInsensitiveString("bar"));
   newUat.addAgent("uuid-1");
   newUat.addEnvironmentVariable("env-three", "THREE");
   HttpLocalizedOperationResult result =
       service.updateEnvironment(
           "uat",
           newUat,
           new Username(new CaseInsensitiveString("foo")),
           goConfigDao.md5OfConfigFile());
   EnvironmentConfig updatedEnv = service.named("prod");
   assertThat(updatedEnv.name(), is(new CaseInsensitiveString("prod")));
   assertThat(updatedEnv.getAgents().getUuids(), is(Arrays.asList("uuid-1")));
   assertThat(updatedEnv.getPipelineNames(), is(Arrays.asList(new CaseInsensitiveString("bar"))));
   EnvironmentVariablesConfig updatedVariables = new EnvironmentVariablesConfig();
   updatedVariables.add("env-three", "THREE");
   assertThat(updatedEnv.getVariables(), is(updatedVariables));
   EnvironmentsConfig currentEnvironments = goConfigService.getCurrentConfig().getEnvironments();
   assertThat(currentEnvironments.indexOf(updatedEnv), is(2));
   assertThat(currentEnvironments.size(), is(5));
 }
示例#11
0
  public static void main(String[] args) {

    Environment myEnv;
    Database myDatabase;

    try {
      EnvironmentConfig envConfig = new EnvironmentConfig();
      envConfig.setAllowCreate(true);
      myEnv = new Environment(new File("/Users/broso/testDB"), envConfig);

      // Environment open omitted for clarity
      DatabaseConfig myDbConfig = new DatabaseConfig();
      SecondaryConfig mySecConfig = new SecondaryConfig();
      myDbConfig.setAllowCreate(true);
      mySecConfig.setAllowCreate(true);
      // Duplicates are frequently required for secondary databases.
      mySecConfig.setSortedDuplicates(true);

      // Open the primary

      String dbName = "myPrimaryDatabase";
      Database myDb = myEnv.openDatabase(null, dbName, myDbConfig);

      // Open the secondary.
      // Key creators are described in the next section.
      // AKeyCreator akc = new AKeyCreator();

      // Get a secondary object and set the key creator on it.
      // mySecConfig.setKeyCreator(keyCreator);

      // Perform the actual open
      String secDbName = "mySecondaryDatabase";
      SecondaryDatabase mySecDb = myEnv.openSecondaryDatabase(null, secDbName, myDb, mySecConfig);
    } catch (DatabaseException de) {
      // Exception handling goes here
    }
  }
示例#12
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);
 }
示例#13
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());
    }
  }
示例#14
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;
  }
  /** Test open and close of an environment. */
  public void testCacheStats() throws DatabaseException {

    EnvironmentConfig envConfig = TestUtils.initEnvConfig();
    envConfig.setTransactional(true);
    envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6");
    envConfig.setAllowCreate(true);
    env = new Environment(envHome, envConfig);
    EnvironmentStats stat = env.getStats(TestUtils.FAST_STATS);
    env.close();
    env = null;
    assertEquals(0, stat.getNCacheMiss());
    assertEquals(0, stat.getNNotResident());

    // Try to open and close again, now that the environment exists
    envConfig.setAllowCreate(false);
    envConfig.setConfigParam(EnvironmentParams.JE_LOGGING_LEVEL.getName(), "CONFIG");
    env = new Environment(envHome, envConfig);
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);
    Database db = env.openDatabase(null, "foo", dbConfig);
    db.put(null, new DatabaseEntry(new byte[0]), new DatabaseEntry(new byte[0]));
    Transaction txn = env.beginTransaction(null, null);
    db.put(txn, new DatabaseEntry(new byte[0]), new DatabaseEntry(new byte[0]));
    stat = env.getStats(TestUtils.FAST_STATS);
    MemoryBudget mb = DbInternal.envGetEnvironmentImpl(env).getMemoryBudget();

    assertEquals(mb.getCacheMemoryUsage(), stat.getCacheTotalBytes());
    assertEquals(mb.getLogBufferBudget(), stat.getBufferBytes());
    assertEquals(mb.getTreeMemoryUsage() + mb.getTreeAdminMemoryUsage(), stat.getDataBytes());
    assertEquals(mb.getLockMemoryUsage(), stat.getLockBytes());
    assertEquals(mb.getAdminMemoryUsage(), stat.getAdminBytes());

    assertTrue(stat.getBufferBytes() > 0);
    assertTrue(stat.getDataBytes() > 0);
    assertTrue(stat.getLockBytes() > 0);
    assertTrue(stat.getAdminBytes() > 0);

    assertEquals(
        stat.getCacheTotalBytes(),
        stat.getBufferBytes() + stat.getDataBytes() + stat.getLockBytes() + stat.getAdminBytes());

    assertEquals(12, stat.getNCacheMiss());
    assertEquals(12, stat.getNNotResident());

    /* Test deprecated getCacheDataBytes method. */
    final EnvironmentStats finalStat = stat;
    final long expectCacheDataBytes = mb.getCacheMemoryUsage() - mb.getLogBufferBudget();
    (new Runnable() {
          @Deprecated
          public void run() {
            assertEquals(expectCacheDataBytes, finalStat.getCacheDataBytes());
          }
        })
        .run();

    txn.abort();
    db.close();
    env.close();
    env = null;
  }
  /*
   * Test that changing the Btree comparator really writes it to disk.
   */
  @Test
  public void testConfigOverrideUpdateSR15743() throws Throwable {

    try {
      EnvironmentConfig envConfig = TestUtils.initEnvConfig();
      envConfig.setAllowCreate(true);
      env = create(envHome, envConfig);

      /*
       * Make sure that the database keeps its own copy of the
       * configuration object.
       */
      DatabaseConfig dbConfigA = new DatabaseConfig();
      dbConfigA.setOverrideBtreeComparator(false);
      dbConfigA.setBtreeComparator(TestComparator.class);
      dbConfigA.setAllowCreate(true);
      Database dbA = env.openDatabase(null, "foo", dbConfigA);

      /* Change the original dbConfig */
      dbConfigA.setBtreeComparator(TestComparator2.class);
      DatabaseConfig getConfig1 = dbA.getConfig();
      assertEquals(TestComparator.class, getConfig1.getBtreeComparator().getClass());

      /*
       * Change the retrieved config, ought to have no effect on what the
       * Database is storing.
       */
      getConfig1.setBtreeComparator(TestComparator2.class);
      DatabaseConfig getConfig2 = dbA.getConfig();
      assertEquals(TestComparator.class, getConfig2.getBtreeComparator().getClass());

      dbA.close();
      close(env);

      /* Ensure new comparator is written to disk. */
      envConfig = TestUtils.initEnvConfig();
      env = create(envHome, envConfig);

      dbConfigA = new DatabaseConfig();
      /* Change the comparator. */
      dbConfigA.setOverrideBtreeComparator(true);
      dbConfigA.setBtreeComparator(TestComparator2.class);
      dbA = env.openDatabase(null, "foo", dbConfigA);

      getConfig2 = dbA.getConfig();
      assertEquals(TestComparator2.class, getConfig2.getBtreeComparator().getClass());

      dbA.close();
      close(env);

      /* Read it back during recovery to ensure it was written. */
      envConfig = TestUtils.initEnvConfig();
      env = create(envHome, envConfig);

      dbConfigA = new DatabaseConfig();
      dbA = env.openDatabase(null, "foo", dbConfigA);
      getConfig2 = dbA.getConfig();
      assertEquals(TestComparator2.class, getConfig2.getBtreeComparator().getClass());

      /* Create a root for the tree. */
      dbA.put(null, new DatabaseEntry(new byte[1]), new DatabaseEntry(new byte[1]));

      dbA.close();
      close(env);

      /* Change it to a third one when there is a root present. */
      envConfig = TestUtils.initEnvConfig();
      env = create(envHome, envConfig);

      dbConfigA = new DatabaseConfig();
      /* Change the comparator. */
      dbConfigA.setOverrideBtreeComparator(true);
      dbConfigA.setBtreeComparator(TestComparator3.class);
      dbA = env.openDatabase(null, "foo", dbConfigA);
      getConfig2 = dbA.getConfig();
      assertEquals(TestComparator3.class, getConfig2.getBtreeComparator().getClass());
      dbA.close();
      close(env);

      /* Read it back during recovery to ensure it was written. */
      envConfig = TestUtils.initEnvConfig();
      env = create(envHome, envConfig);

      dbConfigA = new DatabaseConfig();
      dbA = env.openDatabase(null, "foo", dbConfigA);
      getConfig2 = dbA.getConfig();
      assertEquals(TestComparator3.class, getConfig2.getBtreeComparator().getClass());
      dbA.close();
      close(env);

    } catch (Throwable t) {
      t.printStackTrace();
      throw t;
    }
  }
示例#17
0
文件: EnvUtil.java 项目: noter/errai
  public static ReachableTypes getAllReachableClasses(final GeneratorContext context) {
    if (System.getProperty(SYSPROP_USE_REACHABILITY_ANALYSIS) == null
        || !Boolean.getBoolean(SYSPROP_USE_REACHABILITY_ANALYSIS)) {

      log.warn("reachability analysis disabled. errai may generate unnecessary code.");
      log.warn(
          "enable reachability analysis with -D" + SYSPROP_USE_REACHABILITY_ANALYSIS + "=true");
      return ReachableTypes.EVERYTHING_REACHABLE_INSTANCE;
    }

    ReachabilityCache cache;
    if (reachabilityCache == null || (cache = reachabilityCache.get()) == null) {
      reachabilityCache = new SoftReference<ReachabilityCache>(cache = new ReachabilityCache());
    }

    if (cache.isCacheValid(context)) {
      return cache.getCache(context);
    }

    final EnvironmentConfig config = getEnvironmentConfig();

    long time = System.currentTimeMillis();

    final Set<String> packages = new HashSet<String>();

    if (isJUnitTest()) {
      packages.addAll(RebindUtils.findTranslatablePackagesInModule(context));
    } else {
      packages.addAll(RebindUtils.getOuterTranslatablePackages(context));
    }

    class Reachability {
      private final Set<String> packages;
      private final Set<String> negativeHits = new HashSet<String>();

      Reachability(final Set<String> packages) {
        this.packages = new HashSet<String>(packages);
      }

      public boolean isReachablePackage(final String pkg) {
        if (pkg == null || packages.contains(pkg)) {
          return true;
        }
        if (negativeHits.contains(pkg)) {
          return false;
        }

        for (final String p : packages) {
          if (pkg.startsWith(p)) {
            packages.add(pkg);
            return true;
          }
        }

        negativeHits.add(pkg);
        return false;
      }
    }

    final Set<String> allDependencies =
        Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(100));
    final Collection<MetaClass> allCachedClasses = MetaClassFactory.getAllCachedClasses();
    final ClassLoader classLoader = EnvUtil.class.getClassLoader();

    final ExecutorService executor =
        Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    final Reachability reachability = new Reachability(packages);

    try {
      for (final MetaClass mc : allCachedClasses) {
        String fullyQualifiedName = mc.getFullyQualifiedName();
        int splitPoint;
        while ((splitPoint = fullyQualifiedName.lastIndexOf('$')) != -1) {
          fullyQualifiedName = fullyQualifiedName.substring(0, splitPoint);
        }

        if (mc.isPrimitive() || mc.isArray()) {
          continue;
        } else if (isReachabilityExcluded(mc.getPackageName())) {
          continue;
        } else if (!config.getExplicitTypes().contains(fullyQualifiedName)
            && !reachability.isReachablePackage(mc.getPackageName())) {
          continue;
        }

        final URL resource =
            classLoader.getResource(fullyQualifiedName.replaceAll("\\.", "/") + ".java");

        if (resource != null) {
          InputStream stream = null;
          try {
            stream = new BufferedInputStream(resource.openStream());
            final byte[] readBuffer = new byte[stream.available()];
            stream.read(readBuffer);

            if (log.isDebugEnabled()) {
              log.debug("scanning " + fullyQualifiedName + " for reachable types ...");
            }
            executor.execute(new ReachabilityRunnable(readBuffer, allDependencies));
          } catch (IOException e) {
            log.warn("could not open resource: " + resource.getFile());
          } finally {
            if (stream != null) {
              stream.close();
            }
          }
        } else {
          log.warn("source for " + fullyQualifiedName + " is missing.");
        }
      }
    } catch (Throwable e) {
      e.printStackTrace();
    }

    try {
      executor.shutdown();
      executor.awaitTermination(60, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
      log.warn("the reachability analysis was interrupted", e);
      cache.putCache(context, ReachableTypes.EVERYTHING_REACHABLE_INSTANCE);
      return ReachableTypes.EVERYTHING_REACHABLE_INSTANCE;
    }

    if (log.isDebugEnabled()) {
      log.debug("*** REACHABILITY ANALYSIS (production mode: " + EnvUtil.isProdMode() + ") ***");
      for (final String s : allDependencies) {
        log.debug(" -> " + s);
      }

      time = System.currentTimeMillis() - time;

      log.debug("*** END OF REACHABILITY ANALYSIS (" + time + "ms) *** ");
    }

    final ReachableTypes reachableTypes = new ReachableTypes(allDependencies, true);
    cache.putCache(context, reachableTypes);
    return reachableTypes;
  }
  @Test
  public void testOpenReadOnly() throws Throwable {

    try {
      EnvironmentConfig envConfig = TestUtils.initEnvConfig();
      envConfig.setTransactional(true);
      envConfig.setAllowCreate(true);
      env = create(envHome, envConfig);

      DatabaseEntry key = new DatabaseEntry();
      DatabaseEntry data = new DatabaseEntry();

      Transaction txn = env.beginTransaction(null, null);
      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setTransactional(true);
      dbConfig.setAllowCreate(true);
      Database myDb = env.openDatabase(txn, "testDB2", dbConfig);

      key.setData(TestUtils.getTestArray(0));
      data.setData(TestUtils.getTestArray(0));
      try {
        myDb.put(txn, key, data);
      } catch (DatabaseException DBE) {
        fail("unexpected DatabaseException during put");
      }

      txn.commit();
      myDb.close();

      dbConfig = new DatabaseConfig();
      dbConfig.setTransactional(true);
      dbConfig.setReadOnly(true);
      txn = env.beginTransaction(null, null);
      myDb = env.openDatabase(txn, "testDB2", dbConfig);
      assertTrue(myDb.isTransactional());
      assertTrue(myDb.getConfig().getTransactional());

      key.setData(TestUtils.getTestArray(0));
      data.setData(TestUtils.getTestArray(0));
      try {
        myDb.put(txn, key, data);
        fail("expected UnsupportedOperationException " + "because open RDONLY");
      } catch (UnsupportedOperationException expected) {
      }

      key.setData(TestUtils.getTestArray(0));
      data.setData(TestUtils.getTestArray(0));
      assertEquals(OperationStatus.SUCCESS, myDb.get(txn, key, data, LockMode.DEFAULT));

      Cursor cursor = myDb.openCursor(txn, null);

      assertEquals(OperationStatus.SUCCESS, cursor.getFirst(key, data, LockMode.DEFAULT));

      try {
        cursor.delete();
        fail("expected Exception from delete on RD_ONLY db");
      } catch (UnsupportedOperationException e) {
      }

      key.setData(TestUtils.getTestArray(1));
      data.setData(TestUtils.getTestArray(1));
      try {
        myDb.put(txn, key, data);
        fail("expected UnsupportedOperationException because open RDONLY");
      } catch (UnsupportedOperationException expected) {
      }

      cursor.close();
      txn.commit();
      myDb.close();

      close(env);
    } catch (Throwable t) {
      t.printStackTrace();
      throw t;
    }
  }
  @Test
  public void testIsTransactional() throws Throwable {

    try {
      /* Open environment in transactional mode.*/
      EnvironmentConfig envConfig = TestUtils.initEnvConfig();
      envConfig.setTransactional(true);
      envConfig.setAllowCreate(true);
      env = create(envHome, envConfig);

      /* Create a db, open transactionally with implied auto-commit. */
      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setAllowCreate(true);
      dbConfig.setTransactional(true);
      Database myDb = env.openDatabase(null, "testDB", dbConfig);
      assertTrue(myDb.isTransactional());
      assertTrue(myDb.getConfig().getTransactional());
      myDb.close();

      /* Open an existing db, can open it non-transactionally. */
      dbConfig.setTransactional(false);
      myDb = env.openDatabase(null, "testDB", null);
      assertFalse(myDb.isTransactional());
      assertFalse(myDb.getConfig().getTransactional());
      myDb.close();

      /* Open another db, pass an explicit transaction. */
      dbConfig.setTransactional(true);
      Transaction txn = env.beginTransaction(null, null);
      myDb = env.openDatabase(txn, "testDB2", dbConfig);
      assertTrue(myDb.isTransactional());
      assertTrue(myDb.getConfig().getTransactional());

      DatabaseEntry key = new DatabaseEntry();
      DatabaseEntry data = new DatabaseEntry();
      key.setData(TestUtils.getTestArray(0));
      data.setData(TestUtils.getTestArray(0));
      try {
        myDb.put(null, key, data);
      } catch (DatabaseException DBE) {
        fail("didn't expect DatabaseException, implied autocommit");
      }

      key.setData(TestUtils.getTestArray(1));
      data.setData(TestUtils.getTestArray(1));
      try {
        myDb.put(txn, key, data);
      } catch (DatabaseException DBE) {
        fail("didn't expect DatabaseException with txn passed");
      }

      try {
        myDb.get(txn, key, data, LockMode.DEFAULT);
      } catch (DatabaseException DBE) {
        fail("didn't expect DatabaseException with txn passed");
      }

      txn.commit();

      try {
        myDb.get(null, key, data, LockMode.DEFAULT);
      } catch (DatabaseException DBE) {
        fail("didn't expect DatabaseException because no txn passed");
      }

      myDb.close();

      close(env);
    } catch (Throwable t) {
      t.printStackTrace();
      throw t;
    }
  }
示例#20
0
  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);
  }
  /**
   * Test that any conflicts between configuration object settings and the underlying impl object
   * are detected.
   */
  @Test
  public void testConfigConflict() throws Throwable {

    try {
      EnvironmentConfig envConfig = TestUtils.initEnvConfig();
      envConfig.setAllowCreate(true);
      env = create(envHome, envConfig);

      /*
       * Test conflicts of duplicate allowed configuration.
       */

      /* 1a. Create allowing duplicates. */
      DatabaseConfig firstConfig = new DatabaseConfig();
      firstConfig.setAllowCreate(true);
      firstConfig.setSortedDuplicates(true);
      Database firstHandle = env.openDatabase(null, "fooDups", firstConfig);
      /* 1b. Try to open w/no duplicates. */
      DatabaseConfig secondConfig = new DatabaseConfig();
      secondConfig.setSortedDuplicates(false);
      try {
        env.openDatabase(null, "fooDups", secondConfig);
        fail("Conflict in duplicates allowed should be detected.");
      } catch (IllegalArgumentException expected) {
      }

      firstHandle.close();
      env.removeDatabase(null, "fooDups");

      /* 2a. Create dis-allowing duplicates. */
      firstConfig.setSortedDuplicates(false);
      firstConfig.setKeyPrefixing(false);
      firstHandle = env.openDatabase(null, "fooDups", firstConfig);
      /* 2b. Try to open w/duplicates. */
      secondConfig.setSortedDuplicates(true);
      try {
        env.openDatabase(null, "fooDups", secondConfig);
        fail("Conflict in duplicates allowed should be detected.");
      } catch (IllegalArgumentException expected) {
      }
      firstHandle.close();

      /*
       * Test conflicts of read only. If the environment is read/write
       * we should be able to open handles in read only or read/write
       * mode. If the environment is readonly, the database handles
       * must also be read only.
       */
      DatabaseConfig readOnlyConfig = new DatabaseConfig();
      readOnlyConfig.setReadOnly(true);
      Database roHandle = env.openDatabase(null, "fooDups", readOnlyConfig);
      roHandle.close();

      /* Open the environment in read only mode. */
      close(env);
      envConfig = TestUtils.initEnvConfig();
      envConfig.setReadOnly(true);
      env = create(envHome, envConfig);

      /* Open a readOnly database handle, should succeed */
      roHandle = env.openDatabase(null, "fooDups", readOnlyConfig);
      roHandle.close();

      /* Open a read/write database handle, should not succeed. */
      try {
        env.openDatabase(null, "fooDups", null);
        fail("Should not be able to open read/write");
      } catch (IllegalArgumentException expected) {
      }
      close(env);

      /*
       * Check comparator changes.
       */
      /* 1a. Open w/a null comparator */
      env = create(envHome, null);
      firstConfig = new DatabaseConfig();
      firstConfig.setAllowCreate(true);
      firstHandle = env.openDatabase(null, "fooComparator", firstConfig);
      DatabaseConfig firstRetrievedConfig = firstHandle.getConfig();
      assertEquals(null, firstRetrievedConfig.getBtreeComparator());
      assertEquals(null, firstRetrievedConfig.getDuplicateComparator());

      /*
       * 1b. Open a db w/a different comparator, shouldn't take effect
       * because override is not set.
       */
      secondConfig = new DatabaseConfig();
      Comparator btreeComparator = new TestComparator();
      Comparator dupComparator = new TestComparator();
      secondConfig.setBtreeComparator((Class<Comparator<byte[]>>) btreeComparator.getClass());
      secondConfig.setDuplicateComparator((Class<Comparator<byte[]>>) dupComparator.getClass());
      Database secondHandle = env.openDatabase(null, "fooComparator", secondConfig);
      DatabaseConfig retrievedConfig = secondHandle.getConfig();
      assertEquals(null, retrievedConfig.getBtreeComparator());
      assertEquals(null, retrievedConfig.getDuplicateComparator());
      secondHandle.close();

      /* Same as above but with a serialized comparator. */
      secondConfig = new DatabaseConfig();
      btreeComparator = new TestSerialComparator();
      dupComparator = new TestSerialComparator();
      secondConfig.setBtreeComparator(btreeComparator);
      secondConfig.setDuplicateComparator(dupComparator);
      secondHandle = env.openDatabase(null, "fooComparator", secondConfig);
      retrievedConfig = secondHandle.getConfig();
      assertEquals(null, retrievedConfig.getBtreeComparator());
      assertEquals(null, retrievedConfig.getDuplicateComparator());
      secondHandle.close();

      /*
       * Test that update DatabaseConfig while there are open handles
       * should throw exceptions.
       */
      secondConfig.setOverrideBtreeComparator(true);
      secondConfig.setOverrideDuplicateComparator(true);
      btreeComparator = new TestComparator();
      dupComparator = new TestComparator();
      secondConfig.setBtreeComparator((Class<Comparator<byte[]>>) btreeComparator.getClass());
      secondConfig.setDuplicateComparator((Class<Comparator<byte[]>>) dupComparator.getClass());
      try {
        secondHandle = env.openDatabase(null, "fooComparator", secondConfig);
        fail("Expect exceptions here");
      } catch (IllegalStateException e) {
        /* Expected exception. */
      } catch (Exception e) {
        fail("Unexpected exception: " + e.getMessage());
      }
      secondHandle.close();

      /*
       * Open a new database handle without DatabaseConfig changes should
       * be valid.
       */
      try {
        secondHandle = env.openDatabase(null, "fooComparator", firstConfig);
      } catch (Exception e) {
        fail("Unexpected exception: " + e.getMessage());
      }

      secondHandle.close();
      firstHandle.close();
      close(env);
    } catch (Throwable t) {
      t.printStackTrace();
      close(env);
      throw t;
    }
  }
示例#22
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);
  }