/**
   * Close the database.
   *
   * @param cleanLog if true then wait for the BerkeleyDB clean thread to finish.
   */
  @Override
  public void close(boolean cleanLog) {
    log.info("Closing db & env for: " + dir.getAbsolutePath());
    if (openIterators.size() > 0) {
      log.warn("closing " + openIterators.size() + " iterators on close");
      for (Object e : openIterators.toArray(new Object[openIterators.size()])) {
        ((ClosableIterator<Map.Entry<byte[], byte[]>>) e).close();
      }
    }
    log.info("pages:gets=" + gets + " puts=" + puts + " in=" + bytesIn + " out=" + bytesOut);

    bdb.close();
    if (cleanLog) {
      bdb_env.getConfig().setConfigParam(EnvironmentConfig.ENV_RUN_CLEANER, "false");
      int totalLogFiles = 0;
      int logFiles;
      do {
        logFiles = bdb_env.cleanLog();
        totalLogFiles += logFiles;
      } while (logFiles > 0);

      log.info("Total of " + totalLogFiles + " log files cleaned.");

      if (totalLogFiles > 0) {
        CheckpointConfig force = new CheckpointConfig();
        force.setForce(true);
        bdb_env.checkpoint(force);
      }
    }
    bdb_env.close();
  }
 private void expectNothingToClean() {
   env.cleanLog();
   final EnvironmentStats stats = env.getStats(null);
   final String msg =
       String.format(
           "%d probes, %d non-probes", stats.getNCleanerProbeRuns(), stats.getNCleanerRuns());
   assertEquals(msg, 0, stats.getNCleanerRuns() - stats.getNCleanerProbeRuns());
 }
  private int cleanLog(Environment env) {
    int numCleaned = 0;
    int total = 0;
    do {
      numCleaned = env.cleanLog();
      total += numCleaned;
    } while (numCleaned > 0);

    logger.info("cleaned " + total);
    return total;
  }
 /** Forceful cleanup the logs */
 @JmxOperation(description = "Forceful start the cleaner threads")
 public void cleanLogs() {
   synchronized (lock) {
     try {
       for (Environment environment : environments.values()) {
         environment.cleanLog();
       }
     } catch (DatabaseException e) {
       throw new VoldemortException(e);
     }
   }
 }
Exemple #5
0
  /**
   * Invoke an operation for the given environment.
   *
   * @param targetEnv The target JE environment. May be null if the environment is not open.
   * @param actionName operation name.
   * @param params operation parameters. May be null.
   * @param signature operation signature. May be null.
   * @return the operation result
   */
  public Object invoke(
      Environment targetEnv, String actionName, Object[] params, String[] signature)
      throws MBeanException {

    /* Sanity checking. */
    if (actionName == null) {
      throw new IllegalArgumentException("actionName cannot be null");
    }

    try {
      if (targetEnv != null) {
        if (actionName.equals(OP_CLEAN)) {
          int numFiles = targetEnv.cleanLog();
          return new Integer(numFiles);
        } else if (actionName.equals(OP_EVICT)) {
          targetEnv.evictMemory();
          return null;
        } else if (actionName.equals(OP_CHECKPOINT)) {
          CheckpointConfig config = new CheckpointConfig();
          if ((params != null) && (params.length > 0)) {
            Boolean force = (Boolean) params[0];
            config.setForce(force.booleanValue());
          }
          targetEnv.checkpoint(config);
          return null;
        } else if (actionName.equals(OP_SYNC)) {
          targetEnv.sync();
          return null;
        } else if (actionName.equals(OP_ENV_STAT)) {
          return targetEnv.getStats(getStatsConfig(params));
        } else if (actionName.equals(OP_LOCK_STAT)) {
          return targetEnv.getLockStats(getStatsConfig(params));
        } else if (actionName.equals(OP_TXN_STAT)) {
          return targetEnv.getTransactionStats(getStatsConfig(params));
        } else if (actionName.equals(OP_DB_NAMES)) {
          return targetEnv.getDatabaseNames();
        } else if (actionName.equals(OP_DB_STAT)) {
          return getDatabaseStats(targetEnv, params);
        }
      }

      return new IllegalArgumentException("actionName: " + actionName + " is not valid");
    } catch (DatabaseException e) {
      /*
       * Add both the message and the exception for easiest
       * deciphering of the problem. Sometimes the original exception
       * stacktrace gets hidden in server logs.
       */
      throw new MBeanException(e, e.getMessage());
    }
  }
