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.Assert.assertArrayEquals;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertNotNull;
023import static org.junit.Assert.fail;
024
025import java.io.IOException;
026import java.util.Arrays;
027import java.util.concurrent.CountDownLatch;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.fs.Path;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.HBaseConfiguration;
032import org.apache.hadoop.hbase.HBaseTestingUtil;
033import org.apache.hadoop.hbase.HConstants;
034import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
035import org.apache.hadoop.hbase.TableName;
036import org.apache.hadoop.hbase.client.Admin;
037import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
038import org.apache.hadoop.hbase.client.Connection;
039import org.apache.hadoop.hbase.client.ConnectionFactory;
040import org.apache.hadoop.hbase.client.Delete;
041import org.apache.hadoop.hbase.client.Get;
042import org.apache.hadoop.hbase.client.Put;
043import org.apache.hadoop.hbase.client.Result;
044import org.apache.hadoop.hbase.client.Table;
045import org.apache.hadoop.hbase.client.TableDescriptor;
046import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
047import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
048import org.apache.hadoop.hbase.regionserver.HRegion;
049import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
050import org.apache.hadoop.hbase.testclassification.LargeTests;
051import org.apache.hadoop.hbase.testclassification.ReplicationTests;
052import org.apache.hadoop.hbase.util.Bytes;
053import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
054import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
055import org.junit.BeforeClass;
056import org.junit.ClassRule;
057import org.junit.Test;
058import org.junit.experimental.categories.Category;
059import org.slf4j.Logger;
060import org.slf4j.LoggerFactory;
061
062@Category({ ReplicationTests.class, LargeTests.class })
063public class TestMultiSlaveReplication {
064
065  @ClassRule
066  public static final HBaseClassTestRule CLASS_RULE =
067    HBaseClassTestRule.forClass(TestMultiSlaveReplication.class);
068
069  private static final Logger LOG = LoggerFactory.getLogger(TestMultiSlaveReplication.class);
070
071  private static Configuration conf1;
072  private static Configuration conf2;
073  private static Configuration conf3;
074
075  private static HBaseTestingUtil utility1;
076  private static HBaseTestingUtil utility2;
077  private static HBaseTestingUtil utility3;
078  private static final long SLEEP_TIME = 500;
079  private static final int NB_RETRIES = 100;
080
081  private static final TableName tableName = TableName.valueOf("test");
082  private static final byte[] famName = Bytes.toBytes("f");
083  private static final byte[] row = Bytes.toBytes("row");
084  private static final byte[] row1 = Bytes.toBytes("row1");
085  private static final byte[] row2 = Bytes.toBytes("row2");
086  private static final byte[] row3 = Bytes.toBytes("row3");
087  private static final byte[] noRepfamName = Bytes.toBytes("norep");
088
089  private static TableDescriptor table;
090
091  @BeforeClass
092  public static void setUpBeforeClass() throws Exception {
093    conf1 = HBaseConfiguration.create();
094    conf1.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
095    // smaller block size and capacity to trigger more operations
096    // and test them
097    conf1.setInt("hbase.regionserver.hlog.blocksize", 1024 * 20);
098    conf1.setInt("replication.source.size.capacity", 1024);
099    conf1.setLong("replication.source.sleepforretries", 100);
100    conf1.setInt("hbase.regionserver.maxlogs", 10);
101    conf1.setLong("hbase.master.logcleaner.ttl", 10);
102    conf1.setLong(HConstants.THREAD_WAKE_FREQUENCY, 100);
103    conf1.setStrings(CoprocessorHost.USER_REGION_COPROCESSOR_CONF_KEY,
104      "org.apache.hadoop.hbase.replication.TestMasterReplication$CoprocessorCounter");
105    conf1.setInt("hbase.master.cleaner.interval", 5 * 1000);
106
107    utility1 = new HBaseTestingUtil(conf1);
108    utility1.startMiniZKCluster();
109    MiniZooKeeperCluster miniZK = utility1.getZkCluster();
110    utility1.setZkCluster(miniZK);
111    new ZKWatcher(conf1, "cluster1", null, true);
112
113    conf2 = new Configuration(conf1);
114    conf2.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
115
116    conf3 = new Configuration(conf1);
117    conf3.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/3");
118
119    utility2 = new HBaseTestingUtil(conf2);
120    utility2.setZkCluster(miniZK);
121    new ZKWatcher(conf2, "cluster2", null, true);
122
123    utility3 = new HBaseTestingUtil(conf3);
124    utility3.setZkCluster(miniZK);
125    new ZKWatcher(conf3, "cluster3", null, true);
126
127    table = TableDescriptorBuilder.newBuilder(tableName)
128      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(famName)
129        .setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build())
130      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(noRepfamName)).build();
131  }
132
133  @Test
134  public void testMultiSlaveReplication() throws Exception {
135    LOG.info("testCyclicReplication");
136    SingleProcessHBaseCluster master = utility1.startMiniCluster();
137    utility2.startMiniCluster();
138    utility3.startMiniCluster();
139    try (Connection conn = ConnectionFactory.createConnection(conf1);
140      Admin admin1 = conn.getAdmin()) {
141      utility1.getAdmin().createTable(table);
142      utility2.getAdmin().createTable(table);
143      utility3.getAdmin().createTable(table);
144      Table htable1 = utility1.getConnection().getTable(tableName);
145      Table htable2 = utility2.getConnection().getTable(tableName);
146      Table htable3 = utility3.getConnection().getTable(tableName);
147
148      ReplicationPeerConfigBuilder rpcBuilder =
149        ReplicationPeerConfig.newBuilder().setClusterKey(utility2.getClusterKey());
150      admin1.addReplicationPeer("1", rpcBuilder.build());
151
152      // put "row" and wait 'til it got around, then delete
153      putAndWait(row, famName, htable1, htable2);
154      deleteAndWait(row, htable1, htable2);
155      // check it wasn't replication to cluster 3
156      checkRow(row, 0, htable3);
157
158      putAndWait(row2, famName, htable1, htable2);
159
160      // now roll the region server's logs
161      rollWALAndWait(utility1, htable1.getName(), row2);
162
163      // after the log was rolled put a new row
164      putAndWait(row3, famName, htable1, htable2);
165
166      rpcBuilder.setClusterKey(utility3.getClusterKey());
167      admin1.addReplicationPeer("2", rpcBuilder.build());
168
169      // put a row, check it was replicated to all clusters
170      putAndWait(row1, famName, htable1, htable2, htable3);
171      // delete and verify
172      deleteAndWait(row1, htable1, htable2, htable3);
173
174      // make sure row2 did not get replicated after
175      // cluster 3 was added
176      checkRow(row2, 0, htable3);
177
178      // row3 will get replicated, because it was in the
179      // latest log
180      checkRow(row3, 1, htable3);
181
182      Put p = new Put(row);
183      p.addColumn(famName, row, row);
184      htable1.put(p);
185      // now roll the logs again
186      rollWALAndWait(utility1, htable1.getName(), row);
187
188      // cleanup "row2", also conveniently use this to wait replication
189      // to finish
190      deleteAndWait(row2, htable1, htable2, htable3);
191      // Even if the log was rolled in the middle of the replication
192      // "row" is still replication.
193      checkRow(row, 1, htable2);
194      // Replication thread of cluster 2 may be sleeping, and since row2 is not there in it,
195      // we should wait before checking.
196      checkWithWait(row, 1, htable3);
197
198      // cleanup the rest
199      deleteAndWait(row, htable1, htable2, htable3);
200      deleteAndWait(row3, htable1, htable2, htable3);
201
202      utility3.shutdownMiniCluster();
203      utility2.shutdownMiniCluster();
204      utility1.shutdownMiniCluster();
205    }
206  }
207
208  private void rollWALAndWait(final HBaseTestingUtil utility, final TableName table,
209    final byte[] row) throws IOException {
210    final Admin admin = utility.getAdmin();
211    final SingleProcessHBaseCluster cluster = utility.getMiniHBaseCluster();
212
213    // find the region that corresponds to the given row.
214    HRegion region = null;
215    for (HRegion candidate : cluster.getRegions(table)) {
216      if (HRegion.rowIsInRange(candidate.getRegionInfo(), row)) {
217        region = candidate;
218        break;
219      }
220    }
221    assertNotNull("Couldn't find the region for row '" + Arrays.toString(row) + "'", region);
222
223    final CountDownLatch latch = new CountDownLatch(1);
224
225    // listen for successful log rolls
226    final WALActionsListener listener = new WALActionsListener() {
227      @Override
228      public void postLogRoll(final Path oldPath, final Path newPath) throws IOException {
229        latch.countDown();
230      }
231    };
232    region.getWAL().registerWALActionsListener(listener);
233
234    // request a roll
235    admin.rollWALWriter(cluster.getServerHoldingRegion(region.getTableDescriptor().getTableName(),
236      region.getRegionInfo().getRegionName()));
237
238    // wait
239    try {
240      latch.await();
241    } catch (InterruptedException exception) {
242      LOG.warn("Interrupted while waiting for the wal of '" + region + "' to roll. If later "
243        + "replication tests fail, it's probably because we should still be waiting.");
244      Thread.currentThread().interrupt();
245    }
246    region.getWAL().unregisterWALActionsListener(listener);
247  }
248
249  private void checkWithWait(byte[] row, int count, Table table) throws Exception {
250    Get get = new Get(row);
251    for (int i = 0; i < NB_RETRIES; i++) {
252      if (i == NB_RETRIES - 1) {
253        fail("Waited too much time while getting the row.");
254      }
255      boolean rowReplicated = false;
256      Result res = table.get(get);
257      if (res.size() >= 1) {
258        LOG.info("Row is replicated");
259        rowReplicated = true;
260        assertEquals("Table '" + table + "' did not have the expected number of  results.", count,
261          res.size());
262        break;
263      }
264      if (rowReplicated) {
265        break;
266      } else {
267        Thread.sleep(SLEEP_TIME);
268      }
269    }
270  }
271
272  private void checkRow(byte[] row, int count, Table... tables) throws IOException {
273    Get get = new Get(row);
274    for (Table table : tables) {
275      Result res = table.get(get);
276      assertEquals("Table '" + table + "' did not have the expected number of results.", count,
277        res.size());
278    }
279  }
280
281  private void deleteAndWait(byte[] row, Table source, Table... targets) throws Exception {
282    Delete del = new Delete(row);
283    source.delete(del);
284
285    Get get = new Get(row);
286    for (int i = 0; i < NB_RETRIES; i++) {
287      if (i == NB_RETRIES - 1) {
288        fail("Waited too much time for del replication");
289      }
290      boolean removedFromAll = true;
291      for (Table target : targets) {
292        Result res = target.get(get);
293        if (res.size() >= 1) {
294          LOG.info("Row not deleted");
295          removedFromAll = false;
296          break;
297        }
298      }
299      if (removedFromAll) {
300        break;
301      } else {
302        Thread.sleep(SLEEP_TIME);
303      }
304    }
305  }
306
307  private void putAndWait(byte[] row, byte[] fam, Table source, Table... targets) throws Exception {
308    Put put = new Put(row);
309    put.addColumn(fam, row, row);
310    source.put(put);
311
312    Get get = new Get(row);
313    for (int i = 0; i < NB_RETRIES; i++) {
314      if (i == NB_RETRIES - 1) {
315        fail("Waited too much time for put replication");
316      }
317      boolean replicatedToAll = true;
318      for (Table target : targets) {
319        Result res = target.get(get);
320        if (res.isEmpty()) {
321          LOG.info("Row not available");
322          replicatedToAll = false;
323          break;
324        } else {
325          assertArrayEquals(res.value(), row);
326        }
327      }
328      if (replicatedToAll) {
329        break;
330      } else {
331        Thread.sleep(SLEEP_TIME);
332      }
333    }
334  }
335
336}