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