You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by ps...@apache.org on 2021/03/20 18:06:45 UTC

[hbase] branch branch-2.3 updated: HBASE-25629 Reimplement TestCurrentHourProvider to not depend on unstable TZs (#3013)

This is an automated email from the ASF dual-hosted git repository.

psomogyi pushed a commit to branch branch-2.3
in repository https://gitbox.apache.org/repos/asf/hbase.git


The following commit(s) were added to refs/heads/branch-2.3 by this push:
     new b6d4c9e  HBASE-25629 Reimplement TestCurrentHourProvider to not depend on unstable TZs (#3013)
b6d4c9e is described below

commit b6d4c9e1140a512fc25a8cfe7d01f58a65672755
Author: Duo Zhang <zh...@apache.org>
AuthorDate: Wed Mar 10 23:45:38 2021 +0800

    HBASE-25629 Reimplement TestCurrentHourProvider to not depend on unstable TZs (#3013)
    
    Signed-off-by: XinSun <dd...@gmail.com>
---
 .../compactions/CurrentHourProvider.java           | 21 ++++--
 .../compactions/TestCurrentHourProvider.java       | 82 ++++++++++++++++++++++
 2 files changed, 98 insertions(+), 5 deletions(-)

diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CurrentHourProvider.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CurrentHourProvider.java
index 3e30f73..ebbaa45 100644
--- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CurrentHourProvider.java
+++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CurrentHourProvider.java
@@ -17,14 +17,18 @@
  */
 package org.apache.hadoop.hbase.regionserver.compactions;
 
+import com.google.errorprone.annotations.RestrictedApi;
 import java.util.Calendar;
 import java.util.GregorianCalendar;
-
+import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
 import org.apache.yetus.audience.InterfaceAudience;
 
 @InterfaceAudience.Private
 public class CurrentHourProvider {
-  private CurrentHourProvider() { throw new AssertionError(); }
+
+  private CurrentHourProvider() {
+    throw new AssertionError();
+  }
 
   private static final class Tick {
     final int currentHour;
@@ -38,6 +42,7 @@ public class CurrentHourProvider {
 
   private static Tick nextTick() {
     Calendar calendar = new GregorianCalendar();
+    calendar.setTimeInMillis(EnvironmentEdgeManager.currentTime());
     int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
     moveToNextHour(calendar);
     return new Tick(currentHour, calendar.getTimeInMillis());
@@ -54,11 +59,17 @@ public class CurrentHourProvider {
 
   public static int getCurrentHour() {
     Tick tick = CurrentHourProvider.tick;
-    if(System.currentTimeMillis() < tick.expirationTimeInMillis) {
+    if (EnvironmentEdgeManager.currentTime() < tick.expirationTimeInMillis) {
       return tick.currentHour;
     }
-
-    CurrentHourProvider.tick = tick = nextTick();
+    tick = nextTick();
+    CurrentHourProvider.tick = tick;
     return tick.currentHour;
   }
+
+  @RestrictedApi(explanation = "Should only be called in tests", link = "",
+    allowedOnPath = ".*/src/test/.*")
+  static void advanceTick() {
+    tick = nextTick();
+  }
 }
diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/compactions/TestCurrentHourProvider.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/compactions/TestCurrentHourProvider.java
new file mode 100644
index 0000000..4a0e1d0
--- /dev/null
+++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/compactions/TestCurrentHourProvider.java
@@ -0,0 +1,82 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.regionserver.compactions;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Date;
+import java.util.List;
+import java.util.TimeZone;
+import org.apache.hadoop.hbase.HBaseClassTestRule;
+import org.apache.hadoop.hbase.testclassification.RegionServerTests;
+import org.apache.hadoop.hbase.testclassification.SmallTests;
+import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
+
+@Category({ RegionServerTests.class, SmallTests.class })
+public class TestCurrentHourProvider {
+
+  @ClassRule
+  public static final HBaseClassTestRule CLASS_RULE =
+    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 and the unix time
+   * of 2020-08-20 15:04:00 is 1597907081000, by calculating the delta time to get expected time in
+   * current timezone, 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
+   * one hour.
+   */
+  @Test
+  public void testWithEnvironmentEdge() {
+    // test for all available zoneID
+    for (String zoneID : ZONE_IDS) {
+      TimeZone timezone = TimeZone.getTimeZone(zoneID);
+      TimeZone.setDefault(timezone);
+
+      // set a time represent hour 11
+      long deltaFor11 = TimeZone.getDefault().getRawOffset() - 28800000;
+      long timeFor11 = 1597895561000L - deltaFor11;
+      EnvironmentEdgeManager.injectEdge(() -> timeFor11);
+      CurrentHourProvider.advanceTick();
+      int hour11 = CurrentHourProvider.getCurrentHour();
+      if (TimeZone.getDefault().inDaylightTime(new Date(timeFor11))) {
+        hour11 = CurrentHourProvider.getCurrentHour() - 1;
+      }
+      assertEquals(11, hour11);
+
+      // set a time represent hour 15
+      long deltaFor15 = TimeZone.getDefault().getRawOffset() - 28800000;
+      long timeFor15 = 1597907081000L - deltaFor15;
+      EnvironmentEdgeManager.injectEdge(() -> timeFor15);
+      CurrentHourProvider.advanceTick();
+      int hour15 = CurrentHourProvider.getCurrentHour();
+      if (TimeZone.getDefault().inDaylightTime(new Date(timeFor15))) {
+        hour15 = CurrentHourProvider.getCurrentHour() - 1;
+      }
+      assertEquals(15, hour15);
+    }
+  }
+}