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, this method should awake
042   * 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. add it back to the queue, giving priority to someone
079   * 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. This can be implemented to perform cleanups.
086   * @param proc the Procedure that completed the execution.
087   */
088  void completionCleanup(Procedure proc);
089
090  /**
091   * @return true if there are procedures available to process, otherwise false.
092   */
093  boolean hasRunnables();
094
095  /**
096   * Fetch one Procedure from the queue
097   * @return the Procedure to execute, or null if nothing present.
098   */
099  Procedure poll();
100
101  /**
102   * Fetch one Procedure from the queue
103   * @param timeout how long to wait before giving up, in units of unit
104   * @param unit    a TimeUnit determining how to interpret the timeout parameter
105   * @return the Procedure to execute, or null if nothing present.
106   */
107  Procedure poll(long timeout, TimeUnit unit);
108
109  /**
110   * List lock queues.
111   * @return the locks
112   */
113  List<LockedResource> getLocks();
114
115  /**
116   * @return {@link LockedResource} for resource of specified type & name. null if resource is not
117   *         locked.
118   */
119  LockedResource getLockResource(LockedResourceType resourceType, String resourceName);
120
121  /**
122   * Returns the number of elements in this queue.
123   * @return the number of elements in this queue.
124   */
125  int size();
126
127  /**
128   * Clear current state of scheduler such that it is equivalent to newly created scheduler. Used
129   * for testing failure and recovery. To emulate server crash/restart, {@link ProcedureExecutor}
130   * resets its own state and calls clear() on scheduler.
131   */
132  void clear();
133}