private int loadRMApp(RMState rmState, LeveldbIterator iter, String appIdStr, byte[] appData)
      throws IOException {
    ApplicationStateData appState = createApplicationState(appIdStr, appData);
    ApplicationId appId = appState.getApplicationSubmissionContext().getApplicationId();
    rmState.appState.put(appId, appState);
    String attemptNodePrefix = getApplicationNodeKey(appId) + SEPARATOR;
    while (iter.hasNext()) {
      Entry<byte[], byte[]> entry = iter.peekNext();
      String key = asString(entry.getKey());
      if (!key.startsWith(attemptNodePrefix)) {
        break;
      }

      String attemptId = key.substring(attemptNodePrefix.length());
      if (attemptId.startsWith(ApplicationAttemptId.appAttemptIdStrPrefix)) {
        ApplicationAttemptStateData attemptState = createAttemptState(attemptId, entry.getValue());
        appState.attempts.put(attemptState.getAttemptId(), attemptState);
      } else {
        LOG.warn("Ignoring unknown application key: " + key);
      }
      iter.next();
    }
    int numAttempts = appState.attempts.size();
    if (LOG.isDebugEnabled()) {
      LOG.debug("Loaded application " + appId + " with " + numAttempts + " attempts");
    }
    return numAttempts;
  }
  private void loadRMApps(RMState state) throws IOException {
    int numApps = 0;
    int numAppAttempts = 0;
    LeveldbIterator iter = null;
    try {
      iter = new LeveldbIterator(db);
      iter.seek(bytes(RM_APP_KEY_PREFIX));
      while (iter.hasNext()) {
        Entry<byte[], byte[]> entry = iter.next();
        String key = asString(entry.getKey());
        if (!key.startsWith(RM_APP_KEY_PREFIX)) {
          break;
        }

        String appIdStr = key.substring(RM_APP_ROOT.length() + 1);
        if (appIdStr.contains(SEPARATOR)) {
          LOG.warn("Skipping extraneous data " + key);
          continue;
        }

        numAppAttempts += loadRMApp(state, iter, appIdStr, entry.getValue());
        ++numApps;
      }
    } catch (DBException e) {
      throw new IOException(e);
    } finally {
      if (iter != null) {
        iter.close();
      }
    }
    LOG.info(
        "Recovered " + numApps + " applications and " + numAppAttempts + " application attempts");
  }
 private int loadRMDTSecretManagerKeys(RMState state) throws IOException {
   int numKeys = 0;
   LeveldbIterator iter = null;
   try {
     iter = new LeveldbIterator(db);
     iter.seek(bytes(RM_DT_MASTER_KEY_KEY_PREFIX));
     while (iter.hasNext()) {
       Entry<byte[], byte[]> entry = iter.next();
       String key = asString(entry.getKey());
       if (!key.startsWith(RM_DT_MASTER_KEY_KEY_PREFIX)) {
         break;
       }
       DelegationKey masterKey = loadDelegationKey(entry.getValue());
       state.rmSecretManagerState.masterKeyState.add(masterKey);
       ++numKeys;
       if (LOG.isDebugEnabled()) {
         LOG.debug(
             "Loaded RM delegation key from "
                 + key
                 + ": keyId="
                 + masterKey.getKeyId()
                 + ", expirationDate="
                 + masterKey.getExpiryDate());
       }
     }
   } catch (DBException e) {
     throw new IOException(e);
   } finally {
     if (iter != null) {
       iter.close();
     }
   }
   return numKeys;
 }
 @VisibleForTesting
 int getNumEntriesInDatabase() throws IOException {
   int numEntries = 0;
   LeveldbIterator iter = null;
   try {
     iter = new LeveldbIterator(db);
     iter.seekToFirst();
     while (iter.hasNext()) {
       Entry<byte[], byte[]> entry = iter.next();
       LOG.info("entry: " + asString(entry.getKey()));
       ++numEntries;
     }
   } catch (DBException e) {
     throw new IOException(e);
   } finally {
     if (iter != null) {
       iter.close();
     }
   }
   return numEntries;
 }
 private int loadRMDTSecretManagerTokens(RMState state) throws IOException {
   int numTokens = 0;
   LeveldbIterator iter = null;
   try {
     iter = new LeveldbIterator(db);
     iter.seek(bytes(RM_DT_TOKEN_KEY_PREFIX));
     while (iter.hasNext()) {
       Entry<byte[], byte[]> entry = iter.next();
       String key = asString(entry.getKey());
       if (!key.startsWith(RM_DT_TOKEN_KEY_PREFIX)) {
         break;
       }
       RMDelegationTokenIdentifierData tokenData = loadDelegationToken(entry.getValue());
       RMDelegationTokenIdentifier tokenId = tokenData.getTokenIdentifier();
       long renewDate = tokenData.getRenewDate();
       state.rmSecretManagerState.delegationTokenState.put(tokenId, renewDate);
       ++numTokens;
       if (LOG.isDebugEnabled()) {
         LOG.debug(
             "Loaded RM delegation token from "
                 + key
                 + ": tokenId="
                 + tokenId
                 + ", renewDate="
                 + renewDate);
       }
     }
   } catch (DBException e) {
     throw new IOException(e);
   } finally {
     if (iter != null) {
       iter.close();
     }
   }
   return numTokens;
 }