1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
66
67
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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;
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 }