Skip to content

Commit 84ed6ad

Browse files
authored
HADOOP-17158. Test timeout for ITestAbfsInputStreamStatistics#testReadAheadCounters (#2272)
Contributed by: Mehakmeet Singh.
1 parent c4fb404 commit 84ed6ad

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsInputStream.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public class AbfsInputStream extends FSInputStream implements CanUnbuffer,
7070

7171
/** Stream statistics. */
7272
private final AbfsInputStreamStatistics streamStatistics;
73+
private long bytesFromReadAhead; // bytes read from readAhead; for testing
74+
private long bytesFromRemoteRead; // bytes read remotely; for testing
7375

7476
public AbfsInputStream(
7577
final AbfsClient client,
@@ -235,6 +237,7 @@ private int readInternal(final long position, final byte[] b, final int offset,
235237

236238
// try reading from buffers first
237239
receivedBytes = ReadBufferManager.getBufferManager().getBlock(this, position, length, b);
240+
bytesFromReadAhead += receivedBytes;
238241
if (receivedBytes > 0) {
239242
incrementReadOps();
240243
LOG.debug("Received data from read ahead, not doing remote read");
@@ -302,6 +305,7 @@ int readRemote(long position, byte[] b, int offset, int length) throws IOExcepti
302305
throw new IOException("Unexpected Content-Length");
303306
}
304307
LOG.debug("HTTP request read bytes = {}", bytesRead);
308+
bytesFromRemoteRead += bytesRead;
305309
return (int) bytesRead;
306310
}
307311

@@ -503,6 +507,26 @@ public AbfsInputStreamStatistics getStreamStatistics() {
503507
return streamStatistics;
504508
}
505509

510+
/**
511+
* Getter for bytes read from readAhead buffer that fills asynchronously.
512+
*
513+
* @return value of the counter in long.
514+
*/
515+
@VisibleForTesting
516+
public long getBytesFromReadAhead() {
517+
return bytesFromReadAhead;
518+
}
519+
520+
/**
521+
* Getter for bytes read remotely from the data store.
522+
*
523+
* @return value of the counter in long.
524+
*/
525+
@VisibleForTesting
526+
public long getBytesFromRemoteRead() {
527+
return bytesFromRemoteRead;
528+
}
529+
506530
/**
507531
* Get the statistics of the stream.
508532
* @return a string value.

hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/ITestAbfsInputStreamStatistics.java

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ public class ITestAbfsInputStreamStatistics
4141
private static final int ONE_MB = 1024 * 1024;
4242
private static final int ONE_KB = 1024;
4343
private static final int CUSTOM_BLOCK_BUFFER_SIZE = 4 * 1024;
44-
private static final int CUSTOM_READ_AHEAD_BUFFER_SIZE = 8 * CUSTOM_BLOCK_BUFFER_SIZE;
45-
private static final int THREAD_SLEEP_10_SECONDS = 10;
46-
private static final int TIMEOUT_30_SECONDS = 30000;
4744
private byte[] defBuffer = new byte[ONE_MB];
4845

4946
public ITestAbfsInputStreamStatistics() throws Exception {
@@ -295,8 +292,8 @@ public void testWithNullStreamStatistics() throws IOException {
295292
/**
296293
* Testing readAhead counters in AbfsInputStream with 30 seconds timeout.
297294
*/
298-
@Test(timeout = TIMEOUT_30_SECONDS)
299-
public void testReadAheadCounters() throws IOException, InterruptedException {
295+
@Test
296+
public void testReadAheadCounters() throws IOException {
300297
describe("Test to check correct values for readAhead counters in "
301298
+ "AbfsInputStream");
302299

@@ -334,46 +331,35 @@ public void testReadAheadCounters() throws IOException, InterruptedException {
334331
AbfsInputStreamStatisticsImpl stats =
335332
(AbfsInputStreamStatisticsImpl) in.getStreamStatistics();
336333

337-
/*
338-
* Since, readAhead is done in background threads. Sometimes, the
339-
* threads aren't finished in the background and could result in
340-
* inaccurate results. So, we wait till we have the accurate values
341-
* with a limit of 30 seconds as that's when the test times out.
342-
*
343-
*/
344-
while (stats.getRemoteBytesRead() < CUSTOM_READ_AHEAD_BUFFER_SIZE
345-
|| stats.getReadAheadBytesRead() < CUSTOM_BLOCK_BUFFER_SIZE) {
346-
Thread.sleep(THREAD_SLEEP_10_SECONDS);
347-
}
348-
349334
/*
350335
* Verifying the counter values of readAheadBytesRead and remoteBytesRead.
351336
*
352337
* readAheadBytesRead : Since, we read 1KBs 5 times, that means we go
353338
* from 0 to 5KB in the file. The bufferSize is set to 4KB, and since
354339
* we have 8 blocks of readAhead buffer. We would have 8 blocks of 4KB
355340
* buffer. Our read is till 5KB, hence readAhead would ideally read 2
356-
* blocks of 4KB which is equal to 8KB. But, sometimes to get more than
357-
* one block from readAhead buffer we might have to wait for background
341+
* blocks of 4KB which is equal to 8KB. But, sometimes to get blocks
342+
* from readAhead buffer we might have to wait for background
358343
* threads to fill the buffer and hence we might do remote read which
359-
* would be faster. Therefore, readAheadBytesRead would be equal to or
360-
* greater than 4KB.
344+
* would be faster. Therefore, readAheadBytesRead would be greater than
345+
* or equal to the value of bytesFromReadAhead at the point we measure it.
361346
*
362347
* remoteBytesRead : Since, the bufferSize is set to 4KB and the number
363348
* of blocks or readAheadQueueDepth is equal to 8. We would read 8 * 4
364349
* KB buffer on the first read, which is equal to 32KB. But, if we are not
365350
* able to read some bytes that were in the buffer after doing
366351
* readAhead, we might use remote read again. Thus, the bytes read
367-
* remotely could also be greater than 32Kb.
352+
* remotely would be greater than or equal to the bytesFromRemoteRead
353+
* value that we measure at some point of the operation.
368354
*
369355
*/
370356
Assertions.assertThat(stats.getReadAheadBytesRead()).describedAs(
371357
"Mismatch in readAheadBytesRead counter value")
372-
.isGreaterThanOrEqualTo(CUSTOM_BLOCK_BUFFER_SIZE);
358+
.isGreaterThanOrEqualTo(in.getBytesFromReadAhead());
373359

374360
Assertions.assertThat(stats.getRemoteBytesRead()).describedAs(
375361
"Mismatch in remoteBytesRead counter value")
376-
.isGreaterThanOrEqualTo(CUSTOM_READ_AHEAD_BUFFER_SIZE);
362+
.isGreaterThanOrEqualTo(in.getBytesFromRemoteRead());
377363

378364
} finally {
379365
IOUtils.cleanupWithLogger(LOG, out, in);

0 commit comments

Comments
 (0)