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;
  }
 @Override
 protected void storeApplicationAttemptStateInternal(
     ApplicationAttemptId attemptId, ApplicationAttemptStateData attemptStateData)
     throws IOException {
   String key = getApplicationAttemptNodeKey(attemptId);
   if (LOG.isDebugEnabled()) {
     LOG.debug("Storing state for attempt " + attemptId + " at " + key);
   }
   try {
     db.put(bytes(key), attemptStateData.getProto().toByteArray());
   } catch (DBException e) {
     throw new IOException(e);
   }
 }
  private void loadRMAppState(RMState rmState) throws Exception {
    try {
      List<ApplicationAttemptStateData> attempts = new ArrayList<>();
      final RMAppStateFileProcessor rmAppStateFileProcessor =
          new RMAppStateFileProcessor(rmState, attempts);
      final Path rootDirectory = this.rmAppRoot;

      processDirectoriesOfFiles(rmAppStateFileProcessor, rootDirectory);

      // go through all attempts and add them to their apps, Ideally, each
      // attempt node must have a corresponding app node, because remove
      // directory operation remove both at the same time
      for (ApplicationAttemptStateData attemptState : attempts) {
        ApplicationId appId = attemptState.getAttemptId().getApplicationId();
        ApplicationStateData appState = rmState.appState.get(appId);
        assert appState != null;
        appState.attempts.put(attemptState.getAttemptId(), attemptState);
      }
      LOG.info("Done loading applications from FS state store");
    } catch (Exception e) {
      LOG.error("Failed to load state.", e);
      throw e;
    }
  }
 @Override
 public synchronized void updateApplicationAttemptStateInternal(
     ApplicationAttemptId appAttemptId, ApplicationAttemptStateData attemptStateDataPB)
     throws Exception {
   Path appDirPath = getAppDir(rmAppRoot, appAttemptId.getApplicationId());
   Path nodeCreatePath = getNodePath(appDirPath, appAttemptId.toString());
   LOG.info("Updating info for attempt: " + appAttemptId + " at: " + nodeCreatePath);
   byte[] attemptStateData = attemptStateDataPB.getProto().toByteArray();
   try {
     // currently throw all exceptions. May need to respond differently for HA
     // based on whether we have lost the right to write to FS
     updateFile(nodeCreatePath, attemptStateData, true);
   } catch (Exception e) {
     LOG.info("Error updating info for attempt: " + appAttemptId, e);
     throw e;
   }
 }