Skip to content

[FnApi Java] Close named data stream multiplexers once no bundle is u…#39255

Open
krishnamanchikalapudi wants to merge 2 commits into
apache:masterfrom
krishnamanchikalapudi:fix-39001-named-data-stream-lifecycle
Open

[FnApi Java] Close named data stream multiplexers once no bundle is u…#39255
krishnamanchikalapudi wants to merge 2 commits into
apache:masterfrom
krishnamanchikalapudi:fix-39001-named-data-stream-lifecycle

Conversation

@krishnamanchikalapudi

Copy link
Copy Markdown

…sing them

Named data streams introduced in #38863 provide bundle isolation, but the SDK harness cached one multiplexer (and its underlying gRPC stream) per (endpoint, dataStreamId) forever. A runner assigning fresh data stream ids over time would leak gRPC streams and memory in the harness.

This reference counts named data stream usage per bundle in ProcessBundleHandler and closes the multiplexers for a named data stream once no bundle is retaining it. The default (unnamed) data stream keeps its lifetime-of-the-harness behavior.

Also adds a fork-friendly GitHub-hosted CI workflow that builds and tests the Java SDK harness since upstream PreCommit workflows require self-hosted runners.

Fixes #39001 (Java SDK harness portion)

Please add a meaningful description for your change here


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

…sing them

Named data streams introduced in apache#38863 provide bundle isolation, but the
SDK harness cached one multiplexer (and its underlying gRPC stream) per
(endpoint, dataStreamId) forever. A runner assigning fresh data stream ids
over time would leak gRPC streams and memory in the harness.

This reference counts named data stream usage per bundle in
ProcessBundleHandler and closes the multiplexers for a named data stream
once no bundle is retaining it. The default (unnamed) data stream keeps its
lifetime-of-the-harness behavior.

Also adds a fork-friendly GitHub-hosted CI workflow that builds and tests
the Java SDK harness since upstream PreCommit workflows require self-hosted
runners.

Fixes apache#39001 (Java SDK harness portion)
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a resource leak in the Java SDK harness where named data stream multiplexers were cached indefinitely. By introducing reference counting, the harness now correctly tracks bundle usage and closes multiplexers once they are no longer required, significantly improving memory efficiency and resource cleanup. The default unnamed data stream maintains its existing behavior of persisting for the lifetime of the harness.

