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;
019
020import com.google.protobuf.Service;
021import java.io.IOException;
022import java.util.Collections;
023import org.apache.yetus.audience.InterfaceAudience;
024import org.apache.yetus.audience.InterfaceStability;
025
026/**
027 * Base interface for the 4 coprocessors - MasterCoprocessor, RegionCoprocessor,
028 * RegionServerCoprocessor, and WALCoprocessor. Do NOT implement this interface directly. Unless an
029 * implementation implements one (or more) of the above mentioned 4 coprocessors, it'll fail to be
030 * loaded by any coprocessor host. Example: Building a coprocessor to observe Master operations.
031 *
032 * <pre>
033 * class MyMasterCoprocessor implements MasterCoprocessor {
034 *   &#64;Override
035 *   public Optional&lt;MasterObserver> getMasterObserver() {
036 *     return new MyMasterObserver();
037 *   }
038 * }
039 *
040 * class MyMasterObserver implements MasterObserver {
041 *   ....
042 * }
043 * </pre>
044 *
045 * Building a Service which can be loaded by both Master and RegionServer
046 *
047 * <pre>
048 * class MyCoprocessorService implements MasterCoprocessor, RegionServerCoprocessor {
049 *   &#64;Override
050 *   public Optional&lt;Service> getServices() {
051 *     return new ...;
052 *   }
053 * }
054 * </pre>
055 */
056@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
057@InterfaceStability.Evolving
058public interface Coprocessor {
059  int VERSION = 1;
060
061  /** Highest installation priority */
062  int PRIORITY_HIGHEST = 0;
063  /** High (system) installation priority */
064  int PRIORITY_SYSTEM = Integer.MAX_VALUE / 4;
065  /** Default installation priority for user coprocessors */
066  int PRIORITY_USER = Integer.MAX_VALUE / 2;
067  /** Lowest installation priority */
068  int PRIORITY_LOWEST = Integer.MAX_VALUE;
069
070  /**
071   * Lifecycle state of a given coprocessor instance.
072   */
073  enum State {
074    UNINSTALLED,
075    INSTALLED,
076    STARTING,
077    ACTIVE,
078    STOPPING,
079    STOPPED
080  }
081
082  /**
083   * Called by the {@link CoprocessorEnvironment} during it's own startup to initialize the
084   * coprocessor.
085   */
086  default void start(CoprocessorEnvironment env) throws IOException {
087  }
088
089  /**
090   * Called by the {@link CoprocessorEnvironment} during it's own shutdown to stop the coprocessor.
091   */
092  default void stop(CoprocessorEnvironment env) throws IOException {
093  }
094
095  /**
096   * Coprocessor endpoints providing protobuf services should override this method.
097   * @return Iterable of {@link Service}s or empty collection. Implementations should never return
098   *         null.
099   */
100  default Iterable<Service> getServices() {
101    return Collections.EMPTY_SET;
102  }
103}