From 489c8872c14fc8c4c9cd5d36b1953bf5afcf08ec Mon Sep 17 00:00:00 2001 From: ChiaPing Tsai Date: Thu, 9 Feb 2017 01:24:41 +0800 Subject: [PATCH] HBASE-17613 avoid copy of family when initializing the FSWALEntry Signed-off-by: tedyu --- .../hbase/regionserver/wal/FSWALEntry.java | 35 ++++++----- .../regionserver/wal/TestFSWALEntry.java | 62 +++++++++++++++++++ 2 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestFSWALEntry.java diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/FSWALEntry.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/FSWALEntry.java index 7ac276d3ad1..3b8525c9103 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/FSWALEntry.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/FSWALEntry.java @@ -18,14 +18,17 @@ package org.apache.hadoop.hbase.regionserver.wal; -import com.google.common.collect.Sets; +import com.google.common.annotations.VisibleForTesting; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Set; +import java.util.TreeSet; +import static java.util.stream.Collectors.toCollection; import org.apache.hadoop.hbase.Cell; +import org.apache.hadoop.hbase.CellComparator; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HRegionInfo; import org.apache.hadoop.hbase.classification.InterfaceAudience; @@ -64,24 +67,26 @@ class FSWALEntry extends Entry { this.txid = txid; if (inMemstore) { // construct familyNames here to reduce the work of log sinker. - ArrayList cells = this.getEdit().getCells(); - if (CollectionUtils.isEmpty(cells)) { - this.familyNames = Collections. emptySet(); - } else { - Set familySet = Sets.newTreeSet(Bytes.BYTES_COMPARATOR); - for (Cell cell : cells) { - if (!CellUtil.matchingFamily(cell, WALEdit.METAFAMILY)) { - // TODO: Avoid this clone? - familySet.add(CellUtil.cloneFamily(cell)); - } - } - this.familyNames = Collections.unmodifiableSet(familySet); - } + this.familyNames = collectFamilies(edit.getCells()); } else { this.familyNames = Collections. emptySet(); } } + @VisibleForTesting + static Set collectFamilies(List cells) { + if (CollectionUtils.isEmpty(cells)) { + return Collections. emptySet(); + } else { + return cells.stream() + .filter(v -> !CellUtil.matchingFamily(v, WALEdit.METAFAMILY)) + .collect(toCollection(() -> new TreeSet<>(CellComparator::compareFamilies))) + .stream() + .map(CellUtil::cloneFamily) + .collect(toCollection(() -> new TreeSet<>(Bytes.BYTES_COMPARATOR))); + } + } + public String toString() { return "sequence=" + this.txid + ", " + super.toString(); }; diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestFSWALEntry.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestFSWALEntry.java new file mode 100644 index 00000000000..cf059dd9b4c --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestFSWALEntry.java @@ -0,0 +1,62 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.regionserver.wal; + +import java.util.ArrayList; +import java.util.List; +import org.apache.hadoop.hbase.Cell; +import org.apache.hadoop.hbase.CellUtil; +import org.apache.hadoop.hbase.testclassification.RegionServerTests; +import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.apache.hadoop.hbase.util.Bytes; +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import org.junit.experimental.categories.Category; + + +@Category({ RegionServerTests.class, SmallTests.class }) +public class TestFSWALEntry { + @Test + public void testCollectFamilies() { + byte[] family0 = Bytes.toBytes("family0"); + byte[] family1 = Bytes.toBytes("family1"); + byte[] family2 = Bytes.toBytes("family2"); + + List cells = new ArrayList<>(); + assertEquals(0, FSWALEntry.collectFamilies(cells).size()); + + cells.add(CellUtil.createCell(family0, family0, family0)); + assertEquals(1, FSWALEntry.collectFamilies(cells).size()); + + cells.add(CellUtil.createCell(family1, family1, family1)); + assertEquals(2, FSWALEntry.collectFamilies(cells).size()); + + cells.add(CellUtil.createCell(family0, family0, family0)); + cells.add(CellUtil.createCell(family1, family1, family1)); + assertEquals(2, FSWALEntry.collectFamilies(cells).size()); + + cells.add(CellUtil.createCell(family2, family2, family2)); + assertEquals(3, FSWALEntry.collectFamilies(cells).size()); + + cells.add(CellUtil.createCell(WALEdit.METAFAMILY, WALEdit.METAFAMILY, WALEdit.METAFAMILY)); + assertEquals(3, FSWALEntry.collectFamilies(cells).size()); + } +} +