Highlights

  • Resource Management: Implemented reference counting for named data streams in the Java SDK harness to prevent memory and gRPC stream leaks.
  • Lifecycle Control: Added retainDataStream and releaseDataStream methods to the BeamFnDataClient interface to manage the lifecycle of named data streams per bundle.
  • Concurrency Safety: Introduced a lifecycle lock in BeamFnDataGrpcClient to ensure thread-safe creation and closure of multiplexers.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Ignored Files
  • Ignored by pattern: .github/workflows/** (1)
    • .github/workflows/fork_ci_java_harness.yml
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a resource leak in the SDK harness by introducing reference counting for named data streams, ensuring they are closed once no longer in use by any bundle. The changes include adding retention and release hooks in ProcessBundleHandler and implementing the lifecycle management in BeamFnDataGrpcClient, supported by new unit tests. The review feedback suggests simplifying the multiplexer retrieval logic in BeamFnDataGrpcClient to avoid redundant computeIfAbsent calls inside a synchronized block, and addresses potential thread leaks in the new tests by ensuring that ManagedChannel instances are properly shut down in finally blocks.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +207 to +228
synchronized (dataStreamLifecycleLock) {
return multiplexerCache.computeIfAbsent(
key,
k -> {
OutboundObserverFactory.BasicFactory<Elements, Elements> baseOutboundObserverFactory =
inboundObserver -> {
BeamFnDataGrpc.BeamFnDataStub stub =
BeamFnDataGrpc.newStub(channelFactory.apply(apiServiceDescriptor));
if (dataStreamId != null && !dataStreamId.isEmpty()) {
Metadata headers = new Metadata();
headers.put(
Metadata.Key.of("data_stream_id", Metadata.ASCII_STRING_MARSHALLER),
dataStreamId);
stub =
stub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(headers));
}
return stub.data(inboundObserver);
};
return new BeamFnDataGrpcMultiplexer(
apiServiceDescriptor, outboundObserverFactory, baseOutboundObserverFactory);
});
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since we are already inside the synchronized (dataStreamLifecycleLock) block, using computeIfAbsent on multiplexerCache is redundant. We can simplify this by performing a simple get and put check inside the synchronized block, which also avoids allocating the lambda/closure on every cache miss.

    synchronized (dataStreamLifecycleLock) {
      BeamFnDataGrpcMultiplexer multiplexer = multiplexerCache.get(key);
      if (multiplexer == null) {
        OutboundObserverFactory.BasicFactory<Elements, Elements> baseOutboundObserverFactory =
            inboundObserver -> {
              BeamFnDataGrpc.BeamFnDataStub stub =
                  BeamFnDataGrpc.newStub(channelFactory.apply(apiServiceDescriptor));
              if (dataStreamId != null && !dataStreamId.isEmpty()) {
                Metadata headers = new Metadata();
                headers.put(
                    Metadata.Key.of("data_stream_id", Metadata.ASCII_STRING_MARSHALLER),
                    dataStreamId);
                stub =
                    stub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(headers));
              }
              return stub.data(inboundObserver);
            };
        multiplexer = new BeamFnDataGrpcMultiplexer(
            apiServiceDescriptor, outboundObserverFactory, baseOutboundObserverFactory);
        multiplexerCache.put(key, multiplexer);
      }
      return multiplexer;
    }

Comment on lines +399 to +431
try {
ManagedChannel channel =
InProcessChannelBuilder.forName(apiServiceDescriptor.getUrl()).build();

BeamFnDataGrpcClient clientFactory =
new BeamFnDataGrpcClient(
(Endpoints.ApiServiceDescriptor descriptor) -> channel,
OutboundObserverFactory.trivial());

// Two bundles concurrently use the same named data stream.
clientFactory.retainDataStream(dataStreamId);
clientFactory.retainDataStream(dataStreamId);
clientFactory.getOutboundObserver(apiServiceDescriptor, dataStreamId);
assertTrue(firstConnection.await(5, TimeUnit.SECONDS));
assertEquals(1, connectionCount.get());

// Releasing one of the two usages should not close the stream.
clientFactory.releaseDataStream(dataStreamId);
assertFalse(streamTerminated.await(100, TimeUnit.MILLISECONDS));

// Releasing the last usage should close the stream.
clientFactory.releaseDataStream(dataStreamId);
assertTrue(streamTerminated.await(5, TimeUnit.SECONDS));

// A subsequent usage of the same named data stream establishes a new stream.
clientFactory.retainDataStream(dataStreamId);
clientFactory.getOutboundObserver(apiServiceDescriptor, dataStreamId);
assertTrue(secondConnection.await(5, TimeUnit.SECONDS));
assertEquals(2, connectionCount.get());
clientFactory.releaseDataStream(dataStreamId);
} finally {
server.shutdownNow();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The ManagedChannel created for the test is not shut down in the finally block, which can leak threads and other resources in the test JVM. It should be shut down along with the server.

    ManagedChannel channel =
        InProcessChannelBuilder.forName(apiServiceDescriptor.getUrl()).build();
    try {
      BeamFnDataGrpcClient clientFactory =
          new BeamFnDataGrpcClient(
              (Endpoints.ApiServiceDescriptor descriptor) -> channel,
              OutboundObserverFactory.trivial());

      // Two bundles concurrently use the same named data stream.
      clientFactory.retainDataStream(dataStreamId);
      clientFactory.retainDataStream(dataStreamId);
      clientFactory.getOutboundObserver(apiServiceDescriptor, dataStreamId);
      assertTrue(firstConnection.await(5, TimeUnit.SECONDS));
      assertEquals(1, connectionCount.get());

      // Releasing one of the two usages should not close the stream.
      clientFactory.releaseDataStream(dataStreamId);
      assertFalse(streamTerminated.await(100, TimeUnit.MILLISECONDS));

      // Releasing the last usage should close the stream.
      clientFactory.releaseDataStream(dataStreamId);
      assertTrue(streamTerminated.await(5, TimeUnit.SECONDS));

      // A subsequent usage of the same named data stream establishes a new stream.
      clientFactory.retainDataStream(dataStreamId);
      clientFactory.getOutboundObserver(apiServiceDescriptor, dataStreamId);
      assertTrue(secondConnection.await(5, TimeUnit.SECONDS));
      assertEquals(2, connectionCount.get());
      clientFactory.releaseDataStream(dataStreamId);
    } finally {
      channel.shutdownNow();
      server.shutdownNow();
    }

Comment on lines +457 to +476
try {
ManagedChannel channel =
InProcessChannelBuilder.forName(apiServiceDescriptor.getUrl()).build();

BeamFnDataGrpcClient clientFactory =
new BeamFnDataGrpcClient(
(Endpoints.ApiServiceDescriptor descriptor) -> channel,
OutboundObserverFactory.trivial());

clientFactory.retainDataStream("");
StreamObserver<BeamFnApi.Elements> outboundObserver =
clientFactory.getOutboundObserver(apiServiceDescriptor, "");
clientFactory.releaseDataStream("");

// The default data stream is kept open and reused even after release.
assertFalse(streamTerminated.await(100, TimeUnit.MILLISECONDS));
assertSame(outboundObserver, clientFactory.getOutboundObserver(apiServiceDescriptor, ""));
} finally {
server.shutdownNow();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The ManagedChannel created for the test is not shut down in the finally block, which can leak threads and other resources in the test JVM. It should be shut down along with the server.

Suggested change
try {
ManagedChannel channel =
InProcessChannelBuilder.forName(apiServiceDescriptor.getUrl()).build();
BeamFnDataGrpcClient clientFactory =
new BeamFnDataGrpcClient(
(Endpoints.ApiServiceDescriptor descriptor) -> channel,
OutboundObserverFactory.trivial());
clientFactory.retainDataStream("");
StreamObserver<BeamFnApi.Elements> outboundObserver =
clientFactory.getOutboundObserver(apiServiceDescriptor, "");
clientFactory.releaseDataStream("");
// The default data stream is kept open and reused even after release.
assertFalse(streamTerminated.await(100, TimeUnit.MILLISECONDS));
assertSame(outboundObserver, clientFactory.getOutboundObserver(apiServiceDescriptor, ""));
} finally {
server.shutdownNow();
}
ManagedChannel channel =
InProcessChannelBuilder.forName(apiServiceDescriptor.getUrl()).build();
try {
BeamFnDataGrpcClient clientFactory =
new BeamFnDataGrpcClient(
(Endpoints.ApiServiceDescriptor descriptor) -> channel,
OutboundObserverFactory.trivial());
clientFactory.retainDataStream("");
StreamObserver<BeamFnApi.Elements> outboundObserver =
clientFactory.getOutboundObserver(apiServiceDescriptor, "");
clientFactory.releaseDataStream("");
// The default data stream is kept open and reused even after release.
assertFalse(streamTerminated.await(100, TimeUnit.MILLISECONDS));
assertSame(outboundObserver, clientFactory.getOutboundObserver(apiServiceDescriptor, ""));
} finally {
channel.shutdownNow();
server.shutdownNow();
}

@github-actions github-actions Bot added the infra label Jul 9, 2026
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: When using FnApi and DataStream, bundle processing is not isolated due to limited queues and multiplexing

1 participant