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.CoreMatchers.endsWith;
021import static org.hamcrest.MatcherAssert.assertThat;
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertTrue;
024
025import org.apache.hadoop.fs.FileStatus;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.master.MasterFileSystem;
029import org.apache.hadoop.hbase.regionserver.HRegionServer;
030import org.apache.hadoop.hbase.testclassification.LargeTests;
031import org.apache.hadoop.hbase.testclassification.ReplicationTests;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036@Category({ ReplicationTests.class, LargeTests.class })
037public class TestSyncReplicationRemoveRemoteWAL extends SyncReplicationTestBase {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestSyncReplicationRemoveRemoteWAL.class);
042
043  @Test
044  public void testRemoveRemoteWAL() throws Exception {
045    UTIL2.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID,
046      SyncReplicationState.STANDBY);
047    UTIL1.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID,
048      SyncReplicationState.ACTIVE);
049
050    MasterFileSystem mfs = UTIL2.getMiniHBaseCluster().getMaster().getMasterFileSystem();
051    Path remoteWALDir = ReplicationUtils.getPeerRemoteWALDir(
052      new Path(mfs.getWALRootDir(), ReplicationUtils.REMOTE_WAL_DIR_NAME), PEER_ID);
053    FileStatus[] remoteWALStatus = mfs.getWALFileSystem().listStatus(remoteWALDir);
054    assertEquals(1, remoteWALStatus.length);
055    Path remoteWAL = remoteWALStatus[0].getPath();
056    assertThat(remoteWAL.getName(), endsWith(ReplicationUtils.SYNC_WAL_SUFFIX));
057    writeAndVerifyReplication(UTIL1, UTIL2, 0, 100);
058
059    HRegionServer rs = UTIL1.getRSForFirstRegionInTable(TABLE_NAME);
060    rs.getWalRoller().requestRollAll();
061    // The replicated wal file should be deleted finally
062    waitUntilDeleted(UTIL2, remoteWAL);
063    remoteWALStatus = mfs.getWALFileSystem().listStatus(remoteWALDir);
064    assertEquals(1, remoteWALStatus.length);
065    remoteWAL = remoteWALStatus[0].getPath();
066    assertThat(remoteWAL.getName(), endsWith(ReplicationUtils.SYNC_WAL_SUFFIX));
067
068    UTIL1.getAdmin().disableReplicationPeer(PEER_ID);
069    write(UTIL1, 100, 200);
070    UTIL1.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID,
071      SyncReplicationState.DOWNGRADE_ACTIVE);
072
073    // should still be there since the peer is disabled and we haven't replicated the data yet
074    assertTrue(mfs.getWALFileSystem().exists(remoteWAL));
075
076    UTIL1.getAdmin().enableReplicationPeer(PEER_ID);
077    waitUntilReplicationDone(UTIL2, 200);
078    verifyThroughRegion(UTIL2, 100, 200);
079
080    // Confirm that we will also remove the remote wal files in DA state
081    waitUntilDeleted(UTIL2, remoteWAL);
082  }
083}