You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by fp...@apache.org on 2012/12/01 15:04:30 UTC

svn commit: r1416000 - in /zookeeper/bookkeeper/trunk: CHANGES.txt bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java bookkeeper-server/src/test/java/org/apache/bookkeeper/proto/TestPerChannelBookieClient.java

Author: fpj
Date: Sat Dec  1 14:04:29 2012
New Revision: 1416000

URL: http://svn.apache.org/viewvc?rev=1416000&view=rev
Log:
BOOKKEEPER-485: TestFencing hung (ivank via fpj)

--THIS line, and those below, will be ignored--

M    CHANGES.txt
A    bookkeeper-server/src/test/java/org/apache/bookkeeper/proto/TestPerChannelBookieClient.java
M    bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java

Added:
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/proto/TestPerChannelBookieClient.java
Modified:
    zookeeper/bookkeeper/trunk/CHANGES.txt
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java

Modified: zookeeper/bookkeeper/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/CHANGES.txt?rev=1416000&r1=1415999&r2=1416000&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/CHANGES.txt (original)
+++ zookeeper/bookkeeper/trunk/CHANGES.txt Sat Dec  1 14:04:29 2012
@@ -110,6 +110,8 @@ Trunk (unreleased changes)
 
 	BOOKKEEPER-477: In ReadOnlyBookieTest, we should wait for the bookie to die before asserting on it (ivank via fpj)
 
+	BOOKKEEPER-485: TestFencing hung (ivank via fpj)
+
       hedwig-protocol:
 
         BOOKKEEPER-394: CompositeException message is not useful (Stu Hood via sijie)

Modified: zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java?rev=1416000&r1=1415999&r2=1416000&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java (original)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java Sat Dec  1 14:04:29 2012
@@ -115,7 +115,7 @@ public class PerChannelBookieClient exte
     }
 
     private void connect() {
-        LOG.debug("Connecting to bookie: {}", addr);
+        LOG.info("Connecting to bookie: {}", addr);
 
         // Set up the ClientBootStrap so we can create a new Channel connection
         // to the bookie.
@@ -134,11 +134,16 @@ public class PerChannelBookieClient exte
 
                 synchronized (PerChannelBookieClient.this) {
 
-                    if (future.isSuccess()) {
+                    if (future.isSuccess() && state == ConnectionState.CONNECTING) {
                         LOG.info("Successfully connected to bookie: " + addr);
                         rc = BKException.Code.OK;
                         channel = future.getChannel();
                         state = ConnectionState.CONNECTED;
+                    } else if (future.isSuccess() && state == ConnectionState.DISCONNECTED) {
+                        LOG.error("Closed before connection completed, clean up: " + addr);
+                        future.getChannel().close();
+                        rc = BKException.Code.BookieHandleNotAvailableException;
+                        channel = null;
                     } else {
                         LOG.error("Could not connect to bookie: " + addr);
                         rc = BKException.Code.BookieHandleNotAvailableException;
@@ -334,6 +339,9 @@ public class PerChannelBookieClient exte
     }
 
     public void close() {
+        synchronized (this) {
+            state = ConnectionState.DISCONNECTED;
+        }
         if (channel != null) {
             channel.close().awaitUninterruptibly();
         }

Added: zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/proto/TestPerChannelBookieClient.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/proto/TestPerChannelBookieClient.java?rev=1416000&view=auto
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/proto/TestPerChannelBookieClient.java (added)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/proto/TestPerChannelBookieClient.java Sat Dec  1 14:04:29 2012
@@ -0,0 +1,81 @@
+package org.apache.bookkeeper.proto;
+
+/*
+ *
+ * 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.
+ *
+ */
+
+import org.junit.*;
+import java.net.InetSocketAddress;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.Executors;
+
+import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback;
+import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
+import org.apache.bookkeeper.util.OrderedSafeExecutor;
+
+import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
+import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Tests for PerChannelBookieClient. Historically, this class has
+ * had a few race conditions, so this is what these tests focus on.
+ */
+public class TestPerChannelBookieClient extends BookKeeperClusterTestCase {
+    static Logger LOG = LoggerFactory.getLogger(TestPerChannelBookieClient.class);
+
+    public TestPerChannelBookieClient() {
+        super(1);
+    }
+
+    /**
+     * Test that a race does not exist between connection completion
+     * and client closure. If a race does exist, this test will simply
+     * hang at releaseExternalResources() as it is uninterruptible.
+     * This specific race was found in
+     * {@link https://issues.apache.org/jira/browse/BOOKKEEPER-485}.
+     */
+    @Test(timeout=60000)
+    public void testConnectCloseRace() {
+        ClientSocketChannelFactory channelFactory
+            = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(),
+                                                Executors.newCachedThreadPool());
+        OrderedSafeExecutor executor = new OrderedSafeExecutor(1);
+
+        InetSocketAddress addr = getBookie(0);
+        AtomicLong bytesOutstanding = new AtomicLong(0);
+        for (int i = 0; i < 1000; i++) {
+            PerChannelBookieClient client = new PerChannelBookieClient(executor, channelFactory,
+                                                                       addr, bytesOutstanding);
+            client.connectIfNeededAndDoOp(new GenericCallback<Void>() {
+                    @Override
+                    public void operationComplete(int rc, Void result) {
+                        // do nothing, we don't care about doing anything with the connection,
+                        // we just want to trigger it connecting.
+                    }
+                });
+            client.close();
+        }
+        channelFactory.releaseExternalResources();
+        executor.shutdown();
+    }
+}
\ No newline at end of file