use of com.baidu.hugegraph.concurrent.BarrierEvent in project hugegraph-computer by hugegraph.
the class EtcdClient method waitAndPrefixGetFromPutEvent.
/**
* Wait at most expected eventCount events triggered in timeout ms.
* This method wait at most timeout ms regardless whether expected
* eventCount events triggered.
* @param existedKeyValues readonly
*/
private List<byte[]> waitAndPrefixGetFromPutEvent(ByteSequence prefixSeq, int count, List<KeyValue> existedKeyValues, long revision, long timeout, long logInterval) throws InterruptedException {
Map<ByteSequence, ByteSequence> keyValues = new ConcurrentHashMap<>();
for (KeyValue kv : existedKeyValues) {
keyValues.put(kv.getKey(), kv.getValue());
}
WaitEvent<List<byte[]>> barrierEvent = new WaitEvent<>();
Consumer<WatchResponse> consumer = watchResponse -> {
List<WatchEvent> events = watchResponse.getEvents();
for (WatchEvent event : events) {
if (EventType.PUT.equals(event.getEventType())) {
KeyValue keyValue = event.getKeyValue();
keyValues.put(keyValue.getKey(), keyValue.getValue());
if (keyValues.size() == count) {
List<byte[]> result = new ArrayList<>(count);
for (ByteSequence byteSequence : keyValues.values()) {
result.add(byteSequence.getBytes());
}
barrierEvent.signalAll(result);
}
} else if (EventType.DELETE.equals(event.getEventType())) {
keyValues.remove(event.getKeyValue().getKey());
} else {
throw new ComputerException("Unexpected event type '%s'", event.getEventType());
}
}
};
WatchOption watchOption = WatchOption.newBuilder().withPrefix(prefixSeq).withRevision(revision).build();
try (Watch.Watcher watcher = this.watch.watch(prefixSeq, watchOption, consumer)) {
return barrierEvent.await(timeout, logInterval, () -> {
LOG.info("Wait for keys with prefix '{}' and timeout {}ms, " + "expect {} keys but actual got {} keys", prefixSeq.toString(ENCODING), timeout, count, keyValues.size());
});
}
}
use of com.baidu.hugegraph.concurrent.BarrierEvent in project hugegraph-computer by hugegraph.
the class EtcdClient method waitAndGetFromPutEvent.
/**
* Wait put event.
* Return the value from event if event triggered in timeout.
* @throws ComputerException if no event triggered in timeout
*/
private byte[] waitAndGetFromPutEvent(ByteSequence keySeq, long revision, long timeout, long logInterval) throws InterruptedException {
WaitEvent<byte[]> barrierEvent = new WaitEvent<>();
Consumer<WatchResponse> consumer = watchResponse -> {
List<WatchEvent> events = watchResponse.getEvents();
for (WatchEvent event : events) {
if (EventType.PUT.equals(event.getEventType())) {
KeyValue keyValue = event.getKeyValue();
if (keySeq.equals(keyValue.getKey())) {
byte[] result = event.getKeyValue().getValue().getBytes();
barrierEvent.signalAll(result);
return;
} else {
assert false;
throw new ComputerException("Expect event key '%s', found '%s'", keySeq.toString(ENCODING), keyValue.getKey().toString(ENCODING));
}
} else {
assert false;
throw new ComputerException("Unexpected event type '%s'", event.getEventType());
}
}
};
WatchOption watchOption = WatchOption.newBuilder().withRevision(revision).withNoDelete(true).build();
try (Watch.Watcher watcher = this.watch.watch(keySeq, watchOption, consumer)) {
return barrierEvent.await(timeout, logInterval, () -> {
LOG.info("Wait for key '{}' with timeout {}ms", keySeq.toString(ENCODING), timeout);
});
}
}
use of com.baidu.hugegraph.concurrent.BarrierEvent in project hugegraph-common by hugegraph.
the class BarrierEventTest method testAWait.
@Test(timeout = 5000)
public void testAWait() throws InterruptedException {
BarrierEvent barrierEvent = new BarrierEvent();
AtomicInteger result = new AtomicInteger(0);
CountDownLatch latch = new CountDownLatch(2);
Thread awaitThread = new Thread(() -> {
try {
barrierEvent.await();
result.incrementAndGet();
} catch (InterruptedException e) {
// Do nothing.
} finally {
latch.countDown();
}
});
awaitThread.start();
Thread signalThread = new Thread(() -> {
barrierEvent.signalAll();
latch.countDown();
});
signalThread.start();
latch.await();
Assert.assertEquals(1, result.get());
}
use of com.baidu.hugegraph.concurrent.BarrierEvent in project hugegraph-common by hugegraph.
the class BarrierEventTest method testAWaitWithTimeout.
@Test
public void testAWaitWithTimeout() throws InterruptedException {
BarrierEvent barrierEvent = new BarrierEvent();
boolean signaled = barrierEvent.await(1L);
Assert.assertFalse(signaled);
}
use of com.baidu.hugegraph.concurrent.BarrierEvent in project hugegraph-common by hugegraph.
the class BarrierEventTest method testReset.
@Test
public void testReset() throws InterruptedException {
BarrierEvent barrierEvent = new BarrierEvent();
boolean signaled = barrierEvent.await(1L);
Assert.assertFalse(signaled);
barrierEvent.signal();
signaled = barrierEvent.await(1L);
Assert.assertTrue(signaled);
barrierEvent.reset();
signaled = barrierEvent.await(1L);
Assert.assertFalse(signaled);
}
Aggregations