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 */
018package org.apache.hadoop.hbase.master.procedure;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.fs.FileSystem;
023import org.apache.hadoop.fs.Path;
024import org.apache.hadoop.hbase.conf.ConfigurationObserver;
025import org.apache.hadoop.hbase.ipc.RpcServer;
026import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
027import org.apache.hadoop.hbase.master.MasterFileSystem;
028import org.apache.hadoop.hbase.master.MasterServices;
029import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
030import org.apache.hadoop.hbase.master.replication.ReplicationPeerManager;
031import org.apache.hadoop.hbase.procedure2.Procedure;
032import org.apache.hadoop.hbase.procedure2.ProcedureEvent;
033import org.apache.hadoop.hbase.procedure2.store.LeaseRecovery;
034import org.apache.hadoop.hbase.security.Superusers;
035import org.apache.hadoop.hbase.security.User;
036import org.apache.hadoop.hbase.util.CancelableProgressable;
037import org.apache.hadoop.hbase.util.RecoverLeaseFSUtils;
038import org.apache.yetus.audience.InterfaceAudience;
039import org.apache.yetus.audience.InterfaceStability;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
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 FsUtilsLeaseRecovery implements LeaseRecovery {
050    private final MasterServices master;
051
052    public FsUtilsLeaseRecovery(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      RecoverLeaseFSUtils.recoverFileLease(fs, path, conf, new CancelableProgressable() {
060        @Override
061        public boolean progress() {
062          LOG.debug("Recover Procedure Store log lease: " + path);
063          return isRunning();
064        }
065      });
066    }
067
068    private boolean isRunning() {
069      return !master.isStopped() && !master.isStopping() && !master.isAborted();
070    }
071  }
072
073  private final RSProcedureDispatcher remoteDispatcher;
074  private final MasterProcedureScheduler procSched;
075  private final MasterServices master;
076
077  public MasterProcedureEnv(final MasterServices master) {
078    this(master, new RSProcedureDispatcher(master));
079  }
080
081  public MasterProcedureEnv(final MasterServices master,
082    final RSProcedureDispatcher remoteDispatcher) {
083    this.master = master;
084    this.procSched = new MasterProcedureScheduler(
085      procId -> master.getMasterProcedureExecutor().getProcedure(procId));
086    this.remoteDispatcher = remoteDispatcher;
087  }
088
089  public User getRequestUser() {
090    return RpcServer.getRequestUser().orElse(Superusers.getSystemUser());
091  }
092
093  public MasterServices getMasterServices() {
094    return master;
095  }
096
097  public Configuration getMasterConfiguration() {
098    return master.getConfiguration();
099  }
100
101  public AssignmentManager getAssignmentManager() {
102    return master.getAssignmentManager();
103  }
104
105  public MasterCoprocessorHost getMasterCoprocessorHost() {
106    return master.getMasterCoprocessorHost();
107  }
108
109  public MasterProcedureScheduler getProcedureScheduler() {
110    return procSched;
111  }
112
113  public RSProcedureDispatcher getRemoteDispatcher() {
114    return remoteDispatcher;
115  }
116
117  public ReplicationPeerManager getReplicationPeerManager() {
118    return master.getReplicationPeerManager();
119  }
120
121  public MasterFileSystem getMasterFileSystem() {
122    return master.getMasterFileSystem();
123  }
124
125  public boolean isRunning() {
126    if (this.master == null || this.master.getMasterProcedureExecutor() == null) return false;
127    return master.getMasterProcedureExecutor().isRunning();
128  }
129
130  public boolean isInitialized() {
131    return master.isInitialized();
132  }
133
134  public boolean waitInitialized(Procedure<?> proc) {
135    return master.getInitializedEvent().suspendIfNotReady(proc);
136  }
137
138  public void setEventReady(ProcedureEvent<?> event, boolean isReady) {
139    if (isReady) {
140      event.wake(procSched);
141    } else {
142      event.suspend();
143    }
144  }
145
146  @Override
147  public void onConfigurationChange(Configuration conf) {
148    master.getMasterProcedureExecutor().refreshConfiguration(conf);
149  }
150}