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    ReplicationAdmin admin1 = new ReplicationAdmin(conf1);
102    ReplicationPeerConfig rpc = new ReplicationPeerConfig();
103    rpc.setClusterKey(utility2.getClusterKey());
104
105    utility1.startMiniCluster();
106    utility2.startMiniCluster();
107
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
121  volatile private boolean testQuotaPass = false;
122  volatile private boolean testQuotaNonZero = false;
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()
135          .getRegionServer(0).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 the
144        // batch size (after added last entry) exceeds quota, it still keeps the last one in the batch
145        // so total used buffer size can be one "replication.total.buffer.quota" larger than expected
146        if (size > REPLICATION_SOURCE_QUOTA * (numOfPeer + 1)) {
147          // We read logs first then check throttler, so if the buffer quota limiter doesn't
148          // take effect, it will push many logs and exceed the quota.
149          testQuotaPass = false;
150        }
151        Threads.sleep(50);
152      }
153    });
154    watcher.start();
155
156    try(Table t1 = utility1.getConnection().getTable(tableName);
157        Table t2 = utility2.getConnection().getTable(tableName)) {
158      for (int i = 0; i < 50; i++) {
159        Put put = new Put(ROWS[i]);
160        put.addColumn(famName, VALUE, VALUE);
161        t1.put(put);
162      }
163      long start = EnvironmentEdgeManager.currentTime();
164      while (EnvironmentEdgeManager.currentTime() - start < 180000) {
165        Scan scan = new Scan();
166        scan.setCaching(50);
167        int count = 0;
168        try (ResultScanner results = t2.getScanner(scan)) {
169          for (Result result : results) {
170            count++;
171          }
172        }
173        if (count < 50) {
174          LOG.info("Waiting all logs pushed to slave. Expected 50 , actual " + count);
175          Threads.sleep(200);
176          continue;
177        }
178        break;
179      }
180    }
181
182    watcher.interrupt();
183    Assert.assertTrue(testQuotaPass);
184    Assert.assertTrue(testQuotaNonZero);
185  }
186
187
188}