Skip to content

Commit d79019b

Browse files
authored
HBASE-25629 Reimplement TestCurrentHourProvider to not depend on unstable TZs (#3013)
Signed-off-by: XinSun <[email protected]>
1 parent 373dc77 commit d79019b

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
1717
*/
1818
package org.apache.hadoop.hbase.regionserver.compactions;
1919

20+
import com.google.errorprone.annotations.RestrictedApi;
2021
import java.util.Calendar;
2122
import java.util.GregorianCalendar;
22-
2323
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
2424
import org.apache.yetus.audience.InterfaceAudience;
2525

2626
@InterfaceAudience.Private
2727
public class CurrentHourProvider {
28-
private CurrentHourProvider() { throw new AssertionError(); }
28+
29+
private CurrentHourProvider() {
30+
throw new AssertionError();
31+
}
2932

3033
private static final class Tick {
3134
final int currentHour;
@@ -37,7 +40,7 @@ private static final class Tick {
3740
}
3841
}
3942

40-
static Tick nextTick() {
43+
private static Tick nextTick() {
4144
Calendar calendar = new GregorianCalendar();
4245
calendar.setTimeInMillis(EnvironmentEdgeManager.currentTime());
4346
int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
@@ -52,15 +55,21 @@ private static void moveToNextHour(Calendar calendar) {
5255
calendar.set(Calendar.MILLISECOND, 0);
5356
}
5457

55-
static volatile Tick tick = nextTick();
58+
private static volatile Tick tick = nextTick();
5659

5760
public static int getCurrentHour() {
5861
Tick tick = CurrentHourProvider.tick;
5962
if (EnvironmentEdgeManager.currentTime() < tick.expirationTimeInMillis) {
6063
return tick.currentHour;
6164
}
62-
63-
CurrentHourProvider.tick = tick = nextTick();
65+
tick = nextTick();
66+
CurrentHourProvider.tick = tick;
6467
return tick.currentHour;
6568
}
69+
70+
@RestrictedApi(explanation = "Should only be called in tests", link = "",
71+
allowedOnPath = ".*/src/test/.*")
72+
static void advanceTick() {
73+
tick = nextTick();
74+
}
6675
}

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,63 +20,61 @@
2020
import static org.junit.Assert.assertEquals;
2121

2222
import java.util.Date;
23+
import java.util.List;
2324
import java.util.TimeZone;
2425
import org.apache.hadoop.hbase.HBaseClassTestRule;
2526
import org.apache.hadoop.hbase.testclassification.RegionServerTests;
2627
import org.apache.hadoop.hbase.testclassification.SmallTests;
2728
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
2829
import org.junit.ClassRule;
29-
import org.junit.Ignore;
3030
import org.junit.Test;
3131
import org.junit.experimental.categories.Category;
3232

33-
@Category({RegionServerTests.class, SmallTests.class})
34-
@Ignore("See HBASE-25385")
33+
import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
34+
35+
@Category({ RegionServerTests.class, SmallTests.class })
3536
public class TestCurrentHourProvider {
37+
3638
@ClassRule
3739
public static final HBaseClassTestRule CLASS_RULE =
38-
HBaseClassTestRule.forClass(TestCurrentHourProvider.class);
40+
HBaseClassTestRule.forClass(TestCurrentHourProvider.class);
41+
42+
private static final List<String> ZONE_IDS = Lists.newArrayList("UTC", "US/Pacific", "Etc/GMT+8");
3943

4044
/**
41-
* In timezone GMT+08:00, the unix time of 2020-08-20 11:52:41 is 1597895561000
42-
* and the unix time of 2020-08-20 15:04:00 is 1597907081000,
43-
* by calculating the delta time to get expected time in current timezone,
44-
* then we can get special hour no matter which timezone it runs.
45-
*
46-
* In addition, we should consider the Daylight Saving Time.
47-
* 1. If in DaylightTime, we need reduce one hour.
48-
* 2. If in DaylightTime and timezone is "Antarctica/Troll", we need reduce two hours.
45+
* In timezone GMT+08:00, the unix time of 2020-08-20 11:52:41 is 1597895561000 and the unix time
46+
* of 2020-08-20 15:04:00 is 1597907081000, by calculating the delta time to get expected time in
47+
* current timezone, then we can get special hour no matter which timezone it runs.
48+
* <p/>
49+
* In addition, we should consider the Daylight Saving Time. If in DaylightTime, we need reduce
50+
* one hour.
4951
*/
5052
@Test
5153
public void testWithEnvironmentEdge() {
5254
// test for all available zoneID
53-
for (String zoneID : TimeZone.getAvailableIDs()) {
55+
for (String zoneID : ZONE_IDS) {
5456
TimeZone timezone = TimeZone.getTimeZone(zoneID);
5557
TimeZone.setDefault(timezone);
5658

5759
// set a time represent hour 11
5860
long deltaFor11 = TimeZone.getDefault().getRawOffset() - 28800000;
5961
long timeFor11 = 1597895561000L - deltaFor11;
6062
EnvironmentEdgeManager.injectEdge(() -> timeFor11);
61-
CurrentHourProvider.tick = CurrentHourProvider.nextTick();
63+
CurrentHourProvider.advanceTick();
6264
int hour11 = CurrentHourProvider.getCurrentHour();
6365
if (TimeZone.getDefault().inDaylightTime(new Date(timeFor11))) {
64-
hour11 = "Antarctica/Troll".equals(zoneID) ?
65-
CurrentHourProvider.getCurrentHour() - 2 :
66-
CurrentHourProvider.getCurrentHour() - 1;
66+
hour11 = CurrentHourProvider.getCurrentHour() - 1;
6767
}
6868
assertEquals(11, hour11);
6969

7070
// set a time represent hour 15
7171
long deltaFor15 = TimeZone.getDefault().getRawOffset() - 28800000;
7272
long timeFor15 = 1597907081000L - deltaFor15;
7373
EnvironmentEdgeManager.injectEdge(() -> timeFor15);
74-
CurrentHourProvider.tick = CurrentHourProvider.nextTick();
74+
CurrentHourProvider.advanceTick();
7575
int hour15 = CurrentHourProvider.getCurrentHour();
7676
if (TimeZone.getDefault().inDaylightTime(new Date(timeFor15))) {
77-
hour15 = "Antarctica/Troll".equals(zoneID) ?
78-
CurrentHourProvider.getCurrentHour() - 2 :
79-
CurrentHourProvider.getCurrentHour() - 1;
77+
hour15 = CurrentHourProvider.getCurrentHour() - 1;
8078
}
8179
assertEquals(15, hour15);
8280
}

0 commit comments

Comments
 (0)