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.regionserver;
019
020import static org.junit.Assert.assertTrue;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseConfiguration;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.HTestConst;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.Admin;
030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
031import org.apache.hadoop.hbase.client.Connection;
032import org.apache.hadoop.hbase.client.ConnectionFactory;
033import org.apache.hadoop.hbase.client.Put;
034import org.apache.hadoop.hbase.client.ResultScanner;
035import org.apache.hadoop.hbase.client.Scan;
036import org.apache.hadoop.hbase.client.Table;
037import org.apache.hadoop.hbase.client.TableDescriptor;
038import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
039import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
040import org.apache.hadoop.hbase.testclassification.LargeTests;
041import org.apache.hadoop.hbase.testclassification.ReplicationTests;
042import org.apache.hadoop.hbase.util.Bytes;
043import org.apache.hadoop.hbase.util.Threads;
044import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
045import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
046import org.junit.AfterClass;
047import org.junit.BeforeClass;
048import org.junit.ClassRule;
049import org.junit.Rule;
050import org.junit.Test;
051import org.junit.experimental.categories.Category;
052import org.junit.rules.TestName;
053import org.slf4j.Logger;
054import org.slf4j.LoggerFactory;
055
056import org.apache.hbase.thirdparty.com.google.common.collect.Iterables;
057
058@Category({ ReplicationTests.class, LargeTests.class })
059public class TestGlobalReplicationThrottler {
060
061  @ClassRule
062  public static final HBaseClassTestRule CLASS_RULE =
063    HBaseClassTestRule.forClass(TestGlobalReplicationThrottler.class);
064
065  private static final Logger LOG = LoggerFactory.getLogger(TestGlobalReplicationThrottler.class);
066  private static final int REPLICATION_SOURCE_QUOTA = 200;
067  private static int numOfPeer = 0;
068  private static Configuration conf1;
069  private static Configuration conf2;
070
071  private static HBaseTestingUtil utility1;
072  private static HBaseTestingUtil utility2;
073
074  private static final byte[] famName = Bytes.toBytes("f");
075  private static final byte[] VALUE = Bytes.toBytes("v");
076  private static final byte[] ROW = Bytes.toBytes("r");
077  private static final byte[][] ROWS = HTestConst.makeNAscii(ROW, 100);
078
079  @Rule
080  public TestName name = new TestName();
081
082  @BeforeClass
083  public static void setUpBeforeClass() throws Exception {
084    conf1 = HBaseConfiguration.create();
085    conf1.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
086    conf1.setLong("replication.source.sleepforretries", 100);
087    // Each WAL is about 120 bytes
088    conf1.setInt(HConstants.REPLICATION_SOURCE_TOTAL_BUFFER_KEY, REPLICATION_SOURCE_QUOTA);
089    conf1.setLong("replication.source.per.peer.node.bandwidth", 100L);
090
091    utility1 = new HBaseTestingUtil(conf1);
092    utility1.startMiniZKCluster();
093    MiniZooKeeperCluster miniZK = utility1.getZkCluster();
094    new ZKWatcher(conf1, "cluster1", null, true).close();
095
096    conf2 = new Configuration(conf1);
097    conf2.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
098
099    utility2 = new HBaseTestingUtil(conf2);
100    utility2.setZkCluster(miniZK);
101    new ZKWatcher(conf2, "cluster2", null, true).close();
102
103    utility1.startMiniCluster();
104    utility2.startMiniCluster();
105
106    ReplicationPeerConfig rpc =
107      ReplicationPeerConfig.newBuilder().setClusterKey(utility2.getRpcConnnectionURI()).build();
108
109    try (Connection connection = ConnectionFactory.createConnection(utility1.getConfiguration());
110      Admin admin1 = connection.getAdmin()) {
111      admin1.addReplicationPeer("peer1", rpc);
112      admin1.addReplicationPeer("peer2", rpc);
113      admin1.addReplicationPeer("peer3", rpc);
114      numOfPeer = admin1.listReplicationPeers().size();
115    }
116  }
117
118  @AfterClass
119  public static void tearDownAfterClass() throws Exception {
120    utility2.shutdownMiniCluster();
121    utility1.shutdownMiniCluster();
122  }
123
124  private volatile boolean testQuotaPass = false;
125  private volatile boolean testQuotaNonZero = false;
126
127  @Test
128  public void testQuota() throws Exception {
129    final TableName tableName = TableName.valueOf(name.getMethodName());
130    TableDescriptor tableDescriptor =
131      TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(ColumnFamilyDescriptorBuilder
132        .newBuilder(famName).setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build()).build();
133    utility1.getAdmin().createTable(tableDescriptor);
134    utility2.getAdmin().createTable(tableDescriptor);
135
136    Thread watcher = new Thread(() -> {
137      Replication replication = (Replication) utility1.getMiniHBaseCluster().getRegionServer(0)
138        .getReplicationSourceService();
139      testQuotaPass = true;
140      while (!Thread.interrupted()) {
141        long size = replication.getReplicationManager().getTotalBufferUsed();
142        if (size > 0) {
143          testQuotaNonZero = true;
144        }
145        // the reason here doing "numOfPeer + 1" is because by using method addEntryToBatch(), even
146        // the batch size (after added last entry) exceeds quota, it still keeps the last one in the
147        // batch so total used buffer size can be one "replication.total.buffer.quota" larger than
148        // expected
149        if (size > REPLICATION_SOURCE_QUOTA * (numOfPeer + 1)) {
150          // We read logs first then check throttler, so if the buffer quota limiter doesn't
151          // take effect, it will push many logs and exceed the quota.
152          testQuotaPass = false;
153        }
154        Threads.sleep(50);
155      }
156    });
157    watcher.start();
158
159    try (Table t1 = utility1.getConnection().getTable(tableName)) {
160      for (int i = 0; i < 50; i++) {
161        Put put = new Put(ROWS[i]);
162        put.addColumn(famName, VALUE, VALUE);
163        t1.put(put);
164      }
165    }
166    utility2.waitFor(180000, () -> {
167      try (Table t2 = utility2.getConnection().getTable(tableName);
168        ResultScanner results = t2.getScanner(new Scan().setCaching(50))) {
169        int count = Iterables.size(results);
170        return count >= 50;
171      }
172    });
173
174    watcher.interrupt();
175    assertTrue(testQuotaPass);
176    assertTrue(testQuotaNonZero);
177  }
178
179}