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 047 @ClassRule 048 public static final HBaseClassTestRule CLASS_RULE = 049 HBaseClassTestRule.forClass(TestRecoverableZooKeeper.class); 050 051 private final static HBaseZKTestingUtility TEST_UTIL = new HBaseZKTestingUtility(); 052 053 Abortable abortable = new Abortable() { 054 @Override 055 public void abort(String why, Throwable e) { 056 057 } 058 059 @Override 060 public boolean isAborted() { 061 return false; 062 } 063 }; 064 065 @BeforeClass 066 public static void setUpBeforeClass() throws Exception { 067 TEST_UTIL.startMiniZKCluster(); 068 } 069 070 @AfterClass 071 public static void tearDownAfterClass() throws Exception { 072 TEST_UTIL.shutdownMiniZKCluster(); 073 } 074 075 @Test 076 public void testSetDataVersionMismatchInLoop() throws Exception { 077 String znode = "/hbase/splitWAL/9af7cfc9b15910a0b3d714bf40a3248f"; 078 Configuration conf = TEST_UTIL.getConfiguration(); 079 ZKWatcher zkw = new ZKWatcher(conf, "testSetDataVersionMismatchInLoop", 080 abortable, true); 081 String ensemble = ZKConfig.getZKQuorumServersString(conf); 082 RecoverableZooKeeper rzk = ZKUtil.connect(conf, ensemble, zkw); 083 rzk.create(znode, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 084 rzk.setData(znode, "OPENING".getBytes(), 0); 085 Field zkField = RecoverableZooKeeper.class.getDeclaredField("zk"); 086 zkField.setAccessible(true); 087 int timeout = conf.getInt(HConstants.ZK_SESSION_TIMEOUT, HConstants.DEFAULT_ZK_SESSION_TIMEOUT); 088 ZookeeperStub zkStub = new ZookeeperStub(ensemble, timeout, zkw); 089 zkStub.setThrowExceptionInNumOperations(1); 090 zkField.set(rzk, zkStub); 091 byte[] opened = "OPENED".getBytes(); 092 rzk.setData(znode, opened, 1); 093 byte[] data = rzk.getData(znode, false, new Stat()); 094 assertTrue(Bytes.equals(opened, data)); 095 } 096 097 class ZookeeperStub extends ZooKeeper { 098 099 private int throwExceptionInNumOperations; 100 101 public ZookeeperStub(String connectString, int sessionTimeout, Watcher watcher) 102 throws IOException { 103 super(connectString, sessionTimeout, watcher); 104 } 105 106 public void setThrowExceptionInNumOperations(int throwExceptionInNumOperations) { 107 this.throwExceptionInNumOperations = throwExceptionInNumOperations; 108 } 109 110 private void checkThrowKeeperException() throws KeeperException { 111 if (throwExceptionInNumOperations == 1) { 112 throwExceptionInNumOperations = 0; 113 throw new KeeperException.ConnectionLossException(); 114 } 115 if (throwExceptionInNumOperations > 0) { 116 throwExceptionInNumOperations--; 117 } 118 } 119 120 @Override 121 public Stat setData(String path, byte[] data, int version) throws KeeperException, 122 InterruptedException { 123 Stat stat = super.setData(path, data, version); 124 checkThrowKeeperException(); 125 return stat; 126 } 127 } 128}