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 postLogRoll(Path oldPath, Path newPath) throws IOException {
048    manager.postLogRoll(newPath);
049  }
050
051  @Override
052  public void visitLogEntryBeforeWrite(RegionInfo info, WALKey logKey, WALEdit logEdit) {
053    scopeWALEdits(logKey, logEdit, conf);
054  }
055
056  /**
057   * Utility method used to set the correct scopes on each log key. Doesn't set a scope on keys from
058   * compaction WAL edits and if the scope is local.
059   * @param logKey  Key that may get scoped according to its edits
060   * @param logEdit Edits used to lookup the scopes
061   */
062  static void scopeWALEdits(WALKey logKey, WALEdit logEdit, Configuration conf) {
063    // For bulk load replication we need meta family to know the file we want to replicate.
064    if (ReplicationUtils.isReplicationForBulkLoadDataEnabled(conf)) {
065      return;
066    }
067    // Allow replication marker row to pass through.
068    if (WALEdit.isReplicationMarkerEdit(logEdit)) {
069      return;
070    }
071    // For replay, or if all the cells are markers, do not need to store replication scope.
072    if (
073      logEdit.isReplay() || logEdit.getCells().stream().allMatch(c -> WALEdit.isMetaEditFamily(c))
074    ) {
075      ((WALKeyImpl) logKey).clearReplicationScope();
076    }
077  }
078}