Search in sources :

Example 1 with AutoCloseableLock

use of org.apache.gobblin.util.AutoCloseableLock in project incubator-gobblin by apache.

the class HiveMetaStoreBasedRegister method ensureHiveTableExistenceBeforeAlternation.

/**
 * If table existed on Hive side will return false;
 * Or will create the table thru. RPC and return retVal from remote MetaStore.
 */
private boolean ensureHiveTableExistenceBeforeAlternation(String tableName, String dbName, IMetaStoreClient client, Table table, HiveSpec spec) throws TException {
    try (AutoCloseableLock lock = this.locks.getTableLock(dbName, tableName)) {
        try {
            try (Timer.Context context = this.metricContext.timer(CREATE_HIVE_TABLE).time()) {
                client.createTable(getTableWithCreateTimeNow(table));
                log.info(String.format("Created Hive table %s in db %s", tableName, dbName));
                return true;
            } catch (AlreadyExistsException e) {
            }
        } catch (TException e) {
            log.error(String.format("Unable to create Hive table %s in db %s: " + e.getMessage(), tableName, dbName), e);
            throw e;
        }
        log.info("Table {} already exists in db {}.", tableName, dbName);
        try {
            HiveTable existingTable;
            try (Timer.Context context = this.metricContext.timer(GET_HIVE_TABLE).time()) {
                existingTable = HiveMetaStoreUtils.getHiveTable(client.getTable(dbName, tableName));
            }
            if (needToUpdateTable(existingTable, spec.getTable())) {
                try (Timer.Context context = this.metricContext.timer(ALTER_TABLE).time()) {
                    client.alter_table(dbName, tableName, getTableWithCreateTime(table, existingTable));
                }
                log.info(String.format("updated Hive table %s in db %s", tableName, dbName));
            }
        } catch (TException e2) {
            log.error(String.format("Unable to create or alter Hive table %s in db %s: " + e2.getMessage(), tableName, dbName), e2);
            throw e2;
        }
        // When the logic up to here it means table already existed in db and alteration happen. Return false.
        return false;
    }
}
Also used : TException(org.apache.thrift.TException) Timer(com.codahale.metrics.Timer) AlreadyExistsException(org.apache.hadoop.hive.metastore.api.AlreadyExistsException) AutoCloseableLock(org.apache.gobblin.util.AutoCloseableLock) HiveTable(org.apache.gobblin.hive.HiveTable)

Example 2 with AutoCloseableLock

use of org.apache.gobblin.util.AutoCloseableLock in project incubator-gobblin by apache.

the class HiveMetaStoreBasedRegister method createTableIfNotExists.

private boolean createTableIfNotExists(IMetaStoreClient client, Table table, HiveTable hiveTable) throws IOException {
    String dbName = table.getDbName();
    String tableName = table.getTableName();
    try (AutoCloseableLock lock = this.locks.getTableLock(dbName, tableName)) {
        boolean tableExists;
        try (Timer.Context context = this.metricContext.timer(TABLE_EXISTS).time()) {
            tableExists = client.tableExists(table.getDbName(), table.getTableName());
        }
        if (tableExists) {
            return false;
        }
        try (Timer.Context context = this.metricContext.timer(CREATE_HIVE_TABLE).time()) {
            client.createTable(getTableWithCreateTimeNow(table));
        }
        log.info(String.format("Created Hive table %s in db %s", tableName, dbName));
        HiveMetaStoreEventHelper.submitSuccessfulTableCreation(this.eventSubmitter, hiveTable);
        return true;
    } catch (TException e) {
        HiveMetaStoreEventHelper.submitFailedTableCreation(eventSubmitter, hiveTable, e);
        throw new IOException(String.format("Error in creating or altering Hive table %s in db %s", table.getTableName(), table.getDbName()), e);
    }
}
Also used : TException(org.apache.thrift.TException) Timer(com.codahale.metrics.Timer) AutoCloseableLock(org.apache.gobblin.util.AutoCloseableLock) IOException(java.io.IOException)

Example 3 with AutoCloseableLock

use of org.apache.gobblin.util.AutoCloseableLock in project incubator-gobblin by apache.

the class HiveMetaStoreBasedRegister method ensureHiveDbExistence.

/**
 * If databse existed on Hive side will return false;
 * Or will create the table thru. RPC and return retVal from remote MetaStore.
 * @param hiveDbName is the hive databases to be checked for existence
 */
private boolean ensureHiveDbExistence(String hiveDbName, IMetaStoreClient client) throws IOException {
    try (AutoCloseableLock lock = this.locks.getDbLock(hiveDbName)) {
        Database db = new Database();
        db.setName(hiveDbName);
        try {
            try (Timer.Context context = this.metricContext.timer(GET_HIVE_DATABASE).time()) {
                client.getDatabase(db.getName());
            }
            return false;
        } catch (NoSuchObjectException nsoe) {
        // proceed with create
        } catch (TException te) {
            throw new IOException(te);
        }
        Preconditions.checkState(this.hiveDbRootDir.isPresent(), "Missing required property " + HiveRegProps.HIVE_DB_ROOT_DIR);
        db.setLocationUri(new Path(this.hiveDbRootDir.get(), hiveDbName + HIVE_DB_EXTENSION).toString());
        try {
            try (Timer.Context context = this.metricContext.timer(CREATE_HIVE_DATABASE).time()) {
                client.createDatabase(db);
            }
            log.info("Created database " + hiveDbName);
            HiveMetaStoreEventHelper.submitSuccessfulDBCreation(this.eventSubmitter, hiveDbName);
            return true;
        } catch (AlreadyExistsException e) {
            return false;
        } catch (TException e) {
            HiveMetaStoreEventHelper.submitFailedDBCreation(this.eventSubmitter, hiveDbName, e);
            throw new IOException("Unable to create Hive database " + hiveDbName, e);
        }
    }
}
Also used : TException(org.apache.thrift.TException) Path(org.apache.hadoop.fs.Path) Timer(com.codahale.metrics.Timer) AlreadyExistsException(org.apache.hadoop.hive.metastore.api.AlreadyExistsException) AutoCloseableLock(org.apache.gobblin.util.AutoCloseableLock) Database(org.apache.hadoop.hive.metastore.api.Database) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) IOException(java.io.IOException)

Aggregations

Timer (com.codahale.metrics.Timer)3 AutoCloseableLock (org.apache.gobblin.util.AutoCloseableLock)3 TException (org.apache.thrift.TException)3 IOException (java.io.IOException)2 AlreadyExistsException (org.apache.hadoop.hive.metastore.api.AlreadyExistsException)2 HiveTable (org.apache.gobblin.hive.HiveTable)1 Path (org.apache.hadoop.fs.Path)1 Database (org.apache.hadoop.hive.metastore.api.Database)1 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)1