yihua commented on code in PR #13868: URL: https://github.com/apache/hudi/pull/13868#discussion_r2335143020
########## hudi-client/hudi-client-common/src/test/java/org/apache/hudi/client/transaction/lock/audit/TestStorageLockProviderAuditService.java: ########## @@ -0,0 +1,442 @@ +/* + * 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.client.transaction.lock.audit; + +import org.apache.hudi.client.transaction.lock.StorageLockClient; +import org.apache.hudi.storage.StoragePath; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.contains; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Unit tests for StorageLockProviderAuditService. + */ +public class TestStorageLockProviderAuditService { + + private static final String BASE_PATH = "s3://bucket/table"; + private static final String OWNER_ID = "writer-12345678-9abc-def0-1234-567890abcdef"; + private static final long TRANSACTION_START_TIME = 1234567890000L; + private static final long LOCK_EXPIRATION = 1000000L; + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + private StorageLockClient storageLockClient; + private Supplier<Boolean> lockHeldSupplier; + private StorageLockProviderAuditService auditService; + + @BeforeEach + void setUp() { + storageLockClient = mock(StorageLockClient.class); + lockHeldSupplier = (Supplier<Boolean>) mock(Supplier.class); + + when(lockHeldSupplier.get()).thenReturn(true); + + auditService = new StorageLockProviderAuditService( + BASE_PATH, + OWNER_ID, + TRANSACTION_START_TIME, + storageLockClient, + timestamp -> LOCK_EXPIRATION, + lockHeldSupplier); + } + + @Test + void testRecordOperationStartSuccess() throws Exception { + long timestamp = System.currentTimeMillis(); + when(storageLockClient.writeObject(anyString(), anyString())).thenReturn(true); + + auditService.recordOperation(AuditOperationState.START, timestamp); + + ArgumentCaptor<String> pathCaptor = ArgumentCaptor.forClass(String.class); + ArgumentCaptor<String> contentCaptor = ArgumentCaptor.forClass(String.class); + verify(storageLockClient, times(1)).writeObject(pathCaptor.capture(), contentCaptor.capture()); + + String expectedPath = String.format("%s%s.hoodie%s.locks%saudit%s%d_%s.jsonl", + BASE_PATH, + StoragePath.SEPARATOR, + StoragePath.SEPARATOR, + StoragePath.SEPARATOR, + StoragePath.SEPARATOR, + TRANSACTION_START_TIME, + OWNER_ID); + assertEquals(expectedPath, pathCaptor.getValue()); + + // Parse JSONL content (should be a single line) + String[] lines = contentCaptor.getValue().trim().split("\n"); + assertEquals(1, lines.length, "Should have one JSON line"); + + Map<String, Object> auditRecord = OBJECT_MAPPER.readValue(lines[0], Map.class); + assertEquals(OWNER_ID, auditRecord.get("ownerId")); + assertEquals(TRANSACTION_START_TIME, ((Number) auditRecord.get("transactionStartTime")).longValue()); + assertEquals(timestamp, ((Number) auditRecord.get("timestamp")).longValue()); + assertEquals("START", auditRecord.get("state")); + assertEquals(LOCK_EXPIRATION, ((Number) auditRecord.get("lockExpiration")).longValue()); + assertTrue((Boolean) auditRecord.get("lockHeld")); + } + + @Test + void testRecordOperationRenewSuccess() throws Exception { + long timestamp = System.currentTimeMillis(); + when(storageLockClient.writeObject(anyString(), anyString())).thenReturn(true); + when(lockHeldSupplier.get()).thenReturn(true); + + // First record START + auditService.recordOperation(AuditOperationState.START, timestamp - 1000); + // Then record RENEW + auditService.recordOperation(AuditOperationState.RENEW, timestamp); + + // Verify the file is written twice (once for START, once after RENEW) + verify(storageLockClient, times(2)).writeObject( + contains(String.format("%d_%s.jsonl", TRANSACTION_START_TIME, OWNER_ID)), + anyString()); + } + + @Test + void testRecordOperationEndSuccess() throws Exception { + long timestamp = System.currentTimeMillis(); + when(storageLockClient.writeObject(anyString(), anyString())).thenReturn(true); + when(lockHeldSupplier.get()).thenReturn(false); + + auditService.recordOperation(AuditOperationState.END, timestamp); + + ArgumentCaptor<String> contentCaptor = ArgumentCaptor.forClass(String.class); + verify(storageLockClient, times(1)).writeObject( + contains(String.format("%d_%s.jsonl", TRANSACTION_START_TIME, OWNER_ID)), + contentCaptor.capture()); + + String[] lines = contentCaptor.getValue().trim().split("\n"); + Map<String, Object> auditRecord = OBJECT_MAPPER.readValue(lines[0], Map.class); + assertEquals("END", auditRecord.get("state")); + assertFalse((Boolean) auditRecord.get("lockHeld")); + // In real usage, lock expiration is still calculated normally even when ending + assertEquals(LOCK_EXPIRATION, ((Number) auditRecord.get("lockExpiration")).longValue()); + } + + @Test + void testRecordOperationWriteFailure() throws Exception { + long timestamp = System.currentTimeMillis(); + when(storageLockClient.writeObject(anyString(), anyString())).thenReturn(false); Review Comment: On the write path, the `HoodieStorage` object is already available. We can refactor the lock client to use specific `HoodieStorage` implementation like `HoodieS3Storage` in another PR to make the file/object operation consistent. -- 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]
