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.NavigableMap;
021import org.apache.hadoop.hbase.Cell;
022import org.apache.hadoop.hbase.CellUtil;
023import org.apache.hadoop.hbase.HConstants;
024import org.apache.hadoop.hbase.wal.WAL.Entry;
025import org.apache.hadoop.hbase.wal.WALEdit;
026import org.apache.yetus.audience.InterfaceAudience;
027
028import org.apache.hbase.thirdparty.com.google.common.base.Predicate;
029
030/**
031 * Keeps KVs that are scoped other than local
032 */
033@InterfaceAudience.Private
034public class ScopeWALEntryFilter implements WALEntryFilter, WALCellFilter {
035
036  private final BulkLoadCellFilter bulkLoadFilter = new BulkLoadCellFilter();
037
038  @Override
039  public Entry filter(Entry entry) {
040    // Do not filter out an entire entry by replication scopes. As now we support serial
041    // replication, the sequence id of a marker is also needed by upper layer. We will filter out
042    // all the cells in the filterCell method below if the replication scopes is null or empty.
043    return entry;
044  }
045
046  private boolean hasGlobalScope(NavigableMap<byte[], Integer> scopes, byte[] family) {
047    Integer scope = scopes.get(family);
048    return scope != null && scope.intValue() == HConstants.REPLICATION_SCOPE_GLOBAL;
049  }
050  @Override
051  public Cell filterCell(Entry entry, Cell cell) {
052    NavigableMap<byte[], Integer> scopes = entry.getKey().getReplicationScopes();
053    if (scopes == null || scopes.isEmpty()) {
054      return null;
055    }
056    byte[] family = CellUtil.cloneFamily(cell);
057    if (CellUtil.matchingColumn(cell, WALEdit.METAFAMILY, WALEdit.BULK_LOAD)) {
058      return bulkLoadFilter.filterCell(cell, new Predicate<byte[]>() {
059        @Override
060        public boolean apply(byte[] family) {
061          return !hasGlobalScope(scopes, family);
062        }
063      });
064    }
065    return hasGlobalScope(scopes, family) ? cell : null;
066  }
067}