You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/09/13 15:26:55 UTC

[GitHub] [beam] kileys commented on a diff in pull request #17015: [BEAM-12572] - Java examples should get continuously exercised on at least 2 runners

kileys commented on code in PR #17015:
URL: https://github.com/apache/beam/pull/17015#discussion_r969320428


##########
examples/java/src/main/java/org/apache/beam/examples/complete/game/StatefulTeamScore.java:
##########
@@ -114,31 +115,14 @@ private static Map<String, FieldInfo<KV<String, Integer>>> configureCompleteWind
     return tableConfigure;
   }
 
-  public static void main(String[] args) throws Exception {
+  public static void applyStatefulTeamScore(Pipeline p, Options options) throws IOException {
 
-    Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);
-    // Enforce that this pipeline is always run in streaming mode.
-    options.setStreaming(true);
-    ExampleUtils exampleUtils = new ExampleUtils(options);
-    Pipeline pipeline = Pipeline.create(options);
+    PubsubIO.Read<String> records = readRecordsFromPubSub(options);
 
-    pipeline
-        // Read game events from Pub/Sub using custom timestamps, which are extracted from the
-        // pubsub data elements, and parse the data.
-        .apply(
-            PubsubIO.readStrings()
-                .withTimestampAttribute(GameConstants.TIMESTAMP_ATTRIBUTE)
-                .fromTopic(options.getTopic()))
-        .apply("ParseGameEvent", ParDo.of(new ParseEventFn()))
-        // Create <team, GameActionInfo> mapping. UpdateTeamScore uses team name as key.
-        .apply(
-            "MapTeamAsKey",
-            MapElements.into(
-                    TypeDescriptors.kvs(
-                        TypeDescriptors.strings(), TypeDescriptor.of(GameActionInfo.class)))
-                .via((GameActionInfo gInfo) -> KV.of(gInfo.team, gInfo)))
-        // Outputs a team's score every time it passes a new multiple of the threshold.
-        .apply("UpdateTeamScore", ParDo.of(new UpdateTeamScoreFn(options.getThresholdScore())))
+    p.apply(records)
+        // Create <team, GameActionInfo> mapping & Outputs a team's score every time it passes a new
+        // multiple of the threshold
+        .apply(new TeamScore(options))
         // Write the results to BigQuery.
         .apply(

Review Comment:
   Why is this not part of the transform?



##########
examples/java/src/test/java/org/apache/beam/examples/complete/game/GameStatsIT.java:
##########
@@ -0,0 +1,248 @@
+/*
+ * 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.examples.complete.game;
+
+import static org.junit.Assert.assertEquals;
+
+import com.google.api.gax.grpc.GrpcTransportChannel;
+import com.google.api.gax.rpc.FixedTransportChannelProvider;
+import com.google.api.gax.rpc.TransportChannelProvider;
+import com.google.api.services.bigquery.model.QueryResponse;
+import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
+import com.google.cloud.pubsub.v1.SubscriptionAdminSettings;
+import com.google.cloud.pubsub.v1.TopicAdminClient;
+import com.google.cloud.pubsub.v1.TopicAdminSettings;
+import com.google.pubsub.v1.PushConfig;
+import io.grpc.ManagedChannel;
+import io.grpc.ManagedChannelBuilder;
+import java.io.IOException;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.beam.examples.complete.game.utils.GameConstants;
+import org.apache.beam.runners.direct.DirectOptions;
+import org.apache.beam.sdk.extensions.gcp.options.GcpOptions;
+import org.apache.beam.sdk.io.TextIO;
+import org.apache.beam.sdk.io.gcp.pubsub.PubsubClient;
+import org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath;
+import org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath;
+import org.apache.beam.sdk.io.gcp.pubsub.PubsubIO;
+import org.apache.beam.sdk.io.gcp.pubsub.PubsubOptions;
+import org.apache.beam.sdk.io.gcp.testing.BigqueryClient;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.testing.TestPipelineOptions;
+import org.apache.beam.sdk.util.FluentBackoff;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Duration;
+import org.joda.time.Instant;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link GameStats}. */
+@RunWith(JUnit4.class)
+public class GameStatsIT {
+  private static final DateTimeFormatter DATETIME_FORMAT =
+      DateTimeFormat.forPattern("YYYY-MM-dd-HH-mm-ss-SSS");
+  private static final String EVENTS_TOPIC_NAME = "events";
+  public static final String GAME_STATS_TEAM_TABLE = "game_stats_team";
+  private static final Integer DEFAULT_ACK_DEADLINE_SECONDS = 60;
+  public static final String SELECT_COUNT_AS_TOTAL_QUERY =
+      "SELECT total_score FROM `%s.%s.%s` where team like(\"AmaranthKoala\")";
+  private GameStatsOptions options =
+      TestPipeline.testingPipelineOptions().as(GameStatsIT.GameStatsOptions.class);
+  @Rule public final transient TestPipeline testPipeline = TestPipeline.fromOptions(options);
+  private static String pubsubEndpoint;
+  private @Nullable ManagedChannel channel = null;
+  private @Nullable TransportChannelProvider channelProvider = null;
+  private @Nullable TopicAdminClient topicAdmin = null;
+  private @Nullable SubscriptionAdminClient subscriptionAdmin = null;
+  private @Nullable TopicPath eventsTopicPath = null;
+  private @Nullable SubscriptionPath subscriptionPath = null;
+  private String projectId;
+  private static final String TOPIC_PREFIX = "gamestats-";
+  private BigqueryClient bqClient;
+  private final String OUTPUT_DATASET = "game_stats_e2e";
+
+  public interface GameStatsOptions extends TestPipelineOptions, GameStats.Options {};
+
+  @Before
+  public void setupTestEnvironment() throws Exception {
+    PipelineOptionsFactory.register(TestPipelineOptions.class);
+    projectId = TestPipeline.testingPipelineOptions().as(GcpOptions.class).getProject();
+
+    setupBigQuery();

Review Comment:
   It at least doesn't make sense to have the same code in multiple IT files. Do you know what the difference is in the setup for ExamplesUtils? How much longer does it take to be created? Can we use a check with a backoff if the dataset and topics exist before running the pipeline?



##########
examples/java/src/main/java/org/apache/beam/examples/complete/game/StatefulTeamScore.java:
##########
@@ -147,11 +131,43 @@ public static void main(String[] args) throws Exception {
                 options.getDataset(),
                 options.getLeaderBoardTableName() + "_team_leader",
                 configureCompleteWindowedTableWrite()));
+  }
+
+  public static void runStatefulTeamScore(Options options) throws IOException {
+    Pipeline p = Pipeline.create(options);
+    applyStatefulTeamScore(p, options);
+    p.run();
+  }
+
+  public static void main(String[] args) throws Exception {
 
-    // Run the pipeline and wait for the pipeline to finish; capture cancellation requests from the
-    // command line.
-    PipelineResult result = pipeline.run();
-    exampleUtils.waitToFinish(result);

Review Comment:
   Same here



##########
examples/java/src/main/java/org/apache/beam/examples/complete/game/StatefulTeamScore.java:
##########
@@ -114,31 +115,14 @@ private static Map<String, FieldInfo<KV<String, Integer>>> configureCompleteWind
     return tableConfigure;
   }
 
-  public static void main(String[] args) throws Exception {
+  public static void applyStatefulTeamScore(Pipeline p, Options options) throws IOException {
 
-    Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);
-    // Enforce that this pipeline is always run in streaming mode.
-    options.setStreaming(true);
-    ExampleUtils exampleUtils = new ExampleUtils(options);

Review Comment:
   How come we don't use ExampleUtils for the main method?



-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org