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.regionserver.handler;
020
021import java.io.IOException;
022import java.util.concurrent.atomic.AtomicInteger;
023
024import org.apache.yetus.audience.InterfaceAudience;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027import org.apache.hadoop.hbase.Server;
028import org.apache.hadoop.hbase.ServerName;
029import org.apache.hadoop.hbase.SplitLogCounters;
030import org.apache.hadoop.hbase.SplitLogTask;
031import org.apache.hadoop.hbase.coordination.SplitLogWorkerCoordination;
032import org.apache.hadoop.hbase.executor.EventHandler;
033import org.apache.hadoop.hbase.executor.EventType;
034import org.apache.hadoop.hbase.regionserver.SplitLogWorker.TaskExecutor;
035import org.apache.hadoop.hbase.regionserver.SplitLogWorker.TaskExecutor.Status;
036import org.apache.hadoop.hbase.util.CancelableProgressable;
037
038/**
039 * Handles log splitting a wal
040 */
041@InterfaceAudience.Private
042public class WALSplitterHandler extends EventHandler {
043  private static final Logger LOG = LoggerFactory.getLogger(WALSplitterHandler.class);
044  private final ServerName serverName;
045  private final CancelableProgressable reporter;
046  private final AtomicInteger inProgressTasks;
047  private final TaskExecutor splitTaskExecutor;
048  private final SplitLogWorkerCoordination.SplitTaskDetails splitTaskDetails;
049  private final SplitLogWorkerCoordination coordination;
050
051
052  public WALSplitterHandler(final Server server, SplitLogWorkerCoordination coordination,
053      SplitLogWorkerCoordination.SplitTaskDetails splitDetails, CancelableProgressable reporter,
054      AtomicInteger inProgressTasks, TaskExecutor splitTaskExecutor) {
055    super(server, EventType.RS_LOG_REPLAY);
056    this.splitTaskDetails = splitDetails;
057    this.coordination = coordination;
058    this.reporter = reporter;
059    this.inProgressTasks = inProgressTasks;
060    this.inProgressTasks.incrementAndGet();
061    this.serverName = server.getServerName();
062    this.splitTaskExecutor = splitTaskExecutor;
063  }
064
065  @Override
066  public void process() throws IOException {
067    long startTime = System.currentTimeMillis();
068    Status status = null;
069    try {
070      status = this.splitTaskExecutor.exec(splitTaskDetails.getWALFile(), reporter);
071      switch (status) {
072      case DONE:
073        coordination.endTask(new SplitLogTask.Done(this.serverName),
074          SplitLogCounters.tot_wkr_task_done, splitTaskDetails);
075        break;
076      case PREEMPTED:
077        SplitLogCounters.tot_wkr_preempt_task.increment();
078        LOG.warn("task execution preempted " + splitTaskDetails.getWALFile());
079        break;
080      case ERR:
081        if (server != null && !server.isStopped()) {
082          coordination.endTask(new SplitLogTask.Err(this.serverName),
083            SplitLogCounters.tot_wkr_task_err, splitTaskDetails);
084          break;
085        }
086        // if the RS is exiting then there is probably a tons of stuff
087        // that can go wrong. Resign instead of signaling error.
088        //$FALL-THROUGH$
089      case RESIGNED:
090        if (server != null && server.isStopped()) {
091          LOG.info("task execution interrupted because worker is exiting "
092              + splitTaskDetails.toString());
093        }
094        coordination.endTask(new SplitLogTask.Resigned(this.serverName),
095          SplitLogCounters.tot_wkr_task_resigned, splitTaskDetails);
096        break;
097      }
098    } finally {
099      LOG.info("Worker " + serverName + " done with task " + splitTaskDetails.toString() + " in "
100          + (System.currentTimeMillis() - startTime) + "ms. Status = " + status);
101      this.inProgressTasks.decrementAndGet();
102    }
103  }
104}