001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.wake.time.runtime; 020 021import org.apache.reef.wake.time.Time; 022 023import javax.inject.Inject; 024import java.util.concurrent.atomic.AtomicLong; 025 026/** 027 * Logical timer that is only bound to the timestamps of the events tracked against it. 028 * In such setting, all events occur immediately, i.e. isReady() always return true, 029 * and the duration of the delay to the next event is always 0. 030 * Current time for this timer is always the timestamp of the tracked event that is 031 * the most distant in the future. 032 */ 033public final class LogicalTimer implements Timer { 034 035 /** 036 * Current time in milliseconds since the beginning of the epoch (01/01/1970), 037 * according to the timer. For this implementation, always keep the largest seen 038 * timestamp (i.e. track the event that is the most distant into the future). 039 */ 040 private final AtomicLong current = new AtomicLong(0); 041 042 /** 043 * Instances of the timer should only be created automatically by Tang. 044 */ 045 @Inject 046 private LogicalTimer() { 047 } 048 049 /** 050 * Get current time in milliseconds since the beginning of the epoch (01/01/1970). 051 * This timer implementation always returns the timestamp of the most distant 052 * future event ever checked against this timer in getDuration() or isReady() methods. 053 * Return 0 if there were no calls yet to getDuration() or isReady(). 054 * @return Timestamp of the latest event (in milliseconds since the start of the epoch). 055 */ 056 @Override 057 public long getCurrent() { 058 return this.current.get(); 059 } 060 061 /** 062 * Get the number of milliseconds between current time as tracked by the Timer implementation 063 * and a given event. This implementation always returns 0 and updates current timer's time 064 * to the timestamp of the most distant future event. 065 * @param time Timestamp in milliseconds. 066 * @return Always returns 0. 067 * @deprecated [REEF-1532] Prefer passing Time object instead of the numeric timestamp. 068 * Remove after release 0.16. 069 */ 070 @Override 071 public long getDuration(final long time) { 072 this.isReady(time); 073 return 0; 074 } 075 076 /** 077 * Get the number of milliseconds between current time as tracked by the Timer implementation 078 * and a given event. This implementation always returns 0 and updates current timer's time 079 * to the timestamp of the most distant future event. 080 * @param time Timestamp object that wraps time in milliseconds. 081 * @return Always returns 0. 082 */ 083 @Override 084 public long getDuration(final Time time) { 085 return this.getDuration(time.getTimestamp()); 086 } 087 088 /** 089 * Check if the event with a given timestamp has occurred, according to the timer. 090 * This implementation always returns true and updates current timer's time to the timestamp 091 * of the most distant future event. 092 * @param time Timestamp in milliseconds. 093 * @return Always returns true. 094 * @deprecated [REEF-1532] Prefer passing Time object instead of the numeric timestamp. 095 * Remove after release 0.16. 096 */ 097 @Override 098 public boolean isReady(final long time) { 099 while (true) { 100 final long thisTs = this.current.get(); 101 if (thisTs >= time || this.current.compareAndSet(thisTs, time)) { 102 return true; 103 } 104 } 105 } 106 107 /** 108 * Check if the event with a given timestamp has occurred, according to the timer. 109 * This implementation always returns true and updates current timer's time to the timestamp 110 * of the most distant future event. 111 * @param time Timestamp object that wraps time in milliseconds. 112 * @return Always returns true. 113 */ 114 @Override 115 public boolean isReady(final Time time) { 116 return this.isReady(time.getTimestamp()); 117 } 118}