Skip to content

Commit 1c28718

Browse files
committed
[fix](fe) Split the auto-detect group's bookkeeping out of the table lock
Two follow-ups from the previous commit. The task-group publication ran all of its bookkeeping under the target table's write lock, which the wrapper holds: that bookkeeping writes an edit-log entry for every task (an ADD and a DROP each), and with the default journal durability (SYNC/SYNC/SIMPLE_MAJORITY) every one of them waits for a replica acknowledgement, so a PARTITION(*) overwrite spanning many partitions kept readers and writers of the target blocked through all of those waits. And the unit test added for the dropped-target branch still called the publication method with its pre-refactor signature, so its reflective call never reached the branch it was written for. Key changes: - taskGroupSuccess is split into the replacement and the bookkeeping. The overwrite calls the replacement under its last-chance lock and the bookkeeping after it; the owner-side entry point keeps calling the composed method, so that path is unchanged. - The dropped-target test calls the current method with a publication callback and asserts the callback was not reached. Unit Test: - InsertOverwriteDroppedTargetTest#targetDroppedBeforeTheSwapFailsTheOverwrite (Tests run: 1, Failures: 0, Errors: 0) - test_insert_overwrite_cancel, test_iot_auto_detect, test_iot_auto_detect_fail, test_iot_auto_detect_concurrent and test_iot_overwrite_and_create pass.
1 parent b2f346c commit 1c28718

5 files changed

