Skip to content

Commit c9d03c4

Browse files
authored
HBASE-25740 - Backport HBASE-25629 to branch-1 (#3130)
Signed-off-by Reid Chan <reidchan@apache.org>
1 parent b231dd6 commit c9d03c4

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CurrentHourProvider.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424

2525
@InterfaceAudience.Private
2626
public class CurrentHourProvider {
27-
private CurrentHourProvider() { throw new AssertionError(); }
27+
28+
private CurrentHourProvider() {
29+
throw new AssertionError();
30+
}
2831

2932
private static final class Tick {
3033
final int currentHour;
@@ -36,7 +39,7 @@ private static final class Tick {
3639
}
3740
}
3841

39-
static Tick nextTick() {
42+
private static Tick nextTick() {
4043
Calendar calendar = new GregorianCalendar();
4144
calendar.setTimeInMillis(EnvironmentEdgeManager.currentTime());
4245
int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
@@ -51,15 +54,20 @@ private static void moveToNextHour(Calendar calendar) {
5154
calendar.set(Calendar.MILLISECOND, 0);
5255
}
5356

54-
static volatile Tick tick = nextTick();
57+
private static volatile Tick tick = nextTick();
5558

5659
public static int getCurrentHour() {
5760
Tick tick = CurrentHourProvider.tick;
5861
if (EnvironmentEdgeManager.currentTime() < tick.expirationTimeInMillis) {
5962
return tick.currentHour;
6063
}
61-
62-
CurrentHourProvider.tick = tick = nextTick();
64+
tick = nextTick();
65+
CurrentHourProvider.tick = tick;
6366
return tick.currentHour;
6467
}
68+
69+
//RestrictedApi - only to be called for tests
70+
static void advanceTick() {
71+
tick = nextTick();
72+
}
6573
}

hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/compactions/TestCurrentHourProvider.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
import static org.junit.Assert.assertEquals;
2121

22+
import com.google.common.collect.Lists;
2223
import java.util.Date;
24+
import java.util.List;
2325
import java.util.TimeZone;
2426
import org.apache.hadoop.hbase.testclassification.RegionServerTests;
2527
import org.apache.hadoop.hbase.testclassification.SmallTests;
@@ -31,20 +33,20 @@
3133
@Category({RegionServerTests.class, SmallTests.class})
3234
public class TestCurrentHourProvider {
3335

36+
private static final List<String> ZONE_IDS = Lists.newArrayList("UTC", "US/Pacific", "Etc/GMT+8");
37+
3438
/**
35-
* In timezone GMT+08:00, the unix time of 2020-08-20 11:52:41 is 1597895561000
36-
* and the unix time of 2020-08-20 15:04:00 is 1597907081000,
37-
* by calculating the delta time to get expected time in current timezone,
38-
* then we can get special hour no matter which timezone it runs.
39-
*
40-
* In addition, we should consider the Daylight Saving Time.
41-
* 1. If in DaylightTime, we need reduce one hour.
42-
* 2. If in DaylightTime and timezone is "Antarctica/Troll", we need reduce two hours.
39+
* In timezone GMT+08:00, the unix time of 2020-08-20 11:52:41 is 1597895561000 and the unix time
40+
* of 2020-08-20 15:04:00 is 1597907081000, by calculating the delta time to get expected time in
41+
* current timezone, then we can get special hour no matter which timezone it runs.
42+
* <p/>
43+
* In addition, we should consider the Daylight Saving Time. If in DaylightTime, we need reduce
44+
* one hour.
4345
*/
4446
@Test
4547
public void testWithEnvironmentEdge() {
4648
// test for all available zoneID
47-
for (String zoneID : TimeZone.getAvailableIDs()) {
49+
for (String zoneID : ZONE_IDS) {
4850
TimeZone timezone = TimeZone.getTimeZone(zoneID);
4951
TimeZone.setDefault(timezone);
5052

@@ -57,12 +59,10 @@ public long currentTime() {
5759
return timeFor11;
5860
}
5961
});
60-
CurrentHourProvider.tick = CurrentHourProvider.nextTick();
62+
CurrentHourProvider.advanceTick();
6163
int hour11 = CurrentHourProvider.getCurrentHour();
6264
if (TimeZone.getDefault().inDaylightTime(new Date(timeFor11))) {
63-
hour11 = "Antarctica/Troll".equals(zoneID) ?
64-
CurrentHourProvider.getCurrentHour() - 2 :
65-
CurrentHourProvider.getCurrentHour() - 1;
65+
hour11 = CurrentHourProvider.getCurrentHour() - 1;
6666
}
6767
assertEquals(11, hour11);
6868

@@ -75,12 +75,10 @@ public long currentTime() {
7575
return timeFor15;
7676
}
7777
});
78-
CurrentHourProvider.tick = CurrentHourProvider.nextTick();
78+
CurrentHourProvider.advanceTick();
7979
int hour15 = CurrentHourProvider.getCurrentHour();
8080
if (TimeZone.getDefault().inDaylightTime(new Date(timeFor15))) {
81-
hour15 = "Antarctica/Troll".equals(zoneID) ?
82-
CurrentHourProvider.getCurrentHour() - 2 :
83-
CurrentHourProvider.getCurrentHour() - 1;
81+
hour15 = CurrentHourProvider.getCurrentHour() - 1;
8482
}
8583
assertEquals(15, hour15);
8684
}

0 commit comments

Comments
 (0)