diff --git a/grpc/grpc-operation-ledger-jpa/build.gradle b/grpc/grpc-operation-ledger-jpa/build.gradle index 05d90ae..e4a8fc3 100644 --- a/grpc/grpc-operation-ledger-jpa/build.gradle +++ b/grpc/grpc-operation-ledger-jpa/build.gradle @@ -17,9 +17,11 @@ configurations { } // One definition of a versioned migration file and of Flyway's order for both builds (re-review -// README 12, r6-b; r9). This build cannot depend on persistence-jpa's testkit, so the one file the -// ledger lanes need from it — VersionedMigrationFiles, JDK only — is copied from there into this -// lane's sources at build time instead of being kept here as a second copy that could drift. +// README 12, r6-b; r9), and one concurrent index check (round 7, RR7-03-01). This build cannot +// depend on persistence-jpa's testkit, so the files the ledger lanes need from it, all JDK only — +// VersionedMigrationFiles, and ConcurrentIndexMigrationInspector with the MigrationResource it +// takes — are copied from there into this lane's sources at build time instead of being kept here +// as second copies that could drift. // Rejected: that testkit directory as a source root of this lane (Spotless refuses a target outside // this project's directory, and the include filter would cover this lane's own sources too), a // dependency on the main build (it would pull the whole main build into this one), and a test that @@ -27,6 +29,8 @@ configurations { def sharedMigrationFileSources = tasks.register('sharedMigrationFileSources', Sync) { from(file('../../adapter/outbound/persistence-jpa/src/testFixtures/java')) { include 'dev/caskeleton/adapter/outbound/persistence/testkit/migration/VersionedMigrationFiles.java' + include 'dev/caskeleton/adapter/outbound/persistence/testkit/migration/ConcurrentIndexMigrationInspector.java' + include 'dev/caskeleton/adapter/outbound/persistence/testkit/migration/MigrationResource.java' } into(layout.buildDirectory.dir('generated/sources/sharedMigrationFiles/java')) } diff --git a/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrationLockWaitIT.java b/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrationLockWaitIT.java index 723fe1d..7efd263 100644 --- a/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrationLockWaitIT.java +++ b/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrationLockWaitIT.java @@ -3,19 +3,24 @@ package dev.caskeleton.grpc.ledger; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; +import dev.caskeleton.adapter.outbound.persistence.testkit.migration.ConcurrentIndexMigrationInspector; +import dev.caskeleton.adapter.outbound.persistence.testkit.migration.MigrationResource; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.time.Duration; +import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.stream.Stream; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.postgresql.ds.PGSimpleDataSource; @@ -38,9 +43,18 @@ import org.testcontainers.postgresql.PostgreSQLContainer; *

A file Flyway runs outside a transaction — one holding {@code CREATE INDEX CONCURRENTLY} or * another statement on Flyway's list, or with {@code executeInTransaction=false} beside it — is not * one this test can judge: run here in a transaction, PostgreSQL refuses it ({@code 25001}), and no - * {@code SET LOCAL} would bound it in a deploy anyway. Name such a file as an exception in this - * test, and keep it alone in its migration ({@code docs/jpa/migration-guide.md}, "Concurrent index - * builds"; re-review 2026-11-03 round 6, RR6-03-05). + * {@code SET LOCAL} would bound it in a deploy anyway. The one such file the ledger may have is a + * concurrent index build, and it goes in {@link #RUN_OUTSIDE_A_TRANSACTION} with the reason it + * needs to: the lock-wait case leaves it out, and {@code + * everyMigrationListedAsRunningOutsideATransactionIsOneConcurrentIndexBuildThatSaysSo} holds it to + * the guide instead — a concurrent index statement, alone in its file, with {@code + * executeInTransaction=false} beside it ({@code docs/jpa/migration-guide.md}, "Concurrent index + * builds"). The list takes nothing else: a {@code VACUUM FULL} or another locking statement that + * cannot run in a transaction does not belong in a migration at all ("Bound the lock wait"). The + * migrations before the one under test are applied one file at a time ({@link + * GrpcOperationLedgerMigrations#before(String)}), so a listed file is no obstacle to a later one. + * Re-review 2026-11-03 round 6, RR6-03-05; round 7, RR7-03-01: the message pointed at a list this + * test did not have. * *

