Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose etcd index metadata in EtcdResponse #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 46 additions & 15 deletions src/main/java/com/justinsb/etcd/EtcdClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ public EtcdResult createDirectory(String key) throws EtcdClientException {
data.add(new BasicNameValuePair("dir", "true"));
return set0(key, data, new int[] { 200, 201 });
}

/**
* Lists a directory
*/
public List<EtcdNode> listDirectory(String key) throws EtcdClientException {
EtcdResult result = get(key + "/");
if (result == null || result.node == null) {
return null;
}
return result.node.nodes;
EtcdResult result = get(key + "/");
if (result == null || result.node == null) {
return null;
}
return result.node.nodes;
}
/**
* Delete a directory
Expand Down Expand Up @@ -156,13 +156,13 @@ public ListenableFuture<EtcdResult> watch(String key) throws EtcdClientException
* Watches the given subtree
*/
public ListenableFuture<EtcdResult> watch(String key, Long index, boolean recursive) throws EtcdClientException {
String suffix = "?wait=true";
if (index != null) {
suffix += "&waitIndex=" + index;
}
if (recursive) {
suffix += "&recursive=true";
}
String suffix = "?wait=true";
if (index != null) {
suffix += "&waitIndex=" + index;
}
if (recursive) {
suffix += "&recursive=true";
}
URI uri = buildKeyUri("v2/keys", key, suffix);

HttpGet request = new HttpGet(uri);
Expand Down Expand Up @@ -211,6 +211,7 @@ protected ListenableFuture<EtcdResult> asyncExecute(HttpUriRequest request, int[
throws EtcdClientException {
ListenableFuture<JsonResponse> json = asyncExecuteJson(request, expectedHttpStatusCodes);
return Futures.transform(json, new AsyncFunction<JsonResponse, EtcdResult>() {
@Override
public ListenableFuture<EtcdResult> apply(JsonResponse json) throws Exception {
EtcdResult result = jsonToEtcdResult(json, expectedErrorCodes);
return Futures.immediateFuture(result);
Expand Down Expand Up @@ -246,6 +247,10 @@ private EtcdResult jsonToEtcdResult(JsonResponse response, int... expectedErrorC
}
EtcdResult result = parseEtcdResult(response.json);

result.etcdIndex = response.etcdIndex;
result.raftIndex = response.raftIndex;
result.raftTerm = response.raftTerm;

if (result.isError()) {
if (!contains(expectedErrorCodes, result.errorCode)) {
throw new EtcdClientException(result.message, result);
Expand Down Expand Up @@ -330,6 +335,7 @@ protected ListenableFuture<JsonResponse> asyncExecuteJson(HttpUriRequest request
ListenableFuture<HttpResponse> response = asyncExecuteHttp(request);

return Futures.transform(response, new AsyncFunction<HttpResponse, JsonResponse>() {
@Override
public ListenableFuture<JsonResponse> apply(HttpResponse httpResponse) throws Exception {
JsonResponse json = extractJsonResponse(httpResponse, expectedHttpStatusCodes);
return Futures.immediateFuture(json);
Expand All @@ -343,10 +349,14 @@ public ListenableFuture<JsonResponse> apply(HttpResponse httpResponse) throws Ex
static class JsonResponse {
final String json;
final int httpStatusCode;
final long etcdIndex, raftIndex, raftTerm;

public JsonResponse(String json, int statusCode) {
public JsonResponse(String json, int statusCode, long etcdIndex, long raftIndex, long raftTerm) {
this.json = json;
this.httpStatusCode = statusCode;
this.etcdIndex = etcdIndex;
this.raftIndex = raftIndex;
this.raftTerm = raftTerm;
}

}
Expand Down Expand Up @@ -375,12 +385,30 @@ protected JsonResponse extractJsonResponse(HttpResponse httpResponse, int[] expe
}
}

return new JsonResponse(json, statusCode);
final long etcdIndex = parseLongHeader(httpResponse.getFirstHeader("X-Etcd-Index"));
final long raftIndex = parseLongHeader(httpResponse.getFirstHeader("X-Raft-Index"));
final long raftTerm = parseLongHeader(httpResponse.getFirstHeader("X-Raft-Term"));

return new JsonResponse(json, statusCode, etcdIndex, raftIndex, raftTerm);
} finally {
close(httpResponse);
}
}

private static long parseLongHeader(Header header)
{
return parseLongHeader(header, Long.MIN_VALUE);
}

private static long parseLongHeader(Header header, long dfl)
{
if (header == null) {
return dfl;
} else {
return Long.parseLong(header.getValue());
}
}

private URI buildKeyUri(String prefix, String key, String suffix) {
StringBuilder sb = new StringBuilder();
sb.append(prefix);
Expand All @@ -401,14 +429,17 @@ protected ListenableFuture<HttpResponse> asyncExecuteHttp(HttpUriRequest request
final SettableFuture<HttpResponse> future = SettableFuture.create();

httpClient.execute(request, new FutureCallback<HttpResponse>() {
@Override
public void completed(HttpResponse result) {
future.set(result);
}

@Override
public void failed(Exception ex) {
future.setException(ex);
}

@Override
public void cancelled() {
future.setException(new InterruptedException());
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/justinsb/etcd/EtcdClientException.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public EtcdClientException(String message, EtcdResult result) {
this.httpStatusCode = null;
this.result = result;
}

public int getHttpStatusCode() {
return httpStatusCode;
return httpStatusCode;
}

public boolean isHttpError(int httpStatusCode) {
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/justinsb/etcd/EtcdNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import java.util.List;

public class EtcdNode {
public String key;
public long createdIndex;
public long modifiedIndex;
public String value;
public String key;
public long createdIndex;
public long modifiedIndex;
public String value;

// For TTL keys
public String expiration;
public Integer ttl;
// For TTL keys
public String expiration;
public Integer ttl;

// For listings
public boolean dir;
public List<EtcdNode> nodes;
// For listings
public boolean dir;
public List<EtcdNode> nodes;

@Override
public String toString() {
return EtcdClient.format(this);
}
@Override
public String toString() {
return EtcdClient.format(this);
}
}
37 changes: 21 additions & 16 deletions src/main/java/com/justinsb/etcd/EtcdResult.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package com.justinsb.etcd;

public class EtcdResult {
// General values
public String action;
public EtcdNode node;
public EtcdNode prevNode;
// General values
public String action;
public EtcdNode node;
public EtcdNode prevNode;

// For errors
public Integer errorCode;
public String message;
public String cause;
public int errorIndex;
// For errors
public Integer errorCode;
public String message;
public String cause;
public int errorIndex;

public boolean isError() {
return errorCode != null;
}
// Server metadata
public long etcdIndex;
public long raftIndex;
public long raftTerm;

@Override
public String toString() {
return EtcdClient.format(this);
}
public boolean isError() {
return errorCode != null;
}

@Override
public String toString() {
return EtcdClient.format(this);
}
}
Loading