@Override
 public void processChildNode(String appDirName, String childNodeName, byte[] childData)
     throws com.google.protobuf.InvalidProtocolBufferException {
   if (childNodeName.startsWith(ApplicationId.appIdStrPrefix)) {
     // application
     if (LOG.isDebugEnabled()) {
       LOG.debug("Loading application from node: " + childNodeName);
     }
     ApplicationStateDataPBImpl appState =
         new ApplicationStateDataPBImpl(ApplicationStateDataProto.parseFrom(childData));
     ApplicationId appId = appState.getApplicationSubmissionContext().getApplicationId();
     rmState.appState.put(appId, appState);
   } else if (childNodeName.startsWith(ApplicationAttemptId.appAttemptIdStrPrefix)) {
     // attempt
     if (LOG.isDebugEnabled()) {
       LOG.debug("Loading application attempt from node: " + childNodeName);
     }
     ApplicationAttemptStateDataPBImpl attemptState =
         new ApplicationAttemptStateDataPBImpl(
             ApplicationAttemptStateDataProto.parseFrom(childData));
     attempts.add(attemptState);
   } else {
     LOG.info("Unknown child node with name: " + childNodeName);
   }
 }
 private ApplicationStateData createApplicationState(String appIdStr, byte[] data)
     throws IOException {
   ApplicationId appId = ConverterUtils.toApplicationId(appIdStr);
   ApplicationStateDataPBImpl appState =
       new ApplicationStateDataPBImpl(ApplicationStateDataProto.parseFrom(data));
   if (!appId.equals(appState.getApplicationSubmissionContext().getApplicationId())) {
     throw new YarnRuntimeException(
         "The database entry for "
             + appId
             + " contains data for "
             + appState.getApplicationSubmissionContext().getApplicationId());
   }
   return appState;
 }
  @Override
  public synchronized void storeApplicationState(
      String appId, ApplicationStateDataPBImpl appStateDataPB) throws Exception {
    Path appDirPath = getAppDir(rmAppRoot, appId);
    fs.mkdirs(appDirPath);
    Path nodeCreatePath = getNodePath(appDirPath, appId);

    LOG.info("Storing info for app: " + appId + " at: " + nodeCreatePath);
    byte[] appStateData = appStateDataPB.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
      writeFile(nodeCreatePath, appStateData);
    } catch (Exception e) {
      LOG.info("Error storing info for app: " + appId, e);
      throw e;
    }
  }
  private void loadRMAppState(RMState rmState) throws Exception {
    try {
      List<ApplicationAttemptState> attempts = new ArrayList<ApplicationAttemptState>();

      for (FileStatus appDir : fs.listStatus(rmAppRoot)) {
        for (FileStatus childNodeStatus : fs.listStatus(appDir.getPath())) {
          assert childNodeStatus.isFile();
          String childNodeName = childNodeStatus.getPath().getName();
          byte[] childData = readFile(childNodeStatus.getPath(), childNodeStatus.getLen());
          if (childNodeName.startsWith(ApplicationId.appIdStrPrefix)) {
            // application
            LOG.info("Loading application from node: " + childNodeName);
            ApplicationId appId = ConverterUtils.toApplicationId(childNodeName);
            ApplicationStateDataPBImpl appStateData =
                new ApplicationStateDataPBImpl(ApplicationStateDataProto.parseFrom(childData));
            ApplicationState appState =
                new ApplicationState(
                    appStateData.getSubmitTime(),
                    appStateData.getApplicationSubmissionContext(),
                    appStateData.getUser());
            // assert child node name is same as actual applicationId
            assert appId.equals(appState.context.getApplicationId());
            rmState.appState.put(appId, appState);
          } else if (childNodeName.startsWith(ApplicationAttemptId.appAttemptIdStrPrefix)) {
            // attempt
            LOG.info("Loading application attempt from node: " + childNodeName);
            ApplicationAttemptId attemptId = ConverterUtils.toApplicationAttemptId(childNodeName);
            ApplicationAttemptStateDataPBImpl attemptStateData =
                new ApplicationAttemptStateDataPBImpl(
                    ApplicationAttemptStateDataProto.parseFrom(childData));
            Credentials credentials = null;
            if (attemptStateData.getAppAttemptTokens() != null) {
              credentials = new Credentials();
              DataInputByteBuffer dibb = new DataInputByteBuffer();
              dibb.reset(attemptStateData.getAppAttemptTokens());
              credentials.readTokenStorageStream(dibb);
            }
            ApplicationAttemptState attemptState =
                new ApplicationAttemptState(
                    attemptId, attemptStateData.getMasterContainer(), credentials);

            // assert child node name is same as application attempt id
            assert attemptId.equals(attemptState.getAttemptId());
            attempts.add(attemptState);
          } else {
            LOG.info("Unknown child node with name: " + childNodeName);
          }
        }
      }

      // 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 (ApplicationAttemptState attemptState : attempts) {
        ApplicationId appId = attemptState.getAttemptId().getApplicationId();
        ApplicationState appState = rmState.appState.get(appId);
        assert appState != null;
        appState.attempts.put(attemptState.getAttemptId(), attemptState);
      }
    } catch (Exception e) {
      LOG.error("Failed to load state.", e);
      throw e;
    }
  }