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.UserProvider;
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 abortProcess() {
79        master.abort("The Procedure Store lost the lease");
80      }
81    }
82  
83    private final MasterProcedureQueue procQueue;
84    private final MasterServices master;
85  
86    public MasterProcedureEnv(final MasterServices master) {
87      this.master = master;
88      this.procQueue = new MasterProcedureQueue(master.getConfiguration(),
89        master.getTableLockManager());
90    }
91  
92    public User getRequestUser() throws IOException {
93      User user = RpcServer.getRequestUser();
94      if (user == null) {
95        user = UserProvider.instantiate(getMasterConfiguration()).getCurrent();
96      }
97      return user;
98    }
99  
100   public MasterServices getMasterServices() {
101     return master;
102   }
103 
104   public Configuration getMasterConfiguration() {
105     return master.getConfiguration();
106   }
107 
108   public MasterCoprocessorHost getMasterCoprocessorHost() {
109     return master.getMasterCoprocessorHost();
110   }
111 
112   public MasterProcedureQueue getProcedureQueue() {
113     return procQueue;
114   }
115 
116   public boolean isRunning() {
117     return master.getMasterProcedureExecutor().isRunning();
118   }
119 
120   public boolean isInitialized() {
121     return master.isInitialized();
122   }
123 }