-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[Dataflow Streaming] Reuse ByteStringOutputStream buffers in WindmillBag #36742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
babef89
[Dataflow Streaming] Reuse ByteStringOutputStream buffers in WindmillBag
arunpandianp 2888131
improve test
arunpandianp ed7aabe
fix typo
arunpandianp 6b2bf3b
Address comments
arunpandianp ca9c22b
fix test
arunpandianp 7f6b211
Address comments
arunpandianp 04bd1ef
Address comments
arunpandianp 6f5b892
Fix spotless
arunpandianp bdb5465
Fix spotless
arunpandianp a554769
Fix spotless
arunpandianp b8a998c
Address comments
arunpandianp 317c550
Address comments
arunpandianp b7cec11
fix format
arunpandianp 76a4a66
Address comments
arunpandianp 8760e13
Address comments
arunpandianp 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
103 changes: 103 additions & 0 deletions
103
.../java/org/apache/beam/runners/dataflow/worker/util/ThreadLocalByteStringOutputStream.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,103 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.beam.runners.dataflow.worker.util; | ||
|
|
||
| import java.lang.ref.SoftReference; | ||
| import javax.annotation.concurrent.ThreadSafe; | ||
| import org.apache.beam.sdk.annotations.Internal; | ||
| import org.apache.beam.sdk.util.ByteStringOutputStream; | ||
| import org.apache.beam.sdk.util.Preconditions; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
|
||
| @Internal | ||
| @ThreadSafe | ||
| /* | ||
| * A utility class for caching a thread-local {@link ByteStringOutputStream}. | ||
| * | ||
| * Example Usage: | ||
| * try (StreamHandle streamHandle = ThreadLocalByteStringOutputStream.acquire()) { | ||
| * ByteStringOutputStream stream = streamHandle.stream(); | ||
| * stream.write(1); | ||
| * ByteString byteString = stream.toByteStringAndReset(); | ||
| * } | ||
| */ | ||
| public class ThreadLocalByteStringOutputStream { | ||
|
|
||
| private static final ThreadLocal<@Nullable SoftRefHolder> threadLocalSoftRefHolder = | ||
| ThreadLocal.withInitial(SoftRefHolder::new); | ||
|
|
||
| // Private constructor to prevent instantiations from outside. | ||
| private ThreadLocalByteStringOutputStream() {} | ||
|
|
||
| /** @return An AutoClosable StreamHandle that holds a cached ByteStringOutputStream. */ | ||
| public static StreamHandle acquire() { | ||
| StreamHandle streamHandle = getStreamHandleFromThreadLocal(); | ||
| if (streamHandle.inUse) { | ||
| // Stream is already in use, create a new uncached one | ||
| return new StreamHandle(); | ||
| } | ||
| streamHandle.inUse = true; | ||
| return streamHandle; // inUse will be unset when streamHandle closes. | ||
| } | ||
|
|
||
| /** | ||
| * Handle to a thread-local {@link ByteStringOutputStream}. If the thread local stream is already | ||
| * in use, a new one is used. The streams are cached and reused across calls. Users should not | ||
| * keep a reference to the stream after closing the StreamHandle. | ||
| */ | ||
| public static class StreamHandle implements AutoCloseable { | ||
|
|
||
| private final ByteStringOutputStream stream = new ByteStringOutputStream(); | ||
|
|
||
| private boolean inUse = false; | ||
|
|
||
| /** | ||
| * Returns the underlying cached ByteStringOutputStream. Callers should not keep a reference to | ||
| * the stream after closing the StreamHandle. | ||
| */ | ||
| public ByteStringOutputStream stream() { | ||
| return stream; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| stream.reset(); | ||
| inUse = false; | ||
| } | ||
| } | ||
|
|
||
| private static class SoftRefHolder { | ||
| private @Nullable SoftReference<StreamHandle> softReference; | ||
| } | ||
|
|
||
| private static StreamHandle getStreamHandleFromThreadLocal() { | ||
| // softRefHolder is only set by Threadlocal initializer and should not be null | ||
| SoftRefHolder softRefHolder = | ||
| Preconditions.checkArgumentNotNull(threadLocalSoftRefHolder.get()); | ||
| @Nullable StreamHandle streamHandle = null; | ||
| @Nullable SoftReference<StreamHandle> softReference = softRefHolder.softReference; | ||
| if (softReference != null) { | ||
| streamHandle = softReference.get(); | ||
| } | ||
| if (streamHandle == null) { | ||
| streamHandle = new StreamHandle(); | ||
| softRefHolder.softReference = new SoftReference<>(streamHandle); | ||
| } | ||
| return streamHandle; | ||
| } | ||
| } | ||
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
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
68 changes: 68 additions & 0 deletions
68
...a/org/apache/beam/runners/dataflow/worker/util/ThreadLocalByteStringOutputStreamTest.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,68 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.beam.runners.dataflow.worker.util; | ||
|
|
||
| import static org.junit.Assert.*; | ||
|
|
||
| import org.apache.beam.runners.dataflow.worker.util.ThreadLocalByteStringOutputStream.StreamHandle; | ||
| import org.apache.beam.sdk.util.ByteStringOutputStream; | ||
| import org.apache.beam.vendor.grpc.v1p69p0.com.google.protobuf.ByteString; | ||
| import org.junit.Test; | ||
|
|
||
| public class ThreadLocalByteStringOutputStreamTest { | ||
|
|
||
| @Test | ||
| public void simple() { | ||
| try (StreamHandle streamHandle = ThreadLocalByteStringOutputStream.acquire()) { | ||
| ByteStringOutputStream stream = streamHandle.stream(); | ||
| stream.write(1); | ||
| stream.write(2); | ||
| stream.write(3); | ||
| assertEquals(ByteString.copyFrom(new byte[] {1, 2, 3}), stream.toByteStringAndReset()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void nested() { | ||
| try (StreamHandle streamHandle = ThreadLocalByteStringOutputStream.acquire()) { | ||
| ByteStringOutputStream stream = streamHandle.stream(); | ||
| stream.write(1); | ||
| try (StreamHandle streamHandle1 = ThreadLocalByteStringOutputStream.acquire()) { | ||
| ByteStringOutputStream stream1 = streamHandle1.stream(); | ||
| stream1.write(2); | ||
| assertEquals(ByteString.copyFrom(new byte[] {2}), stream1.toByteStringAndReset()); | ||
| } | ||
| stream.write(3); | ||
| assertEquals(ByteString.copyFrom(new byte[] {1, 3}), stream.toByteStringAndReset()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void resetDirtyStream() { | ||
| try (StreamHandle streamHandle = ThreadLocalByteStringOutputStream.acquire()) { | ||
| ByteStringOutputStream stream = streamHandle.stream(); | ||
| stream.write(1); | ||
| // Don't read/reset stream | ||
| } | ||
|
|
||
| try (StreamHandle streamHandle = ThreadLocalByteStringOutputStream.acquire()) { | ||
| ByteStringOutputStream stream = streamHandle.stream(); | ||
| assertEquals(ByteString.EMPTY, stream.toByteStringAndReset()); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.