View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.replication;
20  
21  import java.util.NavigableMap;
22  
23  import org.apache.hadoop.hbase.Cell;
24  import org.apache.hadoop.hbase.CellUtil;
25  import org.apache.hadoop.hbase.HConstants;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
28  import org.apache.hadoop.hbase.wal.WAL.Entry;
29  
30  import com.google.common.base.Predicate;
31  
32  /**
33   * Keeps KVs that are scoped other than local
34   */
35  @InterfaceAudience.Private
36  public class ScopeWALEntryFilter implements WALEntryFilter, WALCellFilter {
37  
38    BulkLoadCellFilter bulkLoadFilter = new BulkLoadCellFilter();
39  
40    @Override
41    public Entry filter(Entry entry) {
42      NavigableMap<byte[], Integer> scopes = entry.getKey().getScopes();
43      if (scopes == null || scopes.isEmpty()) {
44        return null;
45      }
46      return entry;
47    }
48  
49    @Override
50    public Cell filterCell(Entry entry, Cell cell) {
51      final NavigableMap<byte[], Integer> scopes = entry.getKey().getScopes();
52        // The scope will be null or empty if
53        // there's nothing to replicate in that WALEdit
54        byte[] fam = CellUtil.cloneFamily(cell);
55        if (CellUtil.matchingColumn(cell, WALEdit.METAFAMILY, WALEdit.BULK_LOAD)) {
56          cell = bulkLoadFilter.filterCell(cell, new Predicate<byte[]>() {
57            @Override
58            public boolean apply(byte[] fam) {
59              return !scopes.containsKey(fam) || scopes.get(fam) == HConstants.REPLICATION_SCOPE_LOCAL;
60            }
61          });
62        } else {
63          if (!scopes.containsKey(fam) || scopes.get(fam) == HConstants.REPLICATION_SCOPE_LOCAL) {
64            return null;
65          }
66        }
67      return cell;
68    }
69  }