@Override
  public CancelDelegationTokenResponse cancelDelegationToken(
      CancelDelegationTokenRequest request) throws YarnException, IOException {
    try {
      if (!RouterServerUtil.isAllowedDelegationTokenOp()) {
        routerMetrics.incrCancelDelegationTokenFailedRetrieved();
        String msg = "Delegation Token can be cancelled only with kerberos authentication";
        RouterAuditLogger.logFailure(user.getShortUserName(), CANCEL_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 currentUser = UserGroupInformation.getCurrentUser().getUserName();
      this.getTokenSecretManager().cancelToken(token, currentUser);
      long stopTime = clock.getTime();
      routerMetrics.succeededCancelDelegationTokenRetrieved((stopTime - startTime));
      RouterAuditLogger.logSuccess(user.getShortUserName(), CANCEL_DELEGATIONTOKEN,
          TARGET_CLIENT_RM_SERVICE);
      return Records.newRecord(CancelDelegationTokenResponse.class);
    } catch (IOException e) {
      routerMetrics.incrCancelDelegationTokenFailedRetrieved();
      RouterAuditLogger.logFailure(user.getShortUserName(), CANCEL_DELEGATIONTOKEN, UNKNOWN,
          TARGET_CLIENT_RM_SERVICE, "cancelDelegationToken error, errMsg = " + e.getMessage());
      throw new YarnException(e);
    }
  }
@Test
  public void testCancelDelegationToken() throws IOException, YarnException {

    // We design such a unit test to check
    // that the execution of the CancelDelegationToken method is as expected
    // 1. Call GetDelegationToken to apply for delegationToken.
    // 2. Call CancelDelegationToken to cancel delegationToken.
    // 3. Query the data in the StateStore and confirm that the Delegation has been deleted.

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

    // Step2. Call CancelDelegationToken to cancel delegationToken.
    CancelDelegationTokenRequest cancelTokenRequest =
        CancelDelegationTokenRequest.newInstance(delegationToken);
    CancelDelegationTokenResponse cancelTokenResponse =
        interceptor.cancelDelegationToken(cancelTokenRequest);
    Assert.assertNotNull(cancelTokenResponse);

    // Step3. Query the data in the StateStore and confirm that the Delegation has been deleted.
    // At this point, the size of delegationTokenState should be 0.
    RouterRMDTSecretManagerState managerState = stateStore.getRouterRMSecretManagerState();
    Map<RMDelegationTokenIdentifier, RouterStoreToken> delegationTokenState =
        managerState.getTokenState();
    Assert.assertNotNull(delegationTokenState);
    Assert.assertEquals(0, delegationTokenState.size());
  }