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.HBaseTestingUtility;
026import org.apache.hadoop.hbase.HColumnDescriptor;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.HTableDescriptor;
029import org.apache.hadoop.hbase.HTestConst;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.Put;
032import org.apache.hadoop.hbase.client.Result;
033import org.apache.hadoop.hbase.client.ResultScanner;
034import org.apache.hadoop.hbase.client.Scan;
035import org.apache.hadoop.hbase.client.Table;
036import org.apache.hadoop.hbase.client.replication.ReplicationAdmin;
037import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
038import org.apache.hadoop.hbase.testclassification.LargeTests;
039import org.apache.hadoop.hbase.testclassification.ReplicationTests;
040import org.apache.hadoop.hbase.util.Bytes;
041import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
042import org.apache.hadoop.hbase.util.Threads;
043import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
044import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
045import org.junit.AfterClass;
046import org.junit.Assert;
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
056@Category({ ReplicationTests.class, LargeTests.class })
057public class TestGlobalReplicationThrottler {
058
059  @ClassRule
060  public static final HBaseClassTestRule CLASS_RULE =
061    HBaseClassTestRule.forClass(TestGlobalReplicationThrottler.class);
062
063  private static final Logger LOG = LoggerFactory.getLogger(TestGlobalReplicationThrottler.class);
064  private static final int REPLICATION_SOURCE_QUOTA = 200;
065  private static int numOfPeer = 0;
066  private static Configuration conf1;
067  private static Configuration conf2;
068
069  private static HBaseTestingUtility utility1;
070  private static HBaseTestingUtility utility2;
071
072  private static final byte[] famName = Bytes.toBytes("f");
073  private static final byte[] VALUE = Bytes.toBytes("v");
074  private static final byte[] ROW = Bytes.toBytes("r");
075  private static final byte[][] ROWS = HTestConst.makeNAscii(ROW, 100);
076
077  @Rule
078  public TestName name = new TestName();
079
080  @BeforeClass
081  public static void setUpBeforeClass() throws Exception {
082    conf1 = HBaseConfiguration.create();
083    conf1.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
084    conf1.setLong("replication.source.sleepforretries", 100);
085    // Each WAL is about 120 bytes
086    conf1.setInt(HConstants.REPLICATION_SOURCE_TOTAL_BUFFER_KEY, REPLICATION_SOURCE_QUOTA);
087    conf1.setLong("replication.source.per.peer.node.bandwidth", 100L);
088
089    utility1 = new HBaseTestingUtility(conf1);
090    utility1.startMiniZKCluster();
091    MiniZooKeeperCluster miniZK = utility1.getZkCluster();
092    new ZKWatcher(conf1, "cluster1", null, true);
093
094    conf2 = new Configuration(conf1);
095    conf2.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
096
097    utility2 = new HBaseTestingUtility(conf2);
098    utility2.setZkCluster(miniZK);
099    new ZKWatcher(conf2, "cluster2", null, true);
100
101    ReplicationPeerConfig rpc = new ReplicationPeerConfig();
102    rpc.setClusterKey(utility2.getClusterKey());
103
104    utility1.startMiniCluster();
105    utility2.startMiniCluster();
106
107    ReplicationAdmin admin1 = new ReplicationAdmin(conf1);
108    admin1.addPeer("peer1", rpc, null);
109    admin1.addPeer("peer2", rpc, null);
110    admin1.addPeer("peer3", rpc, null);
111    numOfPeer = admin1.getPeersCount();
112  }
113
114  @AfterClass
115  public static void tearDownAfterClass() throws Exception {
116    utility2.shutdownMiniCluster();
117    utility1.shutdownMiniCluster();
118  }
119
120  volatile private boolean testQuotaPass = false;
121  volatile private boolean testQuotaNonZero = false;
122
123  @Test
124  public void testQuota() throws IOException {
125    final TableName tableName = TableName.valueOf(name.getMethodName());
126    HTableDescriptor table = new HTableDescriptor(tableName);
127    HColumnDescriptor fam = new HColumnDescriptor(famName);
128    fam.setScope(HConstants.REPLICATION_SCOPE_GLOBAL);
129    table.addFamily(fam);
130    utility1.getAdmin().createTable(table);
131    utility2.getAdmin().createTable(table);
132
133    Thread watcher = new Thread(() -> {
134      Replication replication = (Replication) utility1.getMiniHBaseCluster().getRegionServer(0)
135        .getReplicationSourceService();
136      AtomicLong bufferUsed = replication.getReplicationManager().getTotalBufferUsed();
137      testQuotaPass = true;
138      while (!Thread.interrupted()) {
139        long size = bufferUsed.get();
140        if (size > 0) {
141          testQuotaNonZero = true;
142        }
143        // the reason here doing "numOfPeer + 1" is because by using method addEntryToBatch(), even
144        // the
145        // batch size (after added last entry) exceeds quota, it still keeps the last one in the
146        // batch
147        // 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      Table t2 = utility2.getConnection().getTable(tableName)) {
161      for (int i = 0; i < 50; i++) {
162        Put put = new Put(ROWS[i]);
163        put.addColumn(famName, VALUE, VALUE);
164        t1.put(put);
165      }
166      long start = EnvironmentEdgeManager.currentTime();
167      while (EnvironmentEdgeManager.currentTime() - start < 180000) {
168        Scan scan = new Scan();
169        scan.setCaching(50);
170        int count = 0;
171        try (ResultScanner results = t2.getScanner(scan)) {
172          for (Result result : results) {
173            count++;
174          }
175        }
176        if (count < 50) {
177          LOG.info("Waiting all logs pushed to slave. Expected 50 , actual " + count);
178          Threads.sleep(200);
179          continue;
180        }
181        break;
182      }
183    }
184
185    watcher.interrupt();
186    Assert.assertTrue(testQuotaPass);
187    Assert.assertTrue(testQuotaNonZero);
188  }
189
190}