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