matanper opened a new pull request, #70882:
URL: https://github.com/apache/doris/pull/70882

   ### Background
   
   When an FE receives SIGTERM, its shutdown hook (`DorisFE`) sets 
`serverReady` to false, so `/api/health` fails and load balancers stop routing 
new connections to it. It then waits up to 300 s for the queries it coordinates 
and stops its query, thrift and HTTP servers. On followers and observers that 
is enough. On the master, nothing in this path hands off the master role: the 
other electable FEs learn that the master is gone only when the process exits 
and the BDB JE replication heartbeat times out, and only then do they elect a 
new master.
   
   ### What problem does this PR solve?
   
   Issue Number: close #70848
   
   Related PR: None
   
   PR Type: Enhancement
   
   Problem Summary:
   
   A planned restart of the master FE (rolling upgrade, config change, node 
maintenance) today costs an unplanned master election. Metadata writes, loads 
and every statement the other FEs forward stall or fail for the whole drain + 
heartbeat-timeout + election window. In a 3-FE test cluster under a steady 
pooled workload, writes paused for about 97 s, and about 850 statements failed 
per master restart.
   
   With this PR, a master that gets SIGTERM drains, refuses new work, and hands 
its mastership to a ready follower with BDB JE 
`ReplicatedEnvironment.transferMaster`. Other FEs hold the statements they 
forward until the new master takes them. In the same test, the write pause 
drops to about 2 s and about 8 statements fail. Those 8 are the clients 
connected to the departing master itself. Crashes (SIGKILL) are unchanged. With 
the new configs switched off, the behaviour is the same as before.
   
   ### Details
   
   - **Change map:** 955 lines changed in total.
     - Production code (591 added, 26 removed):
       - `DorisFE`: the shutdown sequence.
       - `Env`, `BDBHA`: target choice and `transferMaster`.
       - `ShutdownGate` (new): refuses new statements and forwards.
       - `FEOpExecutor`, `MasterOpExecutor`: hold and resend forwarded 
statements.
       - `FrontendServiceImpl.forward`, `MysqlConnectProcessor`, 
`DorisFlightSqlProducer`: the gates.
       - `BDBJournalCursor`: the replayer early exit.
       - `Config`, `ErrorCode`.
     - Tests (364 added): `MasterTransferTargetTest`, `ShutdownGateTest`, 
`FEOpExecutorHoldTest`.
   - **Before and after:** shutdown sequence on SIGTERM.
   
     | Step | Before | After |
     |---|---|---|
     | 1 | Wait up to 300 s for all queries (new ones keep arriving on existing 
connections). | Wait for the queries running at SIGTERM. Existing connections 
keep working, so their clients see no error. |
     | 2 | Stop servers and exit as master. | Pick transfer targets. Each alive 
follower is probed with an FE heartbeat (2 s timeout). Only followers that 
answer, and whose replayed journal id is within 
`master_transfer_on_shutdown_max_journal_lag` of the master's, qualify. A 
frozen or lagging follower is left out. |
     | 3 | Followers wait for the BDB JE heartbeat timeout, then elect. | 
Refuse new statements (`ShutdownGate`) and close idle MySQL connections. MySQL 
clients get error 1053 `ERR_SERVER_SHUTDOWN` (SQLSTATE 08S01) and the 
connection closes; Arrow Flight SQL clients get `UNAVAILABLE`. A master with 
targets also refuses forwarded statements with a 1053 packet that says the 
statement did not run. |
     | 4 | – | Wait for the statements in flight. Before a handover, the wait 
is a sixth of `master_change_wait_timeout_second`; without a handover, it is 
the rest of the 300 s. |
     | 5 | – | `transferMaster(targets)`. Its timeout is cut so that this wait 
and the transfer end inside the hold window. |
     | 6 | – | On success, the state listener does not call `System.exit` for 
the MASTER→FOLLOWER transition. `System.exit` would block forever behind the 
running shutdown hook. The process stays up for 
`master_transfer_on_shutdown_settle_ms`, so late forwards get the fast refusal, 
then exits without stopping its servers one by one. On failure, the normal 
shutdown runs. |
   
     On the other FEs, `MasterOpExecutor` holds and resends a forwarded 
statement while the master changes, for up to 
`master_change_wait_timeout_second`. It does so only while no master has 
accepted the statement:
     - the departing master refused it;
     - the master cannot be reached (the client could not be borrowed);
     - this FE is not ready. This case holds for at most 5 s, counted from when 
