Skip to content

Commit

Permalink
api: add HashKV and test
Browse files Browse the repository at this point in the history
  • Loading branch information
fanminshi committed Feb 9, 2018
1 parent 5a593f3 commit bcc3048
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
9 changes: 9 additions & 0 deletions jetcd-core/src/main/java/com/coreos/jetcd/Maintenance.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.coreos.jetcd.maintenance.AlarmMember;
import com.coreos.jetcd.maintenance.AlarmResponse;
import com.coreos.jetcd.maintenance.DefragmentResponse;
import com.coreos.jetcd.maintenance.HashKVResponse;
import com.coreos.jetcd.maintenance.MoveLeaderResponse;
import com.coreos.jetcd.maintenance.StatusResponse;
import java.io.Closeable;
Expand Down Expand Up @@ -83,6 +84,14 @@ public interface Maintenance extends CloseableClient {
*/
CompletableFuture<StatusResponse> statusMember(String endpoint);


/**
* returns a hash of the KV state at the time of the RPC.
* If revision is zero, the hash is computed on all keys. If the revision
* is non-zero, the hash is computed on all keys at or below the given revision.
*/
CompletableFuture<HashKVResponse> hashKV(String endpoint, long rev);

/**
* retrieves backend snapshot.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.coreos.jetcd.api.AlarmRequest;
import com.coreos.jetcd.api.AlarmType;
import com.coreos.jetcd.api.DefragmentRequest;
import com.coreos.jetcd.api.HashKVRequest;
import com.coreos.jetcd.api.MaintenanceGrpc;
import com.coreos.jetcd.api.MoveLeaderRequest;
import com.coreos.jetcd.api.SnapshotRequest;
Expand All @@ -34,6 +35,7 @@
import com.coreos.jetcd.exception.ErrorCode;
import com.coreos.jetcd.maintenance.AlarmResponse;
import com.coreos.jetcd.maintenance.DefragmentResponse;
import com.coreos.jetcd.maintenance.HashKVResponse;
import com.coreos.jetcd.maintenance.MoveLeaderResponse;
import com.coreos.jetcd.maintenance.SnapshotReaderResponseWithError;
import com.coreos.jetcd.maintenance.StatusResponse;
Expand Down Expand Up @@ -156,6 +158,19 @@ public CompletableFuture<StatusResponse> statusMember(
);
}

@Override
public CompletableFuture<HashKVResponse> hashKV(String endpoint, long rev) {
return this.connectionManager.withNewChannel(
endpoint,
MaintenanceGrpc::newFutureStub,
stub -> Util.toCompletableFuture(
stub.hashKV(HashKVRequest.newBuilder().setRevision(rev).build()),
HashKVResponse::new,
this.connectionManager.getExecutorService()
)
);
}

@Override
public Snapshot snapshot() {
SnapshotImpl snapshot = new SnapshotImpl();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright 2017 The jetcd authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.coreos.jetcd.maintenance;

import com.coreos.jetcd.Maintenance;
import com.coreos.jetcd.data.AbstractResponse;

/**
* HashKVResponse returned by {@link Maintenance#HashKV(String, long)}.
*/
public class HashKVResponse extends AbstractResponse<com.coreos.jetcd.api.HashKVResponse> {

public HashKVResponse(com.coreos.jetcd.api.HashKVResponse response) {
super(response, response.getHeader());
}

/**
* return the hash value computed from the responding member's MVCC keys up to a given revision.
*/
public int getHash() {
return getResponse().getHash();
}

/**
* return compact_revision is the compacted revision of key-value store when hash begins.
*/
public long getCompacted() {
return getResponse().getCompactRevision();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public void testNnapshot() throws IOException {
}
}

@Test
public void testHashKV() throws ExecutionException, InterruptedException {
maintenance.hashKV(TestConstants.endpoints[0], 0).get();
}

/**
* test alarm list function
* TODO trigger alarm, valid whether listAlarms will work.
Expand Down

0 comments on commit bcc3048

Please sign in to comment.