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