View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.master.procedure;
20  
21  import java.io.IOException;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.master.MasterServices;
28  import org.apache.hadoop.hbase.procedure2.Procedure;
29  import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
30  import org.apache.hadoop.hbase.protobuf.generated.RPCProtos.UserInformation;
31  import org.apache.hadoop.hbase.security.User;
32  import org.apache.hadoop.hbase.util.NonceKey;
33  import org.apache.hadoop.security.UserGroupInformation;
34  
35  @InterfaceAudience.Private
36  @InterfaceStability.Evolving
37  public final class MasterProcedureUtil {
38    private static final Log LOG = LogFactory.getLog(MasterProcedureUtil.class);
39  
40    private MasterProcedureUtil() {}
41  
42    public static UserInformation toProtoUserInfo(UserGroupInformation ugi) {
43      UserInformation.Builder userInfoPB = UserInformation.newBuilder();
44      userInfoPB.setEffectiveUser(ugi.getUserName());
45      if (ugi.getRealUser() != null) {
46        userInfoPB.setRealUser(ugi.getRealUser().getUserName());
47      }
48      return userInfoPB.build();
49    }
50  
51    public static UserGroupInformation toUserInfo(UserInformation userInfoProto) {
52      if (userInfoProto.hasEffectiveUser()) {
53        String effectiveUser = userInfoProto.getEffectiveUser();
54        if (userInfoProto.hasRealUser()) {
55          String realUser = userInfoProto.getRealUser();
56          UserGroupInformation realUserUgi = UserGroupInformation.createRemoteUser(realUser);
57          return UserGroupInformation.createProxyUser(effectiveUser, realUserUgi);
58        }
59        return UserGroupInformation.createRemoteUser(effectiveUser);
60      }
61      return null;
62    }
63  
64    /**
65     * Helper Runnable used in conjunction with submitProcedure() to deal with
66     * submitting procs with nonce.
67     * See submitProcedure() for an example.
68     */
69    public static abstract class NonceProcedureRunnable {
70      private final MasterServices master;
71      private final NonceKey nonceKey;
72      private Long procId;
73  
74      public NonceProcedureRunnable(final MasterServices master,
75          final long nonceGroup, final long nonce) {
76        this.master = master;
77        this.nonceKey = getProcedureExecutor().createNonceKey(nonceGroup, nonce);
78      }
79  
80      protected NonceKey getNonceKey() {
81        return nonceKey;
82      }
83  
84      protected MasterServices getMaster() {
85        return master;
86      }
87  
88      protected ProcedureExecutor<MasterProcedureEnv> getProcedureExecutor() {
89        return master.getMasterProcedureExecutor();
90      }
91  
92      protected long getProcId() {
93        return procId != null ? procId.longValue() : -1;
94      }
95  
96      protected long setProcId(final long procId) {
97        this.procId = procId;
98        return procId;
99      }
100 
101     protected abstract void run() throws IOException;
102     protected abstract String getDescription();
103 
104     protected long submitProcedure(final Procedure proc) {
105       assert procId == null : "submitProcedure() was already called, running procId=" + procId;
106       procId = getProcedureExecutor().submitProcedure(proc, nonceKey);
107       return procId;
108     }
109   }
110 
111   /**
112    * Helper used to deal with submitting procs with nonce.
113    * Internally the NonceProcedureRunnable.run() will be called only if no one else
114    * registered the nonce. any Exception thrown by the run() method will be
115    * collected/handled and rethrown.
116    * <code>
117    * long procId = MasterProcedureUtil.submitProcedure(
118    *      new NonceProcedureRunnable(procExec, nonceGroup, nonce) {
119    *   {@literal @}Override
120    *   public void run() {
121    *     cpHost.preOperation();
122    *     submitProcedure(new MyProc());
123    *     cpHost.postOperation();
124    *   }
125    * });
126    * </code>
127    */
128   public static long submitProcedure(final NonceProcedureRunnable runnable) throws IOException {
129     final ProcedureExecutor<MasterProcedureEnv> procExec = runnable.getProcedureExecutor();
130     final long procId = procExec.registerNonce(runnable.getNonceKey());
131     if (procId >= 0) return procId; // someone already registered the nonce
132     try {
133       runnable.run();
134     } catch (IOException e) {
135       procExec.setFailureResultForNonce(runnable.getNonceKey(),
136           runnable.getDescription(),
137           procExec.getEnvironment().getRequestUser(), e);
138       throw e;
139     } finally {
140       procExec.unregisterNonceIfProcedureWasNotSubmitted(runnable.getNonceKey());
141     }
142     return runnable.getProcId();
143   }
144 }