Search in sources :

Example 6 with BarrierEvent

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());
        });
    }
}
Also used : DeleteOption(io.etcd.jetcd.options.DeleteOption) Client(io.etcd.jetcd.Client) WatchEvent(io.etcd.jetcd.watch.WatchEvent) Watch(io.etcd.jetcd.Watch) ArrayList(java.util.ArrayList) DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) Charset(java.nio.charset.Charset) ByteSequence(io.etcd.jetcd.ByteSequence) Map(java.util.Map) GetResponse(io.etcd.jetcd.kv.GetResponse) E(com.baidu.hugegraph.util.E) GetOption(io.etcd.jetcd.options.GetOption) KV(io.etcd.jetcd.KV) Logger(org.slf4j.Logger) EventType(io.etcd.jetcd.watch.WatchEvent.EventType) SortOrder(io.etcd.jetcd.options.GetOption.SortOrder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KeyValue(io.etcd.jetcd.KeyValue) StandardCharsets(java.nio.charset.StandardCharsets) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) Log(com.baidu.hugegraph.util.Log) WatchResponse(io.etcd.jetcd.watch.WatchResponse) WatchOption(io.etcd.jetcd.options.WatchOption) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) BarrierEvent(com.baidu.hugegraph.concurrent.BarrierEvent) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) KeyValue(io.etcd.jetcd.KeyValue) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) Watch(io.etcd.jetcd.Watch) ArrayList(java.util.ArrayList) List(java.util.List) WatchEvent(io.etcd.jetcd.watch.WatchEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ByteSequence(io.etcd.jetcd.ByteSequence) WatchOption(io.etcd.jetcd.options.WatchOption)

Example 7 with BarrierEvent

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);
        });
    }
}
Also used : DeleteOption(io.etcd.jetcd.options.DeleteOption) Client(io.etcd.jetcd.Client) WatchEvent(io.etcd.jetcd.watch.WatchEvent) Watch(io.etcd.jetcd.Watch) ArrayList(java.util.ArrayList) DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) Charset(java.nio.charset.Charset) ByteSequence(io.etcd.jetcd.ByteSequence) Map(java.util.Map) GetResponse(io.etcd.jetcd.kv.GetResponse) E(com.baidu.hugegraph.util.E) GetOption(io.etcd.jetcd.options.GetOption) KV(io.etcd.jetcd.KV) Logger(org.slf4j.Logger) EventType(io.etcd.jetcd.watch.WatchEvent.EventType) SortOrder(io.etcd.jetcd.options.GetOption.SortOrder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KeyValue(io.etcd.jetcd.KeyValue) StandardCharsets(java.nio.charset.StandardCharsets) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) Log(com.baidu.hugegraph.util.Log) WatchResponse(io.etcd.jetcd.watch.WatchResponse) WatchOption(io.etcd.jetcd.options.WatchOption) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) BarrierEvent(com.baidu.hugegraph.concurrent.BarrierEvent) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) KeyValue(io.etcd.jetcd.KeyValue) Watch(io.etcd.jetcd.Watch) ArrayList(java.util.ArrayList) List(java.util.List) WatchEvent(io.etcd.jetcd.watch.WatchEvent) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) WatchOption(io.etcd.jetcd.options.WatchOption)

Example 8 with BarrierEvent

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) BarrierEvent(com.baidu.hugegraph.concurrent.BarrierEvent) Test(org.junit.Test)

Example 9 with BarrierEvent

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);
}
Also used : BarrierEvent(com.baidu.hugegraph.concurrent.BarrierEvent) Test(org.junit.Test)

Example 10 with BarrierEvent

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);
}
Also used : BarrierEvent(com.baidu.hugegraph.concurrent.BarrierEvent) Test(org.junit.Test)

Aggregations

BarrierEvent (com.baidu.hugegraph.concurrent.BarrierEvent)12 Test (org.junit.Test)10 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ExecutorService (java.util.concurrent.ExecutorService)5 ComputerException (com.baidu.hugegraph.computer.core.common.exception.ComputerException)2 E (com.baidu.hugegraph.util.E)2 Log (com.baidu.hugegraph.util.Log)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ByteSequence (io.etcd.jetcd.ByteSequence)2 Client (io.etcd.jetcd.Client)2 KV (io.etcd.jetcd.KV)2 KeyValue (io.etcd.jetcd.KeyValue)2 Watch (io.etcd.jetcd.Watch)2 DeleteResponse (io.etcd.jetcd.kv.DeleteResponse)2 GetResponse (io.etcd.jetcd.kv.GetResponse)2 DeleteOption (io.etcd.jetcd.options.DeleteOption)2 GetOption (io.etcd.jetcd.options.GetOption)2 SortOrder (io.etcd.jetcd.options.GetOption.SortOrder)2 WatchOption (io.etcd.jetcd.options.WatchOption)2