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 */
018package org.apache.hadoop.hbase.procedure2.util;
019
020import java.util.Objects;
021import java.util.concurrent.DelayQueue;
022import java.util.concurrent.Delayed;
023import java.util.concurrent.TimeUnit;
024import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.apache.yetus.audience.InterfaceStability;
027
028// FIX namings. TODO.
029@InterfaceAudience.Private
030@InterfaceStability.Evolving
031public final class DelayedUtil {
032  private DelayedUtil() {
033  }
034
035  /**
036   * Add a timeout to a Delay
037   */
038  public interface DelayedWithTimeout extends Delayed {
039    long getTimeout();
040  }
041
042  /**
043   * POISON implementation; used to mark special state: e.g. shutdown.
044   */
045  public static final DelayedWithTimeout DELAYED_POISON = new DelayedWithTimeout() {
046    @Override
047    public long getTimeout() {
048      return 0;
049    }
050
051    @Override
052    public long getDelay(final TimeUnit unit) {
053      return 0;
054    }
055
056    @Override
057    public int compareTo(final Delayed o) {
058      return Long.compare(0, DelayedUtil.getTimeout(o));
059    }
060
061    @Override
062    public boolean equals(final Object other) {
063      return this == other;
064    }
065
066    @Override
067    public int hashCode() {
068      return Objects.hash(this);
069    }
070
071    @Override
072    public String toString() {
073      return getClass().getSimpleName() + "(POISON)";
074    }
075  };
076
077  /**
078   * @return null (if an interrupt) or an instance of E; resets interrupt on calling thread.
079   */
080  public static <E extends Delayed> E takeWithoutInterrupt(final DelayQueue<E> queue,
081    final long timeout, final TimeUnit timeUnit) {
082    try {
083      return queue.poll(timeout, timeUnit);
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) {
142        return true;
143      }
144
145      if (!(other instanceof DelayedContainer)) {
146        return false;
147      }
148
149      return Objects.equals(getObject(), ((DelayedContainer) other).getObject());
150    }
151
152    @Override
153    public int hashCode() {
154      return object != null ? object.hashCode() : 0;
155    }
156
157    @Override
158    public String toString() {
159      return "containedObject=" + getObject() + ", " + super.toString();
160    }
161  }
162
163  /**
164   * Has a timeout.
165   */
166  public static class DelayedContainerWithTimestamp<T> extends DelayedContainer<T> {
167    private long timeout;
168
169    public DelayedContainerWithTimestamp(final T object, final long timeout) {
170      super(object);
171      setTimeout(timeout);
172    }
173
174    @Override
175    public long getTimeout() {
176      return timeout;
177    }
178
179    public void setTimeout(final long timeout) {
180      this.timeout = timeout;
181    }
182  }
183}