use of java.util.concurrent.atomic.AtomicBoolean in project hbase by apache.
the class TestWeakObjectPool method testCongestion.
@Test(timeout = 1000)
public void testCongestion() throws Exception {
final int THREAD_COUNT = 100;
final AtomicBoolean assertionFailed = new AtomicBoolean();
final AtomicReference<Object> expectedObjRef = new AtomicReference<>();
final CountDownLatch prepareLatch = new CountDownLatch(THREAD_COUNT);
final CountDownLatch startLatch = new CountDownLatch(1);
final CountDownLatch endLatch = new CountDownLatch(THREAD_COUNT);
for (int i = 0; i < THREAD_COUNT; i++) {
new Thread() {
@Override
public void run() {
prepareLatch.countDown();
try {
startLatch.await();
Object obj = pool.get("a");
if (!expectedObjRef.compareAndSet(null, obj)) {
if (expectedObjRef.get() != obj) {
assertionFailed.set(true);
}
}
} catch (Exception e) {
assertionFailed.set(true);
} finally {
endLatch.countDown();
}
}
}.start();
}
prepareLatch.await();
startLatch.countDown();
endLatch.await();
if (assertionFailed.get()) {
Assert.fail();
}
}
use of java.util.concurrent.atomic.AtomicBoolean in project hbase by apache.
the class TestProcedureSuspended method testSuspendWhileHoldingLocks.
@Test(timeout = 10000)
public void testSuspendWhileHoldingLocks() {
final AtomicBoolean lockA = new AtomicBoolean(false);
final AtomicBoolean lockB = new AtomicBoolean(false);
final TestLockProcedure p1keyA = new TestLockProcedure(lockA, "keyA", false, true);
final TestLockProcedure p2keyA = new TestLockProcedure(lockA, "keyA", false, true);
final TestLockProcedure p3keyB = new TestLockProcedure(lockB, "keyB", false, true);
procExecutor.submitProcedure(p1keyA);
procExecutor.submitProcedure(p2keyA);
procExecutor.submitProcedure(p3keyB);
// first run p1, p3 are able to run p2 is blocked by p1
waitAndAssertTimestamp(p1keyA, 1, 1);
waitAndAssertTimestamp(p2keyA, 0, -1);
waitAndAssertTimestamp(p3keyB, 1, 2);
assertEquals(true, lockA.get());
assertEquals(true, lockB.get());
// release p3
p3keyB.setThrowSuspend(false);
procExecutor.getScheduler().addFront(p3keyB);
waitAndAssertTimestamp(p1keyA, 1, 1);
waitAndAssertTimestamp(p2keyA, 0, -1);
waitAndAssertTimestamp(p3keyB, 2, 3);
assertEquals(true, lockA.get());
// wait until p3 is fully completed
ProcedureTestingUtility.waitProcedure(procExecutor, p3keyB);
assertEquals(false, lockB.get());
// rollback p2 and wait until is fully completed
p1keyA.setTriggerRollback(true);
procExecutor.getScheduler().addFront(p1keyA);
ProcedureTestingUtility.waitProcedure(procExecutor, p1keyA);
// p2 should start and suspend
waitAndAssertTimestamp(p1keyA, 4, 60000);
waitAndAssertTimestamp(p2keyA, 1, 7);
waitAndAssertTimestamp(p3keyB, 2, 3);
assertEquals(true, lockA.get());
// wait until p2 is fully completed
p2keyA.setThrowSuspend(false);
procExecutor.getScheduler().addFront(p2keyA);
ProcedureTestingUtility.waitProcedure(procExecutor, p2keyA);
waitAndAssertTimestamp(p1keyA, 4, 60000);
waitAndAssertTimestamp(p2keyA, 2, 8);
waitAndAssertTimestamp(p3keyB, 2, 3);
assertEquals(false, lockA.get());
assertEquals(false, lockB.get());
}
use of java.util.concurrent.atomic.AtomicBoolean in project hbase by apache.
the class TestAsyncTableGetMultiThreaded method test.
@Test
public void test() throws IOException, InterruptedException, ExecutionException {
int numThreads = 20;
AtomicBoolean stop = new AtomicBoolean(false);
ExecutorService executor = Executors.newFixedThreadPool(numThreads, Threads.newDaemonThreadFactory("TestAsyncGet-"));
List<Future<?>> futures = new ArrayList<>();
IntStream.range(0, numThreads).forEach(i -> futures.add(executor.submit(() -> {
run(stop);
return null;
})));
Collections.shuffle(Arrays.asList(SPLIT_KEYS), new Random(123));
Admin admin = TEST_UTIL.getAdmin();
for (byte[] splitPoint : SPLIT_KEYS) {
admin.split(TABLE_NAME, splitPoint);
for (HRegion region : TEST_UTIL.getHBaseCluster().getRegions(TABLE_NAME)) {
region.compact(true);
}
Thread.sleep(5000);
admin.balancer(true);
Thread.sleep(5000);
ServerName metaServer = TEST_UTIL.getHBaseCluster().getServerHoldingMeta();
ServerName newMetaServer = TEST_UTIL.getHBaseCluster().getRegionServerThreads().stream().map(t -> t.getRegionServer().getServerName()).filter(s -> !s.equals(metaServer)).findAny().get();
admin.move(HRegionInfo.FIRST_META_REGIONINFO.getEncodedNameAsBytes(), Bytes.toBytes(newMetaServer.getServerName()));
Thread.sleep(5000);
}
stop.set(true);
executor.shutdown();
for (Future<?> future : futures) {
future.get();
}
}
use of java.util.concurrent.atomic.AtomicBoolean in project hbase by apache.
the class TestAsyncSingleRequestRpcRetryingCaller method testLocateError.
@Test
public void testLocateError() throws IOException, InterruptedException, ExecutionException {
AtomicBoolean errorTriggered = new AtomicBoolean(false);
AtomicInteger count = new AtomicInteger(0);
HRegionLocation loc = CONN.getRegionLocator(TABLE_NAME).getRegionLocation(ROW).get();
AsyncRegionLocator mockedLocator = new AsyncRegionLocator(CONN, AsyncConnectionImpl.RETRY_TIMER) {
@Override
CompletableFuture<HRegionLocation> getRegionLocation(TableName tableName, byte[] row, RegionLocateType locateType, long timeoutNs) {
if (tableName.equals(TABLE_NAME)) {
CompletableFuture<HRegionLocation> future = new CompletableFuture<>();
if (count.getAndIncrement() == 0) {
errorTriggered.set(true);
future.completeExceptionally(new RuntimeException("Inject error!"));
} else {
future.complete(loc);
}
return future;
} else {
return super.getRegionLocation(tableName, row, locateType, timeoutNs);
}
}
@Override
void updateCachedLocation(HRegionLocation loc, Throwable exception) {
}
};
try (AsyncConnectionImpl mockedConn = new AsyncConnectionImpl(CONN.getConfiguration(), CONN.registry, CONN.registry.getClusterId().get(), User.getCurrent()) {
@Override
AsyncRegionLocator getLocator() {
return mockedLocator;
}
}) {
RawAsyncTable table = mockedConn.getRawTableBuilder(TABLE_NAME).setRetryPause(100, TimeUnit.MILLISECONDS).setMaxRetries(5).build();
table.put(new Put(ROW).addColumn(FAMILY, QUALIFIER, VALUE)).get();
assertTrue(errorTriggered.get());
errorTriggered.set(false);
count.set(0);
Result result = table.get(new Get(ROW).addColumn(FAMILY, QUALIFIER)).get();
assertArrayEquals(VALUE, result.getValue(FAMILY, QUALIFIER));
assertTrue(errorTriggered.get());
}
}
use of java.util.concurrent.atomic.AtomicBoolean in project kafka by apache.
the class KafkaConsumerTest method prepareOffsetCommitResponse.
private AtomicBoolean prepareOffsetCommitResponse(MockClient client, Node coordinator, final Map<TopicPartition, Long> partitionOffsets) {
final AtomicBoolean commitReceived = new AtomicBoolean(true);
Map<TopicPartition, Errors> response = new HashMap<>();
for (TopicPartition partition : partitionOffsets.keySet()) response.put(partition, Errors.NONE);
client.prepareResponseFrom(new MockClient.RequestMatcher() {
@Override
public boolean matches(AbstractRequest body) {
OffsetCommitRequest commitRequest = (OffsetCommitRequest) body;
for (Map.Entry<TopicPartition, Long> partitionOffset : partitionOffsets.entrySet()) {
OffsetCommitRequest.PartitionData partitionData = commitRequest.offsetData().get(partitionOffset.getKey());
// verify that the expected offset has been committed
if (partitionData.offset != partitionOffset.getValue()) {
commitReceived.set(false);
return false;
}
}
return true;
}
}, offsetCommitResponse(response), coordinator);
return commitReceived;
}
Aggregations