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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.procedure2.util;
020
021import java.util.Objects;
022import java.util.concurrent.DelayQueue;
023import java.util.concurrent.Delayed;
024import java.util.concurrent.TimeUnit;
025
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.yetus.audience.InterfaceStability;
028import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
029
030// FIX namings. TODO.
031@InterfaceAudience.Private
032@InterfaceStability.Evolving
033public final class DelayedUtil {
034  private DelayedUtil() { }
035
036  /**
037   * Add a timeout to a Delay
038   */
039  public interface DelayedWithTimeout extends Delayed {
040    long getTimeout();
041  }
042
043  /**
044   * POISON implementation; used to mark special state: e.g. shutdown.
045   */
046  public static final DelayedWithTimeout DELAYED_POISON = new DelayedWithTimeout() {
047    @Override
048    public long getTimeout() {
049      return 0;
050    }
051
052    @Override
053    public long getDelay(final TimeUnit unit) {
054      return 0;
055    }
056
057    @Override
058    public int compareTo(final Delayed o) {
059      return Long.compare(0, DelayedUtil.getTimeout(o));
060    }
061
062    @Override
063    public boolean equals(final Object other) {
064      return this == other;
065    }
066
067    @Override
068    public int hashCode() {
069      return Objects.hash(this);
070    }
071
072    @Override
073    public String toString() {
074      return getClass().getSimpleName() + "(POISON)";
075    }
076  };
077
078  /**
079   * @return null (if an interrupt) or an instance of E; resets interrupt on calling thread.
080   */
081  public static <E extends Delayed> E takeWithoutInterrupt(final DelayQueue<E> queue) {
082    try {
083      return queue.take();
084    } catch (InterruptedException e) {
085      Thread.currentThread().interrupt();
086      return null;
087    }
088  }
089
090  /**
091   * @return Time remaining as milliseconds.
092   */
093  public static long getRemainingTime(final TimeUnit resultUnit, final long timeout) {
094    final long currentTime = EnvironmentEdgeManager.currentTime();
095    if (currentTime >= timeout) {
096      return 0;
097    }
098    return resultUnit.convert(timeout - currentTime, TimeUnit.MILLISECONDS);
099  }
100
101  public static int compareDelayed(final Delayed o1, final Delayed o2) {
102    return Long.compare(getTimeout(o1), getTimeout(o2));
103  }
104
105  private static long getTimeout(final Delayed o) {
106    assert o instanceof DelayedWithTimeout : "expected DelayedWithTimeout instance, got " + o;
107    return ((DelayedWithTimeout)o).getTimeout();
108  }
109
110  public static abstract class DelayedObject implements DelayedWithTimeout {
111    @Override
112    public long getDelay(final TimeUnit unit) {
113      return DelayedUtil.getRemainingTime(unit, getTimeout());
114    }
115
116    @Override
117    public int compareTo(final Delayed other) {
118      return DelayedUtil.compareDelayed(this, other);
119    }
120
121    @Override
122    public String toString() {
123      long timeout = getTimeout();
124      return "timeout=" + timeout + ", delay=" + getDelay(TimeUnit.MILLISECONDS);
125    }
126  }
127
128  public static abstract class DelayedContainer<T> extends DelayedObject {
129    private final T object;
130
131    public DelayedContainer(final T object) {
132      this.object = object;
133    }
134
135    public T getObject() {
136      return this.object;
137    }
138
139    @Override
140    public boolean equals(final Object other) {
141      if (other == this) return true;
142      if (!(other instanceof DelayedContainer)) return false;
143      return Objects.equals(getObject(), ((DelayedContainer)other).getObject());
144    }
145
146    @Override
147    public int hashCode() {
148      return object != null ? object.hashCode() : 0;
149    }
150
151    @Override
152    public String toString() {
153      return "containedObject=" + getObject() + ", " + super.toString();
154    }
155  }
156
157  /**
158   * Has a timeout.
159   */
160  public static class DelayedContainerWithTimestamp<T> extends DelayedContainer<T> {
161    private long timeout;
162
163    public DelayedContainerWithTimestamp(final T object, final long timeout) {
164      super(object);
165      setTimeout(timeout);
166    }
167
168    @Override
169    public long getTimeout() {
170      return timeout;
171    }
172
173    public void setTimeout(final long timeout) {
174      this.timeout = timeout;
175    }
176  }
177}