Author: fpj
Date: Wed Sep 18 10:24:00 2013
New Revision: 1524355
URL: http://svn.apache.org/r1524355
Log:
ZOOKEEPER-1657. Increased CPU usage by unnecessary SASL checks (Philip K. Warren via fpj)
Added:
zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/SaslClientTest.java
Modified:
zookeeper/branches/branch-3.4/CHANGES.txt
zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/ClientCnxn.java
zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/client/ZooKeeperSaslClient.java
Modified: zookeeper/branches/branch-3.4/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/CHANGES.txt?rev=1524355&r1=1524354&r2=1524355&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/CHANGES.txt (original)
+++ zookeeper/branches/branch-3.4/CHANGES.txt Wed Sep 18 10:24:00 2013
@@ -102,6 +102,8 @@ BUGFIXES:
ZOOKEEPER-1751. ClientCnxn#run could miss the second ping or connection get
dropped before a ping. (Jeffrey Zhong via mahadev)
+ ZOOKEEPER-1657. Increased CPU usage by unnecessary SASL checks (Philip K. Warren via fpj)
+
IMPROVEMENTS:
ZOOKEEPER-1564. Allow JUnit test build with IBM Java
Modified: zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/ClientCnxn.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/ClientCnxn.java?rev=1524355&r1=1524354&r2=1524355&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/ClientCnxn.java (original)
+++ zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/ClientCnxn.java Wed Sep
18 10:24:00 2013
@@ -939,19 +939,21 @@ public class ClientCnxn {
setName(getName().replaceAll("\\(.*\\)",
"(" + addr.getHostName() + ":" + addr.getPort() + ")"));
- try {
- zooKeeperSaslClient = new ZooKeeperSaslClient("zookeeper/"+addr.getHostName());
- } catch (LoginException e) {
- // An authentication error occurred when the SASL client tried to initialize:
- // for Kerberos this means that the client failed to authenticate with the
KDC.
- // This is different from an authentication error that occurs during communication
- // with the Zookeeper server, which is handled below.
- LOG.warn("SASL configuration failed: " + e + " Will continue connection to
Zookeeper server without "
- + "SASL authentication, if Zookeeper server allows it.");
- eventThread.queueEvent(new WatchedEvent(
- Watcher.Event.EventType.None,
- Watcher.Event.KeeperState.AuthFailed, null));
- saslLoginFailed = true;
+ if (ZooKeeperSaslClient.isEnabled()) {
+ try {
+ zooKeeperSaslClient = new ZooKeeperSaslClient("zookeeper/"+addr.getHostName());
+ } catch (LoginException e) {
+ // An authentication error occurred when the SASL client tried to initialize:
+ // for Kerberos this means that the client failed to authenticate with
the KDC.
+ // This is different from an authentication error that occurs during
communication
+ // with the Zookeeper server, which is handled below.
+ LOG.warn("SASL configuration failed: " + e + " Will continue connection
to Zookeeper server without "
+ + "SASL authentication, if Zookeeper server allows it.");
+ eventThread.queueEvent(new WatchedEvent(
+ Watcher.Event.EventType.None,
+ Watcher.Event.KeeperState.AuthFailed, null));
+ saslLoginFailed = true;
+ }
}
logStartConnect(addr);
@@ -1230,18 +1232,23 @@ public class ClientCnxn {
}
public boolean clientTunneledAuthenticationInProgress() {
- // 1. SASL login failed.
+ // 1. SASL client is disabled.
+ if (!ZooKeeperSaslClient.isEnabled()) {
+ return false;
+ }
+
+ // 2. SASL login failed.
if (saslLoginFailed == true) {
return false;
}
- // 2. SendThread has not created the authenticating object yet,
+ // 3. SendThread has not created the authenticating object yet,
// therefore authentication is (at the earliest stage of being) in progress.
if (zooKeeperSaslClient == null) {
return true;
}
- // 3. authenticating object exists, so ask it for its progress.
+ // 4. authenticating object exists, so ask it for its progress.
return zooKeeperSaslClient.clientTunneledAuthenticationInProgress();
}
Modified: zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/client/ZooKeeperSaslClient.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/client/ZooKeeperSaslClient.java?rev=1524355&r1=1524354&r2=1524355&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/client/ZooKeeperSaslClient.java
(original)
+++ zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/client/ZooKeeperSaslClient.java
Wed Sep 18 10:24:00 2013
@@ -62,6 +62,21 @@ import org.slf4j.LoggerFactory;
*/
public class ZooKeeperSaslClient {
public static final String LOGIN_CONTEXT_NAME_KEY = "zookeeper.sasl.clientconfig";
+ public static final String ENABLE_CLIENT_SASL_KEY = "zookeeper.sasl.client";
+ public static final String ENABLE_CLIENT_SASL_DEFAULT = "true";
+
+ /**
+ * Returns true if the SASL client is enabled. By default, the client
+ * is enabled but can be disabled by setting the system property
+ * <code>zookeeper.sasl.client</code> to <code>false</code>.
See
+ * ZOOKEEPER-1657 for more information.
+ *
+ * @return If the SASL client is enabled.
+ */
+ public static boolean isEnabled() {
+ return Boolean.valueOf(System.getProperty(ENABLE_CLIENT_SASL_KEY, ENABLE_CLIENT_SASL_DEFAULT));
+ }
+
private static final Logger LOG = LoggerFactory.getLogger(ZooKeeperSaslClient.class);
private static Login login = null;
private SaslClient saslClient;
Added: zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/SaslClientTest.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/SaslClientTest.java?rev=1524355&view=auto
==============================================================================
--- zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/SaslClientTest.java
(added)
+++ zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/SaslClientTest.java
Wed Sep 18 10:24:00 2013
@@ -0,0 +1,62 @@
+/**
+ * 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.test;
+
+import org.apache.zookeeper.ZKTestCase;
+import org.apache.zookeeper.client.ZooKeeperSaslClient;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+
+public class SaslClientTest extends ZKTestCase {
+
+ private String existingPropertyValue = null;
+
+ @Before
+ public void setUp() {
+ existingPropertyValue = System.getProperty(ZooKeeperSaslClient.ENABLE_CLIENT_SASL_KEY);
+ }
+
+ @After
+ public void tearDown() {
+ // Restore the System property if it was set previously
+ if (existingPropertyValue != null) {
+ System.setProperty(ZooKeeperSaslClient.ENABLE_CLIENT_SASL_KEY, existingPropertyValue);
+ }
+ }
+
+ @Test
+ public void testSaslClientDisabled() {
+ System.clearProperty(ZooKeeperSaslClient.ENABLE_CLIENT_SASL_KEY);
+ Assert.assertTrue("SASL client disabled", ZooKeeperSaslClient.isEnabled());
+
+ for (String value : Arrays.asList("true", "TRUE")) {
+ System.setProperty(ZooKeeperSaslClient.ENABLE_CLIENT_SASL_KEY, value);
+ Assert.assertTrue("SASL client disabled", ZooKeeperSaslClient.isEnabled());
+ }
+
+ for (String value : Arrays.asList("false", "FALSE")) {
+ System.setProperty(ZooKeeperSaslClient.ENABLE_CLIENT_SASL_KEY, value);
+ Assert.assertFalse("SASL client disabled", ZooKeeperSaslClient.isEnabled());
+ }
+ }
+}
|