Exemple #6
0
  public static void main(String[] args) {
    Environment env = null;
    Database db = null;
    EnvironmentConfig envconfig = new EnvironmentConfig();
    envconfig.setAllowCreate(true);
    try {
      env = new Environment(new File("D://bdb"), envconfig);
      DatabaseConfig dbconfig = new DatabaseConfig();
      dbconfig.setAllowCreate(true);
      db = env.openDatabase(null, "dbac.db", dbconfig);
      String key = "mykey";
      DatabaseEntry thekey = new DatabaseEntry();
      thekey.setData(key.getBytes("utf-8"));

      Long value = new Long(123456);
      DatabaseEntry thevalue = new DatabaseEntry();
      EntryBinding myBinging = TupleBinding.getPrimitiveBinding(Long.class);
      myBinging.objectToEntry(value, thevalue);
      // LongBinding myLongBinging=(LongBinding)TupleBinding.getPrimitiveBinding(Long.class);
      // myLongBinging.objectToEntry(value, thevalue);
      db.put(null, thekey, thevalue);
      DatabaseEntry valueEntry = new DatabaseEntry();
      OperationStatus status = db.get(null, thekey, valueEntry, LockMode.DEFAULT);
      if (status == OperationStatus.SUCCESS) {
        // Long number=myLongBinging.entryToObject(valueEntry);
        Long number = (Long) myBinging.entryToObject(valueEntry);
        System.out.println(env.getDatabaseNames());
        System.out.println(number);
      }
    } catch (EnvironmentLockedException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (db != null) {
        try {
          db.close();
        } catch (DatabaseException e) {
          e.printStackTrace();
        }
      }
      if (env != null) {
        try {
          env.cleanLog();
          env.close();
        } catch (DatabaseException e) {
          e.printStackTrace();
        }
      }
    }
  }
Exemple #7
0
  public static void close() {
    long startTs = System.currentTimeMillis();

    try {
      if (database != null) {
        database.close(); // 会调用sync
      }
      env.cleanLog();
      env.close();
    } catch (Exception e) {
      // ingore
      logger.warn("close exception ", e);
    }

    logger.warn("close takes " + (System.currentTimeMillis() - startTs));
  }
Exemple #8
0
  static {
    long stime = System.currentTimeMillis();

    File f = new File(FILE_PATH);
    if (!f.exists()) {
      f.mkdirs();
    }
    /** =====================env配置======================= */
    envConfig = new EnvironmentConfig();
    envConfig.setAllowCreate(true);
    envConfig.setTransactional(false);
    /** 缓存40M 日志文件最大为20M,默认是10M */
    envConfig.setCacheSize(1024 * 1024 * 40);
    envConfig.setConfigParam("je.log.fileMax", String.valueOf(1024 * 1024 * 20));

    /** 修改写缓存比例,总共6M */
    envConfig.setConfigParam("je.log.bufferSize", String.valueOf(1024 * 1024 * 2));
    envConfig.setConfigParam("je.log.totalBufferBytes", String.valueOf(1024 * 1024 * 6)); //
    env = new Environment(new File(FILE_PATH), envConfig);
    env.cleanLog();
    /** db配置 */
    dbConfig = new DatabaseConfig();
    dbConfig.setAllowCreate(true);
    // dbConfig.setDeferredWrite(true);

    database = env.openDatabase(null, SOURCE_DB, dbConfig);

    Runtime.getRuntime()
        .addShutdownHook(
            new Thread() {
              public void run() {
                close();
                logger.warn("close db and env");
              }
            });

    logger.warn("init bdb success " + (System.currentTimeMillis() - stime));
  }