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;
019
020import java.util.Iterator;
021import java.util.List;
022import java.util.concurrent.TimeUnit;
023import org.apache.yetus.audience.InterfaceAudience;
024
025import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
026
027/**
028 * Keep track of the runnable procedures
029 */
030@InterfaceAudience.Private
031public interface ProcedureScheduler {
032  /**
033   * Start the scheduler
034   */
035  void start();
036
037  /**
038   * Stop the scheduler
039   */
040  void stop();
041
042  /**
043   * In case the class is blocking on poll() waiting for items to be added,
044   * this method should awake poll() and poll() should return.
045   */
046  void signalAll();
047
048  /**
049   * Inserts the specified element at the front of this queue.
050   * @param proc the Procedure to add
051   */
052  void addFront(Procedure proc);
053
054  /**
055   * Inserts the specified element at the front of this queue.
056   * @param proc the Procedure to add
057   * @param notify whether need to notify worker
058   */
059  void addFront(Procedure proc, boolean notify);
060
061  /**
062   * Inserts all elements in the iterator at the front of this queue.
063   */
064  void addFront(Iterator<Procedure> procedureIterator);
065
066  /**
067   * Inserts the specified element at the end of this queue.
068   * @param proc the Procedure to add
069   */
070  void addBack(Procedure proc);
071
072  /**
073   * Inserts the specified element at the end of this queue.
074   * @param proc the Procedure to add
075   * @param notify whether need to notify worker
076   */
077  void addBack(Procedure proc, boolean notify);
078
079  /**
080   * The procedure can't run at the moment.
081   * add it back to the queue, giving priority to someone else.
082   * @param proc the Procedure to add back to the list
083   */
084  void yield(Procedure proc);
085
086  /**
087   * The procedure in execution completed.
088   * This can be implemented to perform cleanups.
089   * @param proc the Procedure that completed the execution.
090   */
091  void completionCleanup(Procedure proc);
092
093  /**
094   * @return true if there are procedures available to process, otherwise false.
095   */
096  boolean hasRunnables();
097
098  /**
099   * Fetch one Procedure from the queue
100   * @return the Procedure to execute, or null if nothing present.
101   */
102  Procedure poll();
103
104  /**
105   * Fetch one Procedure from the queue
106   * @param timeout how long to wait before giving up, in units of unit
107   * @param unit a TimeUnit determining how to interpret the timeout parameter
108   * @return the Procedure to execute, or null if nothing present.
109   */
110  Procedure poll(long timeout, TimeUnit unit);
111
112  /**
113   * List lock queues.
114   * @return the locks
115   */
116  List<LockedResource> getLocks();
117
118  /**
119   * @return {@link LockedResource} for resource of specified type & name. null if resource is not
120   *         locked.
121   */
122  LockedResource getLockResource(LockedResourceType resourceType, String resourceName);
123
124  /**
125   * Returns the number of elements in this queue.
126   * @return the number of elements in this queue.
127   */
128  @VisibleForTesting
129  int size();
130
131  /**
132   * Clear current state of scheduler such that it is equivalent to newly created scheduler.
133   * Used for testing failure and recovery. To emulate server crash/restart,
134   * {@link ProcedureExecutor} resets its own state and calls clear() on scheduler.
135   */
136  @VisibleForTesting
137  void clear();
138}