@Override
  public void run() {
    while (true) {
      long interval = HiveConf.getTimeVar(hiveConf, ConfVars.HIVE_PRIVILEGE_SYNCHRONIZER_INTERVAL, TimeUnit.SECONDS);
      try {
        for (HivePolicyProvider policyProvider : policyProviderContainer) {
          LOG.info("Start synchronize privilege " + policyProvider.getClass().getName());
          String authorizer = policyProvider.getClass().getSimpleName();
          if (!privilegeSynchronizerLatch.await(interval, TimeUnit.SECONDS)) {
            LOG.info("Not selected as leader, skip");
            continue;
          }
          int numDc = 0, numDb = 0, numTbl = 0;
          for (String dcName : hiveClient.getAllDataConnectorNames()) {
            numDc++;
            HiveObjectRef dcToRefresh = getObjToRefresh(HiveObjectType.DATACONNECTOR, null, dcName);
            PrivilegeBag grantDataConnectorBag = new PrivilegeBag();
            addGrantPrivilegesToBag(policyProvider, grantDataConnectorBag, HiveObjectType.DATACONNECTOR,
                    null, dcName, null, authorizer);
            hiveClient.refresh_privileges(dcToRefresh, authorizer, grantDataConnectorBag);
            LOG.debug("processing data connector: " + dcName);
          }
          LOG.info("Success synchronize privilege " + policyProvider.getClass().getName() + ":" + numDc + " dataconnectors");

          for (String dbName : hiveClient.getAllDatabases()) {
            numDb++;
            HiveObjectRef dbToRefresh = getObjToRefresh(HiveObjectType.DATABASE, dbName, null);
            PrivilegeBag grantDatabaseBag = new PrivilegeBag();
            addGrantPrivilegesToBag(policyProvider, grantDatabaseBag, HiveObjectType.DATABASE,
                dbName, null, null, authorizer);
            hiveClient.refresh_privileges(dbToRefresh, authorizer, grantDatabaseBag);
            LOG.debug("processing " + dbName);

            for (String tblName : hiveClient.getAllTables(dbName)) {
              numTbl++;
              LOG.debug("processing " + dbName + "." + tblName);
              HiveObjectRef tableToRefresh = getObjToRefresh(HiveObjectType.TABLE, dbName, tblName);
              PrivilegeBag grantTableBag = new PrivilegeBag();
              addGrantPrivilegesToBag(policyProvider, grantTableBag, HiveObjectType.TABLE,
                  dbName, tblName, null, authorizer);
              hiveClient.refresh_privileges(tableToRefresh, authorizer, grantTableBag);

              HiveObjectRef tableOfColumnsToRefresh = getObjToRefresh(HiveObjectType.COLUMN, dbName, tblName);
              PrivilegeBag grantColumnBag = new PrivilegeBag();
              Table tbl = null;
              try {
                tbl = hiveClient.getTable(dbName, tblName);
                for (FieldSchema fs : tbl.getPartitionKeys()) {
                  addGrantPrivilegesToBag(policyProvider, grantColumnBag, HiveObjectType.COLUMN,
                          dbName, tblName, fs.getName(), authorizer);
                }
                for (FieldSchema fs : tbl.getSd().getCols()) {
                  addGrantPrivilegesToBag(policyProvider, grantColumnBag, HiveObjectType.COLUMN,
                          dbName, tblName, fs.getName(), authorizer);
                }
                hiveClient.refresh_privileges(tableOfColumnsToRefresh, authorizer, grantColumnBag);
              } catch (MetaException e) {
                LOG.debug("Unable to synchronize " + tblName + ":" + e.getMessage());
              }
            }
          }
          LOG.info("Success synchronize privilege " + policyProvider.getClass().getName() + ":" + numDb + " databases, "
                  + numTbl + " tables");
        }
      } catch (Exception e) {
        LOG.error("Error initializing PrivilegeSynchronizer: " + e.getMessage(), e);
      }
      LOG.info("Wait for " + interval + " seconds");
      try {
        Thread.sleep(interval * 1000);
      } catch (InterruptedException e) {
        // do nothing
      }
    }
  }
PrivilegeSynchronizer defines a thread to synchronize privileges from
external authorizer to Hive metastore.
ql/src/java/org/apache/hadoop/hive/ql/security/authorization/PrivilegeSynchronizer.java