apurtell commented on PR #2658:
URL: https://github.com/apache/phoenix/pull/2658#issuecomment-6703284370

   @virajjasani I have reinvented this PR at your urging and investigated the 
full scope of the issues. This is basically a new PR. 
   
   ## Transitioning from S $\to$ E
   
   Transitioning the index consistency mode between `STRONG` and `EVENTUAL` 
requires propagating updated metadata to all active clients and synchronizing 
state transitions with CDC consumers.
   
   Index metadata changes are written directly to `SYSTEM.CATALOG`. Without 
coordinated cache invalidation, existing clients continue executing writes 
against cached parent table schemas, preserving the stale index definition and 
retaining the prior maintenance mode. To enforce atomic propagation:
   * The DDL operation updates the consistency mode and increments 
`LAST_DDL_TIMESTAMP` simultaneously for both the target index and its parent 
table in a single commit.
   * Server-side metadata cache entries for both tables are evicted immediately 
after commit.
   * The initiating client invalidates both entities in its local cache.
   * Remote clients detect metadata staleness via standard `LAST_DDL_TIMESTAMP` 
checks, reloading the parent table and updated index definitions in tandem.
   
   This mechanism applies across all client versions without relying on 
`phoenix.metadata.invalidate.cache.enabled`.
   
   ```mermaid
   sequenceDiagram
       participant DDL as DDL client
       participant Cat as SYSTEM.CATALOG
       participant Srv as Server metadata cache
       participant Loc as Initiating client cache
       participant Rem as Remote client
   
       DDL->>Cat: Commit consistency mode and LAST_DDL_TIMESTAMP on the index 
and parent table
       DDL->>Srv: Evict both tables
       DDL->>Loc: Invalidate both entities
       Rem->>Rem: LAST_DDL_TIMESTAMP check sees staleness
       Rem->>Cat: Reload parent table and index together
       Note over DDL,Rem: Same path on every client version, independent of 
phoenix.metadata.invalidate.cache.enabled
   ```
   
   Under `EVENTUAL` consistency, client writers bypass synchronous index 
updates and defer to CDC replay. Before any writer observes `EVENTUAL`, the 
system must ensure the underlying CDC index and stream exist, and that the 
stream is actively tracking partition lineage across region splits and merges.
   
   The transition pipeline provisions the CDC stream and CDC index while 
maintaining the target index in `STRONG` mode. Because `CREATE CDC` 
asynchronously initializes stream partitions, leaving the stream in an 
`ENABLING` state until a background task persists partition boundaries, any 
split or merge in this window would fail to record parent partition lineage, 
preventing daughter regions from replaying uncommitted parent data. So, the 
`ALTER` operation blocks until the stream transitions to `ENABLED` before 
committing the `EVENTUAL` state change. During this phase, the index remains 
`STRONG`. Writes continue synchronous maintenance while concurrently emitting 
CDC mutations. Overlapping consumer replays during this window are idempotent. 
Because data integrity is preserved while blocked, this wait does not time out. 
The operation fails only if the CDC stream enters an unrecoverable state where 
reaching `ENABLED` is impossible.
   
   ```mermaid
   sequenceDiagram
       participant DDL as ALTER INDEX
       participant Stream as CDC stream
       participant Writers as Client writers
       participant Cat as SYSTEM.CATALOG
   
       DDL->>Stream: Provision CDC stream and CDC index
       Note over Stream: Stays ENABLING until partition boundaries are persisted
       Note over Writers: Index remains STRONG
       Writers->>Writers: Synchronous index maintenance
       Writers->>Stream: Emit CDC mutations
       Note over Writers,Stream: Consumer replays in this window are idempotent
       alt Stream reaches ENABLED
           Stream-->>DDL: ENABLED
           DDL->>Cat: Commit EVENTUAL
       else Stream cannot reach ENABLED
           Stream-->>DDL: Unrecoverable
           Note over DDL,Cat: ALTER fails and the index stays STRONG
       end
   ```
   
   ## Transitioning from E $\to$ S
   
   Transitioning from `EVENTUAL` to `STRONG` introduces two potential race 
conditions:
   1. Pending CDC events generated during the `EVENTUAL` window may still be 
queued when the index switches to `STRONG`. If consumers immediately ignore 
mutations targeting `STRONG` indexes, those events are dropped.
   2. Clients with a high `UPDATE_CACHE_FREQUENCY` (UCF) may continue operating 
against stale metadata and skip synchronous index maintenance for the remainder 
of their cache window.
   
   To prevent write loss, CDC consumers continue applying events to newly 
