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.hadoop.hbase.ipc.RpcServer;
027import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
028import org.apache.hadoop.hbase.master.MasterFileSystem;
029import org.apache.hadoop.hbase.master.MasterServices;
030import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
031import org.apache.hadoop.hbase.master.replication.ReplicationPeerManager;
032import org.apache.hadoop.hbase.procedure2.Procedure;
033import org.apache.hadoop.hbase.procedure2.ProcedureEvent;
034import org.apache.hadoop.hbase.procedure2.store.LeaseRecovery;
035import org.apache.hadoop.hbase.security.Superusers;
036import org.apache.hadoop.hbase.security.User;
037import org.apache.hadoop.hbase.util.CancelableProgressable;
038import org.apache.hadoop.hbase.util.RecoverLeaseFSUtils;
039import org.apache.yetus.audience.InterfaceAudience;
040import org.apache.yetus.audience.InterfaceStability;
041import org.slf4j.Logger;
042import org.slf4j.LoggerFactory;
043
044@InterfaceAudience.Private
045@InterfaceStability.Evolving
046public class MasterProcedureEnv implements ConfigurationObserver {
047  private static final Logger LOG = LoggerFactory.getLogger(MasterProcedureEnv.class);
048
049  @InterfaceAudience.Private
050  public static class FsUtilsLeaseRecovery implements LeaseRecovery {
051    private final MasterServices master;
052
053    public FsUtilsLeaseRecovery(final MasterServices master) {
054      this.master = master;
055    }
056
057    @Override
058    public void recoverFileLease(final FileSystem fs, final Path path) throws IOException {
059      final Configuration conf = master.getConfiguration();
060      RecoverLeaseFSUtils.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 ReplicationPeerManager getReplicationPeerManager() {
119    return master.getReplicationPeerManager();
120  }
121
122  public MasterFileSystem getMasterFileSystem() {
123    return master.getMasterFileSystem();
124  }
125
126  public boolean isRunning() {
127    if (this.master == null || this.master.getMasterProcedureExecutor() == null) return false;
128    return master.getMasterProcedureExecutor().isRunning();
129  }
130
131  public boolean isInitialized() {
132    return master.isInitialized();
133  }
134
135  public boolean waitInitialized(Procedure<?> proc) {
136    return master.getInitializedEvent().suspendIfNotReady(proc);
137  }
138
139  public void setEventReady(ProcedureEvent<?> event, boolean isReady) {
140    if (isReady) {
141      event.wake(procSched);
142    } else {
143      event.suspend();
144    }
145  }
146
147  @Override
148  public void onConfigurationChange(Configuration conf) {
149    master.getMasterProcedureExecutor().refreshConfiguration(conf);
150  }
151}