This repository was archived by the owner on Sep 25, 2026. It is now read-only.
Repository navigation
chore: add BidiAppendableUnbufferedWritableByteChannel #3224
Closed
BenWhitehead
wants to merge
1
commit into
nonblocking-appendable/07/stream
from
nonblocking-appendable/08/unbuffered-channel
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
193 changes: 193 additions & 0 deletions
193
...e/src/main/java/com/google/cloud/storage/BidiAppendableUnbufferedWritableByteChannel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.storage; | ||
|
|
||
| import com.google.cloud.BaseServiceException; | ||
| import com.google.cloud.storage.ChunkSegmenter.ChunkSegment; | ||
| import com.google.cloud.storage.UnbufferedWritableByteChannelSession.UnbufferedWritableByteChannel; | ||
| import java.io.IOException; | ||
| import java.io.InterruptedIOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.channels.ClosedChannelException; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| final class BidiAppendableUnbufferedWritableByteChannel implements UnbufferedWritableByteChannel { | ||
| private static final Logger LOGGER = | ||
| LoggerFactory.getLogger(BidiAppendableUnbufferedWritableByteChannel.class); | ||
|
|
||
| private final BidiUploadStreamingStream stream; | ||
| private final ChunkSegmenter chunkSegmenter; | ||
|
|
||
| private boolean open; | ||
| private long writeOffset; | ||
| private volatile boolean nextWriteShouldFinalize; | ||
| private boolean writeCalledAtLeastOnce; | ||
|
|
||
| /** If write throws an error, don't attempt to finalize things when {@link #close()} is called. */ | ||
| private boolean writeThrewError; | ||
|
|
||
| BidiAppendableUnbufferedWritableByteChannel( | ||
| BidiUploadStreamingStream stream, ChunkSegmenter chunkSegmenter, long writeOffset) { | ||
| this.stream = stream; | ||
| this.chunkSegmenter = chunkSegmenter; | ||
| this.open = true; | ||
| this.writeOffset = writeOffset; | ||
| this.nextWriteShouldFinalize = false; | ||
| this.writeThrewError = false; | ||
| } | ||
|
|
||
| @Override | ||
| public long write(ByteBuffer[] srcs, int srcsOffset, int srcsLength) throws IOException { | ||
| return internalWrite(srcs, srcsOffset, srcsLength); | ||
| } | ||
|
|
||
| @Override | ||
| public long writeAndClose(ByteBuffer[] srcs, int offset, int length) throws IOException { | ||
| long totalRemaining = Buffers.totalRemaining(srcs, offset, length); | ||
| long written = 0; | ||
| do { | ||
| written += internalWrite(srcs, offset, length); | ||
| } while (written < totalRemaining); | ||
| close(); | ||
| return written; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isOpen() { | ||
| return open; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| if (!open) { | ||
| return; | ||
| } | ||
| try { | ||
| if (writeThrewError) { | ||
| return; | ||
| } | ||
|
|
||
| if (!writeCalledAtLeastOnce) { | ||
| stream.flush(); | ||
| } | ||
| if (nextWriteShouldFinalize) { | ||
| //noinspection StatementWithEmptyBody | ||
| while (!stream.finishWrite(writeOffset)) {} | ||
| } else { | ||
| //noinspection StatementWithEmptyBody | ||
| while (!stream.closeStream(writeOffset)) {} | ||
| } | ||
|
|
||
| awaitResultFuture(); | ||
| } finally { | ||
| stream.sendClose(); | ||
| open = false; | ||
| } | ||
| } | ||
|
|
||
| public void nextWriteShouldFinalize() { | ||
| this.nextWriteShouldFinalize = true; | ||
| } | ||
|
|
||
| private long internalWrite(ByteBuffer[] srcs, int srcsOffset, int srcsLength) throws IOException { | ||
| if (!open) { | ||
| throw new ClosedChannelException(); | ||
| } | ||
| // error early. if the result future is already failed, await it to throw the error | ||
| if (stream.getResultFuture().isDone()) { | ||
| awaitResultFuture(); | ||
| return 0; | ||
| } | ||
| writeCalledAtLeastOnce = true; | ||
|
|
||
| long availableCapacity = stream.availableCapacity(); | ||
| if (availableCapacity <= 0) { | ||
| return 0; | ||
| } | ||
| RewindableContent rewindableContent = RewindableContent.of(srcs, srcsOffset, srcsLength); | ||
| long totalBufferRemaining = rewindableContent.getLength(); | ||
|
|
||
| ChunkSegment[] data = chunkSegmenter.segmentBuffers(srcs, srcsOffset, srcsLength, true); | ||
| if (data.length == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| long bytesConsumed = 0; | ||
| for (int i = 0, len = data.length, lastIdx = len - 1; i < len; i++) { | ||
| ChunkSegment datum = data[i]; | ||
| int size = datum.getB().size(); | ||
| boolean appended; | ||
| if (i < lastIdx) { | ||
| appended = stream.append(datum); | ||
| } else if (i == lastIdx && nextWriteShouldFinalize) { | ||
| appended = stream.appendAndFinalize(datum); | ||
| } else { | ||
| appended = stream.appendAndFlush(datum); | ||
| } | ||
| if (appended) { | ||
| bytesConsumed += size; | ||
| writeOffset += size; | ||
| } else { | ||
| // if we weren't able to trigger a flush by reaching the end of the array and calling | ||
| // appendAndFlush, explicitly call flush here so that some progress can be made. | ||
| // we prefer appendAndFlush so a separate message is not needed, but an extra message | ||
| // in order to make progress and free buffer space is better than ending up in a live-lock. | ||
| stream.flush(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (bytesConsumed != totalBufferRemaining) { | ||
| rewindableContent.rewindTo(bytesConsumed); | ||
| } | ||
|
|
||
| return bytesConsumed; | ||
| } | ||
|
|
||
| private void awaitResultFuture() throws IOException { | ||
| try { | ||
| stream.getResultFuture().get(10_717, TimeUnit.MILLISECONDS); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| InterruptedIOException ioe = new InterruptedIOException(); | ||
| ioe.initCause(e); | ||
| writeThrewError = true; | ||
| throw ioe; | ||
| } catch (ExecutionException e) { | ||
| BaseServiceException coalesce = StorageException.coalesce(e.getCause()); | ||
| String message = coalesce.getMessage(); | ||
| String ioExceptionMessage = message; | ||
| // if the failure is an upload scenario we detect client side, it's message will be | ||
| // verbose. To avoid duplication, select the first line only for the io exception | ||
| int firstNewLineIndex = message != null ? message.indexOf('\n') : -1; | ||
| if (firstNewLineIndex > -1) { | ||
| ioExceptionMessage = message.substring(0, firstNewLineIndex); | ||
| } | ||
| IOException ioException = new IOException(ioExceptionMessage, coalesce); | ||
| // ioException.addSuppressed(new AsyncStorageTaskException()); | ||
| writeThrewError = true; | ||
| throw ioException; | ||
| } catch (TimeoutException e) { | ||
| writeThrewError = true; | ||
| throw new IOException(e); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't this just
long written = write(srcs, offset, length);
close();
return written;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
internalWriteandwritemethods are both non-blocking and may not consume all of the bytes present in srcs. However, close does need to block. So we loop here to only call close once all of the bytes have been successfully enqueued. Then close can do it's normal blocking waiting for the necessary ack from gcs.