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", 078 abortable, true); 079 String ensemble = ZKConfig.getZKQuorumServersString(conf); 080 RecoverableZooKeeper rzk = ZKUtil.connect(conf, ensemble, zkw); 081 rzk.create(znode, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 082 rzk.setData(znode, "OPENING".getBytes(), 0); 083 Field zkField = RecoverableZooKeeper.class.getDeclaredField("zk"); 084 zkField.setAccessible(true); 085 int timeout = conf.getInt(HConstants.ZK_SESSION_TIMEOUT, HConstants.DEFAULT_ZK_SESSION_TIMEOUT); 086 ZookeeperStub zkStub = new ZookeeperStub(ensemble, timeout, zkw); 087 zkStub.setThrowExceptionInNumOperations(1); 088 zkField.set(rzk, zkStub); 089 byte[] opened = "OPENED".getBytes(); 090 rzk.setData(znode, opened, 1); 091 byte[] data = rzk.getData(znode, false, new Stat()); 092 assertTrue(Bytes.equals(opened, data)); 093 } 094 095 static class ZookeeperStub extends ZooKeeper { 096 private int throwExceptionInNumOperations; 097 098 ZookeeperStub(String connectString, int sessionTimeout, Watcher watcher) 099 throws IOException { 100 super(connectString, sessionTimeout, watcher); 101 } 102 103 void setThrowExceptionInNumOperations(int throwExceptionInNumOperations) { 104 this.throwExceptionInNumOperations = throwExceptionInNumOperations; 105 } 106 107 private void checkThrowKeeperException() throws KeeperException { 108 if (throwExceptionInNumOperations == 1) { 109 throwExceptionInNumOperations = 0; 110 throw new KeeperException.ConnectionLossException(); 111 } 112 if (throwExceptionInNumOperations > 0) { 113 throwExceptionInNumOperations--; 114 } 115 } 116 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; 123 } 124 } 125}