sijie commented on a change in pull request #873: Issue280: Add StateManager to 
manage Bookie's state, and use it to turn bookie into readonly when 
sortedLedgerStorage failed to flush data
URL: https://github.com/apache/bookkeeper/pull/873#discussion_r158770846
 
 

 ##########
 File path: 
bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/StateManagerTest.java
 ##########
 @@ -0,0 +1,207 @@
+/**
+ *
+ * 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.bookkeeper.bookie;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import java.io.File;
+import java.io.IOException;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.conf.TestBKConfiguration;
+import org.apache.bookkeeper.discover.ZKRegistrationManager;
+import org.apache.bookkeeper.proto.BookieServer;
+import org.apache.bookkeeper.stats.NullStatsLogger;
+import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
+import org.apache.zookeeper.KeeperException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TestName;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Testing StateManager cases.
+ */
+public class StateManagerTest extends BookKeeperClusterTestCase {
+    private static final Logger LOG = LoggerFactory
+            .getLogger(StateManagerTest.class);
+
+    @Rule
+    public final TestName runtime = new TestName();
+    MockZKRegistrationManager rm;
+
+    public StateManagerTest() {
+        super(0);
+        String ledgersPath = "/" + "ledgers" + runtime.getMethodName();
+        baseClientConf.setZkLedgersRootPath(ledgersPath);
+        baseConf.setZkLedgersRootPath(ledgersPath);
+    }
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        zkUtil.createBKEnsemble("/" + runtime.getMethodName());
+        rm = new MockZKRegistrationManager();
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        super.tearDown();
+        if (rm != null) {
+            rm.close();
+        }
+    }
+
+    private static class MockZKRegistrationManager extends 
ZKRegistrationManager {
+        boolean registerFailed = false;
+        void setRegisterFail(boolean failOrNot){
+            registerFailed = failOrNot;
+        }
+        @Override
+        public void registerBookie(String bookieId, boolean readOnly) throws 
BookieException {
+            if (registerFailed) {
+                throw BookieException.create(-100);
+            }
+            super.registerBookie(bookieId, readOnly);
+        }
+
+    }
+
+    /**
+     * Bookie should shutdown when it register to Registration service fail.
+     * On ZooKeeper exception, should return exit code ZK_REG_FAIL = 4
+     */
+    @Test
+    public void testShutdown() throws Exception {
+        File tmpDir = createTempDir("stateManger", "test");
+
+        final ServerConfiguration conf = 
TestBKConfiguration.newServerConfiguration();
+        conf.setJournalDirName(tmpDir.getPath())
+            .setLedgerDirNames(new String[] { tmpDir.getPath() })
+            .setZkServers(zkUtil.getZooKeeperConnectString());
+
+        BookieServer bkServer = new BookieServer(conf) {
+            protected Bookie newBookie(ServerConfiguration conf)
+                    throws IOException, KeeperException, InterruptedException,
+                    BookieException {
+                Bookie bookie = new Bookie(conf);
+                rm.setRegisterFail(true);
+                rm.initialize(conf, () -> {}, NullStatsLogger.INSTANCE);
+                LOG.info(" is {} ", zkc == null);
+                bookie.setRegistrationManager(rm);
+                return bookie;
+            }
+        };
+        bkServer.start();
+        bkServer.join();
+        assertTrue("Failed to return failCode ZK_REG_FAIL",
+                ExitCode.ZK_REG_FAIL == bkServer.getExitCode());
+    }
+
+    /**
+     * StateManager can transition between writable mode and readOnly mode if 
it was not created with readOnly mode.
+     */
+    @Test
+    public void testBookieTransitions() throws Exception {
+        File tmpDir = createTempDir("stateManger", "test");
+
+        final ServerConfiguration conf = 
TestBKConfiguration.newServerConfiguration();
+        conf.setJournalDirName(tmpDir.getPath())
+                .setLedgerDirNames(new String[] { tmpDir.getPath() })
+                .setZkServers(zkUtil.getZooKeeperConnectString());
+        Bookie bookie = new Bookie(conf);
+        bookie.start();
+        assertTrue(bookie.isRunning());
+
+        bookie.getStateManager().transitionToReadOnlyMode();
+        //sleep a little to wait transition finish
+        Thread.sleep(1000);
 
 Review comment:
   This is not a good idea. It is not good to use `sleep(1000)`. Can you wait 
on the future returned by transitionToReadOnlyMode?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to