public void cancelOperation(OperationHandle opHandle, String errMsg) throws HiveSQLException {
    Operation operation = getOperation(opHandle);
    OperationState opState = operation.getState();
    if (opState.isTerminal()) {
      // Cancel should be a no-op in either cases
      LOG.debug(opHandle + ": Operation is already aborted in state - " + opState);
    } else {
      LOG.debug(opHandle + ": Attempting to cancel from state - " + opState);
      OperationState operationState = OperationState.CANCELED;
      operationState.setErrorMessage(errMsg);
      operation.cancel(operationState);
      queryInfoCache.ifPresent(cache -> cache.removeLiveQueryInfo(operation));
    }
  }
Cancel the running operation unless it is already in a terminal state
@param opHandle operation handle
@param errMsg error message
@throws HiveSQLException
service/src/java/org/apache/hive/service/cli/operation/OperationManager.java