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.conf.Configuration;
26 import org.apache.hadoop.fs.FileSystem;
27 import org.apache.hadoop.fs.Path;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.classification.InterfaceStability;
30 import org.apache.hadoop.hbase.ipc.RpcServer;
31 import org.apache.hadoop.hbase.master.HMaster;
32 import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
33 import org.apache.hadoop.hbase.master.MasterServices;
34 import org.apache.hadoop.hbase.master.procedure.MasterProcedureScheduler.ProcedureEvent;
35 import org.apache.hadoop.hbase.procedure2.Procedure;
36 import org.apache.hadoop.hbase.procedure2.store.ProcedureStore;
37 import org.apache.hadoop.hbase.procedure2.store.wal.WALProcedureStore;
38 import org.apache.hadoop.hbase.security.User;
39 import org.apache.hadoop.hbase.security.Superusers;
40 import org.apache.hadoop.hbase.util.CancelableProgressable;
41 import org.apache.hadoop.hbase.util.FSUtils;
42
43 @InterfaceAudience.Private
44 @InterfaceStability.Evolving
45 public class MasterProcedureEnv {
46 private static final Log LOG = LogFactory.getLog(MasterProcedureEnv.class);
47
48 @InterfaceAudience.Private
49 public static class WALStoreLeaseRecovery implements WALProcedureStore.LeaseRecovery {
50 private final HMaster master;
51
52 public WALStoreLeaseRecovery(final HMaster master) {
53 this.master = master;
54 }
55
56 @Override
57 public void recoverFileLease(final FileSystem fs, final Path path) throws IOException {
58 final Configuration conf = master.getConfiguration();
59 final FSUtils fsUtils = FSUtils.getInstance(fs, conf);
60 fsUtils.recoverFileLease(fs, path, conf, new CancelableProgressable() {
61 @Override
62 public boolean progress() {
63 LOG.debug("Recover Procedure Store log lease: " + path);
64 return master.isActiveMaster();
65 }
66 });
67 }
68 }
69
70 @InterfaceAudience.Private
71 public static class MasterProcedureStoreListener
72 implements ProcedureStore.ProcedureStoreListener {
73 private final HMaster master;
74
75 public MasterProcedureStoreListener(final HMaster master) {
76 this.master = master;
77 }
78
79 @Override
80 public void postSync() {
81
82 }
83
84 @Override
85 public void abortProcess() {
86 master.abort("The Procedure Store lost the lease");
87 }
88 }
89
90 private final MasterProcedureScheduler procSched;
91 private final MasterServices master;
92
93 public MasterProcedureEnv(final MasterServices master) {
94 this.master = master;
95 this.procSched = new MasterProcedureScheduler(master.getConfiguration(),
96 master.getTableLockManager());
97 }
98
99 public User getRequestUser() {
100 User user = RpcServer.getRequestUser();
101 if (user == null) {
102 user = Superusers.getSystemUser();
103 }
104 return user;
105 }
106
107 public MasterServices getMasterServices() {
108 return master;
109 }
110
111 public Configuration getMasterConfiguration() {
112 return master.getConfiguration();
113 }
114
115 public MasterCoprocessorHost getMasterCoprocessorHost() {
116 return master.getMasterCoprocessorHost();
117 }
118
119 public MasterProcedureScheduler getProcedureQueue() {
120 return procSched;
121 }
122
123 public boolean isRunning() {
124 return master.getMasterProcedureExecutor().isRunning();
125 }
126
127 public boolean isInitialized() {
128 return master.isInitialized();
129 }
130
131 public boolean waitInitialized(Procedure proc) {
132 return procSched.waitEvent(((HMaster)master).getInitializedEvent(), proc);
133 }
134
135 public boolean waitServerCrashProcessingEnabled(Procedure proc) {
136 return procSched.waitEvent(((HMaster)master).getServerCrashProcessingEnabledEvent(), proc);
137 }
138
139 public void wake(ProcedureEvent event) {
140 procSched.wake(event);
141 }
142
143 public void suspend(ProcedureEvent event) {
144 procSched.suspend(event);
145 }
146
147 public void setEventReady(ProcedureEvent event, boolean isReady) {
148 if (isReady) {
149 procSched.wake(event);
150 } else {
151 procSched.suspend(event);
152 }
153 }
154 }