the FE turned not ready, and not at all with `ignore_meta_check`.
   
     The hold has these limits:
     - A statement that may have run is never resent. Neither is one cancelled 
or killed while held.
     - Fixed-target forwards, such as KILL fan-out and config apply-to-all, are 
not held.
     - A refusal clears the pooled connections to the departing master.
   
     On the new master, the replayer that is being stopped no longer sleeps (up 
to 3 × 3 s) on a journal id the old master assigned but never committed 
(`BDBJournalCursor`).
   - **Configs and defaults** (all mutable):
   
     | Config | Default | Meaning |
     |---|---|---|
     | `enable_master_transfer_on_shutdown` | `true` | Hand mastership over on 
graceful shutdown |
     | `master_transfer_on_shutdown_timeout_second` | `30` | Upper bound of the 
`transferMaster` timeout; cut to fit the hold window (12 s with the defaults) |
     | `master_transfer_on_shutdown_max_journal_lag` | `1000` | Largest replay 
lag of a transfer target |
     | `master_transfer_on_shutdown_settle_ms` | `2000` | How long the old 
master stays up after the handover |
     | `enable_reject_new_statements_on_shutdown` | `true` | Refuse new 
statements after the first drain |
     | `master_change_wait_timeout_second` | `30` | How long an FE holds a 
forwarded statement while the master changes; `0` disables the hold |
   
   - **Impact on existing users:**
     - A graceful master restart no longer causes an election stall.
     - During a graceful shutdown of any FE, clients on its existing 
connections get error 1053 instead of having their new statements run, once the 
queries that were running at SIGTERM have finished. This is so that their pools 
reconnect elsewhere.
     - Forwarded statements may wait up to `master_change_wait_timeout_second` 
while the master changes, instead of failing at once.
     - During a rolling upgrade, an old FE that forwards to an upgraded master 
that is shutting down gets the 1053 error without a retry.
     - Setting `enable_master_transfer_on_shutdown=false`, 
`enable_reject_new_statements_on_shutdown=false` and 
`master_change_wait_timeout_second=0` restores the previous behaviour.
   - **Known limits:**
     - **Crashes are unaffected.** SIGKILL or a host failure still waits for 
the BDB JE heartbeat timeout and an election.
     - **Clients connected to the departing master** see one error each (a lost 
connection or 1053). A running master cannot turn into a follower that 
forwards, so those connections have to reconnect.
     - **A frozen follower still costs about 20 s.** It is left out of the 
targets, but `transferMaster` to the healthy follower takes about 20 s in every 
run. That looks like the JE election waiting on the frozen member; this was not 
confirmed in JE's code. It is still about 2.6× faster than before.
     - **Arrow Flight SQL** refusal is covered by code and unit-level reasoning 
only; no Flight client ran in the chaos tests.
   
   ### Testing
   
   - **FE unit tests:** `MasterTransferTargetTest` (5), `ShutdownGateTest` (8) 
and `FEOpExecutorHoldTest` (6) pass on this branch (`Tests run: 19, Failures: 
0, Errors: 0`), via `run-fe-ut.sh --run`, with the FE built from this branch.
   - **Chaos tests.** All runs use 3 FE followers, each in its own container, 
in non-cloud mode. Each scenario sends SIGTERM to the master FE at a random 
point under a continuous workload. Write pause is the longest stretch with no 
successful write. Failed ops are counted per run. Numbers are median (max) over 
the runs.
   
   **1. This branch (master) vs master without it.** Metadata-only workload: 8 
pooled MySQL connections, each running CREATE DATABASE / ALTER DATABASE / DROP 
DATABASE / SHOW DATABASES and reconnecting to a healthy FE on error. No BE. 3 
runs per scenario.
   
   | Scenario | master: write pause / failed ops | This PR: write pause / 
failed ops |
   |---|---|---|
   | SIGTERM master | 82.1 s (100.8) / 1746 (1841) | **1.3 s (6.4) / 8 (11)** |
   | Frozen follower (SIGSTOP), then SIGTERM master | 89.1 s (97.2) / 5860 
(8155) | **35.0 s (37.5) / 8 (8)** |
   
   Old master exit after SIGTERM: 60.4 s on master vs 2.4 s with this PR. A new 
master was serving after 86.3 s vs 1.2 s. At the end of every run, all three 
FEs listed the same databases, and no FE hung.
   
   **2. The same change on a 4.1 build.** Mixed workload (INSERT, SELECT and 
DDL on 8 pooled connections), 1 BE, 5 runs per scenario.
   
   | Scenario | Without the change: pause / failed ops | With the change: pause 
