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/* SplitLogWorker part */
052  int DEFAULT_MAX_SPLITTERS = 2;
053
054  /**
055   * Initialize internal values. This method should be used when corresponding SplitLogWorker
056   * instance is created
057   * @param server instance of RegionServerServices to work with
058   * @param conf is current configuration.
059   * @param splitTaskExecutor split executor from SplitLogWorker
060   * @param worker instance of SplitLogWorker
061   */
062  void init(RegionServerServices server, Configuration conf,
063      TaskExecutor splitTaskExecutor, SplitLogWorker worker);
064
065  /**
066   *  called when Coordination should stop processing tasks and exit
067   */
068  void stopProcessingTasks();
069
070  /**
071   * @return the current value of exitWorker
072   */
073  boolean isStop();
074
075  /**
076   * Wait for the new tasks and grab one
077   * @throws InterruptedException if the SplitLogWorker was stopped
078   */
079  void taskLoop() throws InterruptedException;
080
081  /**
082   * marks log file as corrupted
083   * @param rootDir where to find the log
084   * @param name of the log
085   * @param fs file system
086   */
087  void markCorrupted(Path rootDir, String name, FileSystem fs);
088
089  /**
090   * Check whether the log splitter is ready to supply tasks
091   * @return false if there is no tasks
092   * @throws InterruptedException if the SplitLogWorker was stopped
093   */
094  boolean isReady() throws InterruptedException;
095
096  /**
097   * Used by unit tests to check how many tasks were processed
098   * @return number of tasks
099   */
100  @VisibleForTesting
101  int getTaskReadySeq();
102
103  /**
104   * set the listener for task changes. Implementation specific
105   */
106  void registerListener();
107
108  /**
109   * remove the listener for task changes. Implementation specific
110   */
111  void removeListener();
112
113  /* WALSplitterHandler part */
114
115  /**
116   * Notify coordination engine that splitting task has completed.
117   * @param slt See {@link SplitLogTask}
118   * @param ctr counter to be updated
119   * @param splitTaskDetails details about log split task (specific to coordination engine being
120   *          used).
121   */
122  void endTask(SplitLogTask slt, LongAdder ctr, SplitTaskDetails splitTaskDetails);
123
124  /**
125   * Interface for log-split tasks Used to carry implementation details in encapsulated way through
126   * Handlers to the coordination API.
127   */
128  interface SplitTaskDetails {
129
130    /**
131     * @return full file path in HDFS for the WAL file to be split.
132     */
133    String getWALFile();
134  }
135}