Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ abstract class BaseObjectReadSessionStreamRead<Projection>
protected final RangeSpec rangeSpec;
protected final RetryContext retryContext;
protected final AtomicLong readOffset;
protected boolean closed;
protected volatile boolean closed;
Comment thread
Dhriti07 marked this conversation as resolved.
Resolved
protected boolean tombstoned;
protected IOAutoCloseable onCloseCallback;

Expand Down Expand Up @@ -182,14 +182,17 @@ long readId() {

@Override
public boolean acceptingBytes() {
return !complete.isDone() && !tombstoned;
return !closed && !complete.isDone() && !tombstoned;
}

@Override
public void accept(ChildRef childRef) throws IOException {
retryContext.reset();
int size = childRef.byteString().size();
childRefs.add(childRef);
if (closed) {
drainChildRefs();
}
readOffset.addAndGet(size);
}

Expand All @@ -216,8 +219,20 @@ public void internalClose() throws IOException {
if (!closed) {
retryContext.reset();
closed = true;
GrpcUtils.closeAll(childRefs);
drainChildRefs();
}
}

private void drainChildRefs() throws IOException {
List<ChildRef> drained;
synchronized (childRefs) {
if (childRefs.isEmpty()) {
return;
}
drained = new ArrayList<>(childRefs);
childRefs.clear();
}
GrpcUtils.closeAll(drained);
}

@Override
Expand Down Expand Up @@ -328,8 +343,10 @@ public ApiFuture<?> fail(Throwable t) {
try {
offer(new SmuggledFailure(t));
failFuture.set(null);
} catch (InterruptedIOException e) {
Thread.currentThread().interrupt();
} catch (IOException e) {
if (e instanceof InterruptedIOException) {
Thread.currentThread().interrupt();
}
failFuture.setException(e);
}
return failFuture;
Expand Down Expand Up @@ -433,9 +450,13 @@ public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
return read;
}

private void offer(Closeable offer) throws InterruptedIOException {
private void offer(Closeable offer) throws IOException {
try {
queue.put(offer);
if (closed) {
// the reader closed while we waited, nobody will take this anymore
drainQueue();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
InterruptedIOException ioe = new InterruptedIOException();
Expand All @@ -449,7 +470,14 @@ private void internalCleanup() throws IOException {
if (leftovers != null) {
leftovers.ref.close();
}
GrpcUtils.closeAll(queue);
drainQueue();
}

/** Remove and close everything in the queue, freeing space for a producer waiting to add. */
private void drainQueue() throws IOException {
List<Closeable> drained = new ArrayList<>();
queue.drainTo(drained);
GrpcUtils.closeAll(drained);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.locks.ReentrantLock;
Expand All @@ -52,21 +55,29 @@ final class ObjectReadSessionImpl implements ObjectReadSession {

private final ConcurrentIdentityMap<ObjectReadSessionStream, ObjectReadSessionState> children;

// Fast open reads: reads sent in the request that opened the stream, keyed by the config each was
// created from. The first readAs with an equal config is given the read instead of sending a new
// one.
private final ConcurrentMap<ReadProjectionConfig<?>, ObjectReadSessionStreamRead<?>>
fastOpenReads;
Comment thread
Dhriti07 marked this conversation as resolved.
Resolved

private volatile boolean open;

ObjectReadSessionImpl(
ScheduledExecutorService executor,
ZeroCopyBidiStreamingCallable<BidiReadObjectRequest, BidiReadObjectResponse> callable,
ObjectReadSessionStream stream,
ObjectReadSessionState state,
RetryContextProvider retryContextProvider) {
RetryContextProvider retryContextProvider,
Map<ReadProjectionConfig<?>, ObjectReadSessionStreamRead<?>> fastOpenReads) {
this.executor = executor;
this.callable = callable;
this.stream = stream;
this.state = state;
this.resource = state.getMetadata();
this.retryContextProvider = retryContextProvider;
this.children = new ConcurrentIdentityMap<>();
this.fastOpenReads = new ConcurrentHashMap<>(fastOpenReads);
this.open = true;
}
Comment thread
Dhriti07 marked this conversation as resolved.
Resolved

Expand All @@ -80,6 +91,14 @@ public <Projection> Projection readAs(ReadProjectionConfig<Projection> config) {
checkState(open, "Session already closed");
switch (config.getType()) {
case STREAM_READ:
ObjectReadSessionStreamRead<?> fastOpenRead = fastOpenReads.remove(config);
if (fastOpenRead != null) {
// configs are final classes with a class-exact equals, so an equal config has the same
// Projection type
@SuppressWarnings("unchecked")
Projection projection = (Projection) fastOpenRead.project();
return projection;
}
Comment thread
Dhriti07 marked this conversation as resolved.
Resolved
long readId = state.newReadId();
ObjectReadSessionStreamRead<Projection> read =
config.cast().newRead(readId, retryContextProvider.create());
Expand All @@ -104,6 +123,19 @@ public void close() throws IOException {
return;
}
open = false;
// close fast open reads nobody asked for before the stream: an unread channel read holds the
// stream's thread, and a completed read is out of the state, so stream.close() can't reach it
List<ObjectReadSessionStreamRead<?>> unclaimed = new ArrayList<>(fastOpenReads.size());
fastOpenReads
.keySet()
.forEach(
k -> {
ObjectReadSessionStreamRead<?> read = fastOpenReads.remove(k);
if (read != null) {
unclaimed.add(read);
}
});
GrpcUtils.closeAll(unclaimed);
List<ApiFuture<Void>> closing =
children.drainEntries((subStream, subStreamState) -> subStream.closeAsync());
stream.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
import com.google.cloud.storage.ReadProjectionConfig.ProjectionType;
import com.google.cloud.storage.RetryContext.RetryContextProvider;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.storage.v2.BidiReadObjectRequest;
import com.google.storage.v2.BidiReadObjectResponse;
import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -78,6 +81,7 @@ ApiFuture<ObjectReadSession> readSession(
checkArgument(
req.getReadRangesList().isEmpty(),
"ranges included in the initial request are not supported");
checkArgument(initialReadConfigs.size() <= 1, "at most one fast open read is supported");
ObjectReadSessionState state = new ObjectReadSessionState(ctx, req);

ZeroCopyBidiStreamingCallable<BidiReadObjectRequest, BidiReadObjectResponse> callable =
Expand All @@ -87,18 +91,21 @@ ApiFuture<ObjectReadSession> readSession(

// Add each fast open read to the session state. The open request below is built from the
// state, so these reads are sent as part of the open.
Map<ReadProjectionConfig<?>, ObjectReadSessionStreamRead<?>> fastOpenReads = new HashMap<>();
for (ReadProjectionConfig<?> config : initialReadConfigs) {
long readId = state.newReadId();
ObjectReadSessionStreamRead<?> read =
config.cast().newRead(readId, retryContextProvider.create());
state.putOutstandingRead(readId, read);
fastOpenReads.put(config, read);
}

ApiFuture<ObjectReadSession> objectReadSessionFuture =
ApiFutures.transform(
stream,
nowOpen ->
new ObjectReadSessionImpl(executor, callable, stream, state, retryContextProvider),
new ObjectReadSessionImpl(
executor, callable, stream, state, retryContextProvider, fastOpenReads),
executor);
stream.send(state.getOpenArguments().getReq());
return objectReadSessionFuture;
Expand Down Expand Up @@ -133,7 +140,7 @@ <Projection> ApiFuture<FastOpenObjectReadSession<Projection>> fastOpenReadSessio
nowOpen ->
new FastOpenObjectReadSession<>(
new ObjectReadSessionImpl(
executor, callable, stream, state, retryContextProvider),
executor, callable, stream, state, retryContextProvider, ImmutableMap.of()),
read,
stream),
executor);
Expand Down
Loading
Loading