Search in sources :

Example 1 with GetResponse

use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.

the class KVTest method testGetWithRev.

@Test
public void testGetWithRev() throws Exception {
    CompletableFuture<PutResponse> feature = kvClient.put(SAMPLE_KEY_3, SAMPLE_VALUE);
    PutResponse putResp = feature.get();
    kvClient.put(SAMPLE_KEY_3, SAMPLE_VALUE_2).get();
    GetOption option = GetOption.newBuilder().withRevision(putResp.getHeader().getRevision()).build();
    CompletableFuture<GetResponse> getFeature = kvClient.get(SAMPLE_KEY_3, option);
    GetResponse response = getFeature.get();
    assertThat(response.getKvs()).hasSize(1);
    assertThat(response.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(SAMPLE_VALUE.toString(UTF_8));
}
Also used : GetOption(io.etcd.jetcd.options.GetOption) PutResponse(io.etcd.jetcd.kv.PutResponse) GetResponse(io.etcd.jetcd.kv.GetResponse) Test(org.junit.jupiter.api.Test)

Example 2 with GetResponse

use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.

the class KVTest method testKVClientCanRetryPutOnEtcdRestart.

@Test
@SuppressWarnings("FutureReturnValueIgnored")
public void testKVClientCanRetryPutOnEtcdRestart() throws InterruptedException {
    try (Client customClient = TestUtil.client(cluster).retryMaxDuration(Duration.ofMinutes(5)).retryDelay(10).retryMaxDelay(30).retryChronoUnit(ChronoUnit.SECONDS).build()) {
        ByteSequence key = ByteSequence.from("retry_dummy_key", StandardCharsets.UTF_8);
        int putCount = 1000;
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
        // start putting values in a sequence, at least on of them has to be retried
        executor.submit(() -> {
            for (int i = 0; i < putCount; ++i) {
                ByteSequence value = ByteSequence.from(Integer.toString(i), StandardCharsets.UTF_8);
                customClient.getKVClient().put(key, value).join();
            }
        });
        // restart the cluster while uploading
        executor.schedule(() -> cluster.restart(), 100, TimeUnit.MILLISECONDS);
        executor.shutdown();
        assertThat(executor.awaitTermination(30, TimeUnit.SECONDS)).isTrue();
        GetResponse getResponse = kvClient.get(key).join();
        assertThat(getResponse.getKvs().size()).as("There should be exactly one KeyValue for the test key").isEqualTo(1);
        ByteSequence lastPutValue = ByteSequence.from(Integer.toString(putCount - 1), StandardCharsets.UTF_8);
        assertThat(getResponse.getKvs().get(0).getValue()).as("The sequence of put operations should finish successfully. " + "Last seen value should match the expected value.").isEqualTo(lastPutValue);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Client(io.etcd.jetcd.Client) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 3 with GetResponse

use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.

the class KVTest method testDelete.

@Test
public void testDelete() throws Exception {
    // Put content so that we actually have something to delete
    testPut();
    ByteSequence keyToDelete = SAMPLE_KEY;
    // count keys about to delete
    CompletableFuture<GetResponse> getFeature = kvClient.get(keyToDelete);
    GetResponse resp = getFeature.get();
    // delete the keys
    CompletableFuture<DeleteResponse> deleteFuture = kvClient.delete(keyToDelete);
    DeleteResponse delResp = deleteFuture.get();
    assertThat(delResp.getDeleted()).isEqualTo(resp.getKvs().size());
}
Also used : DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 4 with GetResponse

use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.

the class KVTest method testTxn.

@Test
public void testTxn() 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.GREATER, 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 5 with GetResponse

use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.

the class KVTest method testTxnGetAndDeleteWithPrefix.

@Test
void testTxnGetAndDeleteWithPrefix() throws ExecutionException, InterruptedException {
    String prefix = randomString();
    ByteSequence sampleKey = bytesOf(prefix);
    int numPrefixes = 10;
    putKeysWithPrefix(prefix, numPrefixes);
    // always false cmp
    Cmp cmp = new Cmp(sampleKey, Cmp.Op.EQUAL, CmpTarget.value(bytesOf("not_exists")));
    Op.PutOp putOp = Op.put(bytesOf("other_string"), bytesOf("other_value"), PutOption.DEFAULT);
    Op.GetOp getByPrefix = Op.get(sampleKey, GetOption.newBuilder().isPrefix(true).build());
    Op.DeleteOp delete = Op.delete(sampleKey, DeleteOption.newBuilder().isPrefix(true).withPrevKV(true).build());
    TxnResponse txnResponse = kvClient.txn().If(cmp).Then(putOp).Else(getByPrefix, delete).commit().get();
    List<GetResponse> getResponse = txnResponse.getGetResponses();
    assertThat(getResponse).hasSize(1);
    assertThat(getResponse.get(0).getKvs()).hasSize(10);
    assertThat(getResponse.get(0).getKvs()).anyMatch(keyValue -> keyValue.getKey().startsWith(sampleKey));
    List<DeleteResponse> deleteResponses = txnResponse.getDeleteResponses();
    assertThat(deleteResponses).hasSize(1);
    assertThat(deleteResponses.get(0).getDeleted()).isEqualTo(10);
    assertThat(deleteResponses.get(0).getPrevKvs()).anyMatch(keyValue -> keyValue.getKey().startsWith(sampleKey));
    assertThat(txnResponse.getPutResponses()).isEmpty();
}
Also used : Op(io.etcd.jetcd.op.Op) Cmp(io.etcd.jetcd.op.Cmp) TestUtil.randomString(io.etcd.jetcd.impl.TestUtil.randomString) GetResponse(io.etcd.jetcd.kv.GetResponse) DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) TxnResponse(io.etcd.jetcd.kv.TxnResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Aggregations

GetResponse (io.etcd.jetcd.kv.GetResponse)21 Test (org.junit.jupiter.api.Test)16 ByteSequence (io.etcd.jetcd.ByteSequence)7 TxnResponse (io.etcd.jetcd.kv.TxnResponse)4 Cmp (io.etcd.jetcd.op.Cmp)4 URL (org.apache.dubbo.common.URL)4 ServiceMetadataIdentifier (org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier)4 SubscriberMetadataIdentifier (org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier)4 Gson (com.google.gson.Gson)3 Txn (io.etcd.jetcd.Txn)3 TestUtil.randomString (io.etcd.jetcd.impl.TestUtil.randomString)3 DeleteResponse (io.etcd.jetcd.kv.DeleteResponse)3 PutResponse (io.etcd.jetcd.kv.PutResponse)2 Op (io.etcd.jetcd.op.Op)2 GetOption (io.etcd.jetcd.options.GetOption)2 MetadataIdentifier (org.apache.dubbo.metadata.report.identifier.MetadataIdentifier)2 Client (io.etcd.jetcd.Client)1 KeyValue (io.etcd.jetcd.KeyValue)1 DeleteOption (io.etcd.jetcd.options.DeleteOption)1 Errors (io.etcd.jetcd.support.Errors)1