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