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.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.procedure2.store.ProcedureStore;
35  import org.apache.hadoop.hbase.procedure2.store.wal.WALProcedureStore;
36  import org.apache.hadoop.hbase.security.User;
37  import org.apache.hadoop.hbase.security.Superusers;
38  import org.apache.hadoop.hbase.util.CancelableProgressable;
39  import org.apache.hadoop.hbase.util.FSUtils;
40  
41  @InterfaceAudience.Private
42  @InterfaceStability.Evolving
43  public class MasterProcedureEnv {
44    private static final Log LOG = LogFactory.getLog(MasterProcedureEnv.class);
45  
46    @InterfaceAudience.Private
47    public static class WALStoreLeaseRecovery implements WALProcedureStore.LeaseRecovery {
48      private final HMaster master;
49  
50      public WALStoreLeaseRecovery(final HMaster master) {
51        this.master = master;
52      }
53  
54      @Override
55      public void recoverFileLease(final FileSystem fs, final Path path) throws IOException {
56        final Configuration conf = master.getConfiguration();
57        final FSUtils fsUtils = FSUtils.getInstance(fs, conf);
58        fsUtils.recoverFileLease(fs, path, conf, new CancelableProgressable() {
59          @Override
60          public boolean progress() {
61            LOG.debug("Recover Procedure Store log lease: " + path);
62            return master.isActiveMaster();
63          }
64        });
65      }
66    }
67  
68    @InterfaceAudience.Private
69    public static class MasterProcedureStoreListener
70        implements ProcedureStore.ProcedureStoreListener {
71      private final HMaster master;
72  
73      public MasterProcedureStoreListener(final HMaster master) {
74        this.master = master;
75      }
76  
77      @Override
78      public void postSync() {
79        // no-op
80      }
81  
82      @Override
83      public void abortProcess() {
84        master.abort("The Procedure Store lost the lease");
85      }
86    }
87  
88    private final MasterProcedureQueue procQueue;
89    private final MasterServices master;
90  
91    public MasterProcedureEnv(final MasterServices master) {
92      this.master = master;
93      this.procQueue = new MasterProcedureQueue(master.getConfiguration(),
94        master.getTableLockManager());
95    }
96  
97    public User getRequestUser() {
98      User user = RpcServer.getRequestUser();
99      if (user == null) {
100       user = Superusers.getSystemUser();
101     }
102     return user;
103   }
104 
105   public MasterServices getMasterServices() {
106     return master;
107   }
108 
109   public Configuration getMasterConfiguration() {
110     return master.getConfiguration();
111   }
112 
113   public MasterCoprocessorHost getMasterCoprocessorHost() {
114     return master.getMasterCoprocessorHost();
115   }
116 
117   public MasterProcedureQueue getProcedureQueue() {
118     return procQueue;
119   }
120 
121   public boolean isRunning() {
122     return master.getMasterProcedureExecutor().isRunning();
123   }
124 
125   public boolean isInitialized() {
126     return master.isInitialized();
127   }
128 }