Browse Source

ZOOKEEPER-3416: Remove redundant ServerCnxnFactoryAccessor.

We have two ways to access the private zkServer inside ServerCnxnFactory, and there is really no need to keep maintaining both. We could remove ServerCnxnFactoryAccessor when we added the public accessor for ServerCnxnFactory in ZOOKEEPER-1346, but we did not.

The solution is to consolidate all access of the zkServer through the public accessor of ServerCnxnFactory. The end result is cleaner code base and less confusion.

Author: Michael Han <lhan@twitter.com>

Reviewers: Enrico Olivelli <eolivelli@apache.org>

Closes #969 from hanm/twitter/91216a8821e3aac9ab56f8cc51bd30d425eebae8
Michael Han 6 năm trước cách đây
mục cha
commit
89e7e55811

+ 1 - 1
zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerCnxnFactory.java

@@ -94,7 +94,7 @@ public abstract class ServerCnxnFactory {
         return cnxns.size();
     }
 
-    public ZooKeeperServer getZooKeeperServer() {
+    public final ZooKeeperServer getZooKeeperServer() {
         return zkServer;
     }
 

+ 1 - 1
zookeeper-server/src/test/java/org/apache/zookeeper/RemoveWatchesTest.java

@@ -1249,7 +1249,7 @@ public class RemoveWatchesTest extends ClientBase {
         CollectionUtils.addAll(cnxns, serverFactory.getConnections().iterator());
         for (ServerCnxn cnxn : cnxns) {
             if (cnxn.getSessionId() == sessionId) {
-                return getServer(serverFactory).getZKDatabase().getDataTree()
+                return serverFactory.getZooKeeperServer().getZKDatabase().getDataTree()
                         .containsWatcher(path, type, cnxn);
             }
         }

+ 1 - 1
zookeeper-server/src/test/java/org/apache/zookeeper/server/NettyServerCnxnTest.java

@@ -78,7 +78,7 @@ public class NettyServerCnxnTest extends ClientBase {
                 serverFactory instanceof NettyServerCnxnFactory);
 
         final ZooKeeper zk = createClient();
-        final ZooKeeperServer zkServer = getServer(serverFactory);
+        final ZooKeeperServer zkServer = serverFactory.getZooKeeperServer();
         final String path = "/a";
         try {
             // make sure zkclient works

+ 0 - 24
zookeeper-server/src/test/java/org/apache/zookeeper/server/ServerCnxnFactoryAccessor.java

@@ -1,24 +0,0 @@
-/**
- * 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;
-
-public class ServerCnxnFactoryAccessor {
-    public static ZooKeeperServer getZkServer(ServerCnxnFactory fac) {
-	return fac.zkServer;
-    }
-}

+ 1 - 10
zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java

@@ -54,11 +54,9 @@ import org.apache.zookeeper.ZKTestCase;
 import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.common.IOUtils;
 import org.apache.zookeeper.server.ServerCnxnFactory;
-import org.apache.zookeeper.server.ServerCnxnFactoryAccessor;
 import org.apache.zookeeper.server.ZKDatabase;
 import org.apache.zookeeper.server.ZooKeeperServer;
 import org.apache.zookeeper.server.persistence.FilePadding;
-import org.apache.zookeeper.server.persistence.FileTxnLog;
 import org.apache.zookeeper.server.quorum.QuorumPeer;
 import org.apache.zookeeper.server.util.OSMXBean;
 import org.junit.After;
@@ -460,7 +458,7 @@ public abstract class ClientBase extends ZKTestCase {
         if (factory != null) {
             ZKDatabase zkDb = null;
             {
-                ZooKeeperServer zs = getServer(factory);
+                ZooKeeperServer zs = factory.getZooKeeperServer();
                 if (zs != null) {
                     zkDb = zs.getZKDatabase();
                 }
@@ -589,13 +587,6 @@ public abstract class ClientBase extends ZKTestCase {
         JMXEnv.ensureOnly();
     }
 
-
-    protected static ZooKeeperServer getServer(ServerCnxnFactory fac) {
-        ZooKeeperServer zs = ServerCnxnFactoryAccessor.getZkServer(fac);
-
-        return zs;
-    }
-
     protected void tearDownAll() throws Exception {
         synchronized (this) {
             if (allClients != null) for (ZooKeeper zk : allClients) {

+ 4 - 4
zookeeper-server/src/test/java/org/apache/zookeeper/test/LoadFromLogTest.java

@@ -180,7 +180,7 @@ public class LoadFromLogTest extends ClientBase {
         String expectedPath = "/invalidsnap/test-"
                 + String.format("%010d",
                 (Integer.parseInt(tokens[1])) + 1);
-        ZooKeeperServer zks = getServer(serverFactory);
+        ZooKeeperServer zks = serverFactory.getZooKeeperServer();
         long eZxid = zks.getZKDatabase().getDataTreeLastProcessedZxid();
         // force the zxid to be behind the content
         zks.getZKDatabase().setlastProcessedZxid(
@@ -193,7 +193,7 @@ public class LoadFromLogTest extends ClientBase {
         stopServer();
 
         startServer();
-        zks = getServer(serverFactory);
+        zks = serverFactory.getZooKeeperServer();
         long fZxid = zks.getZKDatabase().getDataTreeLastProcessedZxid();
 
         // Verify lastProcessedZxid is set correctly
@@ -243,7 +243,7 @@ public class LoadFromLogTest extends ClientBase {
         }
 
         // force the zxid to be behind the content
-        ZooKeeperServer zks = getServer(serverFactory);
+        ZooKeeperServer zks = serverFactory.getZooKeeperServer();
         zks.getZKDatabase().setlastProcessedZxid(
                 zks.getZKDatabase().getDataTreeLastProcessedZxid() - 10);
         LOG.info("Set lastProcessedZxid to "
@@ -296,7 +296,7 @@ public class LoadFromLogTest extends ClientBase {
         zk.delete("/a/b", -1);
         zk.delete("/a", -1);
         // force the zxid to be behind the content
-        ZooKeeperServer zks = getServer(serverFactory);
+        ZooKeeperServer zks = serverFactory.getZooKeeperServer();
         zks.getZKDatabase().setlastProcessedZxid(createZxId);
         LOG.info("Set lastProcessedZxid to {}", zks.getZKDatabase()
                 .getDataTreeLastProcessedZxid());

+ 1 - 1
zookeeper-server/src/test/java/org/apache/zookeeper/test/ResponseCacheTest.java

@@ -64,7 +64,7 @@ public class ResponseCacheTest extends ClientBase {
         long expectedHits = 0;
         long expectedMisses = 0;
 
-        getServer(serverFactory).setResponseCachingEnabled(useCache);
+        serverFactory.getZooKeeperServer().setResponseCachingEnabled(useCache);
         LOG.info("caching: {}", useCache);
 
         byte[] writeData = "test1".getBytes();

+ 1 - 1
zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionTest.java

@@ -362,7 +362,7 @@ public class SessionTest extends ZKTestCase {
         final int MINSESS = 20000;
         final int MAXSESS = 240000;
         {
-            ZooKeeperServer zs = ClientBase.getServer(serverFactory);
+            ZooKeeperServer zs = serverFactory.getZooKeeperServer();
             zs.setMinSessionTimeout(MINSESS);
             zs.setMaxSessionTimeout(MAXSESS);
         }

+ 1 - 1
zookeeper-server/src/test/java/org/apache/zookeeper/test/TruncateTest.java

@@ -172,7 +172,7 @@ public class TruncateTest extends ZKTestCase {
         
         ZKDatabase zkDb;
         {
-            ZooKeeperServer zs = ClientBase.getServer(factory);
+            ZooKeeperServer zs = factory.getZooKeeperServer();
     
             zkDb = zs.getZKDatabase();
         }

+ 1 - 1
zookeeper-server/src/test/java/org/apache/zookeeper/test/WatcherTest.java

@@ -213,7 +213,7 @@ public class WatcherTest extends ClientBase {
             zk1.getData("/watch-count-test-2", w1, stat);
             zk2.getData("/watch-count-test", w2, stat);
 
-            Assert.assertEquals(ClientBase.getServer(serverFactory)
+            Assert.assertEquals(serverFactory.getZooKeeperServer()
                     .getZKDatabase().getDataTree().getWatchCount(), 3);
 
         } finally {

+ 1 - 1
zookeeper-server/src/test/java/org/apache/zookeeper/test/ZooKeeperQuotaTest.java

@@ -72,7 +72,7 @@ public class ZooKeeperQuotaTest extends ClientBase {
         startServer();
         stopServer();
         startServer();
-        ZooKeeperServer server = getServer(serverFactory);
+        ZooKeeperServer server = serverFactory.getZooKeeperServer();
         Assert.assertNotNull("Quota is still set",
             server.getZKDatabase().getDataTree().getMaxPrefixWithQuota(path) != null);
     }