Skip to content

Commit 9466e47

Browse files
authored
HBASE-28586 Backport HBASE-24791 Improve HFileOutputFormat2 to avoid always call getTableRelativePath method (#5887)
Signed-off-by: Duo Zhang <zhangduo@apache.org>
1 parent 23246e9 commit 9466e47

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ static <V extends Cell> RecordWriter<ImmutableBytesWritable, V> createRecordWrit
242242
private final Map<byte[], WriterLength> writers = new TreeMap<>(Bytes.BYTES_COMPARATOR);
243243
private final Map<byte[], byte[]> previousRows = new TreeMap<>(Bytes.BYTES_COMPARATOR);
244244
private final long now = EnvironmentEdgeManager.currentTime();
245+
private byte[] tableNameBytes = writeMultipleTables ? null : Bytes.toBytes(writeTableNames);
245246

246247
@Override
247248
public void write(ImmutableBytesWritable row, V cell) throws IOException {
@@ -255,25 +256,22 @@ public void write(ImmutableBytesWritable row, V cell) throws IOException {
255256
byte[] rowKey = CellUtil.cloneRow(kv);
256257
int length = (PrivateCellUtil.estimatedSerializedSizeOf(kv)) - Bytes.SIZEOF_INT;
257258
byte[] family = CellUtil.cloneFamily(kv);
258-
byte[] tableNameBytes = null;
259259
if (writeMultipleTables) {
260260
tableNameBytes = MultiTableHFileOutputFormat.getTableName(row.get());
261261
tableNameBytes = TableName.valueOf(tableNameBytes).toBytes();
262262
if (!allTableNames.contains(Bytes.toString(tableNameBytes))) {
263263
throw new IllegalArgumentException(
264264
"TableName " + Bytes.toString(tableNameBytes) + " not expected");
265265
}
266-
} else {
267-
tableNameBytes = Bytes.toBytes(writeTableNames);
268266
}
269-
Path tableRelPath = getTableRelativePath(tableNameBytes);
270267
byte[] tableAndFamily = getTableNameSuffixedWithFamily(tableNameBytes, family);
271268
WriterLength wl = this.writers.get(tableAndFamily);
272269

273270
// If this is a new column family, verify that the directory exists
274271
if (wl == null) {
275272
Path writerPath = null;
276273
if (writeMultipleTables) {
274+
Path tableRelPath = getTableRelativePath(tableNameBytes);
277275
writerPath = new Path(outputDir, new Path(tableRelPath, Bytes.toString(family)));
278276
} else {
279277
writerPath = new Path(outputDir, Bytes.toString(family));
@@ -292,6 +290,7 @@ public void write(ImmutableBytesWritable row, V cell) throws IOException {
292290

293291
// create a new WAL writer, if necessary
294292
if (wl == null || wl.writer == null) {
293+
InetSocketAddress[] favoredNodes = null;
295294
if (conf.getBoolean(LOCALITY_SENSITIVE_CONF_KEY, DEFAULT_LOCALITY_SENSITIVE)) {
296295
HRegionLocation loc = null;
297296

@@ -308,26 +307,22 @@ public void write(ImmutableBytesWritable row, V cell) throws IOException {
308307
loc = null;
309308
}
310309
}
311-
312310
if (null == loc) {
313311
LOG.trace("Failed get of location, use default writer {}", Bytes.toString(rowKey));
314-
wl = getNewWriter(tableNameBytes, family, conf, null);
315312
} else {
316313
LOG.debug("First rowkey: [{}]", Bytes.toString(rowKey));
317314
InetSocketAddress initialIsa =
318315
new InetSocketAddress(loc.getHostname(), loc.getPort());
319316
if (initialIsa.isUnresolved()) {
320317
LOG.trace("Failed resolve address {}, use default writer", loc.getHostnamePort());
321-
wl = getNewWriter(tableNameBytes, family, conf, null);
322318
} else {
323319
LOG.debug("Use favored nodes writer: {}", initialIsa.getHostString());
324-
wl = getNewWriter(tableNameBytes, family, conf,
325-
new InetSocketAddress[] { initialIsa });
320+
favoredNodes = new InetSocketAddress[] { initialIsa };
326321
}
327322
}
328-
} else {
329-
wl = getNewWriter(tableNameBytes, family, conf, null);
330323
}
324+
wl = getNewWriter(tableNameBytes, family, conf, favoredNodes);
325+
331326
}
332327

333328
// we now have the proper WAL writer. full steam ahead
@@ -342,9 +337,9 @@ public void write(ImmutableBytesWritable row, V cell) throws IOException {
342337
private Path getTableRelativePath(byte[] tableNameBytes) {
343338
String tableName = Bytes.toString(tableNameBytes);
344339
String[] tableNameParts = tableName.split(":");
345-
Path tableRelPath = new Path(tableName.split(":")[0]);
340+
Path tableRelPath = new Path(tableNameParts[0]);
346341
if (tableNameParts.length > 1) {
347-
tableRelPath = new Path(tableRelPath, tableName.split(":")[1]);
342+
tableRelPath = new Path(tableRelPath, tableNameParts[1]);
348343
}
349344
return tableRelPath;
350345
}

0 commit comments

Comments
 (0)