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.hbase.classification.InterfaceAudience;
23 import org.apache.hadoop.hbase.classification.InterfaceStability;
24 import org.apache.hadoop.hbase.CoprocessorEnvironment;
25 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
26 import org.apache.hadoop.hbase.ipc.RpcServer;
27 import org.apache.hadoop.hbase.security.User;
28
29 /**
30 * Carries the execution state for a given invocation of an Observer coprocessor
31 * ({@link RegionObserver}, {@link MasterObserver}, or {@link WALObserver})
32 * method. The same ObserverContext instance is passed sequentially to all loaded
33 * coprocessors for a given Observer method trigger, with the
34 * <code>CoprocessorEnvironment</code> reference swapped out for each
35 * coprocessor.
36 * @param <E> The {@link CoprocessorEnvironment} subclass applicable to the
37 * revelant Observer interface.
38 */
39 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
40 @InterfaceStability.Evolving
41 public class ObserverContext<E extends CoprocessorEnvironment> {
42 private E env;
43 private boolean bypass;
44 private boolean complete;
45 private User caller;
46
47 public ObserverContext(User caller) {
48 this.caller = caller;
49 }
50
51 public E getEnvironment() {
52 return env;
53 }
54
55 public void prepare(E env) {
56 this.env = env;
57 }
58
59 /**
60 * Call to indicate that the current coprocessor's return value should be
61 * used in place of the normal HBase obtained value.
62 */
63 public void bypass() {
64 bypass = true;
65 }
66
67 /**
68 * Call to indicate that additional coprocessors further down the execution
69 * chain do not need to be invoked. Implies that this coprocessor's response
70 * is definitive.
71 */
72 public void complete() {
73 complete = true;
74 }
75
76 /**
77 * For use by the coprocessor framework.
78 * @return <code>true</code> if {@link ObserverContext#bypass()}
79 * was called by one of the loaded coprocessors, <code>false</code> otherwise.
80 */
81 public boolean shouldBypass() {
82 boolean current = bypass;
83 bypass = false;
84 return current;
85 }
86
87 /**
88 * For use by the coprocessor framework.
89 * @return <code>true</code> if {@link ObserverContext#complete()}
90 * was called by one of the loaded coprocessors, <code>false</code> otherwise.
91 */
92 public boolean shouldComplete() {
93 boolean current = complete;
94 complete = false;
95 return current;
96 }
97
98 /**
99 * Returns the active user for the coprocessor call.
100 * If an explicit {@code User} instance was provided to the constructor, that will be returned,
101 * otherwise if we are in the context of an RPC call, the remote user is used. May return null
102 * if the execution is outside of an RPC context.
103 */
104 public User getCaller() {
105 return caller;
106 }
107
108 /**
109 * Instantiates a new ObserverContext instance if the passed reference is
110 * <code>null</code> and sets the environment in the new or existing instance.
111 * This allows deferring the instantiation of a ObserverContext until it is
112 * actually needed.
113 *
114 * @param env The coprocessor environment to set
115 * @param context An existing ObserverContext instance to use, or <code>null</code>
116 * to create a new instance
117 * @param <T> The environment type for the context
118 * @return An instance of <code>ObserverContext</code> with the environment set
119 * @deprecated
120 */
121 @Deprecated
122 // TODO: Remove this method, ObserverContext should not depend on RpcServer
123 public static <T extends CoprocessorEnvironment> ObserverContext<T> createAndPrepare(
124 T env, ObserverContext<T> context) {
125 if (context == null) {
126 context = new ObserverContext<T>(RpcServer.getRequestUser());
127 }
128 context.prepare(env);
129 return context;
130 }
131
132 /**
133 * Instantiates a new ObserverContext instance if the passed reference is
134 * <code>null</code> and sets the environment in the new or existing instance.
135 * This allows deferring the instantiation of a ObserverContext until it is
136 * actually needed.
137 *
138 * @param env The coprocessor environment to set
139 * @param context An existing ObserverContext instance to use, or <code>null</code>
140 * to create a new instance
141 * @param user The requesting caller for the execution context
142 * @param <T> The environment type for the context
143 * @return An instance of <code>ObserverContext</code> with the environment set
144 */
145 public static <T extends CoprocessorEnvironment> ObserverContext<T> createAndPrepare(
146 T env, ObserverContext<T> context, User user) {
147 if (context == null) {
148 context = new ObserverContext<T>(user);
149 }
150 context.prepare(env);
151 return context;
152 }
153 }