package com.ankurm.expandcontract; import com.ankurm.expandcontract.customer.Customer; import com.ankurm.expandcontract.customer.CustomerService; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.springframework.jdbc.core.simple.JdbcClient; import java.nio.file.Path; import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; /** * The gap between "the expand migration ran" and "every Stage 1 instance in the fleet * has been replaced by Stage 2" is not instantaneous - a real rolling deploy takes * minutes, and every row a lingering Stage 1 instance writes during that window has * {@code email_address = NULL}. The migration's one-time backfill (V2) only ever sees * rows that existed *before* Deploy 1 ran; it cannot see rows a Stage 1 instance writes * *after* that, during its own rollout window. *

* This test reproduces the resulting bug with a naive Stage 3 read (email_address alone) * and then shows the fix that ships in * {@link com.ankurm.expandcontract.customer.CustomerService#findById}: a plain * {@code COALESCE(email_address, email)}. See docs/07-the-backfill-window-bug.md. */ class BackfillWindowBugTest { @Test void naiveStage3ReadReturnsNullForARowStage1WroteDuringTheRollout(@TempDir Path tmp) throws Exception { Path db = tmp.resolve("backfill-window"); TestSupport.migrateTo(db, "2"); Transcript t = Transcript.start("07-backfill-window-bug", "The backfill window: a Stage 1 write after Deploy 1, read by a naive Stage 3"); // A Stage 1 instance is still serving traffic during the Deploy 2 rollout and // writes a brand new row exactly as it always has - it has never heard of // email_address. This is not a hypothetical; it is guaranteed to happen for // however long the rollout takes. JdbcClient jdbc = TestSupport.jdbcClient(db); jdbc.sql("INSERT INTO customers(name, email) VALUES (?, ?)") .param("Katherine Johnson").param("katherine@example.test") .update(); long insertedId = jdbc.sql("SELECT id FROM customers WHERE name = ?") .param("Katherine Johnson").query(Long.class).single(); t.section("the row a lingering Stage 1 instance just wrote"); String naiveRead = jdbc.sql("SELECT name, email, email_address FROM customers WHERE id = ?") .param(insertedId).query().listOfRows().toString(); t.line(naiveRead); assertThat(naiveRead).containsIgnoringCase("email_address=null"); t.section("a NAIVE Stage 3 read (email_address alone) - the bug"); String naiveEmail = jdbc.sql("SELECT email_address FROM customers WHERE id = ?") .param(insertedId).query(String.class).optional().orElse(null); t.line("naive Stage 3 email column value: " + naiveEmail); assertThat(naiveEmail).isNull(); t.section("the SHIPPED Stage 3 read (CustomerService, COALESCE) - the fix"); CustomerService stage3 = new CustomerService(jdbc, 3); Optional fixed = stage3.findById(insertedId); t.line("CustomerService (stage 3) result: " + fixed); assertThat(fixed).isPresent(); assertThat(fixed.get().email()).isEqualTo("katherine@example.test"); t.write(); } }