-
Notifications
You must be signed in to change notification settings - Fork 7.3k
ZOOKEEPER-3600:[WIP]support the complete Linearizability Read #1137
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
@@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
| * | ||
|
|
||
| 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"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 + | ||
| '}'; | ||
| } | ||
| } |
There was a problem hiding this comment.
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 ?