Search in sources :

Example 1 with Election

use of io.etcd.jetcd.Election 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();
    }
}
Also used : CampaignResponse(io.etcd.jetcd.election.CampaignResponse) ArrayList(java.util.ArrayList) Election(io.etcd.jetcd.Election) NoLeaderException(io.etcd.jetcd.election.NoLeaderException) NotLeaderException(io.etcd.jetcd.election.NotLeaderException) ExecutionException(java.util.concurrent.ExecutionException) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) GetOption(io.etcd.jetcd.options.GetOption) Future(java.util.concurrent.Future) Client(io.etcd.jetcd.Client) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) Test(org.junit.jupiter.api.Test)

Example 2 with Election

use of io.etcd.jetcd.Election in project jetcd by coreos.

the class ElectionImpl method observe.

@Override
public void observe(ByteSequence electionName, Listener listener) {
    checkNotNull(electionName, "election name should not be null");
    checkNotNull(listener, "listener should not be null");
    LeaderRequest request = LeaderRequest.newBuilder().setName(ByteString.copyFrom(electionName.getBytes())).build();
    stub.observe(request).handler(value -> listener.onNext(new LeaderResponse(value, namespace))).endHandler(ignored -> listener.onCompleted()).exceptionHandler(error -> listener.onError(EtcdExceptionFactory.toEtcdException(error)));
}
Also used : ProclaimRequest(io.etcd.jetcd.api.ProclaimRequest) EtcdExceptionFactory.toEtcdException(io.etcd.jetcd.common.exception.EtcdExceptionFactory.toEtcdException) CampaignResponse(io.etcd.jetcd.election.CampaignResponse) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) VertxElectionGrpc(io.etcd.jetcd.api.VertxElectionGrpc) LeaderResponse(io.etcd.jetcd.election.LeaderResponse) CompletableFuture(java.util.concurrent.CompletableFuture) ProclaimResponse(io.etcd.jetcd.election.ProclaimResponse) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) ResignRequest(io.etcd.jetcd.api.ResignRequest) CampaignRequest(io.etcd.jetcd.api.CampaignRequest) LeaderKey(io.etcd.jetcd.election.LeaderKey) NoLeaderException(io.etcd.jetcd.election.NoLeaderException) Util(io.etcd.jetcd.support.Util) ByteSequence(io.etcd.jetcd.ByteSequence) Election(io.etcd.jetcd.Election) ResignResponse(io.etcd.jetcd.election.ResignResponse) NotLeaderException(io.etcd.jetcd.election.NotLeaderException) LeaderRequest(io.etcd.jetcd.api.LeaderRequest) EtcdExceptionFactory(io.etcd.jetcd.common.exception.EtcdExceptionFactory) LeaderRequest(io.etcd.jetcd.api.LeaderRequest) LeaderResponse(io.etcd.jetcd.election.LeaderResponse)

Aggregations

ByteSequence (io.etcd.jetcd.ByteSequence)2 Election (io.etcd.jetcd.Election)2 CampaignResponse (io.etcd.jetcd.election.CampaignResponse)2 NoLeaderException (io.etcd.jetcd.election.NoLeaderException)2 NotLeaderException (io.etcd.jetcd.election.NotLeaderException)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ByteString (com.google.protobuf.ByteString)1 Client (io.etcd.jetcd.Client)1 CampaignRequest (io.etcd.jetcd.api.CampaignRequest)1 LeaderRequest (io.etcd.jetcd.api.LeaderRequest)1 ProclaimRequest (io.etcd.jetcd.api.ProclaimRequest)1 ResignRequest (io.etcd.jetcd.api.ResignRequest)1 VertxElectionGrpc (io.etcd.jetcd.api.VertxElectionGrpc)1 EtcdExceptionFactory (io.etcd.jetcd.common.exception.EtcdExceptionFactory)1 EtcdExceptionFactory.toEtcdException (io.etcd.jetcd.common.exception.EtcdExceptionFactory.toEtcdException)1 LeaderKey (io.etcd.jetcd.election.LeaderKey)1 LeaderResponse (io.etcd.jetcd.election.LeaderResponse)1 ProclaimResponse (io.etcd.jetcd.election.ProclaimResponse)1 ResignResponse (io.etcd.jetcd.election.ResignResponse)1 TestUtil.randomByteSequence (io.etcd.jetcd.impl.TestUtil.randomByteSequence)1