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.replication; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertTrue; 023import static org.mockito.ArgumentMatchers.any; 024import static org.mockito.ArgumentMatchers.anyBoolean; 025import static org.mockito.ArgumentMatchers.anyString; 026import static org.mockito.Mockito.doAnswer; 027import static org.mockito.Mockito.spy; 028import java.io.IOException; 029import org.apache.hadoop.conf.Configuration; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseTestingUtility; 032import org.apache.hadoop.hbase.HConstants; 033import org.apache.hadoop.hbase.client.Admin; 034import org.apache.hadoop.hbase.master.HMaster; 035import org.apache.hadoop.hbase.master.replication.ReplicationPeerManager; 036import org.apache.hadoop.hbase.testclassification.MediumTests; 037import org.apache.hadoop.hbase.testclassification.ReplicationTests; 038import org.junit.After; 039import org.junit.AfterClass; 040import org.junit.BeforeClass; 041import org.junit.ClassRule; 042import org.junit.Test; 043import org.junit.experimental.categories.Category; 044import org.mockito.invocation.InvocationOnMock; 045 046/** 047 * All the modification method will fail once in the test and should finally succeed. 048 */ 049@Category({ ReplicationTests.class, MediumTests.class }) 050public class TestReplicationProcedureRetry { 051 052 @ClassRule 053 public static final HBaseClassTestRule CLASS_RULE = 054 HBaseClassTestRule.forClass(TestReplicationProcedureRetry.class); 055 056 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 057 058 @BeforeClass 059 public static void setUp() throws Exception { 060 UTIL.getConfiguration().setClass(HConstants.MASTER_IMPL, MockHMaster.class, HMaster.class); 061 UTIL.startMiniCluster(3); 062 } 063 064 @AfterClass 065 public static void tearDown() throws Exception { 066 UTIL.shutdownMiniCluster(); 067 } 068 069 @After 070 public void tearDownAfterTest() throws IOException { 071 for (ReplicationPeerDescription desc : UTIL.getAdmin().listReplicationPeers()) { 072 UTIL.getAdmin().removeReplicationPeer(desc.getPeerId()); 073 } 074 } 075 076 private void doTest() throws IOException { 077 Admin admin = UTIL.getAdmin(); 078 String peerId = "1"; 079 ReplicationPeerConfig peerConfig = ReplicationPeerConfig.newBuilder() 080 .setClusterKey(UTIL.getZkCluster().getAddress().toString() + ":/hbase2").build(); 081 admin.addReplicationPeer(peerId, peerConfig, true); 082 083 assertEquals(peerConfig.getClusterKey(), 084 admin.getReplicationPeerConfig(peerId).getClusterKey()); 085 ReplicationPeerConfig newPeerConfig = 086 ReplicationPeerConfig.newBuilder(peerConfig).setBandwidth(123456).build(); 087 admin.updateReplicationPeerConfig(peerId, newPeerConfig); 088 assertEquals(newPeerConfig.getBandwidth(), 089 admin.getReplicationPeerConfig(peerId).getBandwidth()); 090 091 admin.disableReplicationPeer(peerId); 092 assertFalse(admin.listReplicationPeers().get(0).isEnabled()); 093 094 admin.enableReplicationPeer(peerId); 095 assertTrue(admin.listReplicationPeers().get(0).isEnabled()); 096 097 admin.removeReplicationPeer(peerId); 098 assertTrue(admin.listReplicationPeers().isEmpty()); 099 100 // make sure that we have run into the mocked method 101 MockHMaster master = (MockHMaster) UTIL.getHBaseCluster().getMaster(); 102 assertTrue(master.addPeerCalled); 103 assertTrue(master.removePeerCalled); 104 assertTrue(master.updatePeerConfigCalled); 105 assertTrue(master.enablePeerCalled); 106 assertTrue(master.disablePeerCalled); 107 } 108 109 @Test 110 public void testErrorBeforeUpdate() throws IOException, ReplicationException { 111 ((MockHMaster) UTIL.getHBaseCluster().getMaster()).reset(true); 112 doTest(); 113 } 114 115 @Test 116 public void testErrorAfterUpdate() throws IOException, ReplicationException { 117 ((MockHMaster) UTIL.getHBaseCluster().getMaster()).reset(false); 118 doTest(); 119 } 120 121 public static final class MockHMaster extends HMaster { 122 123 volatile boolean addPeerCalled; 124 125 volatile boolean removePeerCalled; 126 127 volatile boolean updatePeerConfigCalled; 128 129 volatile boolean enablePeerCalled; 130 131 volatile boolean disablePeerCalled; 132 133 private ReplicationPeerManager manager; 134 135 public MockHMaster(Configuration conf) throws IOException { 136 super(conf); 137 } 138 139 private Object invokeWithError(InvocationOnMock invocation, boolean errorBeforeUpdate) 140 throws Throwable { 141 if (errorBeforeUpdate) { 142 throw new ReplicationException("mock error before update"); 143 } 144 invocation.callRealMethod(); 145 throw new ReplicationException("mock error after update"); 146 } 147 148 public void reset(boolean errorBeforeUpdate) throws ReplicationException { 149 addPeerCalled = false; 150 removePeerCalled = false; 151 updatePeerConfigCalled = false; 152 enablePeerCalled = false; 153 disablePeerCalled = false; 154 ReplicationPeerManager m = super.getReplicationPeerManager(); 155 manager = spy(m); 156 doAnswer(invocation -> { 157 if (!addPeerCalled) { 158 addPeerCalled = true; 159 return invokeWithError(invocation, errorBeforeUpdate); 160 } else { 161 return invocation.callRealMethod(); 162 } 163 }).when(manager).addPeer(anyString(), any(ReplicationPeerConfig.class), anyBoolean()); 164 doAnswer(invocation -> { 165 if (!removePeerCalled) { 166 removePeerCalled = true; 167 return invokeWithError(invocation, errorBeforeUpdate); 168 } else { 169 return invocation.callRealMethod(); 170 } 171 }).when(manager).removePeer(anyString()); 172 doAnswer(invocation -> { 173 if (!updatePeerConfigCalled) { 174 updatePeerConfigCalled = true; 175 return invokeWithError(invocation, errorBeforeUpdate); 176 } else { 177 return invocation.callRealMethod(); 178 } 179 }).when(manager).updatePeerConfig(anyString(), any(ReplicationPeerConfig.class)); 180 doAnswer(invocation -> { 181 if (!enablePeerCalled) { 182 enablePeerCalled = true; 183 return invokeWithError(invocation, errorBeforeUpdate); 184 } else { 185 return invocation.callRealMethod(); 186 } 187 }).when(manager).enablePeer(anyString()); 188 doAnswer(invocation -> { 189 if (!disablePeerCalled) { 190 disablePeerCalled = true; 191 return invokeWithError(invocation, errorBeforeUpdate); 192 } else { 193 return invocation.callRealMethod(); 194 } 195 }).when(manager).disablePeer(anyString()); 196 } 197 198 @Override 199 public ReplicationPeerManager getReplicationPeerManager() { 200 return manager; 201 } 202 } 203}