Search in sources :

Example 21 with ByteSequence

use of io.etcd.jetcd.ByteSequence 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 22 with ByteSequence

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

the class KVTest method testTxnForCmpOpNotEqual.

@Test
public void testTxnForCmpOpNotEqual() throws Exception {
    ByteSequence sampleKey = bytesOf("txn_key");
    ByteSequence sampleValue = bytesOf("xyz");
    ByteSequence cmpValue = bytesOf("abc");
    ByteSequence putValue = bytesOf("XYZ");
    ByteSequence putValueNew = bytesOf("ABC");
    // put the original txn key value pair
    kvClient.put(sampleKey, sampleValue).get();
    // construct txn operation
    Txn txn = kvClient.txn();
    Cmp cmp = new Cmp(sampleKey, Cmp.Op.NOT_EQUAL, CmpTarget.value(cmpValue));
    CompletableFuture<io.etcd.jetcd.kv.TxnResponse> txnResp = txn.If(cmp).Then(Op.put(sampleKey, putValue, PutOption.DEFAULT)).Else(Op.put(sampleKey, putValueNew, PutOption.DEFAULT)).commit();
    txnResp.get();
    // get the value
    GetResponse getResp = kvClient.get(sampleKey).get();
    assertThat(getResp.getKvs()).hasSize(1);
    assertThat(getResp.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(putValue.toString(UTF_8));
}
Also used : Cmp(io.etcd.jetcd.op.Cmp) Txn(io.etcd.jetcd.Txn) TxnResponse(io.etcd.jetcd.kv.TxnResponse) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 23 with ByteSequence

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

the class KVTest method testByteSequence.

@Test
public void testByteSequence() {
    ByteSequence prefix = bytesOf("/test-service/");
    ByteSequence subPrefix = bytesOf("uuids/");
    String keyString = randomString();
    ByteSequence key = bytesOf(keyString);
    ByteSequence prefixedKey = prefix.concat(subPrefix).concat(key);
    assertThat(prefixedKey.startsWith(prefix)).isTrue();
    assertThat(prefixedKey.substring(prefix.size() + subPrefix.size()).toString(UTF_8)).isEqualTo(keyString);
    assertThat(prefixedKey.substring(prefix.size(), prefix.size() + subPrefix.size())).isEqualTo(subPrefix);
}
Also used : TestUtil.randomString(io.etcd.jetcd.impl.TestUtil.randomString) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 24 with ByteSequence

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

the class KVTest method testNestedTxn.

@Test
public void testNestedTxn() throws Exception {
    ByteSequence foo = bytesOf("txn_foo");
    ByteSequence bar = bytesOf("txn_bar");
    ByteSequence barz = bytesOf("txn_barz");
    ByteSequence abc = bytesOf("txn_abc");
    ByteSequence oneTwoThree = bytesOf("txn_123");
    Txn txn = kvClient.txn();
    Cmp cmp = new Cmp(foo, Cmp.Op.EQUAL, CmpTarget.version(0));
    CompletableFuture<io.etcd.jetcd.kv.TxnResponse> txnResp = txn.If(cmp).Then(Op.put(foo, bar, PutOption.DEFAULT), Op.txn(null, new Op[] { Op.put(abc, oneTwoThree, PutOption.DEFAULT) }, null)).Else(Op.put(foo, barz, PutOption.DEFAULT)).commit();
    txnResp.get();
    GetResponse getResp = kvClient.get(foo).get();
    assertThat(getResp.getKvs()).hasSize(1);
    assertThat(getResp.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(bar.toString(UTF_8));
    GetResponse getResp2 = kvClient.get(abc).get();
    assertThat(getResp2.getKvs()).hasSize(1);
    assertThat(getResp2.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(oneTwoThree.toString(UTF_8));
}
Also used : Op(io.etcd.jetcd.op.Op) Cmp(io.etcd.jetcd.op.Cmp) Txn(io.etcd.jetcd.Txn) TxnResponse(io.etcd.jetcd.kv.TxnResponse) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 25 with ByteSequence

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

the class KVTest method testGetAndDeleteWithPrefix.

@Test
public void testGetAndDeleteWithPrefix() throws Exception {
    String prefix = randomString();
    ByteSequence key = bytesOf(prefix);
    int numPrefixes = 10;
    putKeysWithPrefix(prefix, numPrefixes);
    // verify get withPrefix.
    CompletableFuture<GetResponse> getFuture = kvClient.get(key, GetOption.newBuilder().isPrefix(true).build());
    GetResponse getResp = getFuture.get();
    assertThat(getResp.getCount()).isEqualTo(numPrefixes);
    // verify del withPrefix.
    DeleteOption deleteOpt = DeleteOption.newBuilder().isPrefix(true).build();
    CompletableFuture<DeleteResponse> delFuture = kvClient.delete(key, deleteOpt);
    DeleteResponse delResp = delFuture.get();
    assertThat(delResp.getDeleted()).isEqualTo(numPrefixes);
}
Also used : DeleteOption(io.etcd.jetcd.options.DeleteOption) DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) TestUtil.randomString(io.etcd.jetcd.impl.TestUtil.randomString) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Aggregations

ByteSequence (io.etcd.jetcd.ByteSequence)40 Test (org.junit.jupiter.api.Test)24 TestUtil.randomByteSequence (io.etcd.jetcd.impl.TestUtil.randomByteSequence)17 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 Watcher (io.etcd.jetcd.Watch.Watcher)12 Client (io.etcd.jetcd.Client)11 WatchResponse (io.etcd.jetcd.watch.WatchResponse)8 MethodSource (org.junit.jupiter.params.provider.MethodSource)8 GetResponse (io.etcd.jetcd.kv.GetResponse)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Watch (io.etcd.jetcd.Watch)6 TxnResponse (io.etcd.jetcd.kv.TxnResponse)6 Cmp (io.etcd.jetcd.op.Cmp)6 Txn (io.etcd.jetcd.Txn)5 CampaignResponse (io.etcd.jetcd.election.CampaignResponse)5 LeaderResponse (io.etcd.jetcd.election.LeaderResponse)4 GetOption (io.etcd.jetcd.options.GetOption)4 WatchOption (io.etcd.jetcd.options.WatchOption)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Election (io.etcd.jetcd.Election)3