Skip to content

Commit f088dd5

Browse files
murtazahassan123Apache9
authored andcommitted
HBASE-22766 Code Coverage Improvement: Create Unit Tests for ResultStatsUtil (#520)
Signed-off-by: Jan Hentschel <[email protected]> Signed-off-by: Duo Zhang <[email protected]>
1 parent 4a4208f commit f088dd5

File tree

2 files changed

+96
-38
lines changed

2 files changed

+96
-38
lines changed

hbase-client/src/main/java/org/apache/hadoop/hbase/client/ResultStatsUtil.java

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,63 +17,30 @@
1717
*/
1818
package org.apache.hadoop.hbase.client;
1919

20-
import org.apache.hadoop.hbase.HRegionLocation;
2120
import org.apache.hadoop.hbase.ServerName;
2221
import org.apache.yetus.audience.InterfaceAudience;
2322

2423
/**
25-
* A {@link Result} with some statistics about the server/region status
24+
* Statistics update about a server/region
2625
*/
2726
@InterfaceAudience.Private
2827
public final class ResultStatsUtil {
29-
3028
private ResultStatsUtil() {
3129
//private ctor for util class
3230
}
3331

3432
/**
35-
* Update the stats for the specified region if the result is an instance of {@link
36-
* ResultStatsUtil}
33+
* Update the statistics for the specified region.
3734
*
38-
* @param r object that contains the result and possibly the statistics about the region
39-
* @param serverStats stats tracker to update from the result
35+
* @param tracker tracker to update
4036
* @param server server from which the result was obtained
41-
* @param regionName full region name for the stats.
42-
* @return the underlying {@link Result} if the passed result is an {@link
43-
* ResultStatsUtil} or just returns the result;
37+
* @param regionName full region name for the statistics
38+
* @param stats statistics to update for the specified region
4439
*/
45-
public static <T> T updateStats(T r, ServerStatisticTracker serverStats,
46-
ServerName server, byte[] regionName) {
47-
if (!(r instanceof Result)) {
48-
return r;
49-
}
50-
Result result = (Result) r;
51-
// early exit if there are no stats to collect
52-
RegionLoadStats stats = result.getStats();
53-
if(stats == null){
54-
return r;
55-
}
56-
57-
updateStats(serverStats, server, regionName, stats);
58-
return r;
59-
}
60-
6140
public static void updateStats(StatisticTrackable tracker, ServerName server, byte[] regionName,
6241
RegionLoadStats stats) {
6342
if (regionName != null && stats != null && tracker != null) {
6443
tracker.updateRegionStats(server, regionName, stats);
6544
}
6645
}
67-
68-
public static <T> T updateStats(T r, ServerStatisticTracker stats,
69-
HRegionLocation regionLocation) {
70-
byte[] regionName = null;
71-
ServerName server = null;
72-
if (regionLocation != null) {
73-
server = regionLocation.getServerName();
74-
regionName = regionLocation.getRegionInfo().getRegionName();
75-
}
76-
77-
return updateStats(r, stats, server, regionName);
78-
}
7946
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNull;
22+
23+
import org.apache.hadoop.hbase.HBaseClassTestRule;
24+
import org.apache.hadoop.hbase.ServerName;
25+
import org.apache.hadoop.hbase.client.backoff.ServerStatistics;
26+
import org.apache.hadoop.hbase.testclassification.ClientTests;
27+
import org.apache.hadoop.hbase.testclassification.SmallTests;
28+
import org.junit.ClassRule;
29+
import org.junit.Test;
30+
import org.junit.experimental.categories.Category;
31+
32+
@Category({ClientTests.class, SmallTests.class})
33+
public class TestResultStatsUtil {
34+
@ClassRule
35+
public static final HBaseClassTestRule CLASS_RULE =
36+
HBaseClassTestRule.forClass(TestResultStatsUtil.class);
37+
38+
private static final RegionLoadStats regionLoadStats = new RegionLoadStats(100,
39+
10,90);
40+
private static final byte[] regionName = {80};
41+
private static final ServerName server = ServerName.parseServerName("3.1.yg.n,50,1");
42+
43+
@Test
44+
public void testUpdateStats() {
45+
// Create a tracker
46+
ServerStatisticTracker serverStatisticTracker = new ServerStatisticTracker();
47+
48+
// Pass in the tracker for update
49+
ResultStatsUtil.updateStats(serverStatisticTracker, server, regionName, regionLoadStats);
50+
51+
// Check that the tracker was updated as expected
52+
ServerStatistics stats = serverStatisticTracker.getStats(server);
53+
54+
assertEquals(regionLoadStats.memstoreLoad, stats.getStatsForRegion(regionName)
55+
.getMemStoreLoadPercent());
56+
assertEquals(regionLoadStats.compactionPressure, stats.getStatsForRegion(regionName)
57+
.getCompactionPressure());
58+
assertEquals(regionLoadStats.heapOccupancy, stats.getStatsForRegion(regionName)
59+
.getHeapOccupancyPercent());
60+
}
61+
62+
@Test
63+
public void testUpdateStatsRegionNameNull() {
64+
ServerStatisticTracker serverStatisticTracker = new ServerStatisticTracker();
65+
66+
ResultStatsUtil.updateStats(serverStatisticTracker, server, null, regionLoadStats);
67+
68+
ServerStatistics stats = serverStatisticTracker.getStats(server);
69+
assertNull(stats);
70+
}
71+
72+
@Test
73+
public void testUpdateStatsStatsNull() {
74+
ServerStatisticTracker serverStatisticTracker = new ServerStatisticTracker();
75+
76+
ResultStatsUtil.updateStats(serverStatisticTracker, server, regionName, null);
77+
78+
ServerStatistics stats = serverStatisticTracker.getStats(server);
79+
assertNull(stats);
80+
}
81+
82+
@Test
83+
public void testUpdateStatsTrackerNull() {
84+
ServerStatisticTracker serverStatisticTracker = new ServerStatisticTracker();
85+
86+
ResultStatsUtil.updateStats(null, server, regionName, regionLoadStats);
87+
88+
ServerStatistics stats = serverStatisticTracker.getStats(server);
89+
assertNull(stats);
90+
}
91+
}

0 commit comments

Comments
 (0)