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.coprocessor;
019
020import java.io.IOException;
021import java.util.List;
022import java.util.Map;
023import org.apache.hadoop.hbase.HBaseInterfaceAudience;
024import org.apache.hadoop.hbase.HRegionLocation;
025import org.apache.hadoop.hbase.ServerName;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.yetus.audience.InterfaceStability;
028
029/**
030 * Defines coprocessor hooks for interacting operations for ClientMetaService which is responsible
031 * for ZooKeeperless HBase service discovery. <br>
032 * <br>
033 * Since most implementations will be interested in only a subset of hooks, this class uses
034 * 'default' functions to avoid having to add unnecessary overrides. When the functions are
035 * non-empty, it's simply to satisfy the compiler by returning value of expected (non-void) type. It
036 * is done in a way that these default definitions act as no-op. So our suggestion to implementation
037 * would be to not call these 'default' methods from overrides. <br>
038 * <br>
039 * <h3>Exception Handling</h3> For all functions, exception handling is done as follows:
040 * <ul>
041 * <li>Exceptions of type {@link IOException} are reported back to client.</li>
042 * <li>For any other kind of exception:
043 * <ul>
044 * <li>If the configuration {@link CoprocessorHost#ABORT_ON_ERROR_KEY} is set to true, then the
045 * server aborts.</li>
046 * <li>Otherwise, coprocessor is removed from the server and
047 * {@link org.apache.hadoop.hbase.DoNotRetryIOException} is returned to the client.</li>
048 * </ul>
049 * </li>
050 * </ul>
051 */
052@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
053@InterfaceStability.Evolving
054public interface ClientMetaObserver {
055  /**
056   * Called before getting the cluster ID.
057   * @param ctx the environment to interact with the framework
058   */
059  default void preGetClusterId(ObserverContext<ClientMetaCoprocessorEnvironment> ctx)
060    throws IOException {
061  }
062
063  /**
064   * Called after we got the cluster ID.
065   * @param ctx       the environment to interact with the framework
066   * @param clusterId the actual cluster ID
067   * @return the cluster ID which is returned to the client.
068   */
069  default String postGetClusterId(ObserverContext<ClientMetaCoprocessorEnvironment> ctx,
070    String clusterId) throws IOException {
071    return clusterId;
072  }
073
074  /**
075   * Called before getting the active master.
076   * @param ctx the environment to interact with the framework
077   */
078  default void preGetActiveMaster(ObserverContext<ClientMetaCoprocessorEnvironment> ctx)
079    throws IOException {
080  }
081
082  /**
083   * Called after we got the active master.
084   * @param ctx        the environment to interact with the framework
085   * @param serverName the actual active master address. It can be {@code null} if there is no
086   *                   active master.
087   * @return the active master address which is returned to the client. It can be {@code null}.
088   */
089  default ServerName postGetActiveMaster(ObserverContext<ClientMetaCoprocessorEnvironment> ctx,
090    ServerName serverName) throws IOException {
091    return serverName;
092  }
093
094  /**
095   * Called before getting the master servers.
096   * @param ctx the environment to interact with the framework
097   */
098  default void preGetMasters(ObserverContext<ClientMetaCoprocessorEnvironment> ctx)
099    throws IOException {
100  }
101
102  /**
103   * Called after we got the master servers.
104   * @param ctx         the environment to interact with the framework
105   * @param serverNames the actual master servers addresses and active statuses
106   * @return the master servers addresses and active statuses which are returned to the client.
107   */
108  default Map<ServerName, Boolean> postGetMasters(
109    ObserverContext<ClientMetaCoprocessorEnvironment> ctx, Map<ServerName, Boolean> serverNames)
110    throws IOException {
111    return serverNames;
112  }
113
114  /**
115   * Called before getting bootstrap nodes.
116   * @param ctx the environment to interact with the framework
117   */
118  default void preGetBootstrapNodes(ObserverContext<ClientMetaCoprocessorEnvironment> ctx)
119    throws IOException {
120  }
121
122  /**
123   * Called after we got bootstrap nodes.
124   * @param ctx            the environment to interact with the framework
125   * @param bootstrapNodes the actual bootstrap nodes
126   * @return the bootstrap nodes which are returned to the client.
127   */
128  default List<ServerName> postGetBootstrapNodes(
129    ObserverContext<ClientMetaCoprocessorEnvironment> ctx, List<ServerName> bootstrapNodes)
130    throws IOException {
131    return bootstrapNodes;
132  }
133
134  /**
135   * Called before getting the meta region locations.
136   * @param ctx the environment to interact with the framework
137   */
138  default void preGetMetaLocations(ObserverContext<ClientMetaCoprocessorEnvironment> ctx)
139    throws IOException {
140  }
141
142  /**
143   * Called after we got the meta region locations.
144   * @param ctx           the environment to interact with the framework
145   * @param metaLocations the actual meta region locations
146   * @return the meta region locations which are returned to the client.
147   */
148  default List<HRegionLocation> postGetMetaLocations(
149    ObserverContext<ClientMetaCoprocessorEnvironment> ctx, List<HRegionLocation> metaLocations)
150    throws IOException {
151    return metaLocations;
152  }
153}