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;
019
020import java.util.UUID;
021import org.apache.hadoop.hbase.HBaseInterfaceAudience;
022import org.apache.hadoop.hbase.wal.WAL.Entry;
023import org.apache.hadoop.hbase.wal.WALEdit;
024import org.apache.hadoop.hbase.wal.WALKeyImpl;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.apache.yetus.audience.InterfaceStability;
027
028/**
029 * Filters out entries with our peerClusterId (i.e. already replicated) and marks all other entries
030 * with our clusterID
031 */
032@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.REPLICATION)
033@InterfaceStability.Evolving
034public class ClusterMarkingEntryFilter implements WALEntryFilter {
035  private UUID clusterId;
036  private UUID peerClusterId;
037  private ReplicationEndpoint replicationEndpoint;
038
039  /**
040   * @param clusterId           id of this cluster
041   * @param peerClusterId       of the other cluster
042   * @param replicationEndpoint ReplicationEndpoint which will handle the actual replication
043   */
044  public ClusterMarkingEntryFilter(UUID clusterId, UUID peerClusterId,
045    ReplicationEndpoint replicationEndpoint) {
046    this.clusterId = clusterId;
047    this.peerClusterId = peerClusterId;
048    this.replicationEndpoint = replicationEndpoint;
049  }
050
051  @Override
052  public Entry filter(Entry entry) {
053    // don't replicate if the log entries have already been consumed by the cluster
054    if (
055      replicationEndpoint.canReplicateToSameCluster()
056        || !entry.getKey().getClusterIds().contains(peerClusterId)
057    ) {
058      WALEdit edit = entry.getEdit();
059      WALKeyImpl logKey = (WALKeyImpl) entry.getKey();
060
061      if (edit != null && !edit.isEmpty()) {
062        // Mark that the current cluster has the change
063        logKey.addClusterId(clusterId);
064        return entry;
065      }
066    }
067    return null;
068  }
069}