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 java.util.Map;
021import org.apache.hadoop.fs.Path;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.client.Admin;
024import org.apache.hadoop.hbase.client.Get;
025import org.apache.hadoop.hbase.client.Put;
026import org.apache.hadoop.hbase.regionserver.HRegionServer;
027import org.apache.hadoop.hbase.replication.regionserver.ReplicationStatus;
028import org.apache.hadoop.hbase.testclassification.MediumTests;
029import org.apache.hadoop.hbase.testclassification.ReplicationTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.Assert;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036@Category({ ReplicationTests.class, MediumTests.class })
037public class TestReplicationMetricsforUI extends TestReplicationBase {
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040      HBaseClassTestRule.forClass(TestReplicationMetricsforUI.class);
041  private static final byte[] qualName = Bytes.toBytes("q");
042
043  @Test
044  public void testReplicationMetrics() throws Exception {
045    try (Admin hbaseAdmin = utility1.getConnection().getAdmin()) {
046      Put p = new Put(Bytes.toBytes("starter"));
047      p.addColumn(famName, qualName, Bytes.toBytes("value help to test replication delay"));
048      htable1.put(p);
049      // make sure replication done
050      while (htable2.get(new Get(Bytes.toBytes("starter"))).size() == 0) {
051        Thread.sleep(500);
052      }
053      // sleep 5 seconds to make sure timePassedAfterLastShippedOp > 2 * ageOfLastShippedOp
054      Thread.sleep(5000);
055      HRegionServer rs = utility1.getRSForFirstRegionInTable(tableName);
056      Map<String, ReplicationStatus> metrics = rs.getWalGroupsReplicationStatus();
057      Assert.assertEquals("metric size ", 1, metrics.size());
058      long lastPosition = 0;
059      for (Map.Entry<String, ReplicationStatus> metric : metrics.entrySet()) {
060        Assert.assertEquals("peerId", PEER_ID2, metric.getValue().getPeerId());
061        Assert.assertEquals("queue length", 1, metric.getValue().getQueueSize());
062        Assert.assertEquals("replication delay", 0, metric.getValue().getReplicationDelay());
063        Assert.assertTrue("current position >= 0", metric.getValue().getCurrentPosition() >= 0);
064        lastPosition = metric.getValue().getCurrentPosition();
065      }
066      for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
067        p = new Put(Bytes.toBytes("" + Integer.toString(i)));
068        p.addColumn(famName, qualName, Bytes.toBytes("value help to test replication delay " + i));
069        htable1.put(p);
070      }
071      while (htable2.get(new Get(Bytes.toBytes("" + Integer.toString(NB_ROWS_IN_BATCH - 1))))
072          .size() == 0) {
073        Thread.sleep(500);
074      }
075      rs = utility1.getRSForFirstRegionInTable(tableName);
076      metrics = rs.getWalGroupsReplicationStatus();
077      Path lastPath = null;
078      for (Map.Entry<String, ReplicationStatus> metric : metrics.entrySet()) {
079        lastPath = metric.getValue().getCurrentPath();
080        Assert.assertEquals("peerId", PEER_ID2, metric.getValue().getPeerId());
081        Assert.assertTrue("age of Last Shipped Op should be > 0 ",
082          metric.getValue().getAgeOfLastShippedOp() > 0);
083        Assert.assertTrue("current position should > last position",
084          metric.getValue().getCurrentPosition() - lastPosition > 0);
085        lastPosition = metric.getValue().getCurrentPosition();
086      }
087
088      hbaseAdmin.rollWALWriter(rs.getServerName());
089      p = new Put(Bytes.toBytes("trigger"));
090      p.addColumn(famName, qualName, Bytes.toBytes("value help to test replication delay"));
091      htable1.put(p);
092      // make sure replication rolled to a new log
093      while (htable2.get(new Get(Bytes.toBytes("trigger"))).size() == 0) {
094        Thread.sleep(500);
095      }
096      // sleep 5 seconds to make sure timePassedAfterLastShippedOp > 2 * ageOfLastShippedOp
097      Thread.sleep(5000);
098      metrics = rs.getWalGroupsReplicationStatus();
099      for (Map.Entry<String, ReplicationStatus> metric : metrics.entrySet()) {
100        Assert.assertEquals("replication delay", 0, metric.getValue().getReplicationDelay());
101        Assert.assertTrue("current position should < last position",
102          metric.getValue().getCurrentPosition() < lastPosition);
103        Assert.assertNotEquals("current path", lastPath, metric.getValue().getCurrentPath());
104      }
105    }
106  }
107}