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.util.Optional;
021import org.apache.hadoop.hbase.CoprocessorEnvironment;
022import org.apache.hadoop.hbase.HBaseInterfaceAudience;
023import org.apache.hadoop.hbase.security.User;
024import org.apache.yetus.audience.InterfaceAudience;
025import org.apache.yetus.audience.InterfaceStability;
026
027/**
028 * Carries the execution state for a given invocation of an Observer coprocessor
029 * ({@link RegionObserver}, {@link MasterObserver}, or {@link WALObserver}) method. The same
030 * ObserverContext instance is passed sequentially to all loaded coprocessors for a given Observer
031 * method trigger, with the <code>CoprocessorEnvironment</code> reference set appropriately for each
032 * Coprocessor type: e.g. the RegionCoprocessorEnvironment is passed to RegionCoprocessors, and so
033 * on.
034 * @param <E> The {@link CoprocessorEnvironment} subclass applicable to the revelant Observer
035 *            interface.
036 */
037@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
038@InterfaceStability.Evolving
039public interface ObserverContext<E extends CoprocessorEnvironment> {
040  E getEnvironment();
041
042  /**
043   * Call to indicate that the current coprocessor's return value (or parameter -- depends on the
044   * call-type) should be used in place of the value that would be obtained via normal processing;
045   * i.e. bypass the core call and return the Coprocessor's result instead. DOES NOT work for all
046   * Coprocessor invocations, only on a small subset of methods, mostly preXXX calls in
047   * RegionObserver. Check javadoc on the pertinent Coprocessor Observer to see if
048   * <code>bypass</code> is supported.
049   * <p>
050   * This behavior of honoring only a subset of methods is new since hbase-2.0.0.
051   * <p>
052   * Where bypass is supported what is being bypassed is all of the core code implementing the
053   * remainder of the operation. In order to understand what calling bypass() will skip, a
054   * coprocessor implementer should read and understand all of the remaining code and its nuances.
055   * Although this is good practice for coprocessor developers in general, it demands a lot. What is
056   * skipped is extremely version dependent. The core code will vary, perhaps significantly, even
057   * between point releases. We do not provide the promise of consistent behavior even between point
058   * releases for the bypass semantic. To achieve that we could not change any code between hook
059   * points. Therefore the coprocessor implementer becomes an HBase core developer in practice as
060   * soon as they rely on bypass(). Every release of HBase may break the assumption that the
061   * replacement for the bypassed code takes care of all necessary skipped concerns. Because those
062   * concerns can change at any point, such an assumption is never safe.
063   * </p>
064   * <p>
065   * As of hbase2, when bypass has been set, we will NOT call any Coprocessors follow the bypassing
066   * Coprocessor; we cut short the processing and return the bypassing Coprocessors response (this
067   * used be a separate 'complete' option that has been folded into the 'bypass' in hbase2.
068   * </p>
069   */
070  void bypass();
071
072  /**
073   * Returns the active user for the coprocessor call. If an explicit {@code User} instance was
074   * provided to the constructor, that will be returned, otherwise if we are in the context of an
075   * RPC call, the remote user is used. May not be present if the execution is outside of an RPC
076   * context.
077   */
078  Optional<User> getCaller();
079}