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