/** set task to renew the token */
  @VisibleForTesting
  protected void setTimerForTokenRenewal(DelegationTokenToRenew token) throws IOException {

    // calculate timer time
    long expiresIn = token.expirationDate - System.currentTimeMillis();
    long renewIn = token.expirationDate - expiresIn / 10; // little bit before the expiration
    // need to create new task every time
    TimerTask tTask = new RenewalTimerTask(token);
    token.setTimerTask(tTask); // keep reference to the timer

    renewalTimer.schedule(token.timerTask, new Date(renewIn));

    LOG.info("Renew " + token + " in " + expiresIn + " ms, appId = " + token.applicationId);
  }
    @Override
    public void run() {
      Token<DelegationTokenIdentifier> token = dttr.token;
      long newExpirationDate = 0;
      try {
        newExpirationDate = renewDelegationToken(dttr);
      } catch (Exception e) {
        return; // message logged in renewDT method
      }
      if (LOG.isDebugEnabled())
        LOG.debug("renewing for:" + token.getService() + ";newED=" + newExpirationDate);

      // new expiration date
      dttr.expirationDate = newExpirationDate;
      setTimerForTokenRenewal(dttr, false); // set the next one
    }
 // renew a token
 @VisibleForTesting
 protected void renewToken(final DelegationTokenToRenew dttr) throws IOException {
   // need to use doAs so that http can find the kerberos tgt
   // NOTE: token renewers should be responsible for the correct UGI!
   try {
     dttr.expirationDate =
         UserGroupInformation.getLoginUser()
             .doAs(
                 new PrivilegedExceptionAction<Long>() {
                   @Override
                   public Long run() throws Exception {
                     return dttr.token.renew(dttr.conf);
                   }
                 });
   } catch (InterruptedException e) {
     throw new IOException(e);
   }
   LOG.info("Renewed delegation-token= [" + dttr + "], for " + dttr.applicationId);
 }
  /** find the soonest expiring token and set it for renew */
  private static void setTimerForTokenRenewal(DelegationTokenToRenew token, boolean firstTime) {

    // calculate timer time
    long now = System.currentTimeMillis();
    long renewIn;
    if (firstTime) {
      renewIn = now;
    } else {
      long expiresIn = (token.expirationDate - now);
      renewIn = now + expiresIn - expiresIn / 10; // little before expiration
    }

    try {
      // need to create new timer every time
      TimerTask tTask = new RenewalTimerTask(token);
      token.setTimerTask(tTask); // keep reference to the timer

      renewalTimer.schedule(token.timerTask, new Date(renewIn));
    } catch (Exception e) {
      LOG.warn("failed to schedule a task, token will not renew more", e);
    }
  }