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.Map; 021import java.util.Optional; 022import org.apache.hadoop.hbase.CoprocessorEnvironment; 023import org.apache.hadoop.hbase.ipc.RpcServer; 024import org.apache.hadoop.hbase.security.User; 025import org.apache.yetus.audience.InterfaceAudience; 026 027/** 028 * This is the only implementation of {@link ObserverContext}, which serves as the interface for 029 * third-party Coprocessor developers. 030 */ 031@InterfaceAudience.Private 032public class ObserverContextImpl<E extends CoprocessorEnvironment> implements ObserverContext<E> { 033 private E env; 034 private boolean bypass; 035 /** 036 * Is this operation bypassable? 037 */ 038 private final boolean bypassable; 039 private final ObserverRpcCallContext rpcCallContext; 040 041 public ObserverContextImpl(ObserverRpcCallContext rpcCallContext) { 042 this(rpcCallContext, false); 043 } 044 045 public ObserverContextImpl(ObserverRpcCallContext rpcCallContext, boolean bypassable) { 046 this.rpcCallContext = rpcCallContext; 047 this.bypassable = bypassable; 048 } 049 050 @Override 051 public E getEnvironment() { 052 return env; 053 } 054 055 public void prepare(E env) { 056 this.env = env; 057 } 058 059 public boolean isBypassable() { 060 return this.bypassable; 061 } 062 063 @Override 064 public void bypass() { 065 if (!this.bypassable) { 066 throw new UnsupportedOperationException("This method does not support 'bypass'."); 067 } 068 bypass = true; 069 } 070 071 /** 072 * @return {@code true}, if {@link ObserverContext#bypass()} was called by one of the loaded 073 * coprocessors, {@code false} otherwise. 074 */ 075 public boolean shouldBypass() { 076 if (!isBypassable()) { 077 return false; 078 } 079 if (bypass) { 080 bypass = false; 081 return true; 082 } 083 return false; 084 } 085 086 @Override 087 public Optional<ObserverRpcCallContext> getRpcCallContext() { 088 return Optional.ofNullable(rpcCallContext); 089 } 090 091 /** 092 * Instantiates a new ObserverContext instance if the passed reference is <code>null</code> and 093 * sets the environment in the new or existing instance. This allows deferring the instantiation 094 * of a ObserverContext until it is actually needed. 095 * @param <E> The environment type for the context 096 * @param env The coprocessor environment to set 097 * @return An instance of <code>ObserverContext</code> with the environment set 098 */ 099 @Deprecated 100 // TODO: Remove this method, ObserverContext should not depend on RpcServer 101 public static <E extends CoprocessorEnvironment> ObserverContext<E> createAndPrepare(E env) { 102 Optional<User> user = RpcServer.getRequestUser(); 103 ObserverRpcCallContext rpcCallContext = 104 user.map(value -> new ObserverRpcCallContextImpl(value, Map.of())).orElse(null); 105 106 ObserverContextImpl<E> ctx = new ObserverContextImpl<>(rpcCallContext); 107 ctx.prepare(env); 108 109 return ctx; 110 } 111}