voonhous commented on code in PR #20734:
URL: https://github.com/apache/hudi/pull/20734#discussion_r4638160417


##########
hudi-common/src/main/java/org/apache/hudi/common/table/log/HoodieMergedLogRecordScanner.java:
##########
@@ -100,6 +100,8 @@ public class HoodieMergedLogRecordScanner extends 
AbstractHoodieLogRecordScanner
   private long totalTimeTakenToReadAndMergeBlocks;
   private final String[] orderingFields;
   private final DeleteContext deleteContext;
+  // Merging registers schema IDs, so the context and its local cache must 
belong to this scanner.
+  private final RecordContext recordContext = new AvroRecordContext();

Review Comment:
   Verified: `mergeRecord` now uses the reader-owned `recordContext`, and the 
only `getFieldAccessorInstance()` callers left in main sources call 
`isDeleteRecord`. Thanks!



##########
hudi-common/src/test/java/org/apache/hudi/common/table/log/TestHoodieMergedLogRecordScanner.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * 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.hudi.common.table.log;
+
+import org.apache.hudi.common.engine.RecordContext;
+import org.apache.hudi.common.model.HoodieAvroIndexedRecord;
+import org.apache.hudi.common.model.HoodieKey;
+import org.apache.hudi.common.model.HoodieRecord;
+import org.apache.hudi.common.model.HoodieRecordMerger;
+import org.apache.hudi.common.schema.HoodieSchema;
+import org.apache.hudi.common.table.HoodieTableConfig;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.table.read.BufferedRecord;
+
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.IndexedRecord;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class TestHoodieMergedLogRecordScanner {
+  @TempDir
+  Path tempDir;
+
+  @Test
+  void testSchemaCacheIsOwnedByEachScanner() throws Exception {
+    HoodieSchema firstSchema = schema("first");
+    HoodieSchema secondSchema = schema("second");
+    List<RecordContext<IndexedRecord>> contexts = new ArrayList<>();
+    HoodieRecordMerger merger = mock(HoodieRecordMerger.class);
+    
when(merger.getRecordType()).thenReturn(HoodieRecord.HoodieRecordType.AVRO);
+    when(merger.merge(any(), any(), any(), any())).thenAnswer(invocation -> {
+      BufferedRecord<IndexedRecord> older = invocation.getArgument(0);
+      BufferedRecord<IndexedRecord> newer = invocation.getArgument(1);
+      RecordContext<IndexedRecord> context = invocation.getArgument(2);
+      contexts.add(context);
+      assertEquals(older.getRecord().getSchema(), 
context.getSchemaFromBufferRecord(older).toAvroSchema());
+      assertEquals(newer.getRecord().getSchema(), 
context.getSchemaFromBufferRecord(newer).toAvroSchema());
+      return newer;
+    });
+
+    try (HoodieMergedLogRecordScanner first = scanner(firstSchema, merger);
+         HoodieMergedLogRecordScanner second = scanner(secondSchema, merger)) {
+      first.processNextRecord(record(firstSchema, "first-insert"));
+      first.processNextRecord(record(firstSchema, "first-update"));
+      second.processNextRecord(record(secondSchema, "second-insert"));
+      second.processNextRecord(record(secondSchema, "second-update"));
+      first.processNextRecord(record(firstSchema, "first-update-again"));
+
+      assertEquals(3, contexts.size());
+      RecordContext<IndexedRecord> firstContext = contexts.get(0);
+      RecordContext<IndexedRecord> secondContext = contexts.get(1);
+      assertNotSame(firstContext, secondContext, "Scanners must not share 
their schema cache");
+      assertSame(firstContext, contexts.get(2), "A scanner must reuse its 
context across merges");
+      // Both scanners start their own ID space, and each ID still resolves to 
its own schema.
+      Integer firstId = firstContext.encodeSchema(firstSchema);
+      Integer secondId = secondContext.encodeSchema(secondSchema);
+      assertEquals(firstId, secondId);
+      assertEquals(firstSchema, firstContext.decodeAvroSchema(firstId));
+      assertEquals(secondSchema, secondContext.decodeAvroSchema(secondId));

Review Comment:
   Verified: with the static singleton put back in `processNextRecord`, the 
test fails at the `assertNotSame`; on the PR head it passes. Thanks!



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