/ failed ops |
   |---|---|---|
   | SIGTERM master | 97.5 s (101.5) / 847 | 2.1 s (21.4) / 8 (10) |
   | Frozen follower, then SIGTERM master | 93.9 s / 3915 | 35.3 s (37.6) / 16 
(16) |
   | Back-to-back SIGTERM (master, then the new master) | 96.3 s / 6458 | 13.5 
s (67.7) / 16 |
   | SIGTERM follower ¹ | 0.9 s / 0 | 1.6 s / 8 |
   | SIGKILL master ¹ | 34.5 s / 16 | 33.4 s / 16 |
   
   ¹ These two rows come from an earlier revision of this change. It differed 
only in how forwarded statements are held, and in the follower's last drain.
   
   Every acknowledged insert was present at the end and again 60 s later, and 
no FE hung in any run.
   
   **3. Stream Load with two-phase commit on the 4.1 build.** 8 writers, each 
doing `_stream_load` with `two_phase_commit: true` (300 rows, unique label), 
then `_stream_load_2pc` commit; each retries its label on error and gives up 
after 30 s. 5 runs per scenario. Pause is the longest gap between successful 
commits; failed loads and failed transactions are per run.
   
   | Scenario | Without: pause / failed loads / failed txns | With: pause / 
failed loads / failed txns |
   |---|---|---|
   | SIGTERM master | 43.8 s (79.0) / 64 (110) / 2 (8) | 4.1 s (6.3) / 1 (8) / 
0 (0) |
   | Back-to-back SIGTERM | 51.2 s (76.1) / 114 (156) / 2 (9) | 8.2 s (11.0) / 
7 (9) / 0 (0) |
   | Frozen follower, then SIGTERM master | 74.6 s (86.5) / 74 (114) / 8 (8) | 
23.8 s (29.2) / 1 (3) / 0 (0) |
   | SIGKILL master | 18.7 s (19.7) / 14 (15) / 0 | 22.0 s (23.8) / 13 (16) / 0 
|
   
   Every committed transaction had all its rows, and no label had duplicate 
rows. With the change, `begin` absorbs part of the wait: transactions begun 
during the transfer block, because JE blocks writes while `transferMaster` runs.
   
   ### Notes for Reviewers
   
   - Start with the shutdown hook in `DorisFE` and `ShutdownGate`, then 
`Env.transferMasterOnShutdown` / `chooseReadyMasterTransferTargets`, then 
`FEOpExecutor.forward`.
   - **Why the old master exits right after the handover instead of serving 
on.** After `transferMaster`, the old master's catalog no longer follows the 
journal. Any journal write it attempts fails with `ReplicaWriteException`, and 
`BDBJEJournal.write` then calls `System.exit`, which blocks during the shutdown 
hook. As a result:
     - such a writer never returns, so no client gets an acknowledgement for 
state that is not in the journal;
     - the in-memory change stays under the `DatabaseTransactionMgr` write lock 
the writer holds, so no other thread sees it.
   
     An earlier variant that failed such writes fast did leak 
acknowledged-but-lost writes, so it was dropped.
   - The 1053 packet a departing master returns to a forward is recognised by 
the error code and message, so an older FE simply shows it to the client.
   
   ### Release note
   
   A master FE that is shut down gracefully (SIGTERM) now hands its mastership 
to a ready follower before it exits. Other FEs hold forwarded statements while 
the master changes, so a planned master restart no longer causes a long write 
stall. New mutable FE configs: `enable_master_transfer_on_shutdown`, 
`master_transfer_on_shutdown_timeout_second`, 
`master_transfer_on_shutdown_max_journal_lag`, 
`master_transfer_on_shutdown_settle_ms`, 
`enable_reject_new_statements_on_shutdown`, `master_change_wait_timeout_second`.
   
   ### Check List (For Author)
   
   - Behavior changed: Yes (see Details)
   - Does this need documentation: Yes, the new FE configs and the 
graceful-shutdown behaviour of the FE; a doris-website PR will follow once the 
design is agreed.
   - Pre-submission status: Partially verified: no regression-test suite was 
run, and Arrow Flight SQL refusal was not exercised by a client.
   
   ### AI Disclosure
   
   - Tool: Claude Code (Claude Opus)
   - Generated: code, tests, the chaos harness and this description
   - Human review: the author set the goals, constraints and test plan.
   


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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to