io.etcd.jetcd.kv.GetResponse Java Examples

The following examples show how to use io.etcd.jetcd.kv.GetResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BinaryLogConfigContainer.java    From kkbinlog with Apache License 2.0 6 votes vote down vote up
public List<BinaryLogConfig> getConfigs() {

        KV kvClient = etcdClient.getKVClient();
        List<BinaryLogConfig> binaryLogConfigs = new ArrayList<>();
        try {
            GetResponse configRes = kvClient.get(ByteSequence.from(etcdKeyPrefixUtil.withPrefix(Constants.DEFAULT_BINLOG_CONFIG_KEY), StandardCharsets.UTF_8)).get();

            if(configRes == null || configRes.getCount() == 0) {
                return binaryLogConfigs;
            }
            // not range query
            String configListStr = configRes.getKvs().get(0).getValue().toString(StandardCharsets.UTF_8);
            binaryLogConfigs = JSON.parseArray(configListStr, BinaryLogConfig.class);
        } catch (InterruptedException | ExecutionException e) {
            throw new EtcdServerException("Failed to connect to etcd server.", e);
        }
        return binaryLogConfigs;
    }
 
Example #2
Source File: KVTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #3
Source File: KVTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #4
Source File: KVTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #5
Source File: KVTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAndDeleteWithPrefix() throws Exception {
    String prefix = TestUtil.randomString();
    ByteSequence key = bytesOf(prefix);
    int numPrefixes = 10;

    putKeysWithPrefix(prefix, numPrefixes);

    // verify get withPrefix.
    CompletableFuture<GetResponse> getFuture = kvClient.get(key, GetOption.newBuilder().withPrefix(key).build());
    GetResponse getResp = getFuture.get();
    assertThat(getResp.getCount()).isEqualTo(numPrefixes);

    // verify del withPrefix.
    DeleteOption deleteOpt = DeleteOption.newBuilder().withPrefix(key).build();
    CompletableFuture<DeleteResponse> delFuture = kvClient.delete(key, deleteOpt);
    DeleteResponse delResp = delFuture.get();
    assertThat(delResp.getDeleted()).isEqualTo(numPrefixes);
}
 
Example #6
Source File: KVTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #7
Source File: KVTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetSortedPrefix() throws Exception {
    String prefix = TestUtil.randomString();
    int numPrefix = 3;
    putKeysWithPrefix(prefix, numPrefix);

    GetOption option = GetOption.newBuilder().withSortField(SortTarget.KEY).withSortOrder(SortOrder.DESCEND)
        .withPrefix(bytesOf(prefix)).build();
    CompletableFuture<GetResponse> getFeature = kvClient.get(bytesOf(prefix), option);
    GetResponse response = getFeature.get();

    assertThat(response.getKvs()).hasSize(numPrefix);
    for (int i = 0; i < numPrefix; i++) {
        assertThat(response.getKvs().get(i).getKey().toString(UTF_8)).isEqualTo(prefix + (numPrefix - i - 1));
        assertThat(response.getKvs().get(i).getValue().toString(UTF_8)).isEqualTo(String.valueOf(numPrefix - i - 1));
    }
}
 
Example #8
Source File: KVNamespaceTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
private static void assertExistentKVs(KV kvClient, ByteSequence key, ByteSequence end, List<TestKeyValue> expectedKVs)
    throws Exception {
    CompletableFuture<GetResponse> getFuture = kvClient.get(key, GetOption.newBuilder().withRange(end).build());
    GetResponse getResponse = getFuture.get();
    assertThat(getResponse.getKvs().size()).isEqualTo(expectedKVs.size());
    for (KeyValue keyValue : getResponse.getKvs()) {
        boolean exist = false;
        for (TestKeyValue expectedKV : expectedKVs) {
            if (expectedKV.key.equals(keyValue.getKey())) {
                exist = true;
                assertThat(keyValue.getValue()).isEqualTo(expectedKV.value);
                break;
            }
        }
        assertThat(exist).isTrue();
    }
}
 
Example #9
Source File: EtcdClusterUsingTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@Test
public void testUseEtcd() throws Exception {
    try (EtcdCluster etcd = EtcdClusterFactory.buildCluster(getClass().getSimpleName(), 3, false)) {
        etcd.start();
        try (Client client = Client.builder().endpoints(etcd.getClientEndpoints()).build()) {
            try (KV kvClient = client.getKVClient()) {
                ByteSequence key = bytesOf("test_key");
                ByteSequence value = bytesOf("test_value");
                kvClient.put(key, value).get();

                CompletableFuture<GetResponse> getFuture = kvClient.get(key);
                GetResponse response = getFuture.get();
                List<KeyValue> values = response.getKvs();
                assertThat(values.size()).isEqualTo(1);
                KeyValue value1 = values.get(0);
                assertThat(value1.getValue()).isEqualTo(value);
                assertThat(value1.getKey()).isEqualTo(key);
            }
        }
    }
}
 
