Java Code Examples for org.iq80.leveldb.DBException

The following examples show how to use org.iq80.leveldb.DBException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public void storeToken(MRDelegationTokenIdentifier tokenId, Long renewDate)
    throws IOException {
  if (LOG.isDebugEnabled()) {
    LOG.debug("Storing token " + tokenId.getSequenceNumber());
  }

  ByteArrayOutputStream memStream = new ByteArrayOutputStream();
  DataOutputStream dataStream = new DataOutputStream(memStream);
  try {
    tokenId.write(dataStream);
    dataStream.writeLong(renewDate);
    dataStream.close();
    dataStream = null;
  } finally {
    IOUtils.cleanup(LOG, dataStream);
  }

  String dbKey = getTokenDatabaseKey(tokenId);
  try {
    db.put(bytes(dbKey), memStream.toByteArray());
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 2
Source Project: big-c   Source File: LeveldbRMStateStore.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void removeApplicationStateInternal(ApplicationStateData appState)
    throws IOException {
  ApplicationId appId =
      appState.getApplicationSubmissionContext().getApplicationId();
  String appKey = getApplicationNodeKey(appId);
  try {
    WriteBatch batch = db.createWriteBatch();
    try {
      batch.delete(bytes(appKey));
      for (ApplicationAttemptId attemptId : appState.attempts.keySet()) {
        String attemptKey = getApplicationAttemptNodeKey(appKey, attemptId);
        batch.delete(bytes(attemptKey));
      }
      if (LOG.isDebugEnabled()) {
        LOG.debug("Removing state for app " + appId + " and "
            + appState.attempts.size() + " attempts" + " at " + appKey);
      }
      db.write(batch);
    } finally {
      batch.close();
    }
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 3
Source Project: hadoop   Source File: NMLeveldbStateStoreService.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void storeContainerDiagnostics(ContainerId containerId,
    StringBuilder diagnostics) throws IOException {
  if (LOG.isDebugEnabled()) {
    LOG.debug("storeContainerDiagnostics: containerId=" + containerId
    + ", diagnostics=" + diagnostics);
  }

  String key = CONTAINERS_KEY_PREFIX + containerId.toString()
      + CONTAINER_DIAGS_KEY_SUFFIX;
  try {
    db.put(bytes(key), bytes(diagnostics.toString()));
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 4
Source Project: hadoop   Source File: NMLeveldbStateStoreService.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void removeContainer(ContainerId containerId)
    throws IOException {
  if (LOG.isDebugEnabled()) {
    LOG.debug("removeContainer: containerId=" + containerId);
  }

  String keyPrefix = CONTAINERS_KEY_PREFIX + containerId.toString();
  try {
    WriteBatch batch = db.createWriteBatch();
    try {
      batch.delete(bytes(keyPrefix + CONTAINER_REQUEST_KEY_SUFFIX));
      batch.delete(bytes(keyPrefix + CONTAINER_DIAGS_KEY_SUFFIX));
      batch.delete(bytes(keyPrefix + CONTAINER_LAUNCHED_KEY_SUFFIX));
      batch.delete(bytes(keyPrefix + CONTAINER_KILLED_KEY_SUFFIX));
      batch.delete(bytes(keyPrefix + CONTAINER_EXIT_CODE_KEY_SUFFIX));
      db.write(batch);
    } finally {
      batch.close();
    }
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 5
Source Project: hadoop   Source File: NMLeveldbStateStoreService.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void removeApplication(ApplicationId appId)
    throws IOException {
  if (LOG.isDebugEnabled()) {
    LOG.debug("removeApplication: appId=" + appId);
  }

  try {
    WriteBatch batch = db.createWriteBatch();
    try {
      String key = APPLICATIONS_KEY_PREFIX + appId;
      batch.delete(bytes(key));
      key = FINISHED_APPS_KEY_PREFIX + appId;
      batch.delete(bytes(key));
      db.write(batch);
    } finally {
      batch.close();
    }
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 6
Source Project: hadoop   Source File: NMLeveldbStateStoreService.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void finishResourceLocalization(String user, ApplicationId appId,
    LocalizedResourceProto proto) throws IOException {
  String localPath = proto.getLocalPath();
  String startedKey = getResourceStartedKey(user, appId, localPath);
  String completedKey = getResourceCompletedKey(user, appId, localPath);
  if (LOG.isDebugEnabled()) {
    LOG.debug("Storing localized resource to " + completedKey);
  }
  try {
    WriteBatch batch = db.createWriteBatch();
    try {
      batch.delete(bytes(startedKey));
      batch.put(bytes(completedKey), proto.toByteArray());
      db.write(batch);
    } finally {
      batch.close();
    }
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 7
Source Project: warp10-platform   Source File: WarpDB.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public DBIterator iterator(ReadOptions options) {
  if (null == options) {
    return iterator();
  }
  if (null != options.snapshot()) {
    throw new RuntimeException("Snapshots are unsupported.");
  }
  try {
    mutex.lockInterruptibly();
    pendingOps.incrementAndGet();
  } catch (InterruptedException ie) {
    throw new DBException("Interrupted while acquiring DB mutex.", ie);
  } finally {
    if (mutex.isHeldByCurrentThread()) {
      mutex.unlock();
    }
  }    
  return new WarpIterator(pendingOps, this.db.iterator(options));    
}
 
Example 8
Source Project: warp10-platform   Source File: WarpDB.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void put(byte[] key, byte[] value) throws DBException {
  try {
    mutex.lockInterruptibly();
    pendingOps.incrementAndGet();
  } catch (InterruptedException ie) {
    throw new DBException("Interrupted while acquiring DB mutex.", ie);
  } finally {
    if (mutex.isHeldByCurrentThread()) {
      mutex.unlock();
    }
  }
  try {
    this.db.put(key, value);
  } finally {
    this.pendingOps.decrementAndGet();
  }
}
 
Example 9
Source Project: hadoop   Source File: LeveldbRMStateStore.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void startInternal() throws Exception {
  Path storeRoot = createStorageDir();
  Options options = new Options();
  options.createIfMissing(false);
  options.logger(new LeveldbLogger());
  LOG.info("Using state database at " + storeRoot + " for recovery");
  File dbfile = new File(storeRoot.toString());
  try {
    db = JniDBFactory.factory.open(dbfile, options);
  } catch (NativeDB.DBException e) {
    if (e.isNotFound() || e.getMessage().contains(" does not exist ")) {
      LOG.info("Creating state database at " + dbfile);
      options.createIfMissing(true);
      try {
        db = JniDBFactory.factory.open(dbfile, options);
        // store version
        storeVersion();
      } catch (DBException dbErr) {
        throw new IOException(dbErr.getMessage(), dbErr);
      }
    } else {
      throw e;
    }
  }
}
 
Example 10
Source Project: hadoop   Source File: LeveldbRMStateStore.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public synchronized long getAndIncrementEpoch() throws Exception {
  long currentEpoch = 0;
  byte[] dbKeyBytes = bytes(EPOCH_NODE);
  try {
    byte[] data = db.get(dbKeyBytes);
    if (data != null) {
      currentEpoch = EpochProto.parseFrom(data).getEpoch();
    }
    EpochProto proto = Epoch.newInstance(currentEpoch + 1).getProto();
    db.put(dbKeyBytes, proto.toByteArray());
  } catch (DBException e) {
    throw new IOException(e);
  }
  return currentEpoch;
}
 
Example 11
Source Project: hadoop   Source File: LeveldbRMStateStore.java    License: Apache License 2.0 6 votes vote down vote up
private void loadRMDTSecretManagerTokenSequenceNumber(RMState state)
    throws IOException {
  byte[] data = null;
  try {
    data = db.get(bytes(RM_DT_SEQUENCE_NUMBER_KEY));
  } catch (DBException e) {
    throw new IOException(e);
  }
  if (data != null) {
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(data));
    try {
      state.rmSecretManagerState.dtSequenceNumber = in.readInt();
    } finally {
      IOUtils.cleanup(LOG, in);
    }
  }
}
 
Example 12
Source Project: hadoop   Source File: LeveldbRMStateStore.java    License: Apache License 2.0 6 votes vote down vote up
private void loadAMRMTokenSecretManagerState(RMState rmState)
    throws IOException {
  try {
    byte[] data = db.get(bytes(AMRMTOKEN_SECRET_MANAGER_ROOT));
    if (data != null) {
      AMRMTokenSecretManagerStatePBImpl stateData =
          new AMRMTokenSecretManagerStatePBImpl(
              AMRMTokenSecretManagerStateProto.parseFrom(data));
      rmState.amrmTokenSecretManagerState =
          AMRMTokenSecretManagerState.newInstance(
              stateData.getCurrentMasterKey(),
              stateData.getNextMasterKey());
    }
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 13
Source Project: hadoop   Source File: LeveldbRMStateStore.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void removeApplicationStateInternal(ApplicationStateData appState)
    throws IOException {
  ApplicationId appId =
      appState.getApplicationSubmissionContext().getApplicationId();
  String appKey = getApplicationNodeKey(appId);
  try {
    WriteBatch batch = db.createWriteBatch();
    try {
      batch.delete(bytes(appKey));
      for (ApplicationAttemptId attemptId : appState.attempts.keySet()) {
        String attemptKey = getApplicationAttemptNodeKey(appKey, attemptId);
        batch.delete(bytes(attemptKey));
      }
      if (LOG.isDebugEnabled()) {
        LOG.debug("Removing state for app " + appId + " and "
            + appState.attempts.size() + " attempts" + " at " + appKey);
      }
      db.write(batch);
    } finally {
      batch.close();
    }
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 14
Source Project: big-c   Source File: LeveldbTimelineStateStore.java    License: Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
Version loadVersion() throws IOException {
  try {
    byte[] data = db.get(TIMELINE_STATE_STORE_VERSION_KEY);
    // if version is not stored previously, treat it as CURRENT_VERSION_INFO.
    if (data == null || data.length == 0) {
      return getCurrentVersion();
    }
    Version version =
        new VersionPBImpl(
            YarnServerCommonProtos.VersionProto.parseFrom(data));
    return version;
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 15
Source Project: hadoop   Source File: LeveldbRMStateStore.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void storeRMDTMasterKeyState(DelegationKey masterKey)
    throws IOException {
  String dbKey = getRMDTMasterKeyNodeKey(masterKey);
  if (LOG.isDebugEnabled()) {
    LOG.debug("Storing token master key to " + dbKey);
  }
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  DataOutputStream out = new DataOutputStream(os);
  try {
    masterKey.write(out);
  } finally {
    out.close();
  }
  try {
    db.put(bytes(dbKey), os.toByteArray());
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 16
Source Project: hadoop   Source File: LeveldbTimelineStateStore.java    License: Apache License 2.0 6 votes vote down vote up
private int loadTokens(TimelineServiceState state) throws IOException {
  byte[] base = KeyBuilder.newInstance().add(TOKEN_ENTRY_PREFIX)
      .getBytesForLookup();
  int numTokens = 0;
  LeveldbIterator iterator = null;
  try {
    for (iterator = new LeveldbIterator(db), iterator.seek(base);
        iterator.hasNext(); iterator.next()) {
      byte[] k = iterator.peekNext().getKey();
      if (!prefixMatches(base, base.length, k)) {
        break;
      }
      byte[] v = iterator.peekNext().getValue();
      loadTokenData(state, v);
      ++numTokens;
    }
  } catch (DBException e) {
    throw new IOException(e);
  } finally {
    IOUtils.cleanup(LOG, iterator);
  }
  return numTokens;
}
 
Example 17
Source Project: hadoop   Source File: LeveldbTimelineStateStore.java    License: Apache License 2.0 6 votes vote down vote up
private void loadLatestSequenceNumber(TimelineServiceState state)
    throws IOException {
  byte[] data = null;
  try {
    data = db.get(LATEST_SEQUENCE_NUMBER_KEY);
  } catch (DBException e) {
    throw new IOException(e);
  }
  if (data != null) {
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(data));
    try {
      state.latestSequenceNumber = in.readInt();
    } finally {
      IOUtils.cleanup(LOG, in);
    }
  }
}
 
Example 18
Source Project: hadoop   Source File: LeveldbTimelineStateStore.java    License: Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
Version loadVersion() throws IOException {
  try {
    byte[] data = db.get(TIMELINE_STATE_STORE_VERSION_KEY);
    // if version is not stored previously, treat it as CURRENT_VERSION_INFO.
    if (data == null || data.length == 0) {
      return getCurrentVersion();
    }
    Version version =
        new VersionPBImpl(
            YarnServerCommonProtos.VersionProto.parseFrom(data));
    return version;
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 19
Source Project: hadoop   Source File: ShuffleHandler.java    License: Apache License 2.0 6 votes vote down vote up
private void startStore(Path recoveryRoot) throws IOException {
  Options options = new Options();
  options.createIfMissing(false);
  options.logger(new LevelDBLogger());
  Path dbPath = new Path(recoveryRoot, STATE_DB_NAME);
  LOG.info("Using state database at " + dbPath + " for recovery");
  File dbfile = new File(dbPath.toString());
  try {
    stateDb = JniDBFactory.factory.open(dbfile, options);
  } catch (NativeDB.DBException e) {
    if (e.isNotFound() || e.getMessage().contains(" does not exist ")) {
      LOG.info("Creating state database at " + dbfile);
      options.createIfMissing(true);
      try {
        stateDb = JniDBFactory.factory.open(dbfile, options);
        storeVersion();
      } catch (DBException dbExc) {
        throw new IOException("Unable to create state store", dbExc);
      }
    } else {
      throw e;
    }
  }
  checkVersion();
}
 
Example 20
Source Project: warp10-platform   Source File: WarpDB.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Snapshot put(byte[] key, byte[] value, WriteOptions options) throws DBException {
  if (options.snapshot()) {
    throw new RuntimeException("Snapshots are unsupported.");      
  }
  
  try {
    mutex.lockInterruptibly();
    pendingOps.incrementAndGet();
  } catch (InterruptedException ie) {
    throw new DBException("Interrupted while acquiring DB mutex.", ie);
  } finally {
    if (mutex.isHeldByCurrentThread()) {
      mutex.unlock();
    }
  }
  try {
    return this.db.put(key, value, options);
  } finally {
    this.pendingOps.decrementAndGet();
  }
}
 
Example 21
@Override
public void storeTokenMasterKey(DelegationKey masterKey)
    throws IOException {
  if (LOG.isDebugEnabled()) {
    LOG.debug("Storing master key " + masterKey.getKeyId());
  }

  ByteArrayOutputStream memStream = new ByteArrayOutputStream();
  DataOutputStream dataStream = new DataOutputStream(memStream);
  try {
    masterKey.write(dataStream);
    dataStream.close();
    dataStream = null;
  } finally {
    IOUtils.cleanup(LOG, dataStream);
  }

  String dbKey = getTokenMasterKeyDatabaseKey(masterKey);
  try {
    db.put(bytes(dbKey), memStream.toByteArray());
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 22
@Override
public void storeToken(MRDelegationTokenIdentifier tokenId, Long renewDate)
    throws IOException {
  if (LOG.isDebugEnabled()) {
    LOG.debug("Storing token " + tokenId.getSequenceNumber());
  }

  ByteArrayOutputStream memStream = new ByteArrayOutputStream();
  DataOutputStream dataStream = new DataOutputStream(memStream);
  try {
    tokenId.write(dataStream);
    dataStream.writeLong(renewDate);
    dataStream.close();
    dataStream = null;
  } finally {
    IOUtils.cleanup(LOG, dataStream);
  }

  String dbKey = getTokenDatabaseKey(tokenId);
  try {
    db.put(bytes(dbKey), memStream.toByteArray());
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 23
@Override
public void storeTokenMasterKey(DelegationKey masterKey)
    throws IOException {
  if (LOG.isDebugEnabled()) {
    LOG.debug("Storing master key " + masterKey.getKeyId());
  }

  ByteArrayOutputStream memStream = new ByteArrayOutputStream();
  DataOutputStream dataStream = new DataOutputStream(memStream);
  try {
    masterKey.write(dataStream);
    dataStream.close();
    dataStream = null;
  } finally {
    IOUtils.cleanup(LOG, dataStream);
  }

  String dbKey = getTokenMasterKeyDatabaseKey(masterKey);
  try {
    db.put(bytes(dbKey), memStream.toByteArray());
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 24
Source Project: big-c   Source File: NMLeveldbStateStoreService.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void removeContainer(ContainerId containerId)
    throws IOException {
  String keyPrefix = CONTAINERS_KEY_PREFIX + containerId.toString();
  try {
    WriteBatch batch = db.createWriteBatch();
    try {
      batch.delete(bytes(keyPrefix + CONTAINER_REQUEST_KEY_SUFFIX));
      batch.delete(bytes(keyPrefix + CONTAINER_DIAGS_KEY_SUFFIX));
      batch.delete(bytes(keyPrefix + CONTAINER_LAUNCHED_KEY_SUFFIX));
      batch.delete(bytes(keyPrefix + CONTAINER_KILLED_KEY_SUFFIX));
      batch.delete(bytes(keyPrefix + CONTAINER_EXIT_CODE_KEY_SUFFIX));
      db.write(batch);
    } finally {
      batch.close();
    }
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 25
Source Project: big-c   Source File: NMLeveldbStateStoreService.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void removeApplication(ApplicationId appId)
    throws IOException {
  try {
    WriteBatch batch = db.createWriteBatch();
    try {
      String key = APPLICATIONS_KEY_PREFIX + appId;
      batch.delete(bytes(key));
      key = FINISHED_APPS_KEY_PREFIX + appId;
      batch.delete(bytes(key));
      db.write(batch);
    } finally {
      batch.close();
    }
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 26
Source Project: big-c   Source File: NMLeveldbStateStoreService.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void finishResourceLocalization(String user, ApplicationId appId,
    LocalizedResourceProto proto) throws IOException {
  String localPath = proto.getLocalPath();
  String startedKey = getResourceStartedKey(user, appId, localPath);
  String completedKey = getResourceCompletedKey(user, appId, localPath);
  if (LOG.isDebugEnabled()) {
    LOG.debug("Storing localized resource to " + completedKey);
  }
  try {
    WriteBatch batch = db.createWriteBatch();
    try {
      batch.delete(bytes(startedKey));
      batch.put(bytes(completedKey), proto.toByteArray());
      db.write(batch);
    } finally {
      batch.close();
    }
  } catch (DBException e) {
    throw new IOException(e);
  }
}
 
Example 27
Source Project: warp10-platform   Source File: WarpDB.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Snapshot write(WriteBatch updates, WriteOptions options) throws DBException {
  if (options.snapshot()) {
    throw new RuntimeException("Snapshots are unsupported.");      
  }
  
  try {
    mutex.lockInterruptibly();
    pendingOps.incrementAndGet();
  } catch (InterruptedException ie) {
    throw new DBException("Interrupted while acquiring DB mutex.", ie);
  } finally {
    if (mutex.isHeldByCurrentThread()) {
      mutex.unlock();
    }
  }
  try {
    return this.db.write(updates, options);
  } finally {
    this.pendingOps.decrementAndGet();
  }    
}
 
Example 28
Source Project: tez   Source File: ShuffleHandler.java    License: Apache License 2.0 6 votes vote down vote up
private void startStore(Path recoveryRoot) throws IOException {
  Options options = new Options();
  options.createIfMissing(false);
  options.logger(new LevelDBLogger());
  Path dbPath = new Path(recoveryRoot, STATE_DB_NAME);
  LOG.info("Using state database at " + dbPath + " for recovery");
  File dbfile = new File(dbPath.toString());
  try {
    stateDb = JniDBFactory.factory.open(dbfile, options);
  } catch (NativeDB.DBException e) {
    if (e.isNotFound() || e.getMessage().contains(" does not exist ")) {
      LOG.info("Creating state database at " + dbfile);
      options.createIfMissing(true);
      try {
        stateDb = JniDBFactory.factory.open(dbfile, options);
        storeVersion();
      } catch (DBException dbExc) {
        throw new IOException("Unable to create state store", dbExc);
      }
    } else {
      throw e;
    }
  }
  checkVersion();
}
 
Example 29
Source Project: hadoop-ozone   Source File: BucketManagerImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns Bucket Information.
 *
 * @param volumeName - Name of the Volume.
 * @param bucketName - Name of the Bucket.
 */
@Override
public OmBucketInfo getBucketInfo(String volumeName, String bucketName)
    throws IOException {
  Preconditions.checkNotNull(volumeName);
  Preconditions.checkNotNull(bucketName);
  metadataManager.getLock().acquireReadLock(BUCKET_LOCK, volumeName,
      bucketName);
  try {
    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
    OmBucketInfo value = metadataManager.getBucketTable().get(bucketKey);
    if (value == null) {
      LOG.debug("bucket: {} not found in volume: {}.", bucketName,
          volumeName);
      throw new OMException("Bucket not found",
          BUCKET_NOT_FOUND);
    }
    return value;
  } catch (IOException | DBException ex) {
    if (!(ex instanceof OMException)) {
      LOG.error("Exception while getting bucket info for bucket: {}",
          bucketName, ex);
    }
    throw ex;
  } finally {
    metadataManager.getLock().releaseReadLock(BUCKET_LOCK, volumeName,
        bucketName);
  }
}
 
Example 30
Source Project: aion   Source File: LevelDB.java    License: MIT License 5 votes vote down vote up
@Override
public byte[] getInternal(byte[] k) {
    try {
        return db.get(k);
    } catch (DBException e) {
        repair();
        // will throw the exception if the repair did not work
        return db.get(k);
    }
}