Skip to content
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
8 changes: 8 additions & 0 deletions zookeeper-jute/src/main/resources/zookeeper.jute
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ module org.apache.zookeeper.proto {
buffer data;
org.apache.zookeeper.data.Stat stat;
}
class SyncedReadResponse {
buffer data;
org.apache.zookeeper.data.Stat stat;
}
class LinearizableReadResponse {
buffer data;
org.apache.zookeeper.data.Stat stat;
}
class GetChildrenResponse {
vector<ustring> children;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package org.apache.zookeeper;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.apache.yetus.audience.InterfaceAudience;
import org.apache.zookeeper.client.ZNode;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

Expand Down Expand Up @@ -349,3 +351,22 @@ interface EphemeralsCallback extends AsyncCallback {
}

}

//TODO
class DataFutureCallback implements AsyncCallback.DataCallback {

private final CompletableFuture<ZNode> future;

public DataFutureCallback(CompletableFuture<ZNode> future) {
this.future = future;
}

@Override
public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
if (rc != KeeperException.Code.OK.intValue()) {
future.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc)).fillInStackTrace());
} else {
future.complete(new ZNode(path, data, stat));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.apache.zookeeper.AsyncCallback.ChildrenCallback;
import org.apache.zookeeper.AsyncCallback.Create2Callback;
import org.apache.zookeeper.AsyncCallback.DataCallback;
import org.apache.zookeeper.DataFutureCallback;
import org.apache.zookeeper.AsyncCallback.EphemeralsCallback;
import org.apache.zookeeper.AsyncCallback.MultiCallback;
import org.apache.zookeeper.AsyncCallback.StatCallback;
Expand Down Expand Up @@ -82,12 +83,14 @@
import org.apache.zookeeper.proto.GetDataResponse;
import org.apache.zookeeper.proto.GetEphemeralsResponse;
import org.apache.zookeeper.proto.GetSASLRequest;
import org.apache.zookeeper.proto.LinearizableReadResponse;
import org.apache.zookeeper.proto.ReplyHeader;
import org.apache.zookeeper.proto.RequestHeader;
import org.apache.zookeeper.proto.SetACLResponse;
import org.apache.zookeeper.proto.SetDataResponse;
import org.apache.zookeeper.proto.SetWatches;
import org.apache.zookeeper.proto.SetWatches2;
import org.apache.zookeeper.proto.SyncedReadResponse;
import org.apache.zookeeper.proto.WatcherEvent;
import org.apache.zookeeper.server.ByteBufferInputStream;
import org.apache.zookeeper.server.ZooKeeperThread;
Expand Down Expand Up @@ -723,6 +726,14 @@ private void processEvent(Object event) {
} else {
cb.processResult(rc, p.ctx, null);
}
} else if (p.response instanceof SyncedReadResponse) {
DataFutureCallback cb = (DataFutureCallback) p.cb;
SyncedReadResponse rsp = (SyncedReadResponse) p.response;
cb.processResult(rc, clientPath, p.ctx, rsp.getData(), rsp.getStat());
} else if (p.response instanceof LinearizableReadResponse) {
DataFutureCallback cb = (DataFutureCallback) p.cb;
LinearizableReadResponse rsp = (LinearizableReadResponse) p.response;
cb.processResult(rc, clientPath, p.ctx, rsp.getData(), rsp.getStat());
} else if (p.cb instanceof VoidCallback) {
VoidCallback cb = (VoidCallback) p.cb;
cb.processResult(rc, clientPath, p.ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public interface OpCode {

int sync = 9;

// sync
int syncedRead = 10;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why using 10 ? isn't it better to use 109 ?


int ping = 11;

int getChildren2 = 12;
Expand Down Expand Up @@ -95,6 +98,9 @@ public interface OpCode {

int whoAmI = 107;

// linearable
int linearizableRead = 108;

int createSession = -10;

int closeSession = -11;
Expand Down
86 changes: 86 additions & 0 deletions zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import org.apache.jute.Record;
import org.apache.yetus.audience.InterfaceAudience;
import org.apache.zookeeper.AsyncCallback.ACLCallback;
Expand All @@ -44,8 +45,10 @@
import org.apache.zookeeper.Watcher.WatcherType;
import org.apache.zookeeper.client.ConnectStringParser;
import org.apache.zookeeper.client.HostProvider;
import org.apache.zookeeper.client.ReadConsistencyMode;
import org.apache.zookeeper.client.StaticHostProvider;
import org.apache.zookeeper.client.ZKClientConfig;
import org.apache.zookeeper.client.ZNode;
import org.apache.zookeeper.client.ZooKeeperSaslClient;
import org.apache.zookeeper.common.PathUtils;
import org.apache.zookeeper.data.ACL;
Expand All @@ -72,6 +75,7 @@
import org.apache.zookeeper.proto.GetDataResponse;
import org.apache.zookeeper.proto.GetEphemeralsRequest;
import org.apache.zookeeper.proto.GetEphemeralsResponse;
import org.apache.zookeeper.proto.LinearizableReadResponse;
import org.apache.zookeeper.proto.RemoveWatchesRequest;
import org.apache.zookeeper.proto.ReplyHeader;
import org.apache.zookeeper.proto.RequestHeader;
Expand All @@ -81,6 +85,7 @@
import org.apache.zookeeper.proto.SetDataResponse;
import org.apache.zookeeper.proto.SyncRequest;
import org.apache.zookeeper.proto.SyncResponse;
import org.apache.zookeeper.proto.SyncedReadResponse;
import org.apache.zookeeper.proto.WhoAmIResponse;
import org.apache.zookeeper.server.DataTree;
import org.apache.zookeeper.server.EphemeralType;
Expand Down Expand Up @@ -2026,6 +2031,87 @@ public void getData(final String path, Watcher watcher, DataCallback cb, Object
cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, wcb);
}

/**
* TODO
* @param path
* @param watcher
* @param readMode
* @return
* @throws KeeperException
* @throws InterruptedException
* @since
*/
public CompletableFuture<ZNode> getData(ReadConsistencyMode readMode, final String path, Watcher watcher) throws KeeperException, InterruptedException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CompletableFuture is tricky, because there is no control over the thread that execute any callback/listener

it is appealing but I won't use it here, all the API is based on callbacks that are guaranteed to be executed in the ZK thread

CompletableFuture<ZNode> future = new CompletableFuture<>();

switch (readMode) {
case SEQUENTIAL_READ:
Stat stat = new Stat();
byte[] data = getData(path, watcher, stat);
ZNode n = new ZNode(path, data, stat);
future.complete(n);
return future;
case ORDERED_SEQUENTIAL_READ:
return syncedRead(path, watcher);
case LINEARIZABLE_READ:
return linearizableRead(path, watcher);
default:
throw new IllegalArgumentException("Invalid Read Consistency Mode: " + readMode.getName());
}
}

private CompletableFuture<ZNode> syncedRead(final String path, Watcher watcher) {

final String clientPath = path;
PathUtils.validatePath(clientPath);

// the watch contains the un-chroot path
WatchRegistration wcb = null;
if (watcher != null) {
wcb = new DataWatchRegistration(watcher, clientPath);
}

final String serverPath = prependChroot(clientPath);

RequestHeader h = new RequestHeader();
h.setType(ZooDefs.OpCode.syncedRead);
GetDataRequest request = new GetDataRequest();
SyncedReadResponse response = new SyncedReadResponse();
request.setPath(serverPath);
request.setWatch(watcher != null);
CompletableFuture<ZNode> future = new CompletableFuture<>();
DataFutureCallback callback = new DataFutureCallback(future);
cnxn.queuePacket(h, new ReplyHeader(), request, response, callback, clientPath, serverPath, null, wcb);

return future;
}

private CompletableFuture<ZNode> linearizableRead(final String path, Watcher watcher) {

final String clientPath = path;
PathUtils.validatePath(clientPath);

// the watch contains the un-chroot path
WatchRegistration wcb = null;
if (watcher != null) {
wcb = new DataWatchRegistration(watcher, clientPath);
}

final String serverPath = prependChroot(clientPath);

RequestHeader h = new RequestHeader();
h.setType(ZooDefs.OpCode.linearizableRead);
GetDataRequest request = new GetDataRequest();
LinearizableReadResponse response = new LinearizableReadResponse();
request.setPath(serverPath);
request.setWatch(watcher != null);
CompletableFuture<ZNode> future = new CompletableFuture<>();
DataFutureCallback callback = new DataFutureCallback(future);
cnxn.queuePacket(h, new ReplyHeader(), request, response, callback, clientPath, serverPath, null, wcb);

return future;
}

/**
* The asynchronous version of getData.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.zookeeper.client;

import org.apache.yetus.audience.InterfaceAudience;

/**
* TODO
* @since 3.7.0
*/
@InterfaceAudience.Public
public enum ReadConsistencyMode {
SEQUENTIAL_READ("Sequential Read"),
ORDERED_SEQUENTIAL_READ("Ordered Sequential Read"),
LINEARIZABLE_READ("Linearizable Read"),
LEASE_READ("Lease Read"),
DUMMY_READ("Dummy Read Used For Testing");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should remove this DUMMY_READ mode


public static final ReadConsistencyMode DEFAULT_READ_MODE = SEQUENTIAL_READ;

private String name;

ReadConsistencyMode(String name) {
this.name = name;
}

public String getName() {
return name;
}

public static ReadConsistencyMode fromString(String name) {
for (ReadConsistencyMode c : values()) {
if (c.getName().compareToIgnoreCase(name) == 0) {
return c;
}
}
return DEFAULT_READ_MODE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.zookeeper.client;

import java.util.Arrays;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.yetus.audience.InterfaceAudience;
import org.apache.zookeeper.data.Stat;

/**
* TODO
* @since 3.7.0
*
*/
@InterfaceAudience.Public
@SuppressFBWarnings(value = {"EI_EXPOSE_REP", "EI_EXPOSE_REP2"}, justification = "The field:data is mutable object, " +
"but a clone of it may have performance issue")
public class ZNode {

private final String path;
private final byte[] data;
private final Stat stat;

public ZNode(String path, byte[] data, Stat stat) {
this.path = path;
this.data = data;//.clone()
this.stat = stat;
}

public String getPath() {
return path;
}

public byte[] getData() {
return data;//.clone()
}

public Stat getStat() {
return stat;
}

@Override
public String toString() {
return "ZNode{" +
"path='" + path + '\'' +
", data=" + Arrays.toString(data) +
", stat=" + stat +
'}';
}
}
Loading