From b96ff27081c1496185071579b6c7b3abc030e5d0 Mon Sep 17 00:00:00 2001 From: YeChao Chen Date: Mon, 3 Aug 2020 16:02:48 +0800 Subject: [PATCH 1/2] HBASE-24791 Improve HFileOutputFormat2 to avoid always call getTableRelativePath method (#2167) Signed-off-by: Anoop Signed-off-by: Ted Yu Change-Id: I5386e1c68273acd20806981d291ae31016906612 --- .../hbase/mapreduce/HFileOutputFormat2.java | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java b/hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java index 43dd4a7160ef..c703e5582b59 100644 --- a/hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java +++ b/hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java @@ -266,6 +266,7 @@ static RecordWriter createRecordWrit private final Map writers = new TreeMap<>(Bytes.BYTES_COMPARATOR); private final Map previousRows = new TreeMap<>(Bytes.BYTES_COMPARATOR); private final long now = EnvironmentEdgeManager.currentTime(); + private byte[] tableNameBytes = writeMultipleTables ? null : Bytes.toBytes(writeTableNames); @Override public void write(ImmutableBytesWritable row, V cell) throws IOException { @@ -279,7 +280,6 @@ public void write(ImmutableBytesWritable row, V cell) throws IOException { byte[] rowKey = CellUtil.cloneRow(kv); int length = (PrivateCellUtil.estimatedSerializedSizeOf(kv)) - Bytes.SIZEOF_INT; byte[] family = CellUtil.cloneFamily(kv); - byte[] tableNameBytes = null; if (writeMultipleTables) { tableNameBytes = MultiTableHFileOutputFormat.getTableName(row.get()); tableNameBytes = writeToTableWithNamespace @@ -290,10 +290,7 @@ public void write(ImmutableBytesWritable row, V cell) throws IOException { throw new IllegalArgumentException( "TableName " + Bytes.toString(tableNameBytes) + " not expected"); } - } else { - tableNameBytes = Bytes.toBytes(writeTableNames); } - Path tableRelPath = getTableRelativePath(tableNameBytes); byte[] tableAndFamily = getTableNameSuffixedWithFamily(tableNameBytes, family); WriterLength wl = this.writers.get(tableAndFamily); @@ -302,6 +299,7 @@ public void write(ImmutableBytesWritable row, V cell) throws IOException { if (wl == null) { Path writerPath = null; if (writeMultipleTables) { + Path tableRelPath = getTableRelativePath(tableNameBytes); writerPath = new Path(outputDir, new Path(tableRelPath, Bytes.toString(family))); } else { writerPath = new Path(outputDir, Bytes.toString(family)); @@ -320,14 +318,14 @@ public void write(ImmutableBytesWritable row, V cell) throws IOException { // create a new WAL writer, if necessary if (wl == null || wl.writer == null) { + InetSocketAddress[] favoredNodes = null; if (conf.getBoolean(LOCALITY_SENSITIVE_CONF_KEY, DEFAULT_LOCALITY_SENSITIVE)) { HRegionLocation loc = null; String tableName = Bytes.toString(tableNameBytes); if (tableName != null) { - try ( - Connection connection = - ConnectionFactory.createConnection(createRemoteClusterConf(conf)); - RegionLocator locator = connection.getRegionLocator(TableName.valueOf(tableName))) { + try (Connection connection = ConnectionFactory.createConnection(createRemoteClusterConf(conf)); + RegionLocator locator = + connection.getRegionLocator(TableName.valueOf(tableName))) { loc = locator.getRegionLocation(rowKey); } catch (Throwable e) { LOG.warn("Something wrong locating rowkey {} in {}", Bytes.toString(rowKey), @@ -335,26 +333,22 @@ public void write(ImmutableBytesWritable row, V cell) throws IOException { loc = null; } } - if (null == loc) { LOG.trace("Failed get of location, use default writer {}", Bytes.toString(rowKey)); - wl = getNewWriter(tableNameBytes, family, conf, null); } else { LOG.debug("First rowkey: [{}]", Bytes.toString(rowKey)); InetSocketAddress initialIsa = new InetSocketAddress(loc.getHostname(), loc.getPort()); if (initialIsa.isUnresolved()) { LOG.trace("Failed resolve address {}, use default writer", loc.getHostnamePort()); - wl = getNewWriter(tableNameBytes, family, conf, null); } else { LOG.debug("Use favored nodes writer: {}", initialIsa.getHostString()); - wl = getNewWriter(tableNameBytes, family, conf, - new InetSocketAddress[] { initialIsa }); + favoredNodes = new InetSocketAddress[] { initialIsa }; } } - } else { - wl = getNewWriter(tableNameBytes, family, conf, null); } + wl = getNewWriter(tableNameBytes, family, conf, favoredNodes); + } // we now have the proper WAL writer. full steam ahead @@ -369,9 +363,9 @@ public void write(ImmutableBytesWritable row, V cell) throws IOException { private Path getTableRelativePath(byte[] tableNameBytes) { String tableName = Bytes.toString(tableNameBytes); String[] tableNameParts = tableName.split(":"); - Path tableRelPath = new Path(tableName.split(":")[0]); + Path tableRelPath = new Path(tableNameParts[0]); if (tableNameParts.length > 1) { - tableRelPath = new Path(tableRelPath, tableName.split(":")[1]); + tableRelPath = new Path(tableRelPath, tableNameParts[1]); } return tableRelPath; } From c0c0ee3e93c384d742d90cbdd685ac613f704d3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vill=C5=91=20Sz=C5=B1cs?= Date: Fri, 10 May 2024 13:43:36 +0200 Subject: [PATCH 2/2] spotless fix Change-Id: Ifcc7091a4605fdec07e71e070fc5925ab723328c --- .../apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java b/hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java index c703e5582b59..6ab3bdd25048 100644 --- a/hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java +++ b/hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java @@ -323,9 +323,10 @@ public void write(ImmutableBytesWritable row, V cell) throws IOException { HRegionLocation loc = null; String tableName = Bytes.toString(tableNameBytes); if (tableName != null) { - try (Connection connection = ConnectionFactory.createConnection(createRemoteClusterConf(conf)); - RegionLocator locator = - connection.getRegionLocator(TableName.valueOf(tableName))) { + try ( + Connection connection = + ConnectionFactory.createConnection(createRemoteClusterConf(conf)); + RegionLocator locator = connection.getRegionLocator(TableName.valueOf(tableName))) { loc = locator.getRegionLocation(rowKey); } catch (Throwable e) { LOG.warn("Something wrong locating rowkey {} in {}", Bytes.toString(rowKey),