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.hamcrest.MatcherAssert.assertThat; 021import static org.hamcrest.Matchers.containsString; 022import static org.hamcrest.Matchers.either; 023import static org.hamcrest.Matchers.instanceOf; 024import static org.junit.jupiter.api.Assertions.assertFalse; 025import static org.junit.jupiter.api.Assertions.assertThrows; 026import static org.junit.jupiter.api.Assertions.assertTrue; 027 028import java.io.IOException; 029import java.util.Arrays; 030import org.apache.hadoop.fs.FileSystem; 031import org.apache.hadoop.fs.Path; 032import org.apache.hadoop.hbase.DoNotRetryIOException; 033import org.apache.hadoop.hbase.client.Append; 034import org.apache.hadoop.hbase.client.Delete; 035import org.apache.hadoop.hbase.client.Get; 036import org.apache.hadoop.hbase.client.Mutation; 037import org.apache.hadoop.hbase.client.Put; 038import org.apache.hadoop.hbase.client.RetriesExhaustedException; 039import org.apache.hadoop.hbase.client.RowMutations; 040import org.apache.hadoop.hbase.client.Table; 041import org.apache.hadoop.hbase.master.MasterFileSystem; 042import org.apache.hadoop.hbase.util.Bytes; 043import org.junit.jupiter.api.Test; 044 045public class SyncReplicationStandbyTestBase extends SyncReplicationTestBaseNoBeforeAll { 046 047 @FunctionalInterface 048 private interface TableAction { 049 050 void call(Table table) throws IOException; 051 } 052 053 private void assertDisallow(Table table, TableAction action) throws IOException { 054 Exception error = assertThrows(Exception.class, () -> action.call(table)); 055 assertThat(error, either(instanceOf(DoNotRetryIOException.class)) 056 .or(instanceOf(RetriesExhaustedException.class))); 057 assertThat(error.getMessage(), containsString("STANDBY")); 058 } 059 060 @Test 061 public void testStandby() throws Exception { 062 MasterFileSystem mfs = UTIL2.getHBaseCluster().getMaster().getMasterFileSystem(); 063 Path remoteWALDir = getRemoteWALDir(mfs, PEER_ID); 064 assertFalse(mfs.getWALFileSystem().exists(remoteWALDir)); 065 UTIL2.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID, 066 SyncReplicationState.STANDBY); 067 assertTrue(mfs.getWALFileSystem().exists(remoteWALDir)); 068 try (Table table = UTIL2.getConnection().getTable(TABLE_NAME)) { 069 assertDisallow(table, t -> t.get(new Get(Bytes.toBytes("row")))); 070 assertDisallow(table, 071 t -> t.put(new Put(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("row")))); 072 assertDisallow(table, t -> t.delete(new Delete(Bytes.toBytes("row")))); 073 assertDisallow(table, t -> t.incrementColumnValue(Bytes.toBytes("row"), CF, CQ, 1)); 074 assertDisallow(table, 075 t -> t.append(new Append(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("row")))); 076 assertDisallow(table, 077 t -> t.get(Arrays.asList(new Get(Bytes.toBytes("row")), new Get(Bytes.toBytes("row1"))))); 078 assertDisallow(table, 079 t -> t 080 .put(Arrays.asList(new Put(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("row")), 081 new Put(Bytes.toBytes("row1")).addColumn(CF, CQ, Bytes.toBytes("row1"))))); 082 assertDisallow(table, t -> t.mutateRow(new RowMutations(Bytes.toBytes("row")) 083 .add((Mutation) new Put(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("row"))))); 084 } 085 // We should still allow replication writes 086 writeAndVerifyReplication(UTIL1, UTIL2, 0, 100); 087 088 // Remove the peers in ACTIVE & STANDBY cluster. 089 FileSystem fs2 = REMOTE_WAL_DIR2.getFileSystem(UTIL2.getConfiguration()); 090 assertTrue(fs2.exists(getRemoteWALDir(REMOTE_WAL_DIR2, PEER_ID))); 091 092 UTIL2.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID, 093 SyncReplicationState.DOWNGRADE_ACTIVE); 094 assertFalse(fs2.exists(getRemoteWALDir(REMOTE_WAL_DIR2, PEER_ID))); 095 assertFalse(fs2.exists(getReplayRemoteWALs(REMOTE_WAL_DIR2, PEER_ID))); 096 097 UTIL1.getAdmin().removeReplicationPeer(PEER_ID); 098 verifyRemovedPeer(PEER_ID, REMOTE_WAL_DIR1, UTIL1); 099 100 // Peer remoteWAL dir will be renamed to replay WAL dir when transit from S to DA, and the 101 // replay WAL dir will be removed after replaying all WALs, so create a emtpy dir here to test 102 // whether the removeReplicationPeer would remove the remoteWAL dir. 103 fs2.create(getRemoteWALDir(REMOTE_WAL_DIR2, PEER_ID)); 104 fs2.create(getReplayRemoteWALs(REMOTE_WAL_DIR2, PEER_ID)); 105 assertTrue(fs2.exists(getRemoteWALDir(REMOTE_WAL_DIR2, PEER_ID))); 106 assertTrue(fs2.exists(getReplayRemoteWALs(REMOTE_WAL_DIR2, PEER_ID))); 107 UTIL2.getAdmin().removeReplicationPeer(PEER_ID); 108 verifyRemovedPeer(PEER_ID, REMOTE_WAL_DIR2, UTIL2); 109 } 110}