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.coordination;
020import java.util.concurrent.atomic.LongAdder;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.SplitLogTask;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.hadoop.hbase.regionserver.RegionServerServices;
028import org.apache.hadoop.hbase.regionserver.SplitLogWorker;
029import org.apache.hadoop.hbase.regionserver.SplitLogWorker.TaskExecutor;
030
031import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
032
033/**
034 * Coordinated operations for {@link SplitLogWorker} and
035 * {@link org.apache.hadoop.hbase.regionserver.handler.WALSplitterHandler} Important
036 * methods for SplitLogWorker: <BR>
037 * {@link #isReady()} called from {@link SplitLogWorker#run()} to check whether the coordination is
038 * ready to supply the tasks <BR>
039 * {@link #taskLoop()} loop for new tasks until the worker is stopped <BR>
040 * {@link #isStop()} a flag indicates whether worker should finish <BR>
041 * {@link #registerListener()} called from {@link SplitLogWorker#run()} and could register listener
042 * for external changes in coordination (if required) <BR>
043 * {@link #endTask(SplitLogTask, LongAdder, SplitTaskDetails)} notify coordination engine that
044 * <p>
045 * Important methods for WALSplitterHandler: <BR>
046 * splitting task has completed.
047 */
048@InterfaceAudience.Private
049public interface SplitLogWorkerCoordination {
050
051  /**
052   * Initialize internal values. This method should be used when corresponding SplitLogWorker
053   * instance is created
054   * @param server instance of RegionServerServices to work with
055   * @param conf is current configuration.
056   * @param splitTaskExecutor split executor from SplitLogWorker
057   * @param worker instance of SplitLogWorker
058   */
059  void init(RegionServerServices server, Configuration conf,
060      TaskExecutor splitTaskExecutor, SplitLogWorker worker);
061
062  /**
063   *  called when Coordination should stop processing tasks and exit
064   */
065  void stopProcessingTasks();
066
067  /**
068   * @return the current value of exitWorker
069   */
070  boolean isStop();
071
072  /**
073   * Wait for the new tasks and grab one
074   * @throws InterruptedException if the SplitLogWorker was stopped
075   */
076  void taskLoop() throws InterruptedException;
077
078  /**
079   * marks log file as corrupted
080   * @param rootDir where to find the log
081   * @param name of the log
082   * @param fs file system
083   */
084  void markCorrupted(Path rootDir, String name, FileSystem fs);
085
086  /**
087   * Check whether the log splitter is ready to supply tasks
088   * @return false if there is no tasks
089   * @throws InterruptedException if the SplitLogWorker was stopped
090   */
091  boolean isReady() throws InterruptedException;
092
093  /**
094   * Used by unit tests to check how many tasks were processed
095   * @return number of tasks
096   */
097  @VisibleForTesting
098  int getTaskReadySeq();
099
100  /**
101   * set the listener for task changes. Implementation specific
102   */
103  void registerListener();
104
105  /**
106   * remove the listener for task changes. Implementation specific
107   */
108  void removeListener();
109
110  /* WALSplitterHandler part */
111
112  /**
113   * Notify coordination engine that splitting task has completed.
114   * @param slt See {@link SplitLogTask}
115   * @param ctr counter to be updated
116   * @param splitTaskDetails details about log split task (specific to coordination engine being
117   *          used).
118   */
119  void endTask(SplitLogTask slt, LongAdder ctr, SplitTaskDetails splitTaskDetails);
120
121  /**
122   * Interface for log-split tasks Used to carry implementation details in encapsulated way through
123   * Handlers to the coordination API.
124   */
125  interface SplitTaskDetails {
126
127    /**
128     * @return full file path in HDFS for the WAL file to be split.
129     */
130    String getWALFile();
131  }
132}