Search in sources :

Example 1 with ZooKeeperHiveLock

use of org.apache.hadoop.hive.ql.lockmgr.zookeeper.ZooKeeperHiveLock in project hive by apache.

the class TestDummyTxnManager method testSingleReadTable.

/**
 * Verifies the current database object is not locked if the table read is against different database
 * @throws Exception
 */
@Test
public void testSingleReadTable() throws Exception {
    // Setup
    SessionState.get().setCurrentDatabase("db1");
    List<HiveLock> expectedLocks = new ArrayList<HiveLock>();
    expectedLocks.add(new ZooKeeperHiveLock("default", new HiveLockObject(), HiveLockMode.SHARED));
    expectedLocks.add(new ZooKeeperHiveLock("default.table1", new HiveLockObject(), HiveLockMode.SHARED));
    LockedDriverState lDrvState = new LockedDriverState();
    LockedDriverState lDrvInp = new LockedDriverState();
    lDrvInp.abort();
    LockException lEx = new LockException(ErrorMsg.LOCK_ACQUIRE_CANCELLED.getMsg());
    when(mockLockManager.lock(anyListOf(HiveLockObj.class), eq(false), eq(lDrvState))).thenReturn(expectedLocks);
    when(mockLockManager.lock(anyListOf(HiveLockObj.class), eq(false), eq(lDrvInp))).thenThrow(lEx);
    doNothing().when(mockLockManager).setContext(any(HiveLockManagerCtx.class));
    doNothing().when(mockLockManager).close();
    ArgumentCaptor<List> lockObjsCaptor = ArgumentCaptor.forClass(List.class);
    when(mockQueryPlan.getInputs()).thenReturn(createReadEntities());
    when(mockQueryPlan.getOutputs()).thenReturn(new HashSet<WriteEntity>());
    // Execute
    txnMgr.acquireLocks(mockQueryPlan, ctx, "fred", lDrvState);
    // Verify
    Assert.assertEquals("db1", SessionState.get().getCurrentDatabase());
    List<HiveLock> resultLocks = ctx.getHiveLocks();
    Assert.assertEquals(expectedLocks.size(), resultLocks.size());
    Assert.assertEquals(expectedLocks.get(0).getHiveLockMode(), resultLocks.get(0).getHiveLockMode());
    Assert.assertEquals(expectedLocks.get(0).getHiveLockObject().getName(), resultLocks.get(0).getHiveLockObject().getName());
    Assert.assertEquals(expectedLocks.get(1).getHiveLockMode(), resultLocks.get(1).getHiveLockMode());
    Assert.assertEquals(expectedLocks.get(0).getHiveLockObject().getName(), resultLocks.get(0).getHiveLockObject().getName());
    verify(mockLockManager).lock(lockObjsCaptor.capture(), eq(false), eq(lDrvState));
    List<HiveLockObj> lockObjs = lockObjsCaptor.getValue();
    Assert.assertEquals(2, lockObjs.size());
    Assert.assertEquals("default", lockObjs.get(0).getName());
    Assert.assertEquals(HiveLockMode.SHARED, lockObjs.get(0).mode);
    Assert.assertEquals("default/table1", lockObjs.get(1).getName());
    Assert.assertEquals(HiveLockMode.SHARED, lockObjs.get(1).mode);
    // Execute
    try {
        txnMgr.acquireLocks(mockQueryPlan, ctx, "fred", lDrvInp);
        Assert.fail();
    } catch (LockException le) {
        Assert.assertEquals(le.getMessage(), ErrorMsg.LOCK_ACQUIRE_CANCELLED.getMsg());
    }
}
Also used : ArrayList(java.util.ArrayList) ZooKeeperHiveLock(org.apache.hadoop.hive.ql.lockmgr.zookeeper.ZooKeeperHiveLock) ZooKeeperHiveLock(org.apache.hadoop.hive.ql.lockmgr.zookeeper.ZooKeeperHiveLock) LockedDriverState(org.apache.hadoop.hive.ql.Driver.LockedDriverState) ArrayList(java.util.ArrayList) List(java.util.List) WriteEntity(org.apache.hadoop.hive.ql.hooks.WriteEntity) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 LockedDriverState (org.apache.hadoop.hive.ql.Driver.LockedDriverState)1 WriteEntity (org.apache.hadoop.hive.ql.hooks.WriteEntity)1 ZooKeeperHiveLock (org.apache.hadoop.hive.ql.lockmgr.zookeeper.ZooKeeperHiveLock)1 Test (org.junit.Test)1