Skip to content

Commit 49d4c73

Browse files
authored
YARN-11765. Refactor: Move Clock Class from hadoop-mapreduce-project to hadoop-common-project for Reusability (#7352) Contributed by Jiandan Yang.
Refactor: Move Clock Class from hadoop-mapreduce-project to hadoop-common-project for Reusability Reviewed-by: Chris Nauroth <[email protected]> Reviewed-by: Steve Loughran <[email protected]> Signed-off-by: Shilun Fan <[email protected]>
1 parent ef9043f commit 49d4c73

File tree

135 files changed

+340
-217
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+340
-217
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
19+
package org.apache.hadoop.util;
20+
21+
import org.apache.hadoop.classification.InterfaceAudience.Public;
22+
import org.apache.hadoop.classification.InterfaceStability.Stable;
23+
24+
/**
25+
* A simple clock interface that gives you time.
26+
*/
27+
@Public
28+
@Stable
29+
public interface Clock {
30+
31+
long getTime();
32+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.util;
19+
20+
import org.apache.hadoop.classification.InterfaceAudience.Public;
21+
import org.apache.hadoop.classification.InterfaceStability.Evolving;
22+
23+
/**
24+
* A monotonic clock from some arbitrary time base in the past, counting in
25+
* milliseconds, and not affected by settimeofday or similar system clock
26+
* changes.
27+
* This is appropriate to use when computing how much longer to wait for an
28+
* interval to expire.
29+
* This function can return a negative value and it must be handled correctly
30+
* by callers. See the documentation of System#nanoTime for caveats.
31+
*/
32+
@Public
33+
@Evolving
34+
public class MonotonicClock implements Clock {
35+
36+
/**
37+
* Get current time from some arbitrary time base in the past, counting in
38+
* milliseconds, and not affected by settimeofday or similar system clock
39+
* changes.
40+
* @return a monotonic clock that counts in milliseconds.
41+
*/
42+
public long getTime() {
43+
return Time.monotonicNow();
44+
}
45+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.util;
19+
20+
import org.apache.hadoop.classification.InterfaceAudience.Public;
21+
import org.apache.hadoop.classification.InterfaceStability.Stable;
22+
23+
/**
24+
* Implementation of {@link Clock} that gives the current time from the system
25+
* clock in milliseconds.
26+
*
27+
* NOTE: Do not use this to calculate a duration of expire or interval to sleep,
28+
* because it will be broken by settimeofday. Please use {@link MonotonicClock}
29+
* instead.
30+
*/
31+
@Public
32+
@Stable
33+
public final class SystemClock implements Clock {
34+
35+
private static final SystemClock INSTANCE = new SystemClock();
36+
37+
public static SystemClock getInstance() {
38+
return INSTANCE;
39+
}
40+
41+
private SystemClock() {
42+
// do nothing
43+
}
44+
45+
public long getTime() {
46+
return System.currentTimeMillis();
47+
}
48+
}
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,25 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
package org.apache.hadoop.mapred;
19+
package org.apache.hadoop.util;
2020

21-
import org.junit.jupiter.api.Test;
22-
import org.junit.jupiter.api.Timeout;
21+
import java.util.Calendar;
22+
import java.util.TimeZone;
2323

24-
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import org.apache.hadoop.classification.InterfaceAudience.Public;
25+
import org.apache.hadoop.classification.InterfaceStability.Evolving;
2526

2627
/**
27-
* test Clock class
28+
* Implementation of {@link Clock} that gives the current UTC time in
29+
* milliseconds.
2830
*/
29-
public class TestClock {
31+
@Public
32+
@Evolving
33+
public class UTCClock implements Clock {
3034

31-
@Test
32-
@Timeout(value = 10)
33-
public void testClock(){
34-
Clock clock = new Clock();
35-
long templateTime = System.currentTimeMillis();
36-
long time = clock.getTime();
37-
assertEquals(templateTime, time, 30);
35+
private final TimeZone utcZone = TimeZone.getTimeZone("UTC");
36+
37+
public long getTime() {
38+
return Calendar.getInstance(utcZone).getTimeInMillis();
3839
}
39-
}
40+
}

hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapred/MapTaskAttemptImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.apache.hadoop.security.Credentials;
3131
import org.apache.hadoop.security.token.Token;
3232
import org.apache.hadoop.yarn.event.EventHandler;
33-
import org.apache.hadoop.yarn.util.Clock;
33+
import org.apache.hadoop.util.Clock;
3434

3535
@SuppressWarnings("rawtypes")
3636
public class MapTaskAttemptImpl extends TaskAttemptImpl {

hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapred/ReduceTaskAttemptImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.apache.hadoop.security.Credentials;
3030
import org.apache.hadoop.security.token.Token;
3131
import org.apache.hadoop.yarn.event.EventHandler;
32-
import org.apache.hadoop.yarn.util.Clock;
32+
import org.apache.hadoop.util.Clock;
3333

3434
@SuppressWarnings("rawtypes")
3535
public class ReduceTaskAttemptImpl extends TaskAttemptImpl {

hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapreduce/v2/app/AppContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.apache.hadoop.yarn.event.Event;
3030
import org.apache.hadoop.yarn.event.EventHandler;
3131
import org.apache.hadoop.yarn.security.client.ClientToAMTokenSecretManager;
32-
import org.apache.hadoop.yarn.util.Clock;
32+
import org.apache.hadoop.util.Clock;
3333

3434

3535
/**

hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapreduce/v2/app/MRAppMaster.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@
150150
import org.apache.hadoop.yarn.exceptions.YarnRuntimeException;
151151
import org.apache.hadoop.yarn.security.AMRMTokenIdentifier;
152152
import org.apache.hadoop.yarn.security.client.ClientToAMTokenSecretManager;
153-
import org.apache.hadoop.yarn.util.Clock;
154-
import org.apache.hadoop.yarn.util.SystemClock;
153+
import org.apache.hadoop.util.Clock;
154+
import org.apache.hadoop.util.SystemClock;
155155

156156
import org.apache.hadoop.classification.VisibleForTesting;
157157
import org.slf4j.Logger;

hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapreduce/v2/app/TaskAttemptFinishingMonitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.apache.hadoop.mapreduce.v2.app.job.event.TaskAttemptEventType;
2626
import org.apache.hadoop.yarn.event.EventHandler;
2727
import org.apache.hadoop.yarn.util.AbstractLivelinessMonitor;
28-
import org.apache.hadoop.yarn.util.SystemClock;
28+
import org.apache.hadoop.util.SystemClock;
2929

3030
/**
3131
* This class generates TA_TIMED_OUT if the task attempt stays in FINISHING

hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapreduce/v2/app/TaskHeartbeatHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.apache.hadoop.mapreduce.v2.app.job.event.TaskAttemptEventType;
3535
import org.apache.hadoop.service.AbstractService;
3636
import org.apache.hadoop.yarn.event.EventHandler;
37-
import org.apache.hadoop.yarn.util.Clock;
37+
import org.apache.hadoop.util.Clock;
3838
import org.slf4j.Logger;
3939
import org.slf4j.LoggerFactory;
4040

0 commit comments

Comments
 (0)