HBASE-26614 Refactor code related to "dump"ing ZK nodes (#3969)
[hbase.git] / hbase-zookeeper / src / test / java / org / apache / hadoop / hbase / zookeeper / TestRecoverableZooKeeper.java
blob700781c849fb6b6f1dc5c533112a04e05b8205ff
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.zookeeper;
20 import static org.junit.Assert.assertTrue;
22 import java.io.IOException;
23 import java.lang.reflect.Field;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.hbase.Abortable;
26 import org.apache.hadoop.hbase.HBaseClassTestRule;
27 import org.apache.hadoop.hbase.HBaseZKTestingUtil;
28 import org.apache.hadoop.hbase.HConstants;
29 import org.apache.hadoop.hbase.testclassification.MediumTests;
30 import org.apache.hadoop.hbase.testclassification.ZKTests;
31 import org.apache.hadoop.hbase.util.Bytes;
32 import org.apache.zookeeper.CreateMode;
33 import org.apache.zookeeper.KeeperException;
34 import org.apache.zookeeper.Watcher;
35 import org.apache.zookeeper.ZooDefs.Ids;
36 import org.apache.zookeeper.ZooKeeper;
37 import org.apache.zookeeper.data.Stat;
38 import org.junit.AfterClass;
39 import org.junit.BeforeClass;
40 import org.junit.ClassRule;
41 import org.junit.Test;
42 import org.junit.experimental.categories.Category;
44 @Category({ ZKTests.class, MediumTests.class })
45 public class TestRecoverableZooKeeper {
46 @ClassRule
47 public static final HBaseClassTestRule CLASS_RULE =
48 HBaseClassTestRule.forClass(TestRecoverableZooKeeper.class);
50 private final static HBaseZKTestingUtil TEST_UTIL = new HBaseZKTestingUtil();
52 private Abortable abortable = new Abortable() {
53 @Override
54 public void abort(String why, Throwable e) {
57 @Override
58 public boolean isAborted() {
59 return false;
63 @BeforeClass
64 public static void setUpBeforeClass() throws Exception {
65 TEST_UTIL.startMiniZKCluster();
68 @AfterClass
69 public static void tearDownAfterClass() throws Exception {
70 TEST_UTIL.shutdownMiniZKCluster();
73 @Test
74 public void testSetDataVersionMismatchInLoop() throws Exception {
75 String znode = "/hbase/splitWAL/9af7cfc9b15910a0b3d714bf40a3248f";
76 Configuration conf = TEST_UTIL.getConfiguration();
77 ZKWatcher zkw = new ZKWatcher(conf, "testSetDataVersionMismatchInLoop",
78 abortable, true);
79 String ensemble = ZKConfig.getZKQuorumServersString(conf);
80 RecoverableZooKeeper rzk = RecoverableZooKeeper.connect(conf, ensemble, zkw);
81 rzk.create(znode, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
82 rzk.setData(znode, Bytes.toBytes("OPENING"), 0);
83 Field zkField = RecoverableZooKeeper.class.getDeclaredField("zk");
84 zkField.setAccessible(true);
85 int timeout = conf.getInt(HConstants.ZK_SESSION_TIMEOUT, HConstants.DEFAULT_ZK_SESSION_TIMEOUT);
86 ZookeeperStub zkStub = new ZookeeperStub(ensemble, timeout, zkw);
87 zkStub.setThrowExceptionInNumOperations(1);
88 zkField.set(rzk, zkStub);
89 byte[] opened = Bytes.toBytes("OPENED");
90 rzk.setData(znode, opened, 1);
91 byte[] data = rzk.getData(znode, false, new Stat());
92 assertTrue(Bytes.equals(opened, data));
95 static class ZookeeperStub extends ZooKeeper {
96 private int throwExceptionInNumOperations;
98 ZookeeperStub(String connectString, int sessionTimeout, Watcher watcher)
99 throws IOException {
100 super(connectString, sessionTimeout, watcher);
103 void setThrowExceptionInNumOperations(int throwExceptionInNumOperations) {
104 this.throwExceptionInNumOperations = throwExceptionInNumOperations;
107 private void checkThrowKeeperException() throws KeeperException {
108 if (throwExceptionInNumOperations == 1) {
109 throwExceptionInNumOperations = 0;
110 throw new KeeperException.ConnectionLossException();
112 if (throwExceptionInNumOperations > 0) {
113 throwExceptionInNumOperations--;
117 @Override
118 public Stat setData(String path, byte[] data, int version) throws KeeperException,
119 InterruptedException {
120 Stat stat = super.setData(path, data, version);
121 checkThrowKeeperException();
122 return stat;