HBASE-25629 Reimplement TestCurrentHourProvider to not depend on unstable TZs (#3013)
Signed-off-by: XinSun <ddupgs@gmail.com>
This commit is contained in:
parent
7adc00044c
commit
d546c8e77c
|
@ -17,15 +17,18 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.hadoop.hbase.regionserver.compactions;
|
package org.apache.hadoop.hbase.regionserver.compactions;
|
||||||
|
|
||||||
|
import com.google.errorprone.annotations.RestrictedApi;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
|
|
||||||
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
|
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
|
||||||
import org.apache.yetus.audience.InterfaceAudience;
|
import org.apache.yetus.audience.InterfaceAudience;
|
||||||
|
|
||||||
@InterfaceAudience.Private
|
@InterfaceAudience.Private
|
||||||
public class CurrentHourProvider {
|
public class CurrentHourProvider {
|
||||||
private CurrentHourProvider() { throw new AssertionError(); }
|
|
||||||
|
private CurrentHourProvider() {
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
|
|
||||||
private static final class Tick {
|
private static final class Tick {
|
||||||
final int currentHour;
|
final int currentHour;
|
||||||
|
@ -37,7 +40,7 @@ public class CurrentHourProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Tick nextTick() {
|
private static Tick nextTick() {
|
||||||
Calendar calendar = new GregorianCalendar();
|
Calendar calendar = new GregorianCalendar();
|
||||||
calendar.setTimeInMillis(EnvironmentEdgeManager.currentTime());
|
calendar.setTimeInMillis(EnvironmentEdgeManager.currentTime());
|
||||||
int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
|
int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
|
||||||
|
@ -52,15 +55,21 @@ public class CurrentHourProvider {
|
||||||
calendar.set(Calendar.MILLISECOND, 0);
|
calendar.set(Calendar.MILLISECOND, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static volatile Tick tick = nextTick();
|
private static volatile Tick tick = nextTick();
|
||||||
|
|
||||||
public static int getCurrentHour() {
|
public static int getCurrentHour() {
|
||||||
Tick tick = CurrentHourProvider.tick;
|
Tick tick = CurrentHourProvider.tick;
|
||||||
if (EnvironmentEdgeManager.currentTime() < tick.expirationTimeInMillis) {
|
if (EnvironmentEdgeManager.currentTime() < tick.expirationTimeInMillis) {
|
||||||
return tick.currentHour;
|
return tick.currentHour;
|
||||||
}
|
}
|
||||||
|
tick = nextTick();
|
||||||
CurrentHourProvider.tick = tick = nextTick();
|
CurrentHourProvider.tick = tick;
|
||||||
return tick.currentHour;
|
return tick.currentHour;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RestrictedApi(explanation = "Should only be called in tests", link = "",
|
||||||
|
allowedOnPath = ".*/src/test/.*")
|
||||||
|
static void advanceTick() {
|
||||||
|
tick = nextTick();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,37 +20,39 @@ package org.apache.hadoop.hbase.regionserver.compactions;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
import org.apache.hadoop.hbase.HBaseClassTestRule;
|
import org.apache.hadoop.hbase.HBaseClassTestRule;
|
||||||
import org.apache.hadoop.hbase.testclassification.RegionServerTests;
|
import org.apache.hadoop.hbase.testclassification.RegionServerTests;
|
||||||
import org.apache.hadoop.hbase.testclassification.SmallTests;
|
import org.apache.hadoop.hbase.testclassification.SmallTests;
|
||||||
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
|
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
|
||||||
import org.junit.ClassRule;
|
import org.junit.ClassRule;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.experimental.categories.Category;
|
import org.junit.experimental.categories.Category;
|
||||||
|
|
||||||
@Category({RegionServerTests.class, SmallTests.class})
|
import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
|
||||||
@Ignore("See HBASE-25385")
|
|
||||||
|
@Category({ RegionServerTests.class, SmallTests.class })
|
||||||
public class TestCurrentHourProvider {
|
public class TestCurrentHourProvider {
|
||||||
|
|
||||||
@ClassRule
|
@ClassRule
|
||||||
public static final HBaseClassTestRule CLASS_RULE =
|
public static final HBaseClassTestRule CLASS_RULE =
|
||||||
HBaseClassTestRule.forClass(TestCurrentHourProvider.class);
|
HBaseClassTestRule.forClass(TestCurrentHourProvider.class);
|
||||||
|
|
||||||
|
private static final List<String> ZONE_IDS = Lists.newArrayList("UTC", "US/Pacific", "Etc/GMT+8");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* In timezone GMT+08:00, the unix time of 2020-08-20 11:52:41 is 1597895561000
|
* In timezone GMT+08:00, the unix time of 2020-08-20 11:52:41 is 1597895561000 and the unix time
|
||||||
* and the unix time of 2020-08-20 15:04:00 is 1597907081000,
|
* of 2020-08-20 15:04:00 is 1597907081000, by calculating the delta time to get expected time in
|
||||||
* by calculating the delta time to get expected time in current timezone,
|
* current timezone, then we can get special hour no matter which timezone it runs.
|
||||||
* then we can get special hour no matter which timezone it runs.
|
* <p/>
|
||||||
*
|
* In addition, we should consider the Daylight Saving Time. If in DaylightTime, we need reduce
|
||||||
* In addition, we should consider the Daylight Saving Time.
|
* one hour.
|
||||||
* 1. If in DaylightTime, we need reduce one hour.
|
|
||||||
* 2. If in DaylightTime and timezone is "Antarctica/Troll", we need reduce two hours.
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testWithEnvironmentEdge() {
|
public void testWithEnvironmentEdge() {
|
||||||
// test for all available zoneID
|
// test for all available zoneID
|
||||||
for (String zoneID : TimeZone.getAvailableIDs()) {
|
for (String zoneID : ZONE_IDS) {
|
||||||
TimeZone timezone = TimeZone.getTimeZone(zoneID);
|
TimeZone timezone = TimeZone.getTimeZone(zoneID);
|
||||||
TimeZone.setDefault(timezone);
|
TimeZone.setDefault(timezone);
|
||||||
|
|
||||||
|
@ -58,12 +60,10 @@ public class TestCurrentHourProvider {
|
||||||
long deltaFor11 = TimeZone.getDefault().getRawOffset() - 28800000;
|
long deltaFor11 = TimeZone.getDefault().getRawOffset() - 28800000;
|
||||||
long timeFor11 = 1597895561000L - deltaFor11;
|
long timeFor11 = 1597895561000L - deltaFor11;
|
||||||
EnvironmentEdgeManager.injectEdge(() -> timeFor11);
|
EnvironmentEdgeManager.injectEdge(() -> timeFor11);
|
||||||
CurrentHourProvider.tick = CurrentHourProvider.nextTick();
|
CurrentHourProvider.advanceTick();
|
||||||
int hour11 = CurrentHourProvider.getCurrentHour();
|
int hour11 = CurrentHourProvider.getCurrentHour();
|
||||||
if (TimeZone.getDefault().inDaylightTime(new Date(timeFor11))) {
|
if (TimeZone.getDefault().inDaylightTime(new Date(timeFor11))) {
|
||||||
hour11 = "Antarctica/Troll".equals(zoneID) ?
|
hour11 = CurrentHourProvider.getCurrentHour() - 1;
|
||||||
CurrentHourProvider.getCurrentHour() - 2 :
|
|
||||||
CurrentHourProvider.getCurrentHour() - 1;
|
|
||||||
}
|
}
|
||||||
assertEquals(11, hour11);
|
assertEquals(11, hour11);
|
||||||
|
|
||||||
|
@ -71,12 +71,10 @@ public class TestCurrentHourProvider {
|
||||||
long deltaFor15 = TimeZone.getDefault().getRawOffset() - 28800000;
|
long deltaFor15 = TimeZone.getDefault().getRawOffset() - 28800000;
|
||||||
long timeFor15 = 1597907081000L - deltaFor15;
|
long timeFor15 = 1597907081000L - deltaFor15;
|
||||||
EnvironmentEdgeManager.injectEdge(() -> timeFor15);
|
EnvironmentEdgeManager.injectEdge(() -> timeFor15);
|
||||||
CurrentHourProvider.tick = CurrentHourProvider.nextTick();
|
CurrentHourProvider.advanceTick();
|
||||||
int hour15 = CurrentHourProvider.getCurrentHour();
|
int hour15 = CurrentHourProvider.getCurrentHour();
|
||||||
if (TimeZone.getDefault().inDaylightTime(new Date(timeFor15))) {
|
if (TimeZone.getDefault().inDaylightTime(new Date(timeFor15))) {
|
||||||
hour15 = "Antarctica/Troll".equals(zoneID) ?
|
hour15 = CurrentHourProvider.getCurrentHour() - 1;
|
||||||
CurrentHourProvider.getCurrentHour() - 2 :
|
|
||||||
CurrentHourProvider.getCurrentHour() - 1;
|
|
||||||
}
|
}
|
||||||
assertEquals(15, hour15);
|
assertEquals(15, hour15);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue