io.etcd.jetcd.options.GetOption Java Examples

The following examples show how to use io.etcd.jetcd.options.GetOption. 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: EtcdServiceImpl.java    From kkbinlog with Apache License 2.0 6 votes vote down vote up
/**
 * @return
 */
@Override
public List<ServiceStatus> getServiceStatus() {

    KV client = etcdClient.getKVClient();
    List<ServiceStatus> serviceStatuses = null;
    try {

        ByteSequence prefix = ByteSequence.from(etcdKeyPrefixUtil.withPrefix(Constants.SERVICE_STATUS_PATH), StandardCharsets.UTF_8);

        serviceStatuses = client.get(prefix,GetOption.newBuilder().withPrefix(prefix).build())
                .get().getKvs()
                .stream()
                .map(kv -> JSON.parseObject(
                        new String(kv.getValue().getBytes()),
                        ServiceStatus.class)
                )
                .collect(Collectors.toList());
    } catch (InterruptedException | ExecutionException e) {
        logger.warn("Get service status error.", e);
    }

    return serviceStatuses;
}
 
Example #2
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 #3
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 #4
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 #5
Source File: EtcdCenterRepositoryTest.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
@SneakyThrows({InterruptedException.class, ExecutionException.class})
@SuppressWarnings("unchecked")
private Client mockClient() {
    when(client.getKVClient()).thenReturn(kv);
    when(kv.get(any(ByteSequence.class))).thenReturn(getFuture);
    when(kv.get(any(ByteSequence.class), any(GetOption.class))).thenReturn(getFuture);
    when(kv.put(any(ByteSequence.class), any(ByteSequence.class))).thenReturn(putFuture);
    when(kv.put(any(ByteSequence.class), any(ByteSequence.class), any(PutOption.class))).thenReturn(putFuture);
    when(getFuture.get()).thenReturn(getResponse);
    when(client.getLeaseClient()).thenReturn(lease);
    when(lease.grant(anyLong())).thenReturn(leaseFuture);
    when(leaseFuture.get()).thenReturn(leaseGrantResponse);
    when(leaseGrantResponse.getID()).thenReturn(123L);
    when(client.getWatchClient()).thenReturn(watch);
    return client;
}
 
Example #6
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 #7
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 #8
Source File: EtcdCenterRepository.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@SneakyThrows({InterruptedException.class, ExecutionException.class})
@Override
public List<String> getChildrenKeys(final String key) {
    String prefix = key + "/";
    ByteSequence prefixByteSequence = ByteSequence.from(prefix, Charsets.UTF_8);
    GetOption getOption = GetOption.newBuilder().withPrefix(prefixByteSequence).withSortField(GetOption.SortTarget.KEY).withSortOrder(GetOption.SortOrder.ASCEND).build();
    List<KeyValue> keyValues = client.getKVClient().get(prefixByteSequence, getOption).get().getKvs();
    return keyValues.stream().map(e -> getSubNodeKeyName(prefix, e.getKey().toString(Charsets.UTF_8))).distinct().collect(Collectors.toList());
}
 
Example #9
Source File: EtcdRegistry.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<Void> doSubscribe(final ClusterBooking booking) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    EtcdClusterBooking etcdBooking = (EtcdClusterBooking) booking;
    //先查询
    ByteSequence key = ByteSequence.from(etcdBooking.getPath(), UTF_8);
    //先查询,无异常后添加watcher,若结果不为空,通知FULL事件
    GetOption getOption = GetOption.newBuilder().withPrefix(key).build();
    client.getKVClient().get(key, getOption).whenComplete((res, err) -> {
        if (!isOpen()) {
            future.completeExceptionally(new IllegalStateException("controller is closed."));
        } else if (err != null) {
            logger.error(String.format("Error occurs while subscribe of %s, caused by %s. retry....", etcdBooking.getService(), err.getMessage()), err);
            future.completeExceptionally(err);
        } else {
            List<WatchEvent> events = new ArrayList<>();
            res.getKvs().forEach(kv -> events.add(new WatchEvent(kv, null, PUT)));
            etcdBooking.onUpdate(events, res.getHeader().getRevision(), FULL);
            //添加watch
            try {
                WatchOption watchOption = WatchOption.newBuilder().withPrefix(key).build();
                Watch.Watcher watcher = client.getWatchClient().watch(key, watchOption, etcdBooking);
                etcdBooking.setWatcher(watcher);
                future.complete(null);
            } catch (Exception e) {
                logger.error(String.format("Error occurs while subscribe of %s, caused by %s. retry....", etcdBooking.getService(), e.getMessage()), e);
                future.completeExceptionally(e);
            }
        }
    });
    return future;
}
 
