001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.master; 020 021import java.io.IOException; 022import java.util.ArrayList; 023import java.util.Collections; 024import java.util.HashSet; 025import java.util.List; 026import java.util.Set; 027import java.util.concurrent.locks.Lock; 028import java.util.concurrent.locks.ReentrantLock; 029 030import org.apache.hadoop.conf.Configuration; 031import org.apache.hadoop.fs.FileStatus; 032import org.apache.hadoop.fs.FileSystem; 033import org.apache.hadoop.fs.Path; 034import org.apache.hadoop.fs.PathFilter; 035import org.apache.hadoop.hbase.HConstants; 036import org.apache.hadoop.hbase.ServerName; 037import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 038import org.apache.hadoop.hbase.util.FSUtils; 039import org.apache.hadoop.hbase.wal.AbstractFSWALProvider; 040import org.apache.hadoop.hbase.wal.WALSplitter; 041import org.apache.yetus.audience.InterfaceAudience; 042import org.slf4j.Logger; 043import org.slf4j.LoggerFactory; 044import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; 045 046/** 047 * This class abstracts a bunch of operations the HMaster needs 048 * when splitting log files e.g. finding log files, dirs etc. 049 */ 050@InterfaceAudience.Private 051public class MasterWalManager { 052 private static final Logger LOG = LoggerFactory.getLogger(MasterWalManager.class); 053 054 final static PathFilter META_FILTER = new PathFilter() { 055 @Override 056 public boolean accept(Path p) { 057 return AbstractFSWALProvider.isMetaFile(p); 058 } 059 }; 060 061 final static PathFilter NON_META_FILTER = new PathFilter() { 062 @Override 063 public boolean accept(Path p) { 064 return !AbstractFSWALProvider.isMetaFile(p); 065 } 066 }; 067 068 // metrics for master 069 // TODO: Rename it, since those metrics are split-manager related 070 private final MetricsMasterFileSystem metricsMasterFilesystem = new MetricsMasterFileSystem(); 071 072 // Keep around for convenience. 073 private final MasterServices services; 074 private final Configuration conf; 075 private final FileSystem fs; 076 077 // The Path to the old logs dir 078 private final Path oldLogDir; 079 private final Path rootDir; 080 081 // create the split log lock 082 private final Lock splitLogLock = new ReentrantLock(); 083 private final SplitLogManager splitLogManager; 084 085 // Is the fileystem ok? 086 private volatile boolean fsOk = true; 087 088 public MasterWalManager(MasterServices services) throws IOException { 089 this(services.getConfiguration(), services.getMasterFileSystem().getWALFileSystem(), 090 services.getMasterFileSystem().getWALRootDir(), services); 091 } 092 093 public MasterWalManager(Configuration conf, FileSystem fs, Path rootDir, MasterServices services) 094 throws IOException { 095 this.fs = fs; 096 this.conf = conf; 097 this.rootDir = rootDir; 098 this.services = services; 099 this.splitLogManager = new SplitLogManager(services, conf); 100 101 this.oldLogDir = new Path(rootDir, HConstants.HREGION_OLDLOGDIR_NAME); 102 } 103 104 public void stop() { 105 if (splitLogManager != null) { 106 splitLogManager.stop(); 107 } 108 } 109 110 @VisibleForTesting 111 SplitLogManager getSplitLogManager() { 112 return this.splitLogManager; 113 } 114 115 /** 116 * Get the directory where old logs go 117 * @return the dir 118 */ 119 Path getOldLogDir() { 120 return this.oldLogDir; 121 } 122 123 public FileSystem getFileSystem() { 124 return this.fs; 125 } 126 127 /** 128 * Checks to see if the file system is still accessible. 129 * If not, sets closed 130 * @return false if file system is not available 131 */ 132 private boolean checkFileSystem() { 133 if (this.fsOk) { 134 try { 135 FSUtils.checkFileSystemAvailable(this.fs); 136 FSUtils.checkDfsSafeMode(this.conf); 137 } catch (IOException e) { 138 services.abort("Shutting down HBase cluster: file system not available", e); 139 this.fsOk = false; 140 } 141 } 142 return this.fsOk; 143 } 144 145 /** 146 * Inspect the log directory to find dead servers which need recovery work 147 * @return A set of ServerNames which aren't running but still have WAL files left in file system 148 */ 149 public Set<ServerName> getFailedServersFromLogFolders() { 150 boolean retrySplitting = !conf.getBoolean("hbase.hlog.split.skip.errors", 151 WALSplitter.SPLIT_SKIP_ERRORS_DEFAULT); 152 153 Set<ServerName> serverNames = new HashSet<>(); 154 Path logsDirPath = new Path(this.rootDir, HConstants.HREGION_LOGDIR_NAME); 155 156 do { 157 if (services.isStopped()) { 158 LOG.warn("Master stopped while trying to get failed servers."); 159 break; 160 } 161 try { 162 if (!this.fs.exists(logsDirPath)) return serverNames; 163 FileStatus[] logFolders = FSUtils.listStatus(this.fs, logsDirPath, null); 164 // Get online servers after getting log folders to avoid log folder deletion of newly 165 // checked in region servers . see HBASE-5916 166 Set<ServerName> onlineServers = services.getServerManager().getOnlineServers().keySet(); 167 168 if (logFolders == null || logFolders.length == 0) { 169 LOG.debug("No log files to split, proceeding..."); 170 return serverNames; 171 } 172 for (FileStatus status : logFolders) { 173 FileStatus[] curLogFiles = FSUtils.listStatus(this.fs, status.getPath(), null); 174 if (curLogFiles == null || curLogFiles.length == 0) { 175 // Empty log folder. No recovery needed 176 continue; 177 } 178 final ServerName serverName = AbstractFSWALProvider.getServerNameFromWALDirectoryName( 179 status.getPath()); 180 if (null == serverName) { 181 LOG.warn("Log folder " + status.getPath() + " doesn't look like its name includes a " + 182 "region server name; leaving in place. If you see later errors about missing " + 183 "write ahead logs they may be saved in this location."); 184 } else if (!onlineServers.contains(serverName)) { 185 LOG.info("Log folder " + status.getPath() + " doesn't belong " 186 + "to a known region server, splitting"); 187 serverNames.add(serverName); 188 } else { 189 LOG.info("Log folder " + status.getPath() + " belongs to an existing region server"); 190 } 191 } 192 retrySplitting = false; 193 } catch (IOException ioe) { 194 LOG.warn("Failed getting failed servers to be recovered.", ioe); 195 if (!checkFileSystem()) { 196 LOG.warn("Bad Filesystem, exiting"); 197 Runtime.getRuntime().halt(1); 198 } 199 try { 200 if (retrySplitting) { 201 Thread.sleep(conf.getInt("hbase.hlog.split.failure.retry.interval", 30 * 1000)); 202 } 203 } catch (InterruptedException e) { 204 LOG.warn("Interrupted, aborting since cannot return w/o splitting"); 205 Thread.currentThread().interrupt(); 206 retrySplitting = false; 207 Runtime.getRuntime().halt(1); 208 } 209 } 210 } while (retrySplitting); 211 212 return serverNames; 213 } 214 215 public void splitLog(final ServerName serverName) throws IOException { 216 splitLog(Collections.<ServerName>singleton(serverName)); 217 } 218 219 /** 220 * Specialized method to handle the splitting for meta WAL 221 * @param serverName logs belonging to this server will be split 222 */ 223 public void splitMetaLog(final ServerName serverName) throws IOException { 224 splitMetaLog(Collections.<ServerName>singleton(serverName)); 225 } 226 227 /** 228 * Specialized method to handle the splitting for meta WAL 229 * @param serverNames logs belonging to these servers will be split 230 */ 231 public void splitMetaLog(final Set<ServerName> serverNames) throws IOException { 232 splitLog(serverNames, META_FILTER); 233 } 234 235 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="UL_UNRELEASED_LOCK", justification= 236 "We only release this lock when we set it. Updates to code that uses it should verify use " + 237 "of the guard boolean.") 238 private List<Path> getLogDirs(final Set<ServerName> serverNames) throws IOException { 239 List<Path> logDirs = new ArrayList<>(); 240 boolean needReleaseLock = false; 241 if (!this.services.isInitialized()) { 242 // during master initialization, we could have multiple places splitting a same wal 243 this.splitLogLock.lock(); 244 needReleaseLock = true; 245 } 246 try { 247 for (ServerName serverName : serverNames) { 248 Path logDir = new Path(this.rootDir, 249 AbstractFSWALProvider.getWALDirectoryName(serverName.toString())); 250 Path splitDir = logDir.suffix(AbstractFSWALProvider.SPLITTING_EXT); 251 // Rename the directory so a rogue RS doesn't create more WALs 252 if (fs.exists(logDir)) { 253 if (!this.fs.rename(logDir, splitDir)) { 254 throw new IOException("Failed fs.rename for log split: " + logDir); 255 } 256 logDir = splitDir; 257 LOG.debug("Renamed region directory: " + splitDir); 258 } else if (!fs.exists(splitDir)) { 259 LOG.info("Log dir for server " + serverName + " does not exist"); 260 continue; 261 } 262 logDirs.add(splitDir); 263 } 264 } catch (IOException ioe) { 265 if (!checkFileSystem()) { 266 this.services.abort("Aborting due to filesystem unavailable", ioe); 267 throw ioe; 268 } 269 } finally { 270 if (needReleaseLock) { 271 this.splitLogLock.unlock(); 272 } 273 } 274 return logDirs; 275 } 276 277 public void splitLog(final Set<ServerName> serverNames) throws IOException { 278 splitLog(serverNames, NON_META_FILTER); 279 } 280 281 /** 282 * This method is the base split method that splits WAL files matching a filter. Callers should 283 * pass the appropriate filter for meta and non-meta WALs. 284 * @param serverNames logs belonging to these servers will be split; this will rename the log 285 * directory out from under a soft-failed server 286 */ 287 public void splitLog(final Set<ServerName> serverNames, PathFilter filter) throws IOException { 288 long splitTime = 0, splitLogSize = 0; 289 List<Path> logDirs = getLogDirs(serverNames); 290 291 splitLogManager.handleDeadWorkers(serverNames); 292 splitTime = EnvironmentEdgeManager.currentTime(); 293 splitLogSize = splitLogManager.splitLogDistributed(serverNames, logDirs, filter); 294 splitTime = EnvironmentEdgeManager.currentTime() - splitTime; 295 296 if (this.metricsMasterFilesystem != null) { 297 if (filter == META_FILTER) { 298 this.metricsMasterFilesystem.addMetaWALSplit(splitTime, splitLogSize); 299 } else { 300 this.metricsMasterFilesystem.addSplit(splitTime, splitLogSize); 301 } 302 } 303 } 304}