voonhous commented on code in PR #20734: URL: https://github.com/apache/hudi/pull/20734#discussion_r4610899254
########## 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: **minor:** Not blocking. These lines repeat `TestLocalHoodieSchemaCache.testCreateReturnsIndependentCaches` (L71-85: same equal ids and per-cache decode) through `RecordContext`'s one-line pass-through, and against the old singleton they never run because the `assertNotSame` above fails first. Could we drop them and let L81-82 carry the regression? ```suggestion ``` ########## 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: **major:** `RealtimeCompactedRecordReader.mergeRecord` (hudi-hadoop-mr, L207) owns one of these scanners but still encodes schemas on the static `AvroRecordContext.getFieldAccessorInstance()` via `fromEngineRecord`/`fromHoodieRecord`, so the same shared-cache race remains on the Hive/Presto realtime path, where many splits share one JVM. It is the only remaining stateful caller of that singleton (`git grep getFieldAccessorInstance`; the others only call `isDeleteRecord`). Could we give `RealtimeCompactedRecordReader` its own `AvroRecordContext` field in this PR too? ########## 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; Review Comment: **nit:** Feel free to ignore. In production `processNextRecord` receives payload-wrapped `HoodieAvroRecord`s (`AbstractHoodieLogRecordScanner.java:483`) and runs a real merger; this test feeds bare `HoodieAvroIndexedRecord`s to a mock. Could the answer record the context and then delegate to `new HoodiePreCombineAvroRecordMerger().merge(...)`, so the real decode on the scanner-owned context is exercised too? -- 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]
