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;
031import org.apache.hbase.thirdparty.org.apache.commons.collections4.MapUtils;
032
033/**
034 * Keeps KVs that are scoped other than local
035 */
036@InterfaceAudience.Private
037public class ScopeWALEntryFilter extends WALEntryFilterBase implements WALCellFilter {
038
039  private final BulkLoadCellFilter bulkLoadFilter = new BulkLoadCellFilter();
040
041  @Override
042  public Entry filter(Entry entry) {
043    NavigableMap<byte[], Integer> scopes = entry.getKey().getReplicationScopes();
044    if (MapUtils.isNotEmpty(scopes)) {
045      return entry;
046    }
047    return clearOrNull(entry);
048  }
049
050  private static boolean hasGlobalScope(NavigableMap<byte[], Integer> scopes, byte[] family) {
051    Integer scope = scopes.get(family);
052    return scope != null && scope.intValue() == HConstants.REPLICATION_SCOPE_GLOBAL;
053  }
054
055  @Override
056  public Cell filterCell(Entry entry, Cell cell) {
057    ExtendedCell extendedCell = PrivateCellUtil.ensureExtendedCell(cell);
058    NavigableMap<byte[], Integer> scopes = entry.getKey().getReplicationScopes();
059    if (MapUtils.isEmpty(scopes)) {
060      return null;
061    }
062    byte[] family = CellUtil.cloneFamily(cell);
063    if (CellUtil.matchingColumn(cell, WALEdit.METAFAMILY, WALEdit.BULK_LOAD)) {
064      return bulkLoadFilter.filterCell(extendedCell, new Predicate<byte[]>() {
065        @Override
066        public boolean apply(byte[] family) {
067          return !hasGlobalScope(scopes, family);
068        }
069      });
070    }
071    return hasGlobalScope(scopes, family) ? cell : null;
072  }
073}