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