gemini-code-assist[bot] commented on code in PR #38521:
URL: https://github.com/apache/beam/pull/38521#discussion_r3252679733


##########
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/KafkaStreamsPipelineResult.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.kafka.streams;
+
+import java.io.IOException;
+import org.apache.beam.sdk.PipelineResult;
+import org.apache.beam.sdk.metrics.MetricResults;
+import org.joda.time.Duration;
+
+/**
+ * Forwards {@link PipelineResult} calls to a delegate and stops an embedded 
job server when the
+ * pipeline reaches a terminal state.
+ */
+class KafkaStreamsPipelineResult implements PipelineResult {
+
+  private final PipelineResult delegate;
+  private final Runnable stopJobServer;
+
+  KafkaStreamsPipelineResult(PipelineResult delegate, Runnable stopJobServer) {
+    this.delegate = delegate;
+    this.stopJobServer = stopJobServer;
+  }
+
+  @Override
+  public State getState() {
+    return delegate.getState();
+  }
+
+  @Override
+  public State cancel() throws IOException {
+    State state = delegate.cancel();
+    stopJobServer.run();
+    return state;
+  }
+
+  @Override
+  public State waitUntilFinish(Duration duration) {
+    State state = delegate.waitUntilFinish(duration);
+    stopJobServer.run();
+    return state;
+  }
+
+  @Override
+  public State waitUntilFinish() {
+    State state = delegate.waitUntilFinish();
+    stopJobServer.run();
+    return state;
+  }

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   It is safer to stop the job server in a `finally` block to ensure cleanup 
even if `waitUntilFinish()` throws an exception (e.g., due to communication 
issues with the server).
   
   ```java
     @Override
     public State waitUntilFinish() {
       try {
         return delegate.waitUntilFinish();
       } finally {
         stopJobServer.run();
       }
     }
   ```



##########
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/KafkaStreamsPipelineResult.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.kafka.streams;
+
+import java.io.IOException;
+import org.apache.beam.sdk.PipelineResult;
+import org.apache.beam.sdk.metrics.MetricResults;
+import org.joda.time.Duration;
+
+/**
+ * Forwards {@link PipelineResult} calls to a delegate and stops an embedded 
job server when the
+ * pipeline reaches a terminal state.
+ */
+class KafkaStreamsPipelineResult implements PipelineResult {
+
+  private final PipelineResult delegate;
+  private final Runnable stopJobServer;
+
+  KafkaStreamsPipelineResult(PipelineResult delegate, Runnable stopJobServer) {
+    this.delegate = delegate;
+    this.stopJobServer = stopJobServer;
+  }
+
+  @Override
+  public State getState() {
+    return delegate.getState();
+  }
+
+  @Override
+  public State cancel() throws IOException {
+    State state = delegate.cancel();
+    stopJobServer.run();
+    return state;
+  }

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The job server should be stopped even if `cancel()` throws an `IOException` 
to ensure resources are released. Using a `try-finally` block is recommended.
   
   ```java
     @Override
     public State cancel() throws IOException {
       try {
         return delegate.cancel();
       } finally {
         stopJobServer.run();
       }
     }
   ```



##########
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/KafkaStreamsPipelineResult.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.kafka.streams;
+
+import java.io.IOException;
+import org.apache.beam.sdk.PipelineResult;
+import org.apache.beam.sdk.metrics.MetricResults;
+import org.joda.time.Duration;
+
+/**
+ * Forwards {@link PipelineResult} calls to a delegate and stops an embedded 
job server when the
+ * pipeline reaches a terminal state.
+ */
+class KafkaStreamsPipelineResult implements PipelineResult {
+
+  private final PipelineResult delegate;
+  private final Runnable stopJobServer;
+
+  KafkaStreamsPipelineResult(PipelineResult delegate, Runnable stopJobServer) {
+    this.delegate = delegate;
+    this.stopJobServer = stopJobServer;
+  }
+
+  @Override
+  public State getState() {
+    return delegate.getState();
+  }
+
+  @Override
+  public State cancel() throws IOException {
+    State state = delegate.cancel();
+    stopJobServer.run();
+    return state;
+  }
+
+  @Override
+  public State waitUntilFinish(Duration duration) {
+    State state = delegate.waitUntilFinish(duration);
+    stopJobServer.run();
+    return state;
+  }

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   The current implementation stops the job server even if 
`waitUntilFinish(duration)` times out and the pipeline is still running. This 
prevents further interaction with the job. The server should only be stopped if 
the pipeline has reached a terminal state.
   
   ```java
     @Override
     public State waitUntilFinish(Duration duration) {
       State state = delegate.waitUntilFinish(duration);
       if (state != null && state.isTerminal()) {
         stopJobServer.run();
       }
       return state;
     }
   ```



##########
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/KafkaStreamsPipelineOptions.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.kafka.streams;
+
+import java.nio.file.Paths;
+import org.apache.beam.sdk.options.Default;
+import org.apache.beam.sdk.options.DefaultValueFactory;
+import org.apache.beam.sdk.options.Description;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.options.PortablePipelineOptions;
+
+/** Pipeline options for the Kafka Streams runner. */
+public interface KafkaStreamsPipelineOptions extends PortablePipelineOptions {
+
+  @Description("Comma-separated list of host:port Kafka brokers used by the 
Kafka Streams client.")
+  @Default.String("localhost:9092")
+  String getBootstrapServers();
+
+  void setBootstrapServers(String bootstrapServers);
+
+  @Description(
+      "Kafka Streams application.id (must be unique for each distinct topology 
using the same "
+          + "input topics in a Kafka cluster).")
+  @Default.String("beam-kafka-streams-runner")
+  String getApplicationId();
+
+  void setApplicationId(String applicationId);
+
+  @Description(
+      "Kafka Streams processing.guarantee setting, for example at_least_once 
or exactly_once_v2.")
+  @Default.String("exactly_once_v2")
+  String getProcessingGuarantee();
+
+  void setProcessingGuarantee(String processingGuarantee);
+
+  @Description("Soft cap on the number of elements per bundle.")
+  @Default.Integer(1000)
+  int getMaxBundleSize();
+
+  void setMaxBundleSize(int maxBundleSize);
+
+  @Description("Soft cap on bundle wall-clock duration in milliseconds.")
+  @Default.Integer(1000)
+  int getMaxBundleTimeMs();
+
+  void setMaxBundleTimeMs(int maxBundleTimeMs);
+
+  @Description("Directory where Kafka Streams stores local state.")
+  @Default.InstanceFactory(StateDirDefaultFactory.class)
+  String getStateDir();
+
+  void setStateDir(String stateDir);
+
+  /** Default {@link #getStateDir()} under the JVM temp directory. */
+  class StateDirDefaultFactory implements DefaultValueFactory<String> {
+    @Override
+    public String create(PipelineOptions options) {
+      return Paths.get(System.getProperty("java.io.tmpdir"), 
"beam-kafka-streams-state").toString();

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Using a fixed path for the Kafka Streams state directory in the system temp 
directory can cause conflicts and `LockException` when multiple pipelines 
(e.g., parallel tests) run on the same host. It is recommended to include the 
job name to ensure uniqueness.
   
   ```suggestion
         return Paths.get(System.getProperty("java.io.tmpdir"), 
"beam-kafka-streams-state", options.getJobName()).toString();
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to