001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.zookeeper;
019
020import static org.junit.Assert.assertTrue;
021
022import java.io.IOException;
023import java.lang.reflect.Field;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.Abortable;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseZKTestingUtility;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.apache.hadoop.hbase.testclassification.ZKTests;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.apache.zookeeper.CreateMode;
033import org.apache.zookeeper.KeeperException;
034import org.apache.zookeeper.Watcher;
035import org.apache.zookeeper.ZooDefs.Ids;
036import org.apache.zookeeper.ZooKeeper;
037import org.apache.zookeeper.data.Stat;
038import org.junit.AfterClass;
039import org.junit.BeforeClass;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043
044@Category({ ZKTests.class, MediumTests.class })
045public class TestRecoverableZooKeeper {
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestRecoverableZooKeeper.class);
049
050  private final static HBaseZKTestingUtility TEST_UTIL = new HBaseZKTestingUtility();
051
052  private Abortable abortable = new Abortable() {
053    @Override
054    public void abort(String why, Throwable e) {
055    }
056
057    @Override
058    public boolean isAborted() {
059      return false;
060    }
061  };
062
063  @BeforeClass
064  public static void setUpBeforeClass() throws Exception {
065    TEST_UTIL.startMiniZKCluster();
066  }
067
068  @AfterClass
069  public static void tearDownAfterClass() throws Exception {
070    TEST_UTIL.shutdownMiniZKCluster();
071  }
072
073  @Test
074  public void testSetDataVersionMismatchInLoop() throws Exception {
075    String znode = "/hbase/splitWAL/9af7cfc9b15910a0b3d714bf40a3248f";
076    Configuration conf = TEST_UTIL.getConfiguration();
077    ZKWatcher zkw = new ZKWatcher(conf, "testSetDataVersionMismatchInLoop", abortable, true);
078    String ensemble = ZKConfig.getZKQuorumServersString(conf);
079    RecoverableZooKeeper rzk = RecoverableZooKeeper.connect(conf, ensemble, zkw);
080    rzk.create(znode, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
081    rzk.setData(znode, "OPENING".getBytes(), 0);
082    Field zkField = RecoverableZooKeeper.class.getDeclaredField("zk");
083    zkField.setAccessible(true);
084    int timeout = conf.getInt(HConstants.ZK_SESSION_TIMEOUT, HConstants.DEFAULT_ZK_SESSION_TIMEOUT);
085    ZookeeperStub zkStub = new ZookeeperStub(ensemble, timeout, zkw);
086    zkStub.setThrowExceptionInNumOperations(1);
087    zkField.set(rzk, zkStub);
088    byte[] opened = "OPENED".getBytes();
089    rzk.setData(znode, opened, 1);
090    byte[] data = rzk.getData(znode, false, new Stat());
091    assertTrue(Bytes.equals(opened, data));
092  }
093
094  static class ZookeeperStub extends ZooKeeper {
095    private int throwExceptionInNumOperations;
096
097    ZookeeperStub(String connectString, int sessionTimeout, Watcher watcher) throws IOException {
098      super(connectString, sessionTimeout, watcher);
099    }
100
101    void setThrowExceptionInNumOperations(int throwExceptionInNumOperations) {
102      this.throwExceptionInNumOperations = throwExceptionInNumOperations;
103    }
104
105    private void checkThrowKeeperException() throws KeeperException {
106      if (throwExceptionInNumOperations == 1) {
107        throwExceptionInNumOperations = 0;
108        throw new KeeperException.ConnectionLossException();
109      }
110      if (throwExceptionInNumOperations > 0) {
111        throwExceptionInNumOperations--;
112      }
113    }
114
115    @Override
116    public Stat setData(String path, byte[] data, int version)
117      throws KeeperException, InterruptedException {
118      Stat stat = super.setData(path, data, version);
119      checkThrowKeeperException();
120      return stat;
121    }
122  }
123}