use of io.etcd.jetcd.Client in project dubbo by alibaba.
the class JEtcdClientTest method testWatchWithGrpc.
@Test
public void testWatchWithGrpc() {
String path = "/dubbo/config/test_watch_with_grpc/configurators";
String endpoint = "http://127.0.0.1:2379";
CountDownLatch latch = new CountDownLatch(1);
try (Client client = Client.builder().endpoints(endpoint).build()) {
ManagedChannel channel = getChannel(client);
StreamObserver<WatchRequest> observer = WatchGrpc.newStub(channel).watch(new StreamObserver<WatchResponse>() {
@Override
public void onNext(WatchResponse response) {
for (Event event : response.getEventsList()) {
Assertions.assertEquals("PUT", event.getType().toString());
Assertions.assertEquals(path, event.getKv().getKey().toString(UTF_8));
Assertions.assertEquals("Hello", event.getKv().getValue().toString(UTF_8));
latch.countDown();
}
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCompleted() {
}
});
WatchCreateRequest.Builder builder = WatchCreateRequest.newBuilder().setKey(ByteString.copyFrom(path, UTF_8));
observer.onNext(WatchRequest.newBuilder().setCreateRequest(builder).build());
// try to modify the key
client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello", UTF_8));
latch.await(5, TimeUnit.SECONDS);
} catch (Exception e) {
Assertions.fail(e.getMessage());
}
}
use of io.etcd.jetcd.Client in project jetcd by coreos.
the class ElectionTest method testSynchronizationBarrier.
@Test
public void testSynchronizationBarrier() throws Exception {
final int threadCount = 5;
final Random random = new Random();
ByteSequence electionName = ByteSequence.from(randomString(), StandardCharsets.UTF_8);
final AtomicInteger sharedVariable = new AtomicInteger(0);
// create separate clients so they will compete for access to shared resource
List<Client> clients = new ArrayList<>(threadCount);
List<Long> leases = new ArrayList<>(threadCount);
for (int i = 0; i < threadCount; ++i) {
Client client = TestUtil.client(cluster).build();
long leaseId = client.getLeaseClient().grant(100).get().getID();
clients.add(client);
leases.add(leaseId);
}
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
List<Future<?>> futures = new ArrayList<>(threadCount);
for (int i = 0; i < threadCount; ++i) {
final int id = i;
final ByteSequence proposal = ByteSequence.from(Integer.toString(id), StandardCharsets.UTF_8);
futures.add(executor.submit(() -> {
try {
Election electionClient = clients.get(id).getElectionClient();
CampaignResponse campaignResponse = electionClient.campaign(electionName, leases.get(id), proposal).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
int localCopy = sharedVariable.get();
Thread.sleep(200 + random.nextInt(300));
sharedVariable.set(localCopy + 1);
electionClient.resign(campaignResponse.getLeader()).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
} catch (Exception e) {
fail("Unexpected error in thread {}: {}", id, e);
}
}));
}
executor.shutdown();
executor.awaitTermination(threadCount * OPERATION_TIMEOUT, TimeUnit.SECONDS);
futures.forEach(f -> assertThat(f).isDone());
assertThat(sharedVariable.get()).isEqualTo(threadCount);
GetOption getOption = GetOption.newBuilder().isPrefix(true).build();
assertThat(kvClient.get(electionName, getOption).get().getCount()).isEqualTo(0L);
for (int i = 0; i < threadCount; ++i) {
clients.get(i).getLeaseClient().revoke(leases.get(i)).get();
clients.get(i).close();
}
}
use of io.etcd.jetcd.Client in project jetcd by coreos.
the class ElectionTest method setUp.
@BeforeAll
public static void setUp() {
Client client = TestUtil.client(cluster).build();
electionClient = client.getElectionClient();
leaseClient = client.getLeaseClient();
kvClient = client.getKVClient();
}
use of io.etcd.jetcd.Client in project jetcd by coreos.
the class KVTest method waitForReadySemantics.
@Test()
public void waitForReadySemantics() throws ExecutionException, InterruptedException, TimeoutException {
String nonExistingServer = "http://127.0.0.1:9999";
try (Client customClient = Client.builder().endpoints(nonExistingServer).waitForReady(false).retryMaxDuration(Duration.ofSeconds(3)).retryDelay(1).retryMaxDelay(2).retryChronoUnit(ChronoUnit.SECONDS).connectTimeout(Duration.ofSeconds(1)).build()) {
KV kvClient = customClient.getKVClient();
CompletableFuture<String> future = kvClient.get(ByteSequence.from("/x", StandardCharsets.UTF_8)).thenApply(response -> "we got a response").exceptionally(throwable -> "completed exceptionally");
assertThat(future.get(5, TimeUnit.SECONDS)).isEqualTo("completed exceptionally");
}
}
use of io.etcd.jetcd.Client in project jetcd by coreos.
the class CommandWatch method accept.
@Override
public void accept(Client client) throws Exception {
CountDownLatch latch = new CountDownLatch(maxEvents);
Watcher watcher = null;
try {
ByteSequence watchKey = ByteSequence.from(key, Charsets.UTF_8);
WatchOption watchOpts = WatchOption.newBuilder().withRevision(rev).build();
watcher = client.getWatchClient().watch(watchKey, watchOpts, response -> {
for (WatchEvent event : response.getEvents()) {
LOGGER.info("type={}, key={}, value={}", event.getEventType().toString(), Optional.ofNullable(event.getKeyValue().getKey()).map(bs -> bs.toString(Charsets.UTF_8)).orElse(""), Optional.ofNullable(event.getKeyValue().getValue()).map(bs -> bs.toString(Charsets.UTF_8)).orElse(""));
}
latch.countDown();
});
latch.await();
} catch (Exception e) {
if (watcher != null) {
watcher.close();
}
throw e;
}
}
Aggregations