`STRONG` indexes up to a computed cutoff of the LAST_DDL_TIMESTAMP of the 
index, plus the larger of the UPDATE_CACHE_FREQUENCY values of the table and 
the index, plus one minute. The additional margin accounts for in-flight 
mutations and clock skew. Events generated after the cutoff are processed only 
if the index remains `EVENTUAL`. Redundant CDC replays applied prior to the 
cutoff remain fully idempotent.
   
   ```mermaid
   stateDiagram-v2
       direction TB
   
       state "STRONG" as Strong
       state "ENABLING, index still STRONG" as CdcEnabling
       state "EVENTUAL, stream ENABLED" as Eventual
       state "STRONG, drain until cutoff" as Drain
   
       [*] --> Strong
       Strong --> CdcEnabling: ALTER to EVENTUAL
       CdcEnabling --> Eventual: stream ENABLED, commit EVENTUAL
       CdcEnabling --> Strong: stream cannot reach ENABLED, ALTER fails
       Eventual --> Drain: ALTER to STRONG commits
       Drain --> Strong: cutoff elapsed
   
       note right of CdcEnabling
           ALTER blocks with no timeout.
           Writers keep synchronous maintenance
           and also emit CDC mutations.
       end note
   
       note right of Drain
           Cutoff is the index LAST_DDL_TIMESTAMP
           plus the larger UPDATE_CACHE_FREQUENCY
           of the table and the index, plus one minute.
           Later events apply only while the index is EVENTUAL.
       end note
   ```
   
   ```mermaid
   sequenceDiagram
       participant DDL as ALTER INDEX
       participant Cat as SYSTEM.CATALOG
       participant Cons as CDC consumer
       participant Idx as Index
   
       DDL->>Cat: Commit STRONG and set LAST_DDL_TIMESTAMP
       Note over Cons: cutoff = LAST_DDL_TIMESTAMP + max(table UCF, index UCF) 
+ 1 minute
       Cons->>Idx: Apply events generated before the cutoff
       Note over Cons,Idx: Covers queued EVENTUAL events, in-flight mutations, 
and stale UCF clients
       Note over Cons: Replays before the cutoff are idempotent
       Note over Cons: Events after the cutoff apply only if the index is still 
EVENTUAL
   ```
   
   Region open hooks trigger CDC consumer initialization, at which point the 
consumer evaluates whether to process the region or exit. The lifecycle 
evaluation resolves two failure modes:
   1. **Premature termination on mode evaluation:** A consumer must not 
terminate merely because no index currently evaluates as `EVENTUAL`. An index 
that recently transitioned to `STRONG` may still contain unplayed events in 
parent partitions. The consumer now initializes and remains active whenever an 
active CDC index exists on the table. To avoid metric noise, lag metrics are 
registered only when an index actively requires consumer replay.
   2. **Stale RegionServer metadata:** If a region moves to a RegionServer 
holding a cached table schema from prior to the DDL execution, the consumer 
could evaluate its lifecycle against stale configuration. The consumer 
bootstrap path now queries `SYSTEM.CATALOG` directly to retrieve authoritative 
table metadata.
   
   ```mermaid
   sequenceDiagram
       participant Hook as Region open hook
       participant Cat as SYSTEM.CATALOG
       participant Cons as CDC consumer
   
       Hook->>Cat: Read authoritative table metadata
       Note over Hook,Cat: Bypasses the RegionServer schema cache
       alt Active CDC index exists
           Hook->>Cons: Initialize and stay active
           alt An index requires replay
               Cons->>Cons: Register lag metrics
           else No index requires replay
               Cons->>Cons: Leave lag metrics unregistered
           end
       else No active CDC index
           Hook->>Hook: Do not start a consumer
       end
   ```
   
   ## Test Coverage
   
   * **`AlterIndexConsistencyCrossClientIT`**: Validates isolated client 
environments to verify bidirectional metadata synchronization across multiple 
client caches and ensures CDC infrastructure is fully active before allowing an 
index to transition to `EVENTUAL`.
   * **`IndexCDCConsumerConsistencySwitchIT`**: Employs an artificial 
consumption lag to verify consistency windows with backlogged events across 
four targeted scenarios:
   * Online `EVENTUAL` $\to$ `STRONG` transition during active consumption.
   * `EVENTUAL` $\to$ `STRONG` transition immediately followed by a split, 
verifying parent partition replay by daughter regions.
   * `STRONG` $\to$ `EVENTUAL` transition immediately followed by a split.
   * Region reopen workflows where the target server retains stale cached 
metadata.
   
   ## Multi-Node Cluster Integration
   
   Evaluates concurrent, cross-JVM operations across a multi-node HBase cluster 
mixing patched and unpatched client versions. The test alternates consistency 
modes (`STRONG` $\to$ `EVENTUAL` $\to$ `STRONG` $\to$ `EVENTUAL` $\to$ 
`STRONG`) under continuous ingest and interleaved split/merge operations, 
verifying cluster-wide cache invalidation and row-for-row parity between data 
and index tables.
   
   ## Out of Scope
   
   The following items represent separate limitations tracked for future work:
   
   * **Region-open initialization dependency:** Consumers initialize strictly 
on region assignment. A static region that does not split, merge, or rebalance 
post-cutover will not launch a consumer, delaying asynchronous updates until an 
administrative or operational reassignment occurs.
   
   * **`CREATE INDEX ... CONSISTENCY=EVENTUAL` initialization gap:** The 
initial index creation DDL does not wait for the CDC stream to reach `ENABLED`, 
leaving an identical vulnerability to split/merge lineage loss if topology 
changes occur immediately post-creation.
   
   * **Silent dependency on `PhoenixMasterObserver`:** Absence of 
`PhoenixMasterObserver` prevents CDC stream registration for new regions 
without raising an error or administrative alert, causing consumers to stall 
indefinitely.
   
   * **Unconditional consumer polling on user-created CDC:** Because consumer 
initialization is bound to the presence of an active CDC index, tables 
utilizing standalone CDC streams without eventual indexes incur unnecessary 
consumer polling overhead.
   


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