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 */
018
019package org.apache.hadoop.hbase.replication;
020
021import org.apache.hadoop.hbase.ClusterMetrics;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.ServerMetrics;
024import org.apache.hadoop.hbase.ServerName;
025import org.apache.hadoop.hbase.Waiter;
026import org.apache.hadoop.hbase.client.Admin;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.testclassification.ReplicationTests;
029import org.junit.Assert;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033import java.io.IOException;
034import java.util.EnumSet;
035
036@Category({ ReplicationTests.class, MediumTests.class })
037public class TestReplicationStatusSink extends TestReplicationBase {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestReplicationStatusSink.class);
042
043  @Test
044  public void testReplicationStatusSink() throws Exception {
045    try (Admin admin = UTIL2.getConnection().getAdmin()) {
046      ServerName server = UTIL2.getHBaseCluster().getRegionServer(0).getServerName();
047      ReplicationLoadSink loadSink = getLatestSinkMetric(admin, server);
048      //First checks if status of timestamp of last applied op is same as RS start, since no edits
049      //were replicated yet
050      Assert.assertEquals(loadSink.getTimestampStarted(), loadSink.getTimestampsOfLastAppliedOp());
051      //now insert some rows on source, so that it gets delivered to target
052      TestReplicationStatus.insertRowsOnSource();
053      long wait =
054        Waiter.waitFor(UTIL2.getConfiguration(), 10000, (Waiter.Predicate<Exception>) () -> {
055          ReplicationLoadSink loadSink1 = getLatestSinkMetric(admin, server);
056          return loadSink1.getTimestampsOfLastAppliedOp() > loadSink1.getTimestampStarted();
057        });
058      Assert.assertNotEquals(-1, wait);
059    }
060  }
061
062  private ReplicationLoadSink getLatestSinkMetric(Admin admin, ServerName server)
063      throws IOException {
064    ClusterMetrics metrics =
065      admin.getClusterMetrics(EnumSet.of(ClusterMetrics.Option.LIVE_SERVERS));
066    ServerMetrics sm = metrics.getLiveServerMetrics().get(server);
067    return sm.getReplicationLoadSink();
068  }
069
070}