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,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.hadoop.hbase.coprocessor;
021
022import java.io.IOException;
023
024import org.apache.yetus.audience.InterfaceAudience;
025import org.apache.yetus.audience.InterfaceStability;
026import org.apache.hadoop.hbase.Coprocessor;
027import org.apache.hadoop.hbase.HBaseInterfaceAudience;
028
029import com.google.protobuf.Message;
030import com.google.protobuf.Service;
031
032/**
033 * Coprocessors implement this interface to observe and mediate endpoint invocations
034 * on a region.
035 * <br><br>
036 *
037 * <h3>Exception Handling</h3>
038 * For all functions, exception handling is done as follows:
039 * <ul>
040 *   <li>Exceptions of type {@link IOException} are reported back to client.</li>
041 *   <li>For any other kind of exception:
042 *     <ul>
043 *       <li>If the configuration {@link CoprocessorHost#ABORT_ON_ERROR_KEY} is set to true, then
044 *         the server aborts.</li>
045 *       <li>Otherwise, coprocessor is removed from the server and
046 *         {@link org.apache.hadoop.hbase.DoNotRetryIOException} is returned to the client.</li>
047 *     </ul>
048 *   </li>
049 * </ul>
050 */
051@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
052@InterfaceStability.Evolving
053public interface EndpointObserver {
054
055  /**
056   * Called before an Endpoint service method is invoked.
057   * The request message can be altered by returning a new instance. Throwing an
058   * exception will abort the invocation.
059   * Calling {@link org.apache.hadoop.hbase.coprocessor.ObserverContext#bypass()} has no
060   * effect in this hook.
061   * @param ctx the environment provided by the region server
062   * @param service the endpoint service
063   * @param request  Request message expected by given {@code Service}'s method (by the name
064   *   {@code methodName}).
065   * @param methodName the invoked service method
066   * @return the possibly modified message
067   */
068  default Message preEndpointInvocation(ObserverContext<RegionCoprocessorEnvironment> ctx,
069      Service service, String methodName, Message request) throws IOException {
070    return request;
071  }
072
073  /**
074   * Called after an Endpoint service method is invoked. The response message can be
075   * altered using the builder.
076   * @param ctx the environment provided by the region server
077   * @param service the endpoint service
078   * @param methodName the invoked service method
079   * @param request  Request message expected by given {@code Service}'s method (by the name
080   *   {@code methodName}).
081   * @param responseBuilder Builder for final response to the client, with original response from
082   *   Service's method merged into it.
083   */
084  default void postEndpointInvocation(ObserverContext<RegionCoprocessorEnvironment> ctx,
085      Service service, String methodName, Message request, Message.Builder responseBuilder)
086      throws IOException {}
087}