@Override
  public RenewDelegationTokenResponse renewDelegationToken(
      RenewDelegationTokenRequest request) throws YarnException, IOException {
    try {

      if (!RouterServerUtil.isAllowedDelegationTokenOp()) {
        routerMetrics.incrRenewDelegationTokenFailedRetrieved();
        String msg = "Delegation Token can be renewed only with kerberos authentication";
        RouterAuditLogger.logFailure(user.getShortUserName(), RENEW_DELEGATIONTOKEN, UNKNOWN,
            TARGET_CLIENT_RM_SERVICE, msg);
        throw new IOException(msg);
      }

      long startTime = clock.getTime();
      org.apache.hadoop.yarn.api.records.Token protoToken = request.getDelegationToken();
      Token<RMDelegationTokenIdentifier> token = new Token<>(
          protoToken.getIdentifier().array(), protoToken.getPassword().array(),
          new Text(protoToken.getKind()), new Text(protoToken.getService()));
      String renewer = RouterServerUtil.getRenewerForToken(token);
      long nextExpTime = this.getTokenSecretManager().renewToken(token, renewer);
      RenewDelegationTokenResponse renewResponse =
          Records.newRecord(RenewDelegationTokenResponse.class);
      renewResponse.setNextExpirationTime(nextExpTime);
      long stopTime = clock.getTime();
      routerMetrics.succeededRenewDelegationTokenRetrieved((stopTime - startTime));
      RouterAuditLogger.logSuccess(user.getShortUserName(), RENEW_DELEGATIONTOKEN,
          TARGET_CLIENT_RM_SERVICE);
      return renewResponse;

    } catch (IOException e) {
      routerMetrics.incrRenewDelegationTokenFailedRetrieved();
      RouterAuditLogger.logFailure(user.getShortUserName(), RENEW_DELEGATIONTOKEN, UNKNOWN,
          TARGET_CLIENT_RM_SERVICE, "renewDelegationToken error, errMsg = " + e.getMessage());
      throw new YarnException(e);
    }
  }
@Test
  public void testRenewDelegationToken() throws IOException, YarnException {

    // We design such a unit test to check
    // that the execution of the GetDelegationToken method is as expected
    // 1. Call GetDelegationToken to apply for delegationToken.
    // 2. Call renewDelegationToken to refresh delegationToken.
    // By looking at the code of AbstractDelegationTokenSecretManager#renewToken,
    // we know that renewTime is calculated as Math.min(id.getMaxDate(), now + tokenRenewInterval)
    // so renewTime will be less than or equal to maxDate.
    // 3. We will compare whether the expirationTime returned to the
    // client is consistent with the renewDate in the stateStore.

    // Step1. Call GetDelegationToken to apply for delegationToken.
    GetDelegationTokenRequest request = mock(GetDelegationTokenRequest.class);
    when(request.getRenewer()).thenReturn("renewer2");
    GetDelegationTokenResponse response = interceptor.getDelegationToken(request);
    Assert.assertNotNull(response);
    Token delegationToken = response.getRMDelegationToken();

    org.apache.hadoop.security.token.Token<RMDelegationTokenIdentifier> token =
        ConverterUtils.convertFromYarn(delegationToken, (Text) null);
    RMDelegationTokenIdentifier rMDelegationTokenIdentifier = token.decodeIdentifier();
    String renewer = rMDelegationTokenIdentifier.getRenewer().toString();
    long maxDate = rMDelegationTokenIdentifier.getMaxDate();
    Assert.assertEquals("renewer2", renewer);

    // Step2. Call renewDelegationToken to refresh delegationToken.
    RenewDelegationTokenRequest renewRequest = Records.newRecord(RenewDelegationTokenRequest.class);
    renewRequest.setDelegationToken(delegationToken);
    RenewDelegationTokenResponse renewResponse = interceptor.renewDelegationToken(renewRequest);
    Assert.assertNotNull(renewResponse);

    long expDate = renewResponse.getNextExpirationTime();
    Assert.assertTrue(expDate <= maxDate);

    // Step3. Compare whether the expirationTime returned to
    // the client is consistent with the renewDate in the stateStore
    RouterRMDTSecretManagerState managerState = stateStore.getRouterRMSecretManagerState();
    Map<RMDelegationTokenIdentifier, RouterStoreToken> delegationTokenState =
        managerState.getTokenState();
    Assert.assertNotNull(delegationTokenState);
    Assert.assertTrue(delegationTokenState.containsKey(rMDelegationTokenIdentifier));
    RouterStoreToken resultRouterStoreToken = delegationTokenState.get(rMDelegationTokenIdentifier);
    Assert.assertNotNull(resultRouterStoreToken);
    long renewDate = resultRouterStoreToken.getRenewDate();
    Assert.assertEquals(expDate, renewDate);
  }