Skip to content

Commit 9e98d36

Browse files
authored
[improvement](mtmv) Journal MV alter ops under the MV lock and read IVM info lock-free (apache#69256)
Two lock/journaling gaps in the MTMV metadata path. `ALTER_REFRESH_INFO` and `ALTER_STATUS` changed the MV metadata under the MV write lock but enqueued the matching journal record only after that lock was released (`Alter.processAlterMTMV`). A task result journaled in between could be replayed on the other side of the alter, and a live `ALTER_STATUS` could lose its record if a later step threw after the metadata had already moved. `MTMV.getIvmInfo()` took the MV write lock on the query path (`isIvm()` reads it). `enableIvm` is decided at CREATE and no ALTER path changes it, and the only other mutable field, the sequence prefix, is read when a refresh plans and advanced when the same refresh's transaction commits -- both under the MV's `MTMVJob` write lock, which serializes refreshes of one MV. Changes: - Apply `ALTER_REFRESH_INFO` / `ALTER_STATUS` and submit their journal record under one MV write lock; `processAlterMTMV` awaits the entry after the lock is released, like the other journaled MTMV changes. - Make `getIvmInfo()` a lock-free read and mark the payload reference `volatile`, so a replay that replaces it publishes it safely. `isIvm()` and `getNextSequencePrefix()` no longer queue for the MV write lock. - Correct the `MTMVTask` comment that claimed base table snapshots were captured under a read lock: they are captured before the read, without one. - The remaining item of the same list, the `addTaskResult` race on `getPartitionNames()`, was fixed separately by apache#69166.
1 parent 886cb3e commit 9e98d36

3 files changed

Lines changed: 78 additions & 25 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/alter/Alter.java‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
import org.apache.doris.persist.AlterMTMV;
9696
import org.apache.doris.persist.AlterViewInfo;
9797
import org.apache.doris.persist.BatchModifyPartitionsInfo;
98+
import org.apache.doris.persist.EditLog.EditLogItem;
9899
import org.apache.doris.persist.ModifyCommentOperationLog;
99100
import org.apache.doris.persist.ModifyPartitionInfo;
100101
import org.apache.doris.persist.ModifyTableEngineOperationLog;
@@ -1357,15 +1358,20 @@ public AlterHandler getSystemHandler() {
13571358
public void processAlterMTMV(AlterMTMV alterMTMV, boolean isReplay) {
13581359
TableNameInfo tbl = alterMTMV.getMvName();
13591360
MTMV mtmv = null;
1361+
// ALTER_REFRESH_INFO and ALTER_STATUS journal themselves from inside the MV write lock that
1362+
// applies them: mutation and enqueue have to be the same step, or a record submitted in
1363+
// between can be replayed on either side of this one. Their journal write is awaited after the
1364+
// lock is released, like the other journaled MTMV changes.
1365+
EditLogItem editLogItem = null;
13601366
try {
13611367
Database db = Env.getCurrentInternalCatalog().getDbOrDdlException(tbl.getDb());
13621368
mtmv = (MTMV) db.getTableOrMetaException(tbl.getTbl(), TableType.MATERIALIZED_VIEW);
13631369
switch (alterMTMV.getOpType()) {
13641370
case ALTER_REFRESH_INFO:
1365-
mtmv.alterRefreshInfo(alterMTMV.getRefreshInfo());
1371+
editLogItem = mtmv.alterRefreshInfo(alterMTMV, isReplay);
13661372
break;
13671373
case ALTER_STATUS:
1368-
mtmv.alterStatus(alterMTMV.getStatus());
1374+
editLogItem = mtmv.alterStatus(alterMTMV, isReplay);
13691375
break;
13701376
case ALTER_PROPERTY:
13711377
// Live ALTER PROPERTY statements call processAlterMTMVProperty directly
@@ -1401,8 +1407,8 @@ public void processAlterMTMV(AlterMTMV alterMTMV, boolean isReplay) {
14011407
if (alterMTMV.isNeedRebuildJob()) {
14021408
Env.getCurrentEnv().getMtmvService().alterJob(mtmv, isReplay);
14031409
}
1404-
if (!isReplay) {
1405-
Env.getCurrentEnv().getEditLog().logAlterMTMV(alterMTMV);
1410+
if (editLogItem != null) {
1411+
editLogItem.await();
14061412
}
14071413
} catch (UserException e) {
14081414
// if MTMV has been dropped, ignore this exception

‎fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java‎

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public class MTMV extends OlapTable {
111111
@SerializedName("rs")
112112
private MTMVRefreshSnapshot refreshSnapshot = new MTMVRefreshSnapshot();
113113
@SerializedName("ii")
114-
private IvmInfo ivmInfo = new IvmInfo();
114+
private volatile IvmInfo ivmInfo = new IvmInfo();
115115
/**
116116
* The refresh epoch of every MV partition, keyed by MV partition name.
117117
*
@@ -223,13 +223,25 @@ public MTMVRelation getRelation() {
223223
}
224224
}
225225

226-
public MTMVRefreshInfo alterRefreshInfo(MTMVRefreshInfo newRefreshInfo) {
226+
/**
227+
* Applies an ALTER_REFRESH_INFO op and journals it under one MV write lock, so that the record
228+
* order is the order the change took effect in (see {@link #submitAlterLog}). The journal write
229+
* itself is awaited by the caller after the lock is released.
230+
*
231+
* @return the pending journal entry, or null on replay (which has nothing to record)
232+
*/
233+
public EditLogItem alterRefreshInfo(AlterMTMV alterMTMV, boolean isReplay) {
234+
EditLogItem editLogItem = null;
227235
writeMvLock();
228236
try {
229-
return refreshInfo.updateNotNull(newRefreshInfo);
237+
refreshInfo.updateNotNull(alterMTMV.getRefreshInfo());
238+
if (!isReplay) {
239+
editLogItem = submitAlterLog(alterMTMV);
240+
}
230241
} finally {
231242
writeMvUnlock();
232243
}
244+
return editLogItem;
233245
}
234246

235247
/**
@@ -238,8 +250,8 @@ public MTMVRefreshInfo alterRefreshInfo(MTMVRefreshInfo newRefreshInfo) {
238250
* the transparent rewrite serving rows from it.
239251
*
240252
* <p>This is the step that <em>applies</em> a change, not the one that records it -- it takes the MV
241-
* lock and moves all three, but nothing here is journaled. It is what {@code Alter#processAlterMTMV}
242-
* calls for an {@code ALTER_STATUS} op, both live and on replay; a live caller reaches it through
253+
* lock and moves all three, but nothing here is journaled. {@code Alter#processAlterMTMV} uses the
254+
* overload that journals under the same lock; a live invalidation reaches it through
243255
* {@link #invalidateWholeMv}, which goes the journaled way round. A new invalidation belongs there:
244256
* calling this directly would leave the MV in a state a restart forgets. The one caller that goes
245257
* direct is {@link #processBaseViewChange}, and it is not new: the alter or the drop of the view is
@@ -252,13 +264,40 @@ public MTMVRefreshInfo alterRefreshInfo(MTMVRefreshInfo newRefreshInfo) {
252264
public MTMVStatus alterStatus(MTMVStatus newStatus) {
253265
writeMvLock();
254266
try {
255-
// only can update state, refresh state will be change by add task
256-
this.schemaChangeVersion++;
257-
this.refreshSnapshot = new MTMVRefreshSnapshot();
258-
return this.status.updateStateAndDetail(newStatus);
267+
return applyStatus(newStatus);
268+
} finally {
269+
writeMvUnlock();
270+
}
271+
}
272+
273+
/**
274+
* Applies an ALTER_STATUS op and journals it under one MV write lock. An ALTER_STATUS is applied
275+
* together with what it stands for -- the version bump and the snapshot drop -- and the record has
276+
* to describe that same step, not a later one: a result journaled in between would be replayed on
277+
* either side of it.
278+
*
279+
* @return the pending journal entry, or null on replay (which has nothing to record)
280+
*/
281+
public EditLogItem alterStatus(AlterMTMV alterMTMV, boolean isReplay) {
282+
EditLogItem editLogItem = null;
283+
writeMvLock();
284+
try {
285+
applyStatus(alterMTMV.getStatus());
286+
if (!isReplay) {
287+
editLogItem = submitAlterLog(alterMTMV);
288+
}
259289
} finally {
260290
writeMvUnlock();
261291
}
292+
return editLogItem;
293+
}
294+
295+
// Assumes the MV write lock is held.
296+
private MTMVStatus applyStatus(MTMVStatus newStatus) {
297+
// only can update state, refresh state will be change by add task
298+
this.schemaChangeVersion++;
299+
this.refreshSnapshot = new MTMVRefreshSnapshot();
300+
return this.status.updateStateAndDetail(newStatus);
262301
}
263302

264303
/**
@@ -757,16 +796,21 @@ public boolean hasRefreshSnapshot() {
757796
}
758797
}
759798

799+
/**
800+
* The MV's IVM metadata, readable without the MV lock.
801+
*
802+
* <p>{@code isIvm()} is on the query path, and what it reads is fixed for the MV's lifetime:
803+
* {@code enableIvm} is decided at CREATE and no ALTER path changes it. The rest of the payload is
804+
* touched only by the refresh of this MV -- the sequence prefix is read when a refresh plans and
805+
* advanced when its transaction commits, and the plan signature is published by the same task's
806+
* result -- and one MV's tasks run one at a time, under its job's write lock (see
807+
* {@code MTMVTask#runTask}). So no reader here races a writer.
808+
*
809+
* <p>Never null: the field is created with the MV, and {@link #gsonPostProcess} covers an image
810+
* that carries it as null.
811+
*/
760812
public IvmInfo getIvmInfo() {
761-
writeMvLock();
762-
try {
763-
if (ivmInfo == null) {
764-
ivmInfo = new IvmInfo();
765-
}
766-
return ivmInfo;
767-
} finally {
768-
writeMvUnlock();
769-
}
813+
return ivmInfo;
770814
}
771815

772816
// ALTER_IVM_INFO replay applies a detached snapshot here. Live IVM changes submit their journal

‎fe/fe-core/src/main/java/org/apache/doris/job/extensions/mtmv/MTMVTask.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -893,9 +893,12 @@ private IvmIncrRefreshResult executeSingleIvmAttempt(MTMVRefreshContext refreshC
893893
return IvmIncrRefreshResult.success();
894894
}
895895
IvmIncrRefreshManager ivmIncrRefreshManager = new IvmIncrRefreshManager();
896-
// Capture base table snapshots under read lock before execution, same as
897-
// partition-based refresh. This ensures snapshot versions are consistent
898-
// with the data the INSERT will read.
896+
// Capture the base table snapshots before execution, same as the partition-based path. No
897+
// base table read lock is held: what the record must guarantee is that it is no newer than
898+
// the data the INSERT reads, and capturing first gives exactly that -- a version committed
899+
// in between is simply missing from the record, which leaves the MV looking less fresh than
900+
// it is. Partitions added or dropped in the same window are reconciled by the refresh's own
901+
// partition sync, not by this record.
899902
Map<String, MTMVRefreshPartitionSnapshot> capturedSnapshots;
900903
try {
901904
capturedSnapshots = MTMVPartitionUtil.generatePartitionSnapshots(

0 commit comments

Comments
 (0)