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


##########
examples/java/src/main/java/org/apache/beam/examples/KafkaStreaming.java:
##########
@@ -115,7 +115,7 @@ public static void main(String[] args) {
     // starts.
     Duration windowSize = Duration.standardSeconds(WINDOW_TIME);
     Instant nextWindowStart =
-        new Instant(
+        Instant.ofEpochMilli(
             Instant.now().getMillis()
                 + windowSize.getMillis()
                 - Instant.now().plus(windowSize).getMillis() % 
windowSize.getMillis());

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Calling `Instant.now()` multiple times within the same expression can lead 
to inconsistencies if the system clock ticks between the calls. It's safer to 
call it once and store the result in a local variable before using it. For 
example:
   
   ```java
   final Instant now = Instant.now();
   Instant nextWindowStart =
       Instant.ofEpochMilli(
           now.getMillis()
               + windowSize.getMillis()
               - now.plus(windowSize).getMillis() % windowSize.getMillis());
   ```



##########
examples/java/src/main/java/org/apache/beam/examples/snippets/Snippets.java:
##########
@@ -802,21 +802,21 @@ public static void main(String[] args) {
                   Create.timestamped(
                       TimestampedValue.of(
                           new TableRow().set("user", "mobile").set("score", 
12).set("gap", 5),
-                          new Instant()),
+                          Instant.now()),
                       TimestampedValue.of(
-                          new TableRow().set("user", "desktop").set("score", 
4), new Instant()),
+                          new TableRow().set("user", "desktop").set("score", 
4), Instant.now()),
                       TimestampedValue.of(
                           new TableRow().set("user", "mobile").set("score", 
-3).set("gap", 5),
-                          new Instant().plus(Duration.millis(2000))),
+                          Instant.now().plus(Duration.millis(2000))),
                       TimestampedValue.of(
                           new TableRow().set("user", "mobile").set("score", 
2).set("gap", 5),
-                          new Instant().plus(Duration.millis(9000))),
+                          Instant.now().plus(Duration.millis(9000))),
                       TimestampedValue.of(
                           new TableRow().set("user", "mobile").set("score", 
7).set("gap", 5),
-                          new Instant().plus(Duration.millis(12000))),
+                          Instant.now().plus(Duration.millis(12000))),
                       TimestampedValue.of(
                           new TableRow().set("user", "desktop").set("score", 
10),
-                          new Instant().plus(Duration.millis(12000)))));
+                          Instant.now().plus(Duration.millis(12000)))));

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   There are multiple calls to `Instant.now()` here. This will result in 
slightly different timestamps for each `TimestampedValue`. If the intention is 
for these events to be relative to a single point in time, it would be better 
to call `Instant.now()` once and reuse the value. For example:
   
   ```java
   final Instant now = Instant.now();
   PCollection<TableRow> pc =
       p.apply(
           "Create Events",
           Create.timestamped(
               TimestampedValue.of(
                   new TableRow().set("user", "mobile").set("score", 
12).set("gap", 5),
                   now),
               TimestampedValue.of(
                   new TableRow().set("user", "desktop").set("score", 4), now),
               TimestampedValue.of(
                   new TableRow().set("user", "mobile").set("score", 
-3).set("gap", 5),
                   now.plus(Duration.millis(2000))),
               // ... and so on
               ));
   ```



-- 
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