Every migration after V001 is held to this, read from the tree itself ({@link * GrpcOperationLedgerMigrations#inOrder()}), so a V004 is checked without anyone naming it here. @@ -66,9 +80,18 @@ class GrpcOperationLedgerMigrationLockWaitIT { .withUsername("grpc") .withPassword("grpc"); - /** Every ledger migration after V001, the one that creates the ledger. */ + /** + * The ledger migrations Flyway runs outside a transaction, by file name, each with why it has to: + * a concurrent index build that would stop the ledger's writes if it were built in a transaction. + * Empty while the tree has none. + */ + static final Map RUN_OUTSIDE_A_TRANSACTION = Map.of(); + + /** Every ledger migration after V001, the one that creates the ledger, that runs in one. */ static Stream migrationsAfterTheFirst() throws IOException { - return GrpcOperationLedgerMigrations.inOrder().stream().skip(1); + return GrpcOperationLedgerMigrations.inOrder().stream() + .skip(1) + .filter(migration -> !RUN_OUTSIDE_A_TRANSACTION.containsKey(migration)); } @ParameterizedTest @@ -80,7 +103,9 @@ class GrpcOperationLedgerMigrationLockWaitIT { Statement statement = setup.createStatement()) { // given statement.execute("DROP TABLE IF EXISTS grpc_operation_ledger"); - statement.execute(GrpcOperationLedgerMigrations.before(migration)); + for (String script : GrpcOperationLedgerMigrations.before(migration)) { + statement.execute(script); + } String schemaBefore = schema(statement); // when @@ -100,13 +125,57 @@ class GrpcOperationLedgerMigrationLockWaitIT { ACTIVE_SQL_TRANSACTION.equals(attempt.sqlState()) ? ". PostgreSQL refused it inside a transaction (25001): Flyway runs such a file" + " outside one, where SET LOCAL bounds nothing, so it is no case for this" - + " test. Keep it alone in its migration and name it as an exception here" - + " (\"Concurrent index builds\")" + + " test. If it is a concurrent index build, keep it alone in its migration" + + " with executeInTransaction=false beside it and add it, with the reason, to" + + " RUN_OUTSIDE_A_TRANSACTION in this test (\"Concurrent index builds\");" + + " anything else that cannot run in a transaction does not belong in a" + + " migration (\"Bound the lock wait\")" : "") .containsExactly(LOCK_NOT_AVAILABLE, true, schemaBefore); } } + @Test + @DisplayName( + "트랜잭션 밖에서 도는 예외 목록의 ledger 마이그레이션은 트리에 있고, 동시 인덱스 문장 하나뿐이며, executeInTransaction=false를 적었다") + void everyMigrationListedAsRunningOutsideATransactionIsOneConcurrentIndexBuildThatSaysSo() + throws IOException { + // given + ConcurrentIndexMigrationInspector inspector = new ConcurrentIndexMigrationInspector(); + List tree = GrpcOperationLedgerMigrations.inOrder(); + + // when + List refused = new ArrayList<>(); + for (String migration : RUN_OUTSIDE_A_TRANSACTION.keySet()) { + if (!tree.contains(migration)) { + refused.add(migration + " is not a migration in the ledger tree"); + continue; + } + MigrationResource resource = + new MigrationResource(migration, GrpcOperationLedgerMigrations.named(migration)); + if (!inspector.containsConcurrentIndexStatement(resource.sql())) { + refused.add( + migration + + " holds no concurrent index statement; only a concurrent index build may run" + + " outside a transaction"); + continue; + } + boolean inATransaction = GrpcOperationLedgerMigrations.executeInTransaction(migration); + Throwable thrown = + catchThrowable( + () -> { + inspector.validate(resource, inATransaction); + inspector.requireIsolatedStatement(resource); + }); + if (thrown != null) { + refused.add(thrown.getMessage()); + } + } + + // then + assertThat(refused).isEmpty(); + } + /** Runs the migration in one transaction while another one holds a write to the ledger. */ private static Attempt whileAWriterHolds(PGSimpleDataSource dataSource, String migration) throws SQLException { diff --git a/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrations.java b/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrations.java index f8832fc..c237e17 100644 --- a/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrations.java +++ b/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrations.java @@ -3,12 +3,16 @@ package dev.caskeleton.grpc.ledger; import dev.caskeleton.adapter.outbound.persistence.testkit.migration.VersionedMigrationFiles; import java.io.IOException; import java.io.InputStream; +import java.io.Reader; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.List; +import java.util.Properties; /** - * The ledger's Flyway scripts in version order, as one SQL text for a test database. + * The ledger's Flyway scripts in version order, one SQL text per file for a test database. * *

One list for every PostgreSQL lane, so a new migration is applied by all of them or by none. * The list is the tree on disk, not a copy of it: a hand-kept list let a V004 that nobody added to @@ -38,21 +42,26 @@ final class GrpcOperationLedgerMigrations { .toList(); } - /** Every migration's SQL, concatenated in version order. */ - static String all() throws IOException { + /** Every migration's SQL in version order, one script per file. */ + static List all() throws IOException { return before(null); } - /** The SQL of every migration before the one named, concatenated in version order. */ - static String before(String fileName) throws IOException { - StringBuilder sql = new StringBuilder(); + /** + * The SQL of every migration before the one named, in version order, one script per file. Run + * each as a statement of its own, the way Flyway runs a file: one string holding many files runs + * in one implicit transaction, where PostgreSQL refuses a file Flyway runs outside one — a + * concurrent index build (re-review 2026-11-03 round 7, RR7-03-01). + */ + static List before(String fileName) throws IOException { + List scripts = new ArrayList<>(); for (String name : inOrder()) { if (name.equals(fileName)) { break; } - sql.append(read(name)).append('\n'); + scripts.add(read(name)); } - return sql.toString(); + return scripts; } /** One migration's SQL, by its file name. */ @@ -63,6 +72,23 @@ final class GrpcOperationLedgerMigrations { return read(fileName); } + /** + * Whether Flyway is told to run the migration in a transaction: false only when the {@code + *