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.regionserver; 019 020import java.io.FileNotFoundException; 021import java.io.IOException; 022import java.io.InterruptedIOException; 023import java.net.ConnectException; 024import java.net.SocketTimeoutException; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.fs.FileSystem; 027import org.apache.hadoop.fs.Path; 028import org.apache.hadoop.hbase.NotServingRegionException; 029import org.apache.hadoop.hbase.Server; 030import org.apache.hadoop.hbase.client.RetriesExhaustedException; 031import org.apache.hadoop.hbase.coordination.SplitLogWorkerCoordination; 032import org.apache.hadoop.hbase.regionserver.SplitLogWorker.TaskExecutor.Status; 033import org.apache.hadoop.hbase.util.CancelableProgressable; 034import org.apache.hadoop.hbase.util.CommonFSUtils; 035import org.apache.hadoop.hbase.util.ExceptionUtil; 036import org.apache.hadoop.hbase.wal.WALFactory; 037import org.apache.hadoop.hbase.wal.WALSplitter; 038import org.apache.yetus.audience.InterfaceAudience; 039import org.slf4j.Logger; 040import org.slf4j.LoggerFactory; 041import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; 042 043/** 044 * This worker is spawned in every regionserver, including master. The Worker waits for log 045 * splitting tasks to be put up by the {@link org.apache.hadoop.hbase.master.SplitLogManager} 046 * running in the master and races with other workers in other serves to acquire those tasks. 047 * The coordination is done via coordination engine. 048 * <p> 049 * If a worker has successfully moved the task from state UNASSIGNED to OWNED then it owns the task. 050 * It keeps heart beating the manager by periodically moving the task from UNASSIGNED to OWNED 051 * state. On success it moves the task to TASK_DONE. On unrecoverable error it moves task state to 052 * ERR. If it cannot continue but wants the master to retry the task then it moves the task state to 053 * RESIGNED. 054 * <p> 055 * The manager can take a task away from a worker by moving the task from OWNED to UNASSIGNED. In 056 * the absence of a global lock there is a unavoidable race here - a worker might have just finished 057 * its task when it is stripped of its ownership. Here we rely on the idempotency of the log 058 * splitting task for correctness 059 */ 060@InterfaceAudience.Private 061public class SplitLogWorker implements Runnable { 062 063 private static final Logger LOG = LoggerFactory.getLogger(SplitLogWorker.class); 064 065 Thread worker; 066 // thread pool which executes recovery work 067 private final SplitLogWorkerCoordination coordination; 068 private final Configuration conf; 069 private final RegionServerServices server; 070 071 public SplitLogWorker(Server hserver, Configuration conf, RegionServerServices server, 072 TaskExecutor splitTaskExecutor) { 073 this.server = server; 074 // Unused. 075 this.conf = conf; 076 this.coordination = hserver.getCoordinatedStateManager().getSplitLogWorkerCoordination(); 077 coordination.init(server, conf, splitTaskExecutor, this); 078 } 079 080 public SplitLogWorker(Configuration conf, RegionServerServices server, 081 LastSequenceId sequenceIdChecker, WALFactory factory) { 082 this(server, conf, server, (f, p) -> splitLog(f, p, conf, server, sequenceIdChecker, factory)); 083 } 084 085 /** 086 * @return Result either DONE, RESIGNED, or ERR. 087 */ 088 static Status splitLog(String filename, CancelableProgressable p, Configuration conf, 089 RegionServerServices server, LastSequenceId sequenceIdChecker, WALFactory factory) { 090 Path walDir; 091 FileSystem fs; 092 try { 093 walDir = CommonFSUtils.getWALRootDir(conf); 094 fs = walDir.getFileSystem(conf); 095 } catch (IOException e) { 096 LOG.warn("Resigning, could not find root dir or fs", e); 097 return Status.RESIGNED; 098 } 099 // TODO have to correctly figure out when log splitting has been 100 // interrupted or has encountered a transient error and when it has 101 // encountered a bad non-retry-able persistent error. 102 try { 103 SplitLogWorkerCoordination splitLogWorkerCoordination = 104 server.getCoordinatedStateManager() == null ? null 105 : server.getCoordinatedStateManager().getSplitLogWorkerCoordination(); 106 if (!WALSplitter.splitLogFile(walDir, fs.getFileStatus(new Path(walDir, filename)), fs, conf, 107 p, sequenceIdChecker, splitLogWorkerCoordination, factory, server)) { 108 return Status.PREEMPTED; 109 } 110 } catch (InterruptedIOException iioe) { 111 LOG.warn("Resigning, interrupted splitting WAL {}", filename, iioe); 112 return Status.RESIGNED; 113 } catch (IOException e) { 114 if (e instanceof FileNotFoundException) { 115 // A wal file may not exist anymore. Nothing can be recovered so move on 116 LOG.warn("Done, WAL {} does not exist anymore", filename, e); 117 return Status.DONE; 118 } 119 Throwable cause = e.getCause(); 120 if (e instanceof RetriesExhaustedException && (cause instanceof NotServingRegionException 121 || cause instanceof ConnectException || cause instanceof SocketTimeoutException)) { 122 LOG.warn("Resigning, can't connect to target regionserver splitting WAL {}", filename, e); 123 return Status.RESIGNED; 124 } else if (cause instanceof InterruptedException) { 125 LOG.warn("Resigning, interrupted splitting WAL {}", filename, e); 126 return Status.RESIGNED; 127 } 128 LOG.warn("Error splitting WAL {}", filename, e); 129 return Status.ERR; 130 } 131 LOG.debug("Done splitting WAL {}", filename); 132 return Status.DONE; 133 } 134 135 136 @Override 137 public void run() { 138 try { 139 LOG.info("SplitLogWorker " + server.getServerName() + " starting"); 140 coordination.registerListener(); 141 // wait for Coordination Engine is ready 142 boolean res = false; 143 while (!res && !coordination.isStop()) { 144 res = coordination.isReady(); 145 } 146 if (!coordination.isStop()) { 147 coordination.taskLoop(); 148 } 149 } catch (Throwable t) { 150 if (ExceptionUtil.isInterrupt(t)) { 151 LOG.info("SplitLogWorker interrupted. Exiting. " + (coordination.isStop() ? "" : 152 " (ERROR: exitWorker is not set, exiting anyway)")); 153 } else { 154 // only a logical error can cause here. Printing it out 155 // to make debugging easier 156 LOG.error("unexpected error ", t); 157 } 158 } finally { 159 coordination.removeListener(); 160 LOG.info("SplitLogWorker " + server.getServerName() + " exiting"); 161 } 162 } 163 164 /** 165 * If the worker is doing a task i.e. splitting a log file then stop the task. 166 * It doesn't exit the worker thread. 167 */ 168 public void stopTask() { 169 LOG.info("Sending interrupt to stop the worker thread"); 170 worker.interrupt(); // TODO interrupt often gets swallowed, do what else? 171 } 172 173 /** 174 * start the SplitLogWorker thread 175 */ 176 public void start() { 177 worker = new Thread(null, this, "SplitLogWorker-" + server.getServerName().toShortString()); 178 worker.start(); 179 } 180 181 /** 182 * stop the SplitLogWorker thread 183 */ 184 public void stop() { 185 coordination.stopProcessingTasks(); 186 stopTask(); 187 } 188 189 /** 190 * Objects implementing this interface actually do the task that has been 191 * acquired by a {@link SplitLogWorker}. Since there isn't a water-tight 192 * guarantee that two workers will not be executing the same task therefore it 193 * is better to have workers prepare the task and then have the 194 * {@link org.apache.hadoop.hbase.master.SplitLogManager} commit the work in 195 * SplitLogManager.TaskFinisher 196 */ 197 public interface TaskExecutor { 198 enum Status { 199 DONE(), 200 ERR(), 201 RESIGNED(), 202 PREEMPTED() 203 } 204 Status exec(String name, CancelableProgressable p); 205 } 206 207 /** 208 * Returns the number of tasks processed by coordination. 209 * This method is used by tests only 210 */ 211 @VisibleForTesting 212 public int getTaskReadySeq() { 213 return coordination.getTaskReadySeq(); 214 } 215}