1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 package org.apache.hadoop.hbase.replication;
20
21 import java.util.UUID;
22
23 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.classification.InterfaceStability;
26 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
27 import org.apache.hadoop.hbase.wal.WALKey;
28 import org.apache.hadoop.hbase.wal.WAL.Entry;
29
30
31 /**
32 * Filters out entries with our peerClusterId (i.e. already replicated)
33 * and marks all other entries with our clusterID
34 */
35 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.REPLICATION)
36 @InterfaceStability.Evolving
37 public class ClusterMarkingEntryFilter implements WALEntryFilter {
38 private UUID clusterId;
39 private UUID peerClusterId;
40 private ReplicationEndpoint replicationEndpoint;
41
42 /**
43 * @param clusterId id of this cluster
44 * @param peerClusterId of the other cluster
45 * @param replicationEndpoint ReplicationEndpoint which will handle the actual replication
46 */
47 public ClusterMarkingEntryFilter(UUID clusterId, UUID peerClusterId, ReplicationEndpoint replicationEndpoint) {
48 this.clusterId = clusterId;
49 this.peerClusterId = peerClusterId;
50 this.replicationEndpoint = replicationEndpoint;
51 }
52 @Override
53 public Entry filter(Entry entry) {
54 // don't replicate if the log entries have already been consumed by the cluster
55 if (replicationEndpoint.canReplicateToSameCluster()
56 || !entry.getKey().getClusterIds().contains(peerClusterId)) {
57 WALEdit edit = entry.getEdit();
58 WALKey logKey = entry.getKey();
59
60 if (edit != null && !edit.isEmpty()) {
61 // Mark that the current cluster has the change
62 logKey.addClusterId(clusterId);
63 // We need to set the CC to null else it will be compressed when sent to the sink
64 entry.setCompressionContext(null);
65 return entry;
66 }
67 }
68 return null;
69 }
70 }