Example #10
Source File: ElectionTest.java    From jetcd with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetryCampaignWithDifferentValue() throws Exception {
    ByteSequence electionName = ByteSequence.from(randomString(), StandardCharsets.UTF_8);
    long leaseId = leaseClient.grant(10).get().getID();

    ByteSequence firstProposal = ByteSequence.from("proposal1", StandardCharsets.UTF_8);
    CampaignResponse campaignResponse1 = electionClient.campaign(electionName, leaseId, firstProposal)
        .get(OPERATION_TIMEOUT, TimeUnit.SECONDS);

    ByteSequence secondProposal = ByteSequence.from("proposal2", StandardCharsets.UTF_8);
    CampaignResponse campaignResponse2 = electionClient.campaign(electionName, leaseId, secondProposal)
        .get(OPERATION_TIMEOUT, TimeUnit.SECONDS);

    // check that for sure we are the leader
    LeaderResponse leaderResponse = electionClient.leader(electionName).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    assertThat(leaderResponse.getKv().getKey().getByteString()).isEqualTo(campaignResponse1.getLeader().getKey());
    assertThat(campaignResponse1.getLeader().getKey()).isEqualTo(campaignResponse2.getLeader().getKey());
    assertThat(campaignResponse1.getLeader().getRevision()).isEqualTo(campaignResponse2.getLeader().getRevision());

    // latest proposal should be persisted
    GetOption getOption = GetOption.newBuilder().withPrefix(electionName).build();
    List<KeyValue> keys = kvClient.get(electionName, getOption).get().getKvs();
    assertThat(keys.size()).isEqualTo(1);
    assertThat(keys.get(0).getValue()).isEqualTo(secondProposal);

    electionClient.resign(campaignResponse1.getLeader()).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    leaseClient.revoke(leaseId).get();
}
 
Example #11
Source File: ElectionTest.java    From jetcd with Apache License 2.0 5 votes vote down vote up
@Test
public void testProposeValueNotBeingLeader() throws Exception {
    ByteSequence electionName = ByteSequence.from(randomString(), StandardCharsets.UTF_8);
    LeaderKey leaderKey = new LeaderKey(electionName.getByteString(), randomByteSequence().getByteString(), 1, 1);
    ByteSequence proposal = ByteSequence.from("proposal", StandardCharsets.UTF_8);
    try {
        electionClient.proclaim(leaderKey, proposal).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
        fail("Cannot proclaim proposal not being a leader");
    } catch (ExecutionException e) {
        assertThat(e.getCause()).isInstanceOf(NotLeaderException.class);
    }
    GetOption getOption = GetOption.newBuilder().withPrefix(electionName).build();
    List<KeyValue> keys = kvClient.get(electionName, getOption).get().getKvs();
    assertThat(keys).isEmpty();
}
 
Example #12
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 #13
Source File: Op.java    From jetcd with Apache License 2.0 4 votes vote down vote up
public static GetOp get(ByteSequence key, GetOption option) {
    return new GetOp(ByteString.copyFrom(key.getBytes()), option);
}
 
Example #14
Source File: Op.java    From jetcd with Apache License 2.0 4 votes vote down vote up
protected GetOp(ByteString key, GetOption option) {
    super(Type.RANGE, key);
    this.option = option;
}
 
Example #15
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 #16
Source File: ElectionTest.java    From jetcd with Apache License 2.0 4 votes vote down vote up
@Test
public void testIsolatedElection() throws Exception {
    ByteSequence electionName = ByteSequence.from(randomString(), StandardCharsets.UTF_8);

    // register lease
    long leaseId = leaseClient.grant(10).get().getID();

    // start new campaign
    ByteSequence firstProposal = ByteSequence.from("proposal1", StandardCharsets.UTF_8);
    CampaignResponse campaignResponse = electionClient.campaign(electionName, leaseId, firstProposal)
        .get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    assertThat(campaignResponse.getLeader()).isNotNull();
    assertThat(campaignResponse.getLeader().getLease()).isEqualTo(leaseId);
    assertThat(campaignResponse.getLeader().getName()).isEqualTo(electionName.getByteString());
    // election is backed by standard key in etcd. let us examine it
    GetOption getOption = GetOption.newBuilder().withPrefix(electionName).build();
    List<KeyValue> keys = kvClient.get(electionName, getOption).get().getKvs();
    assertThat(keys.size()).isEqualTo(1);
    assertThat(keys.get(0).getKey().getByteString()).isEqualTo(campaignResponse.getLeader().getKey());
    assertThat(keys.get(0).getValue()).isEqualTo(firstProposal);

    // check that we really are the leader (just to test API)
    LeaderResponse leaderResponse = electionClient.leader(electionName)
        .get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    assertThat(leaderResponse.getKv().getKey().getByteString()).isEqualTo(campaignResponse.getLeader().getKey());
    assertThat(leaderResponse.getKv().getValue()).isEqualTo(firstProposal);
    assertThat(leaderResponse.getKv().getLease()).isEqualTo(leaseId);
    assertThat(leaderResponse.getKv().getCreateRevision()).isEqualTo(campaignResponse.getLeader().getRevision());

    // as a leader change your proposal
    ByteSequence secondProposal = ByteSequence.from("proposal2", StandardCharsets.UTF_8);
    electionClient.proclaim(campaignResponse.getLeader(), secondProposal).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    keys = kvClient.get(electionName, getOption).get().getKvs();
    assertThat(keys.size()).isEqualTo(1);
    assertThat(keys.get(0).getValue()).isEqualTo(secondProposal);

    // finally resign
    electionClient.resign(campaignResponse.getLeader()).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    keys = kvClient.get(electionName, getOption).get().getKvs();
    assertThat(keys).isEmpty();

    leaseClient.revoke(leaseId).get();
}
 
Example #17
Source File: ElectionTest.java    From jetcd with Apache License 2.0 4 votes vote down vote up
@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 = Client.builder().endpoints(cluster.getClientEndpoints()).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().withPrefix(electionName).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();
    }
}
 
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);