Example #10
Source File: CommandGet.java    From jetcd with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(Client client) throws Exception {
    GetResponse getResponse = client.getKVClient()
        .get(ByteSequence.from(key, UTF_8), GetOption.newBuilder().withRevision(rev).build()).get();

    if (getResponse.getKvs().isEmpty()) {
        // key does not exist
        return;
    }

    LOGGER.info(key);
    LOGGER.info(getResponse.getKvs().get(0).getValue().toString(UTF_8));
}
 
Example #11
Source File: KVTest.java    From jetcd with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #12
Source File: KVTest.java    From jetcd with Apache License 2.0 5 votes vote down vote up
@Test
public void testGet() throws Exception {
    CompletableFuture<PutResponse> feature = kvClient.put(SAMPLE_KEY_2, SAMPLE_VALUE_2);
    feature.get();
    CompletableFuture<GetResponse> getFeature = kvClient.get(SAMPLE_KEY_2);
    GetResponse response = getFeature.get();
    assertThat(response.getKvs()).hasSize(1);
    assertThat(response.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(SAMPLE_VALUE_2.toString(UTF_8));
    assertThat(!response.isMore()).isTrue();
}
 
Example #13
Source File: KVNamespaceTest.java    From jetcd with Apache License 2.0 5 votes vote down vote up
private static void assertExistentKey(KV kvClient, ByteSequence key, ByteSequence value) throws Exception {
    CompletableFuture<GetResponse> getFeature = kvClient.get(key);
    GetResponse response = getFeature.get();
    assertThat(response.getKvs().size()).isEqualTo(1);
    assertThat(response.getKvs().get(0).getKey()).isEqualTo(key);
    assertThat(response.getKvs().get(0).getValue()).isEqualTo(value);
}
 
Example #14
Source File: KVImpl.java    From jetcd with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<GetResponse> get(ByteSequence key, GetOption option) {
    checkNotNull(key, "key should not be null");
    checkNotNull(option, "option should not be null");

    RangeRequest.Builder builder = RangeRequest.newBuilder()
        .setKey(Util.prefixNamespace(key.getByteString(), namespace))
        .setCountOnly(option.isCountOnly())
        .setLimit(option.getLimit())
        .setRevision(option.getRevision())
        .setKeysOnly(option.isKeysOnly())
        .setSerializable(option.isSerializable())
        .setSortOrder(toRangeRequestSortOrder(option.getSortOrder()))
        .setSortTarget(toRangeRequestSortTarget(option.getSortField()))
        .setMinCreateRevision(option.getMinCreateRevision())
        .setMaxCreateRevision(option.getMaxCreateRevision())
        .setMinModRevision(option.getMinModRevision())
        .setMaxModRevision(option.getMaxModRevision());

    option.getEndKey().map(endKey -> Util.prefixNamespaceToRangeEnd(endKey.getByteString(), namespace))
        .ifPresent(builder::setRangeEnd);

    RangeRequest request = builder.build();

    return connectionManager.execute(
        () -> stub.range(request),
        response -> new GetResponse(response, namespace),
        Util::isRetryable);
}
 
Example #15
Source File: KVNamespaceTest.java    From jetcd with Apache License 2.0 4 votes vote down vote up
private static void assertNonexistentKey(KV kvClient, ByteSequence key) throws Exception {
    CompletableFuture<GetResponse> getFeature = kvClient.get(key);
    GetResponse response = getFeature.get();
    assertThat(response.getKvs().size()).isEqualTo(0);
}
 
Example #16
Source File: KVImpl.java    From jetcd with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<GetResponse> get(ByteSequence key) {
    return this.get(key, GetOption.DEFAULT);
}
 
Example #17
Source File: EtcdDataSource.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Override
public String readSource() throws Exception {
    CompletableFuture<GetResponse> responseFuture = client.getKVClient().get(ByteSequence.from(key, charset));
    List<KeyValue> kvs = responseFuture.get().getKvs();
    return kvs.size() == 0 ? null : kvs.get(0).getValue().toString(charset);
}
 
Example #18
Source File: KV.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * retrieve keys with GetOption.
 *
 * @param  key    key in ByteSequence
 * @param  option GetOption
 * @return        GetResponse
 */
CompletableFuture<GetResponse> get(ByteSequence key, GetOption option);
 
Example #19
Source File: KV.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * retrieve value for the given key.
 *
 * @param  key key in ByteSequence
 * @return     GetResponse
 */
CompletableFuture<GetResponse> get(ByteSequence key);