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.jupiter.api.Assertions.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.HBaseZKTestingUtil; 027import org.apache.hadoop.hbase.HConstants; 028import org.apache.hadoop.hbase.testclassification.MediumTests; 029import org.apache.hadoop.hbase.testclassification.ZKTests; 030import org.apache.hadoop.hbase.util.Bytes; 031import org.apache.zookeeper.CreateMode; 032import org.apache.zookeeper.KeeperException; 033import org.apache.zookeeper.Watcher; 034import org.apache.zookeeper.ZooDefs.Ids; 035import org.apache.zookeeper.ZooKeeper; 036import org.apache.zookeeper.data.Stat; 037import org.junit.jupiter.api.AfterAll; 038import org.junit.jupiter.api.BeforeAll; 039import org.junit.jupiter.api.Tag; 040import org.junit.jupiter.api.Test; 041 042@Tag(ZKTests.TAG) 043@Tag(MediumTests.TAG) 044public class TestRecoverableZooKeeper { 045 046 private final static HBaseZKTestingUtil TEST_UTIL = new HBaseZKTestingUtil(); 047 048 private Abortable abortable = new Abortable() { 049 @Override 050 public void abort(String why, Throwable e) { 051 } 052 053 @Override 054 public boolean isAborted() { 055 return false; 056 } 057 }; 058 059 @BeforeAll 060 public static void setUpBeforeClass() throws Exception { 061 TEST_UTIL.startMiniZKCluster(); 062 } 063 064 @AfterAll 065 public static void tearDownAfterClass() throws Exception { 066 TEST_UTIL.shutdownMiniZKCluster(); 067 } 068 069 @Test 070 public void testSetDataVersionMismatchInLoop() throws Exception { 071 String znode = "/hbase/splitWAL/9af7cfc9b15910a0b3d714bf40a3248f"; 072 Configuration conf = TEST_UTIL.getConfiguration(); 073 ZKWatcher zkw = new ZKWatcher(conf, "testSetDataVersionMismatchInLoop", abortable, true); 074 String ensemble = ZKConfig.getZKQuorumServersString(conf); 075 RecoverableZooKeeper rzk = RecoverableZooKeeper.connect(conf, ensemble, zkw, null, null); 076 rzk.create(znode, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 077 rzk.setData(znode, Bytes.toBytes("OPENING"), 0); 078 Field zkField = RecoverableZooKeeper.class.getDeclaredField("zk"); 079 zkField.setAccessible(true); 080 int timeout = conf.getInt(HConstants.ZK_SESSION_TIMEOUT, HConstants.DEFAULT_ZK_SESSION_TIMEOUT); 081 ZookeeperStub zkStub = new ZookeeperStub(ensemble, timeout, zkw); 082 zkStub.setThrowExceptionInNumOperations(1); 083 zkField.set(rzk, zkStub); 084 byte[] opened = Bytes.toBytes("OPENED"); 085 rzk.setData(znode, opened, 1); 086 byte[] data = rzk.getData(znode, false, new Stat()); 087 assertTrue(Bytes.equals(opened, data)); 088 } 089 090 static class ZookeeperStub extends ZooKeeper { 091 private int throwExceptionInNumOperations; 092 093 ZookeeperStub(String connectString, int sessionTimeout, Watcher watcher) throws IOException { 094 super(connectString, sessionTimeout, watcher); 095 } 096 097 void setThrowExceptionInNumOperations(int throwExceptionInNumOperations) { 098 this.throwExceptionInNumOperations = throwExceptionInNumOperations; 099 } 100 101 private void checkThrowKeeperException() throws KeeperException { 102 if (throwExceptionInNumOperations == 1) { 103 throwExceptionInNumOperations = 0; 104 throw new KeeperException.ConnectionLossException(); 105 } 106 if (throwExceptionInNumOperations > 0) { 107 throwExceptionInNumOperations--; 108 } 109 } 110 111 @Override 112 public Stat setData(String path, byte[] data, int version) 113 throws KeeperException, InterruptedException { 114 Stat stat = super.setData(path, data, version); 115 checkThrowKeeperException(); 116 return stat; 117 } 118 } 119}