@SuppressWarnings("unchecked")
 private void handleDTRenewerAppSubmitEvent(DelegationTokenRenewerAppSubmitEvent event) {
   /*
    * For applications submitted with delegation tokens we are not submitting
    * the application to scheduler from RMAppManager. Instead we are doing
    * it from here. The primary goal is to make token renewal as a part of
    * application submission asynchronous so that client thread is not
    * blocked during app submission.
    */
   try {
     // Setup tokens for renewal
     DelegationTokenRenewer.this.handleAppSubmitEvent(event);
     rmContext
         .getDispatcher()
         .getEventHandler()
         .handle(new RMAppEvent(event.getApplicationId(), RMAppEventType.START));
   } catch (Throwable t) {
     LOG.warn("Unable to add the application to the delegation token renewer.", t);
     // Sending APP_REJECTED is fine, since we assume that the
     // RMApp is in NEW state and thus we havne't yet informed the
     // Scheduler about the existence of the application
     rmContext
         .getDispatcher()
         .getEventHandler()
         .handle(new RMAppRejectedEvent(event.getApplicationId(), t.getMessage()));
   }
 }
  private void handleAppSubmitEvent(DelegationTokenRenewerAppSubmitEvent evt)
      throws IOException, InterruptedException {
    ApplicationId applicationId = evt.getApplicationId();
    Credentials ts = evt.getCredentials();
    boolean shouldCancelAtEnd = evt.shouldCancelAtEnd();
    if (ts == null) {
      return; // nothing to add
    }

    if (LOG.isDebugEnabled()) {
      LOG.debug("Registering tokens for renewal for:" + " appId = " + applicationId);
    }

    Collection<Token<?>> tokens = ts.getAllTokens();
    long now = System.currentTimeMillis();

    // find tokens for renewal, but don't add timers until we know
    // all renewable tokens are valid
    // At RM restart it is safe to assume that all the previously added tokens
    // are valid
    appTokens.put(
        applicationId, Collections.synchronizedSet(new HashSet<DelegationTokenToRenew>()));
    Set<DelegationTokenToRenew> tokenList = new HashSet<DelegationTokenToRenew>();
    boolean hasHdfsToken = false;
    for (Token<?> token : tokens) {
      if (token.isManaged()) {
        tokenList.add(
            new DelegationTokenToRenew(
                applicationId, token, getConfig(), now, shouldCancelAtEnd, evt.getUser()));
        if (token.getKind().equals(new Text("HDFS_DELEGATION_TOKEN"))) {
          LOG.info(applicationId + " found existing hdfs token " + token);
          hasHdfsToken = true;
        }
      }
    }

    if (!tokenList.isEmpty()) {
      // Renewing token and adding it to timer calls are separated purposefully
      // If user provides incorrect token then it should not be added for
      // renewal.
      for (DelegationTokenToRenew dtr : tokenList) {
        try {
          renewToken(dtr);
        } catch (IOException ioe) {
          throw new IOException("Failed to renew token: " + dtr.token, ioe);
        }
      }
      for (DelegationTokenToRenew dtr : tokenList) {
        appTokens.get(applicationId).add(dtr);
        setTimerForTokenRenewal(dtr);
      }
    }

    if (!hasHdfsToken) {
      requestNewHdfsDelegationToken(applicationId, evt.getUser(), shouldCancelAtEnd);
    }
  }