Skip to content

Commit 591cae0

Browse files
Dhriti07Dhriti Chopranidhiii-27
authored
feat(storage): adding disabling option for bidi reads (#13506)
Add option to disable Bidi Checksum using checksum disable system property --------- Co-authored-by: Dhriti Chopra <dhritichopra@google.com> Co-authored-by: Nidhi <nidhiii@google.com>
1 parent 576f66e commit 591cae0

12 files changed

Lines changed: 38 additions & 14 deletions

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private AccumulatingRead(
151151
super(rangeSpec, retryContext, onCloseCallback);
152152
this.readId = readId;
153153
this.hasher =
154-
(rangeSpec.begin() == 0 && !(hasher instanceof Hasher.NoOpHasher))
154+
(rangeSpec.begin() == 0)
155155
? new CumulativeHasher(hasher, 0, rangeSpec.maxLength())
156156
: hasher;
157157
this.complete = SettableApiFuture.create();
@@ -284,7 +284,7 @@ static class StreamingRead extends BaseObjectReadSessionStreamRead<ScatteringByt
284284
super(rangeSpec, retryContext, onCloseCallback);
285285
this.readId = new AtomicLong(readId);
286286
this.hasher =
287-
(rangeSpec.begin() == 0 && !(hasher instanceof Hasher.NoOpHasher))
287+
(rangeSpec.begin() == 0)
288288
? new CumulativeHasher(hasher, 0, rangeSpec.maxLength())
289289
: hasher;
290290
this.closed = false;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final class CumulativeHasher implements Hasher {
4040
this.delegate = delegate;
4141
this.startOffset = startOffset;
4242
this.limit = limit;
43-
this.cumulativeHash = Crc32cValue.zero();
43+
this.cumulativeHash = delegate.initialValue();
4444
}
4545

4646
@Override
@@ -123,16 +123,19 @@ boolean qualifiesForVerification(Object metadata) {
123123
void validateCumulativeChecksum(Object metadata)
124124
throws UncheckedCumulativeChecksumMismatchException {
125125
if (qualifiesForVerification(metadata)) {
126-
Crc32cValue<?> expected = Crc32cValue.of(metadata.getChecksums().getCrc32C());
127126
Crc32cLengthKnown actual = getCumulativeHash();
127+
if (actual == null) {
128+
return;
129+
}
130+
Crc32cValue<?> expected = Crc32cValue.of(metadata.getChecksums().getCrc32C());
128131
if (!actual.eqValue(expected)) {
129132
throw new UncheckedCumulativeChecksumMismatchException(expected, actual);
130133
}
131134
}
132135
}
133136

134137
private void accumulate(Crc32cLengthKnown actual) {
135-
cumulativeHash = cumulativeHash.concat(actual);
138+
cumulativeHash = nullSafeConcat(cumulativeHash, actual);
136139
}
137140

138141
Crc32cLengthKnown getCumulativeHash() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private ReadableByteChannelSessionBuilder(
6767
this.read = read;
6868
this.retrier = retrier;
6969
this.resultRetryAlgorithm = resultRetryAlgorithm;
70-
this.hasher = Hasher.defaultHasher();
70+
this.hasher = Hasher.readHasher();
7171
this.autoGzipDecompression = false;
7272
}
7373

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ final class GapicUnbufferedReadableByteChannel
9393
this.read = read;
9494
this.req = req;
9595
this.hasher =
96-
(req.getReadOffset() == 0 && !(hasher instanceof Hasher.NoOpHasher))
96+
(req.getReadOffset() == 0)
9797
? new CumulativeHasher(
9898
hasher,
9999
0,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected LazyReadChannel<?, Object> newLazyReadChannel() {
6262
ResumableMedia.gapic()
6363
.read()
6464
.byteChannel(read, retrier, resultRetryAlgorithm)
65-
.setHasher(Hasher.defaultHasher())
65+
.setHasher(Hasher.readHasher())
6666
.setAutoGzipDecompression(autoGzipDecompression);
6767
BufferHandle bufferHandle = getBufferHandle();
6868
// because we're erasing the specific type of channel, we need to declare it here.

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ final class DefaultInstanceHolder {
5757
}
5858
}
5959

60+
final class ReadInstanceHolder {
61+
private static final Logger LOGGER = Logger.getLogger(Hasher.class.getName());
62+
private static final String PROPERTY_NAME = "com.google.cloud.storage.Hasher.read";
63+
private static final String PROPERTY_VALUE =
64+
System.getProperty(PROPERTY_NAME, DefaultInstanceHolder.PROPERTY_VALUE);
65+
static final Hasher READ_HASHER;
66+
67+
static {
68+
LOGGER.fine(String.format(Locale.US, "-D%s=%s", PROPERTY_NAME, PROPERTY_VALUE));
69+
if ("disabled".equalsIgnoreCase(PROPERTY_VALUE)) {
70+
READ_HASHER = noop();
71+
} else {
72+
READ_HASHER = enabled();
73+
}
74+
}
75+
}
76+
77+
static Hasher readHasher() {
78+
return ReadInstanceHolder.READ_HASHER;
79+
}
80+
6081
@Nullable
6182
default Crc32cLengthKnown hash(Supplier<ByteBuffer> b) {
6283
return hash(b.get());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static final class ReadableByteChannelSessionBuilder {
5858

5959
private ReadableByteChannelSessionBuilder(BlobReadChannelContext blobReadChannelContext) {
6060
this.blobReadChannelContext = blobReadChannelContext;
61-
this.hasher = Hasher.defaultHasher();
61+
this.hasher = Hasher.readHasher();
6262
this.autoGzipDecompression = false;
6363
}
6464

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public final class HttpStorageRpcHasherHelper {
4040
private final Hasher hasher;
4141

4242
private HttpStorageRpcHasherHelper() {
43-
hasher = Hasher.defaultHasher();
43+
hasher = Hasher.readHasher();
4444
}
4545

4646
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
@BetaApi
4747
@Immutable
4848
public final class ReadAsChannel extends BaseConfig<ScatteringByteChannel, StreamingRead> {
49-
static final ReadAsChannel INSTANCE = new ReadAsChannel(RangeSpec.all(), Hasher.enabled());
49+
static final ReadAsChannel INSTANCE = new ReadAsChannel(RangeSpec.all(), Hasher.readHasher());
5050

5151
private final RangeSpec range;
5252
private final Hasher hasher;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public final class ReadAsFutureByteString
4646
extends BaseConfig<ApiFuture<DisposableByteString>, AccumulatingRead<DisposableByteString>> {
4747

4848
static final ReadAsFutureByteString INSTANCE =
49-
new ReadAsFutureByteString(RangeSpec.all(), Hasher.enabled());
49+
new ReadAsFutureByteString(RangeSpec.all(), Hasher.readHasher());
5050

5151
private final RangeSpec range;
5252
private final Hasher hasher;

0 commit comments

Comments
 (0)