Search in sources :

Example 1 with LockComponentBuilder

use of org.apache.hadoop.hive.metastore.LockComponentBuilder in project hive by apache.

the class DbTxnManager method acquireLocks.

/**
   * This is for testing only.  Normally client should call {@link #acquireLocks(org.apache.hadoop.hive.ql.QueryPlan, org.apache.hadoop.hive.ql.Context, String)}
   * @param isBlocking if false, the method will return immediately; thus the locks may be in LockState.WAITING
   * @return null if no locks were needed
   */
LockState acquireLocks(QueryPlan plan, Context ctx, String username, boolean isBlocking) throws LockException {
    init();
    // Make sure we've built the lock manager
    getLockManager();
    boolean atLeastOneLock = false;
    queryId = plan.getQueryId();
    LockRequestBuilder rqstBuilder = new LockRequestBuilder(queryId);
    //link queryId to txnId
    LOG.info("Setting lock request transaction to " + JavaUtils.txnIdToString(txnId) + " for queryId=" + queryId);
    rqstBuilder.setTransactionId(txnId).setUser(username);
    // For each source to read, get a shared lock
    for (ReadEntity input : plan.getInputs()) {
        if (!input.needsLock() || input.isUpdateOrDelete() || (input.getType() == Entity.Type.TABLE && input.getTable().isTemporary())) {
            // locks instead. Also, there's no need to lock temp tables since they're session wide
            continue;
        }
        LockComponentBuilder compBuilder = new LockComponentBuilder();
        compBuilder.setShared();
        compBuilder.setOperationType(DataOperationType.SELECT);
        Table t = null;
        switch(input.getType()) {
            case DATABASE:
                compBuilder.setDbName(input.getDatabase().getName());
                break;
            case TABLE:
                t = input.getTable();
                compBuilder.setDbName(t.getDbName());
                compBuilder.setTableName(t.getTableName());
                break;
            case PARTITION:
            case DUMMYPARTITION:
                compBuilder.setPartitionName(input.getPartition().getName());
                t = input.getPartition().getTable();
                compBuilder.setDbName(t.getDbName());
                compBuilder.setTableName(t.getTableName());
                break;
            default:
                // This is a file or something we don't hold locks for.
                continue;
        }
        if (t != null && AcidUtils.isAcidTable(t)) {
            compBuilder.setIsAcid(true);
        }
        LockComponent comp = compBuilder.build();
        LOG.debug("Adding lock component to lock request " + comp.toString());
        rqstBuilder.addLockComponent(comp);
        atLeastOneLock = true;
    }
    // need a SEMI-SHARED.
    for (WriteEntity output : plan.getOutputs()) {
        LOG.debug("output is null " + (output == null));
        if (output.getType() == Entity.Type.DFS_DIR || output.getType() == Entity.Type.LOCAL_DIR || (output.getType() == Entity.Type.TABLE && output.getTable().isTemporary())) {
            // We don't lock files or directories. We also skip locking temp tables.
            continue;
        }
        LockComponentBuilder compBuilder = new LockComponentBuilder();
        Table t = null;
        switch(output.getWriteType()) {
            case DDL_EXCLUSIVE:
            case INSERT_OVERWRITE:
                compBuilder.setExclusive();
                compBuilder.setOperationType(DataOperationType.NO_TXN);
                break;
            case INSERT:
                t = getTable(output);
                if (AcidUtils.isAcidTable(t)) {
                    compBuilder.setShared();
                    compBuilder.setIsAcid(true);
                } else {
                    if (conf.getBoolVar(HiveConf.ConfVars.HIVE_TXN_STRICT_LOCKING_MODE)) {
                        compBuilder.setExclusive();
                    } else {
                        // this is backward compatible for non-ACID resources, w/o ACID semantics
                        compBuilder.setShared();
                    }
                    compBuilder.setIsAcid(false);
                }
                compBuilder.setOperationType(DataOperationType.INSERT);
                break;
            case DDL_SHARED:
                compBuilder.setShared();
                compBuilder.setOperationType(DataOperationType.NO_TXN);
                break;
            case UPDATE:
                compBuilder.setSemiShared();
                compBuilder.setOperationType(DataOperationType.UPDATE);
                t = getTable(output);
                break;
            case DELETE:
                compBuilder.setSemiShared();
                compBuilder.setOperationType(DataOperationType.DELETE);
                t = getTable(output);
                break;
            case DDL_NO_LOCK:
                // No lock required here
                continue;
            default:
                throw new RuntimeException("Unknown write type " + output.getWriteType().toString());
        }
        switch(output.getType()) {
            case DATABASE:
                compBuilder.setDbName(output.getDatabase().getName());
                break;
            case TABLE:
            case // in case of dynamic partitioning lock the table
            DUMMYPARTITION:
                t = output.getTable();
                compBuilder.setDbName(t.getDbName());
                compBuilder.setTableName(t.getTableName());
                break;
            case PARTITION:
                compBuilder.setPartitionName(output.getPartition().getName());
                t = output.getPartition().getTable();
                compBuilder.setDbName(t.getDbName());
                compBuilder.setTableName(t.getTableName());
                break;
            default:
                // This is a file or something we don't hold locks for.
                continue;
        }
        if (t != null && AcidUtils.isAcidTable(t)) {
            compBuilder.setIsAcid(true);
        }
        compBuilder.setIsDynamicPartitionWrite(output.isDynamicPartitionWrite());
        LockComponent comp = compBuilder.build();
        LOG.debug("Adding lock component to lock request " + comp.toString());
        rqstBuilder.addLockComponent(comp);
        atLeastOneLock = true;
    }
    // this operation.
    if (!atLeastOneLock) {
        LOG.debug("No locks needed for queryId" + queryId);
        return null;
    }
    List<HiveLock> locks = new ArrayList<HiveLock>(1);
    LockState lockState = lockMgr.lock(rqstBuilder.build(), queryId, isBlocking, locks);
    ctx.setHiveLocks(locks);
    return lockState;
}
Also used : ReadEntity(org.apache.hadoop.hive.ql.hooks.ReadEntity) LockComponentBuilder(org.apache.hadoop.hive.metastore.LockComponentBuilder) Table(org.apache.hadoop.hive.ql.metadata.Table) ArrayList(java.util.ArrayList) LockRequestBuilder(org.apache.hadoop.hive.metastore.LockRequestBuilder) WriteEntity(org.apache.hadoop.hive.ql.hooks.WriteEntity)

