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