This is an automated email from the ASF dual-hosted git repository.
andor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zookeeper.git
The following commit(s) were added to refs/heads/master by this push:
new b2b06dc ZOOKEEPER-3994: Disconnect reason is wrong for
NOT_READ_ONLY_CLIENT and CLIENT_ZXID_AHEAD
b2b06dc is described below
commit b2b06dca8ecffcfaa35ba5797d3c8d82045cbe08
Author: huangwenbo04 <[email protected]>
AuthorDate: Mon Nov 16 16:37:16 2020 +0100
ZOOKEEPER-3994: Disconnect reason is wrong for NOT_READ_ONLY_CLIENT and
CLIENT_ZXID_AHEAD
Author: huangwenbo04 <[email protected]>
Reviewers: [email protected], [email protected]
Closes #1525 from nsnhuang/ZOOKEEPER-3994 and squashes the following
commits:
578c4d730 [huangwenbo04] checkstyle
aaa5430b9 [huangwenbo04] use assertThrows() method instead of try-catch
0c35eb487 [huangwenbo04] make ServerCnxn.CloseRequestException public
fb4a18eeb [huangwenbo04] add approved licenses And fix checkstyle errors
dfe0c576f [huangwenbo04] DisconnectReason.CLIENT_ZXID_AHEAD test case
406af2279 [huangwenbo04] add comments
2f4efae70 [huangwenbo04] add test case
61a815af6 [huangwenbo04] disconnect reason wrong
---
.../apache/zookeeper/server/ZooKeeperServer.java | 4 +-
.../zookeeper/server/ZooKeeperServerTest.java | 29 ++++++++++
.../server/quorum/ReadOnlyZooKeeperServerTest.java | 65 ++++++++++++++++++++++
3 files changed, 96 insertions(+), 2 deletions(-)
diff --git
a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServer.java
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServer.java
index 00d6ed8..f13f04b 100644
---
a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServer.java
+++
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServer.java
@@ -1394,7 +1394,7 @@ public class ZooKeeperServer implements SessionExpirer,
ServerStats.Provider {
if (!readOnly && this instanceof ReadOnlyZooKeeperServer) {
String msg = "Refusing session request for not-read-only client "
+ cnxn.getRemoteSocketAddress();
LOG.info(msg);
- throw new CloseRequestException(msg,
ServerCnxn.DisconnectReason.CLIENT_ZXID_AHEAD);
+ throw new CloseRequestException(msg,
ServerCnxn.DisconnectReason.NOT_READ_ONLY_CLIENT);
}
if (connReq.getLastZxidSeen() > zkDb.dataTree.lastProcessedZxid) {
String msg = "Refusing session request for client "
@@ -1406,7 +1406,7 @@ public class ZooKeeperServer implements SessionExpirer,
ServerStats.Provider {
+ " client must try another server";
LOG.info(msg);
- throw new CloseRequestException(msg,
ServerCnxn.DisconnectReason.NOT_READ_ONLY_CLIENT);
+ throw new CloseRequestException(msg,
ServerCnxn.DisconnectReason.CLIENT_ZXID_AHEAD);
}
int sessionTimeout = connReq.getTimeOut();
byte[] passwd = connReq.getPasswd();
diff --git
a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerTest.java
b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerTest.java
index 374ed5b..143938b 100644
---
a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerTest.java
+++
b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerTest.java
@@ -20,12 +20,16 @@ package org.apache.zookeeper.server;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
import java.io.File;
import java.io.IOException;
+import java.nio.ByteBuffer;
import java.util.List;
import org.apache.zookeeper.ZKTestCase;
import org.apache.zookeeper.server.persistence.FileTxnLog;
+import org.apache.zookeeper.server.persistence.FileTxnSnapLog;
import org.apache.zookeeper.server.persistence.SnapStream;
import org.apache.zookeeper.server.persistence.Util;
import org.apache.zookeeper.test.ClientBase;
@@ -135,4 +139,29 @@ public class ZooKeeperServerTest extends ZKTestCase {
}
}
+ @Test
+ public void testClientZxidAhead() {
+ ZooKeeperServer zooKeeperServer = new ZooKeeperServer();
+ final ZKDatabase zkDatabase = new
ZKDatabase(mock(FileTxnSnapLog.class));
+ zooKeeperServer.setZKDatabase(zkDatabase);
+
+ final ByteBuffer output = ByteBuffer.allocate(30);
+ // serialize a connReq
+ output.putInt(1);
+ // lastZxid
+ output.putLong(99L);
+ output.putInt(500);
+ output.putLong(123L);
+ output.putInt(1);
+ output.put((byte) 1);
+ output.put((byte) 1);
+ output.flip();
+
+ ServerCnxn.CloseRequestException e =
assertThrows(ServerCnxn.CloseRequestException.class, () -> {
+ final NIOServerCnxn nioServerCnxn = mock(NIOServerCnxn.class);
+ zooKeeperServer.processConnectRequest(nioServerCnxn, output);
+ });
+ assertEquals(e.getReason(),
ServerCnxn.DisconnectReason.CLIENT_ZXID_AHEAD);
+ }
+
}
diff --git
a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReadOnlyZooKeeperServerTest.java
b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReadOnlyZooKeeperServerTest.java
new file mode 100644
index 0000000..165dbdc
--- /dev/null
+++
b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReadOnlyZooKeeperServerTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.server.quorum;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import java.nio.ByteBuffer;
+import org.apache.zookeeper.server.NIOServerCnxn;
+import org.apache.zookeeper.server.ServerCnxn;
+import org.apache.zookeeper.server.ZKDatabase;
+import org.apache.zookeeper.server.ZooKeeperServer;
+import org.apache.zookeeper.server.persistence.FileTxnSnapLog;
+import org.junit.jupiter.api.Test;
+
+/**
+ * test ReadOnlyZooKeeperServer
+ */
+public class ReadOnlyZooKeeperServerTest {
+
+ /**
+ * test method {@link
ZooKeeperServer#processConnectRequest(org.apache.zookeeper.server.ServerCnxn,
java.nio.ByteBuffer)}
+ */
+ @Test
+ public void testReadOnlyZookeeperServer() {
+ ReadOnlyZooKeeperServer readOnlyZooKeeperServer = new
ReadOnlyZooKeeperServer(
+ mock(FileTxnSnapLog.class), mock(QuorumPeer.class),
mock(ZKDatabase.class));
+
+ final ByteBuffer output = ByteBuffer.allocate(30);
+ // serialize a connReq
+ output.putInt(1);
+ output.putLong(1L);
+ output.putInt(500);
+ output.putLong(123L);
+ output.putInt(1);
+ output.put((byte) 1);
+ // set readOnly false
+ output.put((byte) 0);
+ output.flip();
+
+ ServerCnxn.CloseRequestException e =
assertThrows(ServerCnxn.CloseRequestException.class, () -> {
+ final NIOServerCnxn nioServerCnxn = mock(NIOServerCnxn.class);
+ readOnlyZooKeeperServer.processConnectRequest(nioServerCnxn,
output);
+ });
+ assertEquals(e.getReason(),
ServerCnxn.DisconnectReason.NOT_READ_ONLY_CLIENT);
+ }
+
+
+}