View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.coprocessor;
21  
22  import org.apache.hadoop.classification.InterfaceAudience;
23  import org.apache.hadoop.classification.InterfaceStability;
24  import org.apache.hadoop.hbase.CoprocessorEnvironment;
25  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
26  
27  /**
28   * Carries the execution state for a given invocation of an Observer coprocessor
29   * ({@link RegionObserver}, {@link MasterObserver}, or {@link WALObserver})
30   * method.  The same ObserverContext instance is passed sequentially to all loaded
31   * coprocessors for a given Observer method trigger, with the
32   * <code>CoprocessorEnvironment</code> reference swapped out for each
33   * coprocessor.
34   * @param <E> The {@link CoprocessorEnvironment} subclass applicable to the
35   *     revelant Observer interface.
36   */
37  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
38  @InterfaceStability.Evolving
39  public class ObserverContext<E extends CoprocessorEnvironment> {
40    private E env;
41    private boolean bypass;
42    private boolean complete;
43  
44    public ObserverContext() {
45    }
46  
47    public E getEnvironment() {
48      return env;
49    }
50  
51    public void prepare(E env) {
52      this.env = env;
53    }
54  
55    /**
56     * Call to indicate that the current coprocessor's return value should be
57     * used in place of the normal HBase obtained value.
58     */
59    public void bypass() {
60      bypass = true;
61    }
62  
63    /**
64     * Call to indicate that additional coprocessors further down the execution
65     * chain do not need to be invoked.  Implies that this coprocessor's response
66     * is definitive.
67     */
68    public void complete() {
69      complete = true;
70    }
71  
72    /**
73     * For use by the coprocessor framework.
74     * @return <code>true</code> if {@link ObserverContext#bypass()}
75     *     was called by one of the loaded coprocessors, <code>false</code> otherwise.
76     */
77    public boolean shouldBypass() {
78      boolean current = bypass;
79      bypass = false;
80      return current;
81    }
82  
83    /**
84     * For use by the coprocessor framework.
85     * @return <code>true</code> if {@link ObserverContext#complete()}
86     *     was called by one of the loaded coprocessors, <code>false</code> otherwise.
87     */
88    public boolean shouldComplete() {
89      boolean current = complete;
90      complete = false;
91      return current;
92    }
93  
94    /**
95     * Instantiates a new ObserverContext instance if the passed reference is
96     * <code>null</code> and sets the environment in the new or existing instance.
97     * This allows deferring the instantiation of a ObserverContext until it is
98     * actually needed.
99     *
100    * @param env The coprocessor environment to set
101    * @param context An existing ObserverContext instance to use, or <code>null</code>
102    *     to create a new instance
103    * @param <T> The environment type for the context
104    * @return An instance of <code>ObserverContext</code> with the environment set
105    */
106   public static <T extends CoprocessorEnvironment> ObserverContext<T> createAndPrepare(
107       T env, ObserverContext<T> context) {
108     if (context == null) {
109       context = new ObserverContext<T>();
110     }
111     context.prepare(env);
112     return context;
113   }
114 }