Skip to content

Commit bad83be

Browse files
committed
feat(storage): log additional bytes received from GCS in read path
Fixes bug 475824752. Tracks the number of bytes received from the transport layer and emits a warning log if GCS sends more bytes than requested by the user during explicit range requests. [Generated-by: AI]
1 parent 058efbb commit bad83be

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/ApiaryUnbufferedReadableByteChannel.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class ApiaryUnbufferedReadableByteChannel implements UnbufferedReadableByteChann
7373
private ScatteringByteChannel sbc;
7474
private boolean open;
7575
private boolean returnEOF;
76+
private long totalBytesReadFromNetwork;
7677

7778
// returned X-Goog-Generation header value
7879
private Long xGoogGeneration;
@@ -128,6 +129,7 @@ public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
128129
returnEOF = true;
129130
} else {
130131
totalRead += read;
132+
totalBytesReadFromNetwork += read;
131133
}
132134
return totalRead;
133135
} catch (Exception t) {
@@ -163,6 +165,18 @@ public boolean isOpen() {
163165

164166
@Override
165167
public void close() throws IOException {
168+
long requestedLength = apiaryReadRequest.getByteRangeSpec().length();
169+
if (requestedLength >= 0
170+
&& requestedLength < ByteRangeSpec.EFFECTIVE_INFINITY
171+
&& totalBytesReadFromNetwork > requestedLength) {
172+
java.util.logging.Logger.getLogger(ApiaryUnbufferedReadableByteChannel.class.getName())
173+
.warning(
174+
String.format(
175+
"storage: received %d more bytes than requested from GCS for bucket '%s', object '%s'",
176+
totalBytesReadFromNetwork - requestedLength,
177+
apiaryReadRequest.getObject().getBucket(),
178+
apiaryReadRequest.getObject().getName()));
179+
}
166180
open = false;
167181
if (sbc != null) {
168182
sbc.close();

java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GapicUnbufferedReadableByteChannel.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ public boolean isOpen() {
199199

200200
@Override
201201
public void close() throws IOException {
202+
long readLimit = req.getReadLimit();
203+
long receivedBytes = fetchOffset.get() - req.getReadOffset();
204+
if (readLimit > 0 && receivedBytes > readLimit) {
205+
java.util.logging.Logger.getLogger(GapicUnbufferedReadableByteChannel.class.getName())
206+
.warning(
207+
String.format(
208+
"storage: received %d more bytes than requested from GCS for bucket '%s', object '%s'",
209+
receivedBytes - readLimit,
210+
req.getBucket(),
211+
req.getObject()));
212+
}
202213
open = false;
203214
try {
204215
if (leftovers != null) {

0 commit comments

Comments
 (0)