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.util.concurrent.locks.Lock;
021import org.apache.hadoop.hbase.HBaseIOException;
022import org.apache.hadoop.hbase.executor.EventType;
023import org.apache.hadoop.hbase.procedure2.BaseRSProcedureCallable;
024import org.apache.hadoop.hbase.util.KeyLocker;
025import org.apache.yetus.audience.InterfaceAudience;
026
027import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
028
029import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos;
030
031/**
032 * This callable is used to do the real split WAL task. It is called by
033 * {@link org.apache.hadoop.hbase.master.procedure.SplitWALRemoteProcedure} from master and executed
034 * by executor service which is in charge of executing the events of EventType.RS_LOG_REPLAY When
035 * execute this callable, it will call SplitLogWorker.splitLog() to split the WAL. If the return
036 * value is SplitLogWorker.TaskExecutor.Status.DONE, it means the task is successful and it will
037 * return null to end the call. Otherwise it will throw an exception and let
038 * {@link org.apache.hadoop.hbase.master.procedure.SplitWALRemoteProcedure} to handle this problem.
039 * This class is to replace the zk-based WAL splitting related code, {@link SplitLogWorker},
040 * {@link org.apache.hadoop.hbase.coordination.SplitLogWorkerCoordination} and
041 * {@link org.apache.hadoop.hbase.coordination.ZkSplitLogWorkerCoordination} can be removed after we
042 * switch to procedure-based WAL splitting.
043 */
044@InterfaceAudience.Private
045public class SplitWALCallable extends BaseRSProcedureCallable {
046
047  private String walPath;
048  private final KeyLocker<String> splitWALLocks = new KeyLocker<>();
049  private volatile Lock splitWALLock = null;
050
051  @Override
052  protected void initParameter(byte[] parameter) throws InvalidProtocolBufferException {
053    MasterProcedureProtos.SplitWALParameter param =
054      MasterProcedureProtos.SplitWALParameter.parseFrom(parameter);
055    this.walPath = param.getWalPath();
056  }
057
058  @Override
059  public EventType getEventType() {
060    return EventType.RS_LOG_REPLAY;
061  }
062
063  public static class PreemptedWALSplitException extends HBaseIOException {
064    PreemptedWALSplitException(String wal) {
065      super(wal);
066    }
067  }
068
069  public static class ResignedWALSplitException extends HBaseIOException {
070    ResignedWALSplitException(String wal) {
071      super(wal);
072    }
073  }
074
075  public static class ErrorWALSplitException extends HBaseIOException {
076    ErrorWALSplitException(String wal) {
077      super(wal);
078    }
079  }
080
081  @Override
082  protected void doCall() throws Exception {
083    // grab a lock
084    splitWALLock = splitWALLocks.acquireLock(walPath);
085    try {
086      switch (SplitLogWorker.splitLog(walPath, null, rs.getConfiguration(), rs, rs,
087        rs.getWalFactory())) {
088        case DONE:
089          break;
090        case PREEMPTED:
091          throw new PreemptedWALSplitException(this.walPath);
092        case RESIGNED:
093          throw new ResignedWALSplitException(this.walPath);
094        default:
095          throw new ErrorWALSplitException(this.walPath);
096      }
097    } finally {
098      splitWALLock.unlock();
099    }
100  }
101
102  public String getWalPath() {
103    return this.walPath;
104  }
105}