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.apache.hadoop.hbase.replication.ReplicationPeerConfigTestUtil.assertConfigEquals;
021import static org.apache.hadoop.hbase.replication.ReplicationPeerConfigTestUtil.getConfig;
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertFalse;
024import static org.junit.Assert.assertThrows;
025import static org.junit.Assert.fail;
026
027import java.util.List;
028import org.junit.Test;
029
030public abstract class ReplicationPeerStorageTestBase {
031
032  protected static ReplicationPeerStorage STORAGE;
033
034  @Test
035  public void test() throws ReplicationException {
036    int peerCount = 10;
037    for (int i = 0; i < peerCount; i++) {
038      STORAGE.addPeer(Integer.toString(i), getConfig(i), i % 2 == 0);
039    }
040    List<String> peerIds = STORAGE.listPeerIds();
041    assertEquals(peerCount, peerIds.size());
042    for (String peerId : peerIds) {
043      int seed = Integer.parseInt(peerId);
044      assertConfigEquals(getConfig(seed), STORAGE.getPeerConfig(peerId));
045    }
046    for (int i = 0; i < peerCount; i++) {
047      STORAGE.updatePeerConfig(Integer.toString(i), getConfig(i + 1));
048    }
049    for (String peerId : peerIds) {
050      int seed = Integer.parseInt(peerId);
051      assertConfigEquals(getConfig(seed + 1), STORAGE.getPeerConfig(peerId));
052    }
053    for (int i = 0; i < peerCount; i++) {
054      assertEquals(i % 2 == 0, STORAGE.isPeerEnabled(Integer.toString(i)));
055    }
056    for (int i = 0; i < peerCount; i++) {
057      STORAGE.setPeerState(Integer.toString(i), i % 2 != 0);
058    }
059    for (int i = 0; i < peerCount; i++) {
060      assertEquals(i % 2 != 0, STORAGE.isPeerEnabled(Integer.toString(i)));
061    }
062    String toRemove = Integer.toString(peerCount / 2);
063    STORAGE.removePeer(toRemove);
064    peerIds = STORAGE.listPeerIds();
065    assertEquals(peerCount - 1, peerIds.size());
066    assertFalse(peerIds.contains(toRemove));
067
068    try {
069      STORAGE.getPeerConfig(toRemove);
070      fail("Should throw a ReplicationException when getting peer config of a removed peer");
071    } catch (ReplicationException e) {
072    }
073  }
074
075  protected abstract void assertPeerNameControlException(ReplicationException e);
076
077  @Test
078  public void testPeerNameControl() throws Exception {
079    String clusterKey = "key";
080    STORAGE.addPeer("6", ReplicationPeerConfig.newBuilder().setClusterKey(clusterKey).build(),
081      true);
082
083    try {
084      ReplicationException e = assertThrows(ReplicationException.class, () -> STORAGE.addPeer("6",
085        ReplicationPeerConfig.newBuilder().setClusterKey(clusterKey).build(), true));
086      assertPeerNameControlException(e);
087    } finally {
088      // clean up
089      STORAGE.removePeer("6");
090    }
091  }
092}