Lines changed: 65 additions & 6 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/insertoverwrite/AbstractInsertOverwriteManager.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ public interface AbstractInsertOverwriteManager {
3333

3434
void taskGroupSuccess(long groupId, OlapTable targetTable, boolean forceDropPartition) throws DdlException;
3535

36+
/**
37+
* The replacement half of {@link #taskGroupSuccess}: makes every replacement the group recorded visible.
38+
* A caller that publishes the group itself calls this and then {@link #finishTaskGroup}, so that the task
39+
* bookkeeping -- an edit-log write per task, each waiting for its journal -- runs outside whatever lock the
40+
* caller holds the replacement under. A manager whose owning frontend runs the whole sequence behind one
41+
* call replaces there and finishes there.
42+
*/
43+
void replacePartitionsOfTaskGroup(long groupId, OlapTable targetTable, boolean forceDropPartition)
44+
throws DdlException;
45+
46+
/** The bookkeeping half of {@link #taskGroupSuccess}, after the replacement it follows. */
47+
void finishTaskGroup(long groupId);
48+
3649
void taskSuccess(long taskId) throws Exception;
3750

3851
void taskGroupFail(long groupId) throws Exception;

‎fe/fe-core/src/main/java/org/apache/doris/insertoverwrite/InsertOverwriteManager.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,21 @@ public void taskGroupFail(long groupId) {
191191
// here we will make all raplacement of this group visiable. if someone fails, nothing happen.
192192
@Override
193193
public void taskGroupSuccess(long groupId, OlapTable targetTable, boolean forceDropPartition) throws DdlException {
194+
replacePartitionsOfTaskGroup(groupId, targetTable, forceDropPartition);
195+
finishTaskGroup(groupId);
196+
}
197+
198+
/**
199+
* The replacement half of {@link #taskGroupSuccess}: makes every replacement this group recorded visible.
200+
*
201+
* <p>Separate from the bookkeeping that follows it because the caller holds the target table's write lock
202+
* for the last cancellation check before it publishes: the bookkeeping writes an edit-log entry per task,
203+
* each of which waits for its journal to be acknowledged, and readers and writers of the target must not
204+
* stay blocked through those waits -- the utility's own replacement, which the lock is about, does not.
205+
*/
206+
@Override
207+
public void replacePartitionsOfTaskGroup(long groupId, OlapTable targetTable, boolean forceDropPartition)
208+
throws DdlException {
194209
try {
195210
Map<Long, Long> relations = partitionPairs.get(groupId);
196211
ArrayList<String> oldNames = new ArrayList<>();
@@ -205,6 +220,16 @@ public void taskGroupSuccess(long groupId, OlapTable targetTable, boolean forceD
205220
+ "all new partition will not be visible and will be recycled by partition GC.");
206221
throw e;
207222
}
223+
}
224+
225+
/**
226+
* The bookkeeping half of {@link #taskGroupSuccess}: makes the group's tasks final and forgets the group,
227+
* after the replacement has been made. See
228+
* {@link #replacePartitionsOfTaskGroup} for why it is not the replacement's caller to hold this under a
229+
* table lock.
230+
*/
231+
@Override
232+
public void finishTaskGroup(long groupId) {
208233
LOG.info("insert overwrite auto detect partition task group [" + groupId + "] succeed");
209234
for (Long taskId : taskGroups.get(groupId)) {
210235
Env.getCurrentEnv().getEditLog()

‎fe/fe-core/src/main/java/org/apache/doris/insertoverwrite/RemoteInsertOverwriteManager.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ public void taskGroupSuccess(long groupId, OlapTable targetTable, boolean forceD
6363
targetTable.getDatabase().getFullName(), targetTable.getName(), groupId, forceDropPartition);
6464
}
6565

66+
@Override
67+
public void replacePartitionsOfTaskGroup(long groupId, OlapTable targetTable, boolean forceDropPartition)
68+
throws DdlException {
69+
// The owning frontend runs the replacement and the bookkeeping behind this one call, so the caller
70+
// does not hold its lock across them; finishTaskGroup has nothing left to do here.
71+
taskGroupSuccess(groupId, targetTable, forceDropPartition);
72+
}
73+
74+
@Override
75+
public void finishTaskGroup(long groupId) {
76+
// Finished on the owning frontend, which the replacement call reached.
77+
}
78+
6679
@Override
6780
public void taskSuccess(long taskId) throws Exception {
6881
catalog.getFeServiceClient().taskSuccess(taskId);

‎fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertOverwriteTableCommand.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public class InsertOverwriteTableCommand extends Command
153153
* cancellation flag before letting one run.
154154
*/
155155
@FunctionalInterface
156-
private interface OverwritePublication {
156+
interface OverwritePublication {
157157
void publish() throws UserException;
158158
}
159159

@@ -317,9 +317,13 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
317317
// swap waits for the table lock, is taken up again by publishTheOverwrite below.
318318
throw cancelledBeforeTheRowsWereCommitted("after a load that committed nothing", ctx);
319319
}
320+
// The replacement stays under the lock publishTheOverwrite takes, and the group's bookkeeping
321+
// follows it: its edit-log writes wait for their journals, and the target's readers and
322+
// writers must not be blocked through them.
320323
publishTheOverwrite(targetTable, insertCtx, ctx,
321-
() -> insertOverwriteManager.taskGroupSuccess(groupId, (OlapTable) targetTable,
324+
() -> insertOverwriteManager.replacePartitionsOfTaskGroup(groupId, (OlapTable) targetTable,
322325
isForceDropPartition()));
326+
insertOverwriteManager.finishTaskGroup(groupId);
323327
} else {
324328
// it's overwrite table(as all partitions) or specific partition(s)
325329
List<String> tempPartitionNames = InsertOverwriteUtil.generateTempPartitionNames(partitionNames);

‎fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertOverwriteDroppedTargetTest.java‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
2424
import org.apache.doris.qe.ConnectContext;
2525

26-
import com.google.common.collect.Lists;
2726
import org.junit.jupiter.api.Assertions;
2827
import org.junit.jupiter.api.Test;
2928
import org.mockito.Mockito;
3029

3130
import java.util.Optional;
31+
import java.util.concurrent.atomic.AtomicBoolean;
3232

3333
/**
3434
* What the swap of an overwrite does when its target has been dropped underneath it.
@@ -42,7 +42,8 @@
4242
*
4343
* <p>Driven directly rather than through a statement because the window needs a DROP concurrent with the
4444
* swap, which a regression test cannot place without an injection point of its own; the condition the
45-
* branch reads -- {@code writeLockIfExist()} answering false -- is what the mock supplies.
45+
* branch reads -- {@code writeLockIfExist()} answering false -- is what the mock supplies, and the
46+
* publication it is handed records whether it was reached at all.
4647
*/
4748
public class InsertOverwriteDroppedTargetTest {
4849

@@ -55,15 +56,18 @@ public void targetDroppedBeforeTheSwapFailsTheOverwrite() {
5556
Mockito.when(droppedTarget.writeLockIfExist()).thenReturn(false);
5657
ConnectContext ctx = Mockito.mock(ConnectContext.class);
5758
Mockito.when(ctx.getQueryIdentifier()).thenReturn("stmt[1, query-id]");
59+
AtomicBoolean published = new AtomicBoolean(false);
5860

5961
UserException thrown = Assertions.assertThrows(UserException.class,
6062
() -> Deencapsulation.invoke(command, "publishTheOverwrite", droppedTarget,
61-
Lists.newArrayList("p1"), Lists.newArrayList("tp1"),
62-
new OlapInsertCommandContext(false, true), ctx));
63+
new OlapInsertCommandContext(false, true), ctx,
64+
(InsertOverwriteTableCommand.OverwritePublication) () -> published.set(true)));
6365
Assertions.assertTrue(thrown.getMessage() != null
6466
&& thrown.getMessage().contains("iot_dropped_target")
6567
&& thrown.getMessage().contains("was dropped"),
6668
"the failure has to name the table whose swap could not be issued, but was: "
6769
+ thrown.getMessage());
70+
Assertions.assertFalse(published.get(),
71+
"nothing may be published into a table whose write lock could not be taken");
6872
}
6973
}

0 commit comments

Comments
 (0)