From 9cca7b6856e7e624c535ef5f9376e85728a43619 Mon Sep 17 00:00:00 2001 From: ChiaPing Tsai Date: Wed, 8 Feb 2017 20:26:42 +0800 Subject: [PATCH] HBASE-17613 avoid copy of family when initializing the FSWALEntry --- .../hadoop/hbase/regionserver/wal/FSWALEntry.java | 34 +++++++----- .../hbase/regionserver/wal/TestFSWALEntry.java | 60 ++++++++++++++++++++++ 2 files changed, 81 insertions(+), 13 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 7ac276d..32ac03b 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,16 @@ package org.apache.hadoop.hbase.regionserver.wal; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Sets; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Set; 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,21 +66,27 @@ 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 = collectFamilies(edit.getCells()); + } else { + this.familyNames = Collections. emptySet(); + } + } + + @VisibleForTesting + static Set collectFamilies(List cells) { + if (CollectionUtils.isEmpty(cells)) { + return Collections. emptySet(); + } else { + Set familySet = Sets.newTreeSet(CellComparator::compareFamilies); + Set familyNames = Sets.newTreeSet(Bytes.BYTES_COMPARATOR); + for (Cell cell : cells) { + if (!CellUtil.matchingFamily(cell, WALEdit.METAFAMILY)) { + if (familySet.add(cell)) { + familyNames.add(CellUtil.cloneFamily(cell)); } } - this.familyNames = Collections.unmodifiableSet(familySet); } - } else { - this.familyNames = Collections. emptySet(); + return familyNames; } } 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 0000000..3e5c595 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestFSWALEntry.java @@ -0,0 +1,60 @@ +/** + * + * 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.classification.InterfaceAudience; +import org.apache.hadoop.hbase.util.Bytes; +import static org.junit.Assert.assertEquals; +import org.junit.Test; + + +@InterfaceAudience.Private +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()); + } +} + -- 2.10.1 (Apple Git-78)