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.replication.regionserver;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.fs.Path;
023import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
024import org.apache.hadoop.hbase.replication.ReplicationUtils;
025import org.apache.hadoop.hbase.wal.WALEdit;
026import org.apache.hadoop.hbase.wal.WALKey;
027import org.apache.hadoop.hbase.wal.WALKeyImpl;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * Used to receive new wals.
032 */
033@InterfaceAudience.Private
034class ReplicationSourceWALActionListener implements WALActionsListener {
035
036  private final Configuration conf;
037
038  private final ReplicationSourceManager manager;
039
040  public ReplicationSourceWALActionListener(Configuration conf, ReplicationSourceManager manager) {
041    this.conf = conf;
042    this.manager = manager;
043  }
044
045  @Override
046  public void preLogRoll(Path oldPath, Path newPath) throws IOException {
047    manager.preLogRoll(newPath);
048  }
049
050  @Override
051  public void postLogRoll(Path oldPath, Path newPath) throws IOException {
052    manager.postLogRoll(newPath);
053  }
054
055  @Override
056  public void visitLogEntryBeforeWrite(WALKey logKey, WALEdit logEdit) throws IOException {
057    scopeWALEdits(logKey, logEdit, conf);
058  }
059
060  /**
061   * Utility method used to set the correct scopes on each log key. Doesn't set a scope on keys from
062   * compaction WAL edits and if the scope is local.
063   * @param logKey Key that may get scoped according to its edits
064   * @param logEdit Edits used to lookup the scopes
065   */
066  static void scopeWALEdits(WALKey logKey, WALEdit logEdit, Configuration conf) {
067    // For bulk load replication we need meta family to know the file we want to replicate.
068    if (ReplicationUtils.isReplicationForBulkLoadDataEnabled(conf)) {
069      return;
070    }
071    // For replay, or if all the cells are markers, do not need to store replication scope.
072    if (logEdit.isReplay() ||
073      logEdit.getCells().stream().allMatch(c -> WALEdit.isMetaEditFamily(c))) {
074      ((WALKeyImpl) logKey).clearReplicationScope();
075    }
076  }
077}