Example 2 with LockComponentBuilder

use of org.apache.hadoop.hive.metastore.LockComponentBuilder in project hive by apache.

the class Lock method buildLockRequest.

private LockRequest buildLockRequest(Long transactionId) {
    if (transactionId == null && !sinks.isEmpty()) {
        throw new IllegalArgumentException("Cannot sink to tables outside of a transaction: sinks=" + asStrings(sinks));
    }
    LockRequestBuilder requestBuilder = new LockRequestBuilder();
    for (Table table : tables) {
        LockComponentBuilder componentBuilder = new LockComponentBuilder().setDbName(table.getDbName()).setTableName(table.getTableName());
        //and insert/select and if resource (that is written to) is ACID or not
        if (sinks.contains(table)) {
            componentBuilder.setSemiShared().setOperationType(DataOperationType.UPDATE).setIsAcid(true);
        } else {
            componentBuilder.setShared().setOperationType(DataOperationType.INSERT).setIsAcid(true);
        }
        LockComponent component = componentBuilder.build();
        requestBuilder.addLockComponent(component);
    }
    if (transactionId != null) {
        requestBuilder.setTransactionId(transactionId);
    }
    LockRequest request = requestBuilder.setUser(user).build();
    return request;
}
Also used : LockComponentBuilder(org.apache.hadoop.hive.metastore.LockComponentBuilder) Table(org.apache.hadoop.hive.metastore.api.Table) LockComponent(org.apache.hadoop.hive.metastore.api.LockComponent) LockRequestBuilder(org.apache.hadoop.hive.metastore.LockRequestBuilder) LockRequest(org.apache.hadoop.hive.metastore.api.LockRequest)

Aggregations

LockComponentBuilder (org.apache.hadoop.hive.metastore.LockComponentBuilder)2 LockRequestBuilder (org.apache.hadoop.hive.metastore.LockRequestBuilder)2 ArrayList (java.util.ArrayList)1 LockComponent (org.apache.hadoop.hive.metastore.api.LockComponent)1 LockRequest (org.apache.hadoop.hive.metastore.api.LockRequest)1 Table (org.apache.hadoop.hive.metastore.api.Table)1 ReadEntity (org.apache.hadoop.hive.ql.hooks.ReadEntity)1 WriteEntity (org.apache.hadoop.hive.ql.hooks.WriteEntity)1 Table (org.apache.hadoop.hive.ql.metadata.Table)1