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.Cell;
024import org.apache.hadoop.hbase.CellUtil;
025import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
026import org.apache.hadoop.hbase.replication.ReplicationUtils;
027import org.apache.hadoop.hbase.wal.WALEdit;
028import org.apache.hadoop.hbase.wal.WALKey;
029import org.apache.hadoop.hbase.wal.WALKeyImpl;
030import org.apache.yetus.audience.InterfaceAudience;
031
032import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
033
034import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos;
035
036/**
037 * Used to receive new wals.
038 */
039@InterfaceAudience.Private
040class ReplicationSourceWALActionListener implements WALActionsListener {
041
042  private final Configuration conf;
043
044  private final ReplicationSourceManager manager;
045
046  public ReplicationSourceWALActionListener(Configuration conf, ReplicationSourceManager manager) {
047    this.conf = conf;
048    this.manager = manager;
049  }
050
051  @Override
052  public void preLogRoll(Path oldPath, Path newPath) throws IOException {
053    manager.preLogRoll(newPath);
054  }
055
056  @Override
057  public void postLogRoll(Path oldPath, Path newPath) throws IOException {
058    manager.postLogRoll(newPath);
059  }
060
061  @Override
062  public void visitLogEntryBeforeWrite(WALKey logKey, WALEdit logEdit) throws IOException {
063    scopeWALEdits(logKey, logEdit, conf);
064  }
065
066  /**
067   * Utility method used to set the correct scopes on each log key. Doesn't set a scope on keys from
068   * compaction WAL edits and if the scope is local.
069   * @param logKey Key that may get scoped according to its edits
070   * @param logEdit Edits used to lookup the scopes
071   * @throws IOException If failed to parse the WALEdit
072   */
073  @VisibleForTesting
074  static void scopeWALEdits(WALKey logKey, WALEdit logEdit, Configuration conf) throws IOException {
075    boolean replicationForBulkLoadEnabled =
076        ReplicationUtils.isReplicationForBulkLoadDataEnabled(conf);
077    boolean foundOtherEdits = false;
078    for (Cell cell : logEdit.getCells()) {
079      if (!CellUtil.matchingFamily(cell, WALEdit.METAFAMILY)) {
080        foundOtherEdits = true;
081        break;
082      }
083    }
084
085    if (!foundOtherEdits && logEdit.getCells().size() > 0) {
086      WALProtos.RegionEventDescriptor maybeEvent =
087          WALEdit.getRegionEventDescriptor(logEdit.getCells().get(0));
088      if (maybeEvent != null &&
089        (maybeEvent.getEventType() == WALProtos.RegionEventDescriptor.EventType.REGION_CLOSE)) {
090        // In serially replication, we use scopes when reading close marker.
091        foundOtherEdits = true;
092      }
093    }
094    if ((!replicationForBulkLoadEnabled && !foundOtherEdits) || logEdit.isReplay()) {
095      ((WALKeyImpl) logKey).serializeReplicationScope(false);
096    }
097  }
098}