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 java.util.concurrent.ExecutorService;
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  /**
091   * Get a thread pool for executing some asynchronous tasks
092   */
093  public ExecutorService getAsyncTaskExecutor() {
094    return master.getMasterProcedureExecutor().getAsyncTaskExecutor();
095  }
096
097  public User getRequestUser() {
098    return RpcServer.getRequestUser().orElse(Superusers.getSystemUser());
099  }
100
101  public MasterServices getMasterServices() {
102    return master;
103  }
104
105  public Configuration getMasterConfiguration() {
106    return master.getConfiguration();
107  }
108
109  public AssignmentManager getAssignmentManager() {
110    return master.getAssignmentManager();
111  }
112
113  public MasterCoprocessorHost getMasterCoprocessorHost() {
114    return master.getMasterCoprocessorHost();
115  }
116
117  public MasterProcedureScheduler getProcedureScheduler() {
118    return procSched;
119  }
120
121  public RSProcedureDispatcher getRemoteDispatcher() {
122    return remoteDispatcher;
123  }
124
125  public ReplicationPeerManager getReplicationPeerManager() {
126    return master.getReplicationPeerManager();
127  }
128
129  public MasterFileSystem getMasterFileSystem() {
130    return master.getMasterFileSystem();
131  }
132
133  public boolean isRunning() {
134    if (this.master == null || this.master.getMasterProcedureExecutor() == null) return false;
135    return master.getMasterProcedureExecutor().isRunning();
136  }
137
138  public boolean isInitialized() {
139    return master.isInitialized();
140  }
141
142  public boolean waitInitialized(Procedure<?> proc) {
143    return master.getInitializedEvent().suspendIfNotReady(proc);
144  }
145
146  public void setEventReady(ProcedureEvent<?> event, boolean isReady) {
147    if (isReady) {
148      event.wake(procSched);
149    } else {
150      event.suspend();
151    }
152  }
153
154  @Override
155  public void onConfigurationChange(Configuration conf) {
156    master.getMasterProcedureExecutor().refreshConfiguration(conf);
157  }
158}