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