/** * Test that a connection that is aborted while calling isTableDisabled doesn't NPE */ @Test public void testTableDisabledRace() throws Exception { final HConnection connection = new HConnectionRaceTester(TEST_UTIL.getConfiguration(), true); MultithreadedTestUtil.TestContext ctx = new MultithreadedTestUtil.TestContext(TEST_UTIL.getConfiguration()); RepeatingTestThread disabledChecker = new RepeatingTestThread(ctx) { @Override public void doAnAction() throws IOException { try { connection.isTableDisabled(Bytes.toBytes("tableToCheck")); } catch (IOException ioe) { // Ignore. ZK can legitimately fail, only care if we get a NullPointerException } } }; AbortThread abortThread = new AbortThread(ctx, connection); ctx.addThread(disabledChecker); ctx.addThread(abortThread); ctx.startThreads(); ctx.waitFor(MILLIS_TO_WAIT_FOR_RACE); ctx.stop(); }
/** * Test that a connection that is aborted while calling getCurrentNrNRS doesn't NPE */ @Test public void testGetCurrentNrHRSRace() throws Exception { final HConnection connection = new HConnectionRaceTester(TEST_UTIL.getConfiguration(), true); MultithreadedTestUtil.TestContext ctx = new MultithreadedTestUtil.TestContext(TEST_UTIL.getConfiguration()); RepeatingTestThread getCurrentNrHRSCaller = new RepeatingTestThread(ctx) { @Override public void doAnAction() throws IOException { try { connection.getCurrentNrHRS(); } catch (IOException ioe) { // Ignore. ZK can legitimately fail, only care if we get a NullPointerException } } }; AbortThread abortThread = new AbortThread(ctx, connection); ctx.addThread(getCurrentNrHRSCaller); ctx.addThread(abortThread); ctx.startThreads(); ctx.waitFor(MILLIS_TO_WAIT_FOR_RACE); ctx.stop(); }
@Test(timeout = 30000) public void testWriteLockExcludesWriters() throws Exception { final String testName = "testWriteLockExcludesWriters"; final ZKInterProcessReadWriteLock readWriteLock = getReadWriteLock(testName); List<Future<Void>> results = Lists.newArrayList(); for (int i = 0; i < NUM_THREADS; ++i) { final String threadDesc = testName + i; results.add(executor.submit(new Callable<Void>() { @Override public Void call() throws IOException { ZKInterProcessWriteLock writeLock = readWriteLock.writeLock(Bytes.toBytes(threadDesc)); try { writeLock.acquire(); try { // No one else should hold the lock assertTrue(isLockHeld.compareAndSet(false, true)); Thread.sleep(1000); // No one else should have released the lock assertTrue(isLockHeld.compareAndSet(true, false)); } finally { isLockHeld.set(false); writeLock.release(); } } catch (InterruptedException e) { LOG.warn(threadDesc + " interrupted", e); Thread.currentThread().interrupt(); throw new InterruptedIOException(); } return null; } })); } MultithreadedTestUtil.assertOnFutures(results); }
@Test(timeout = 30000) public void testReadLockDoesNotExcludeReaders() throws Exception { final String testName = "testReadLockDoesNotExcludeReaders"; final ZKInterProcessReadWriteLock readWriteLock = getReadWriteLock(testName); final CountDownLatch locksAcquiredLatch = new CountDownLatch(NUM_THREADS); final AtomicInteger locksHeld = new AtomicInteger(0); List<Future<Void>> results = Lists.newArrayList(); for (int i = 0; i < NUM_THREADS; ++i) { final String threadDesc = testName + i; results.add(executor.submit(new Callable<Void>() { @Override public Void call() throws Exception { ZKInterProcessReadLock readLock = readWriteLock.readLock(Bytes.toBytes(threadDesc)); readLock.acquire(); try { locksHeld.incrementAndGet(); locksAcquiredLatch.countDown(); Thread.sleep(1000); } finally { readLock.release(); locksHeld.decrementAndGet(); } return null; } })); } locksAcquiredLatch.await(); assertEquals(locksHeld.get(), NUM_THREADS); MultithreadedTestUtil.assertOnFutures(results); }
/** * Test written as a verifier for HBASE-7051, CheckAndPut should properly read * MVCC. * * Moved into TestAtomicOperation from its original location, TestHBase7051 */ @Test public void testPutAndCheckAndPutInParallel() throws Exception { final String tableName = "testPutAndCheckAndPut"; Configuration conf = TEST_UTIL.getConfiguration(); conf.setClass(HConstants.REGION_IMPL, MockHRegion.class, HeapSize.class); final MockHRegion region = (MockHRegion) TEST_UTIL.createLocalHRegion(Bytes.toBytes(tableName), null, null, tableName, conf, false, Durability.SYNC_WAL, null, Bytes.toBytes(family)); Put[] puts = new Put[1]; Put put = new Put(Bytes.toBytes("r1")); put.add(Bytes.toBytes(family), Bytes.toBytes("q1"), Bytes.toBytes("10")); puts[0] = put; region.batchMutate(puts, HConstants.NO_NONCE, HConstants.NO_NONCE); MultithreadedTestUtil.TestContext ctx = new MultithreadedTestUtil.TestContext(conf); ctx.addThread(new PutThread(ctx, region)); ctx.addThread(new CheckAndPutThread(ctx, region)); ctx.startThreads(); while (testStep != TestStep.CHECKANDPUT_COMPLETED) { Thread.sleep(100); } ctx.stop(); Scan s = new Scan(); RegionScanner scanner = region.getScanner(s); List<Cell> results = new ArrayList<Cell>(); ScannerContext scannerContext = ScannerContext.newBuilder().setBatchLimit(2).build(); scanner.next(results, scannerContext); for (Cell keyValue : results) { assertEquals("50",Bytes.toString(CellUtil.cloneValue(keyValue))); } }
@Test public void testPutAndCheckAndPutInParallel() throws Exception { final String tableName = "testPutAndCheckAndPut"; Configuration conf = HBaseConfiguration.create(); conf.setClass(HConstants.REGION_IMPL, MockHRegion.class, HeapSize.class); final MockHRegion region = (MockHRegion) TestHRegion.initHRegion(Bytes.toBytes(tableName), tableName, conf, Bytes.toBytes(family)); List<Pair<Mutation, Integer>> putsAndLocks = Lists.newArrayList(); Put[] puts = new Put[1]; Put put = new Put(Bytes.toBytes("r1")); put.add(Bytes.toBytes(family), Bytes.toBytes("q1"), Bytes.toBytes("10")); puts[0] = put; Pair<Mutation, Integer> pair = new Pair<Mutation, Integer>(puts[0], null); putsAndLocks.add(pair); region.batchMutate(putsAndLocks.toArray(new Pair[0])); MultithreadedTestUtil.TestContext ctx = new MultithreadedTestUtil.TestContext(conf); ctx.addThread(new PutThread(ctx, region)); ctx.addThread(new CheckAndPutThread(ctx, region)); ctx.startThreads(); while (testStep != TestStep.CHECKANDPUT_COMPLETED) { Thread.sleep(100); } ctx.stop(); Scan s = new Scan(); RegionScanner scanner = region.getScanner(s); List<KeyValue> results = new ArrayList<KeyValue>(); scanner.next(results, 2); for (KeyValue keyValue : results) { assertEquals("50",Bytes.toString(keyValue.getValue())); } }
public static void hammerSingleKey(final BlockCache toBeTested, int BlockSize, int numThreads, int numQueries) throws Exception { final BlockCacheKey key = new BlockCacheKey("key", 0); final byte[] buf = new byte[5 * 1024]; Arrays.fill(buf, (byte) 5); final ByteArrayCacheable bac = new ByteArrayCacheable(buf); Configuration conf = new Configuration(); MultithreadedTestUtil.TestContext ctx = new MultithreadedTestUtil.TestContext( conf); final AtomicInteger totalQueries = new AtomicInteger(); toBeTested.cacheBlock(key, bac); for (int i = 0; i < numThreads; i++) { TestThread t = new MultithreadedTestUtil.RepeatingTestThread(ctx) { @Override public void doAnAction() throws Exception { ByteArrayCacheable returned = (ByteArrayCacheable) toBeTested .getBlock(key, false, false); assertArrayEquals(buf, returned.buf); totalQueries.incrementAndGet(); } }; t.setDaemon(true); ctx.addThread(t); } ctx.startThreads(); while (totalQueries.get() < numQueries && ctx.shouldRun()) { Thread.sleep(10); } ctx.stop(); }
/** * Test written as a verifier for HBASE-7051, CheckAndPut should properly read * MVCC. * * Moved into TestAtomicOperation from its original location, TestHBase7051 */ @Test public void testPutAndCheckAndPutInParallel() throws Exception { final String tableName = "testPutAndCheckAndPut"; Configuration conf = TEST_UTIL.getConfiguration(); conf.setClass(HConstants.REGION_IMPL, MockHRegion.class, HeapSize.class); final MockHRegion region = (MockHRegion) TEST_UTIL.createLocalHRegion(Bytes.toBytes(tableName), null, null, tableName, conf, false, Durability.SYNC_WAL, null, Bytes.toBytes(family)); Put[] puts = new Put[1]; Put put = new Put(Bytes.toBytes("r1")); put.add(Bytes.toBytes(family), Bytes.toBytes("q1"), Bytes.toBytes("10")); puts[0] = put; region.batchMutate(puts); MultithreadedTestUtil.TestContext ctx = new MultithreadedTestUtil.TestContext(conf); ctx.addThread(new PutThread(ctx, region)); ctx.addThread(new CheckAndPutThread(ctx, region)); ctx.startThreads(); while (testStep != TestStep.CHECKANDPUT_COMPLETED) { Thread.sleep(100); } ctx.stop(); Scan s = new Scan(); RegionScanner scanner = region.getScanner(s); List<Cell> results = new ArrayList<Cell>(); scanner.next(results, 2); for (Cell keyValue : results) { assertEquals("50",Bytes.toString(CellUtil.cloneValue(keyValue))); } }
public static void hammerSingleKey(final BlockCache toBeTested, int BlockSize, int numThreads, int numQueries) throws Exception { final BlockCacheKey key = new BlockCacheKey("key", 0); final byte[] buf = new byte[5 * 1024]; Arrays.fill(buf, (byte) 5); final ByteArrayCacheable bac = new ByteArrayCacheable(buf); Configuration conf = new Configuration(); MultithreadedTestUtil.TestContext ctx = new MultithreadedTestUtil.TestContext( conf); final AtomicInteger totalQueries = new AtomicInteger(); toBeTested.cacheBlock(key, bac); for (int i = 0; i < numThreads; i++) { TestThread t = new MultithreadedTestUtil.RepeatingTestThread(ctx) { @Override public void doAnAction() throws Exception { ByteArrayCacheable returned = (ByteArrayCacheable) toBeTested .getBlock(key, false, false, true); assertArrayEquals(buf, returned.buf); totalQueries.incrementAndGet(); } }; t.setDaemon(true); ctx.addThread(t); } ctx.startThreads(); while (totalQueries.get() < numQueries && ctx.shouldRun()) { Thread.sleep(10); } ctx.stop(); }
/** * Test written as a verifier for HBASE-7051, CheckAndPut should properly read * MVCC. * * Moved into TestAtomicOperation from its original location, TestHBase7051 */ @Test public void testPutAndCheckAndPutInParallel() throws Exception { Configuration conf = TEST_UTIL.getConfiguration(); conf.setClass(HConstants.REGION_IMPL, MockHRegion.class, HeapSize.class); HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(name.getMethodName())) .addFamily(new HColumnDescriptor(family)); this.region = TEST_UTIL.createLocalHRegion(htd, null, null); Put[] puts = new Put[1]; Put put = new Put(Bytes.toBytes("r1")); put.addColumn(Bytes.toBytes(family), Bytes.toBytes("q1"), Bytes.toBytes("10")); puts[0] = put; region.batchMutate(puts, HConstants.NO_NONCE, HConstants.NO_NONCE); MultithreadedTestUtil.TestContext ctx = new MultithreadedTestUtil.TestContext(conf); ctx.addThread(new PutThread(ctx, region)); ctx.addThread(new CheckAndPutThread(ctx, region)); ctx.startThreads(); while (testStep != TestStep.CHECKANDPUT_COMPLETED) { Thread.sleep(100); } ctx.stop(); Scan s = new Scan(); RegionScanner scanner = region.getScanner(s); List<Cell> results = new ArrayList<>(); ScannerContext scannerContext = ScannerContext.newBuilder().setBatchLimit(2).build(); scanner.next(results, scannerContext); for (Cell keyValue : results) { assertEquals("50",Bytes.toString(CellUtil.cloneValue(keyValue))); } }