001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.master.procedure;
020
021import java.io.IOException;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.conf.ConfigurationObserver;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.yetus.audience.InterfaceStability;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030import org.apache.hadoop.hbase.ipc.RpcServer;
031import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
032import org.apache.hadoop.hbase.master.MasterFileSystem;
033import org.apache.hadoop.hbase.master.MasterServices;
034import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
035import org.apache.hadoop.hbase.procedure2.Procedure;
036import org.apache.hadoop.hbase.procedure2.ProcedureEvent;
037import org.apache.hadoop.hbase.procedure2.store.wal.WALProcedureStore;
038import org.apache.hadoop.hbase.security.User;
039import org.apache.hadoop.hbase.security.Superusers;
040import org.apache.hadoop.hbase.util.CancelableProgressable;
041import org.apache.hadoop.hbase.util.FSUtils;
042
043@InterfaceAudience.Private
044@InterfaceStability.Evolving
045public class MasterProcedureEnv implements ConfigurationObserver {
046  private static final Logger LOG = LoggerFactory.getLogger(MasterProcedureEnv.class);
047
048  @InterfaceAudience.Private
049  public static class WALStoreLeaseRecovery implements WALProcedureStore.LeaseRecovery {
050    private final MasterServices master;
051
052    public WALStoreLeaseRecovery(final MasterServices master) {
053      this.master = master;
054    }
055
056    @Override
057    public void recoverFileLease(final FileSystem fs, final Path path) throws IOException {
058      final Configuration conf = master.getConfiguration();
059      final FSUtils fsUtils = FSUtils.getInstance(fs, conf);
060      fsUtils.recoverFileLease(fs, path, conf, new CancelableProgressable() {
061        @Override
062        public boolean progress() {
063          LOG.debug("Recover Procedure Store log lease: " + path);
064          return isRunning();
065        }
066      });
067    }
068
069    private boolean isRunning() {
070      return !master.isStopped() && !master.isStopping() && !master.isAborted();
071    }
072  }
073
074  private final RSProcedureDispatcher remoteDispatcher;
075  private final MasterProcedureScheduler procSched;
076  private final MasterServices master;
077
078  public MasterProcedureEnv(final MasterServices master) {
079    this(master, new RSProcedureDispatcher(master));
080  }
081
082  public MasterProcedureEnv(final MasterServices master,
083      final RSProcedureDispatcher remoteDispatcher) {
084    this.master = master;
085    this.procSched = new MasterProcedureScheduler(
086      procId -> master.getMasterProcedureExecutor().getProcedure(procId));
087    this.remoteDispatcher = remoteDispatcher;
088  }
089
090  public User getRequestUser() {
091    return RpcServer.getRequestUser().orElse(Superusers.getSystemUser());
092  }
093
094  public MasterServices getMasterServices() {
095    return master;
096  }
097
098  public Configuration getMasterConfiguration() {
099    return master.getConfiguration();
100  }
101
102  public AssignmentManager getAssignmentManager() {
103    return master.getAssignmentManager();
104  }
105
106  public MasterCoprocessorHost getMasterCoprocessorHost() {
107    return master.getMasterCoprocessorHost();
108  }
109
110  public MasterProcedureScheduler getProcedureScheduler() {
111    return procSched;
112  }
113
114  public RSProcedureDispatcher getRemoteDispatcher() {
115    return remoteDispatcher;
116  }
117
118  public MasterFileSystem getMasterFileSystem() {
119    return master.getMasterFileSystem();
120  }
121
122  public boolean isRunning() {
123    if (this.master == null || this.master.getMasterProcedureExecutor() == null) return false;
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 master.getInitializedEvent().suspendIfNotReady(proc);
133  }
134
135  public void setEventReady(ProcedureEvent event, boolean isReady) {
136    if (isReady) {
137      event.wake(procSched);
138    } else {
139      event.suspend();
140    }
141  }
142
143  @Override
144  public void onConfigurationChange(Configuration conf) {
145    master.getMasterProcedureExecutor().refreshConfiguration(conf);
146  }
147}