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