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