View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.client;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.concurrent.CountDownLatch;
23  import java.util.concurrent.TimeUnit;
24  import java.util.concurrent.atomic.AtomicLong;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.HConstants;
31  import org.apache.hadoop.hbase.ServerName;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.client.backoff.ClientBackoffPolicy;
34  import org.apache.hadoop.hbase.client.backoff.ExponentialClientBackoffPolicy;
35  import org.apache.hadoop.hbase.client.backoff.ServerStatistics;
36  import org.apache.hadoop.hbase.client.coprocessor.Batch;
37  import org.apache.hadoop.hbase.regionserver.Region;
38  import org.apache.hadoop.hbase.testclassification.MediumTests;
39  import org.apache.hadoop.hbase.util.Bytes;
40  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
41  import org.junit.AfterClass;
42  import org.junit.BeforeClass;
43  import org.junit.Test;
44  import org.junit.experimental.categories.Category;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertNotEquals;
48  import static org.junit.Assert.assertNotNull;
49  import static org.junit.Assert.assertTrue;
50  
51  /**
52   * Test that we can actually send and use region metrics to slowdown client writes
53   */
54  @Category(MediumTests.class)
55  public class TestClientPushback {
56  
57    private static final Log LOG = LogFactory.getLog(TestClientPushback.class);
58    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
59  
60    private static final byte[] tableName = Bytes.toBytes("client-pushback");
61    private static final byte[] family = Bytes.toBytes("f");
62    private static final byte[] qualifier = Bytes.toBytes("q");
63    private static long flushSizeBytes = 1024;
64  
65    @BeforeClass
66    public static void setupCluster() throws Exception{
67      Configuration conf = UTIL.getConfiguration();
68      // enable backpressure
69      conf.setBoolean(HConstants.ENABLE_CLIENT_BACKPRESSURE, true);
70      // use the exponential backoff policy
71      conf.setClass(ClientBackoffPolicy.BACKOFF_POLICY_CLASS, ExponentialClientBackoffPolicy.class,
72        ClientBackoffPolicy.class);
73      // turn the memstore size way down so we don't need to write a lot to see changes in memstore
74      // load
75      conf.setLong(HConstants.HREGION_MEMSTORE_FLUSH_SIZE, flushSizeBytes);
76      // ensure we block the flushes when we are double that flushsize
77      conf.setLong(HConstants.HREGION_MEMSTORE_BLOCK_MULTIPLIER, HConstants.DEFAULT_HREGION_MEMSTORE_BLOCK_MULTIPLIER);
78  
79      UTIL.startMiniCluster();
80      UTIL.createTable(tableName, family);
81    }
82  
83    @AfterClass
84    public static void teardownCluster() throws Exception{
85      UTIL.shutdownMiniCluster();
86    }
87  
88    @Test(timeout=60000)
89    public void testClientTracksServerPushback() throws Exception{
90      Configuration conf = UTIL.getConfiguration();
91      TableName tablename = TableName.valueOf(tableName);
92      Connection conn = ConnectionFactory.createConnection(conf);
93      HTable table = (HTable) conn.getTable(tablename);
94      //make sure we flush after each put
95      table.setAutoFlushTo(true);
96  
97      // write some data
98      Put p = new Put(Bytes.toBytes("row"));
99      p.add(family, qualifier, Bytes.toBytes("value1"));
100     table.put(p);
101 
102     // get the stats for the region hosting our table
103     ClusterConnection connection = table.connection;
104     ClientBackoffPolicy backoffPolicy = connection.getBackoffPolicy();
105     assertTrue("Backoff policy is not correctly configured",
106       backoffPolicy instanceof ExponentialClientBackoffPolicy);
107     
108     ServerStatisticTracker stats = connection.getStatisticsTracker();
109     assertNotNull( "No stats configured for the client!", stats);
110     Region region = UTIL.getHBaseCluster().getRegionServer(0).getOnlineRegions(tablename).get(0);
111     // get the names so we can query the stats
112     ServerName server = UTIL.getHBaseCluster().getRegionServer(0).getServerName();
113     byte[] regionName = region.getRegionInfo().getRegionName();
114 
115     // check to see we found some load on the memstore
116     ServerStatistics serverStats = stats.getServerStatsForTesting(server);
117     ServerStatistics.RegionStatistics regionStats = serverStats.getStatsForRegion(regionName);
118     int load = regionStats.getMemstoreLoadPercent();
119     if (load < 11) {
120       assertEquals("Load on memstore too low", 11, load);
121     }
122 
123     // check that the load reported produces a nonzero delay
124     long backoffTime = backoffPolicy.getBackoffTime(server, regionName, serverStats);
125     assertNotEquals("Reported load does not produce a backoff", backoffTime, 0);
126     LOG.debug("Backoff calculated for " + region.getRegionInfo().getRegionNameAsString() + " @ " +
127       server + " is " + backoffTime);
128 
129     // Reach into the connection and submit work directly to AsyncProcess so we can
130     // monitor how long the submission was delayed via a callback
131     List<Row> ops = new ArrayList<Row>(1);
132     ops.add(p);
133     final CountDownLatch latch = new CountDownLatch(1);
134     final AtomicLong endTime = new AtomicLong();
135     long startTime = EnvironmentEdgeManager.currentTime();    
136     table.mutator.ap.submit(tablename, ops, true, new Batch.Callback<Result>() {
137       @Override
138       public void update(byte[] region, byte[] row, Result result) {
139         endTime.set(EnvironmentEdgeManager.currentTime());
140         latch.countDown();
141       }
142     }, true);
143     // Currently the ExponentialClientBackoffPolicy under these test conditions
144     // produces a backoffTime of 151 milliseconds. This is long enough so the
145     // wait and related checks below are reasonable. Revisit if the backoff
146     // time reported by above debug logging has significantly deviated.
147     latch.await(backoffTime * 2, TimeUnit.MILLISECONDS);
148     assertNotEquals("AsyncProcess did not submit the work time", endTime.get(), 0);
149     assertTrue("AsyncProcess did not delay long enough", endTime.get() - startTime >= backoffTime);
150   }
151 }