github-actions[bot] commented on code in PR #41746:
URL: https://github.com/apache/beam/pull/41746#discussion_r4730486272


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/Schema.java:
##########
@@ -1370,15 +1644,15 @@ public Builder addOptions(Options options) {
     }
 
     Options.Builder toBuilder() {
-      return new Builder(new HashMap<>(this.options));
+      return this.options.isEmpty() ? new Builder() : new 
Builder(this.options);

Review Comment:
   **🔴 Critical**: `new Builder(this.options)` stores the passed map reference 
directly without copying (`this.options = options;` at line 1598). When 
`this.options` is non-empty, calling `toBuilder().setOption(...)` will directly 
mutate the backing `HashMap` of the existing `Options` instance, violating 
`Schema` immutability. `toBuilder()` must pass a defensive copy `new 
HashMap<>(this.options)`.
   
   ```suggestion
         return this.options.isEmpty() ? new Builder() : new Builder(new 
HashMap<>(this.options));
   ```



##########
sdks/java/core/src/main/java/org/apache/beam/sdk/values/RowWithGetters.java:
##########
@@ -43,54 +36,61 @@
  */
 @SuppressWarnings("rawtypes")
 public class RowWithGetters<T extends @NonNull Object> extends Row {
+  private static final Object NULL_SENTINEL = new Object();
+
   private final T getterTarget;
   private final TypeDescriptor<?> getterTargetType;
   private final List<FieldValueGetter<T, Object>> getters;
-  private @Nullable Map<Integer, @Nullable Object> cache = null;
+  // Null slot means uncached; NULL_SENTINEL means cached null. 
AtomicReferenceArray ensures
+  // safe JMM publication of cached collections across concurrent readers.
+  private transient volatile @Nullable AtomicReferenceArray<@Nullable Object> 
cache = null;
 
   RowWithGetters(
       Schema schema,
       Factory<List<FieldValueGetter<T, Object>>> getterFactory,
       T getterTarget,
       TypeDescriptor<?> getterTargetType) {
+    this(schema, getterFactory.create(getterTargetType, schema), getterTarget, 
getterTargetType);
+  }
+
+  RowWithGetters(
+      Schema schema,
+      List<FieldValueGetter<T, Object>> getters,
+      T getterTarget,
+      TypeDescriptor<?> getterTargetType) {
     super(schema);
     this.getterTarget = getterTarget;
     this.getterTargetType = getterTargetType;
-    this.getters = getterFactory.create(getterTargetType, schema);
+    this.getters = getters;
   }
 
   @Override
   @SuppressWarnings({"TypeParameterUnusedInFormals", "unchecked"})
   public <W> W getValue(int fieldIdx) {
-    Field field = getSchema().getField(fieldIdx);
-    boolean cacheField = cacheFieldType(field);
-
-    if (cacheField && cache == null) {
-      cache = new TreeMap<>();
-    }
-
+    Schema schema = getSchema();
     @Nullable Object fieldValue;
-    if (cacheField) {
-      if (cache == null) {
-        cache = new TreeMap<>();
+    int cacheSlot = schema.getContainerCacheSlot(fieldIdx);
+    if (cacheSlot >= 0) {
+      AtomicReferenceArray<@Nullable Object> localCache = cache;
+      if (localCache == null) {

Review Comment:
   **🟡 Medium**: If `RowWithGetters` is accessed concurrently by multiple 
readers (as noted in the comment on line 45), lazy initialization of `cache` 
without `compareAndSet` can cause a benign race where one thread's newly 
allocated `AtomicReferenceArray` overwrites another's, causing a cached 
container getter to be invoked more than once. If single-evaluation or strict 
reference identity of cached containers across threads is desired, consider 
using an `AtomicReferenceFieldUpdater` (or initializing `cache` in the 
constructor when `schema.getCacheableContainerCount() > 0`).



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