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.ArrayList;
22  import java.util.List;
23  import java.util.Map;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.regionserver.wal.HLog.Entry;
28  import org.apache.hadoop.hbase.util.Bytes;
29  
30  public class TableCfWALEntryFilter implements WALEntryFilter {
31  
32    private static final Log LOG = LogFactory.getLog(TableCfWALEntryFilter.class);
33    private final ReplicationPeer peer;
34  
35    public TableCfWALEntryFilter(ReplicationPeer peer) {
36      this.peer = peer;
37    }
38  
39    @Override
40    public Entry filter(Entry entry) {
41      String tabName = entry.getKey().getTablename().getNameAsString();
42      ArrayList<KeyValue> kvs = entry.getEdit().getKeyValues();
43      Map<String, List<String>> tableCFs = null;
44  
45      try {
46        tableCFs = this.peer.getTableCFs();
47      } catch (IllegalArgumentException e) {
48        LOG.error("should not happen: can't get tableCFs for peer " + peer.getId() +
49            ", degenerate as if it's not configured by keeping tableCFs==null");
50      }
51      int size = kvs.size();
52  
53      // return null(prevent replicating) if logKey's table isn't in this peer's
54      // replicable table list (empty tableCFs means all table are replicable)
55      if (tableCFs != null && !tableCFs.containsKey(tabName)) {
56        return null;
57      } else {
58        List<String> cfs = (tableCFs == null) ? null : tableCFs.get(tabName);
59        for (int i = size - 1; i >= 0; i--) {
60          KeyValue kv = kvs.get(i);
61          // ignore(remove) kv if its cf isn't in the replicable cf list
62          // (empty cfs means all cfs of this table are replicable)
63          if ((cfs != null && !cfs.contains(Bytes.toString(kv.getFamily())))) {
64            kvs.remove(i);
65          }
66        }
67      }
68      if (kvs.size() < size/2) {
69        kvs.trimToSize();
70      }
71      return entry;
72    }
73  
74  }