|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.job.extensions.insert.streaming; |
| 19 | + |
| 20 | +import org.apache.doris.common.jmockit.Deencapsulation; |
| 21 | +import org.apache.doris.job.offset.Offset; |
| 22 | +import org.apache.doris.job.offset.SourceOffsetProvider; |
| 23 | +import org.apache.doris.nereids.trees.plans.commands.insert.InsertIntoTableCommand; |
| 24 | + |
| 25 | +import org.junit.Assert; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +/** |
| 32 | + * Covers the bounded admission loop in {@link StreamingInsertJob#advanceSplitsIfNeed()}: |
| 33 | + * it must keep admitting within one tick yet terminate on every in-loop exit condition |
| 34 | + * (noMoreSplits / pending cap / no-progress round) without spinning. The deadline term is |
| 35 | + * wall-clock based and is covered by integration/regression rather than here. |
| 36 | + */ |
| 37 | +public class StreamingInsertJobAdvanceSplitsTest { |
| 38 | + |
| 39 | + // Assumed value of StreamingInsertJob.MAX_PENDING_SPLITS; update both together if changed. |
| 40 | + private static final int CAP = 512; |
| 41 | + |
| 42 | + private static StreamingInsertJob newJob(SourceOffsetProvider provider, long maxIntervalSec) { |
| 43 | + StreamingInsertJob job = Deencapsulation.newInstance(StreamingInsertJob.class); |
| 44 | + Map<String, String> props = new HashMap<>(); |
| 45 | + props.put(StreamingJobProperties.MAX_INTERVAL_SECOND_PROPERTY, String.valueOf(maxIntervalSec)); |
| 46 | + Deencapsulation.setField(job, "jobProperties", new StreamingJobProperties(props)); |
| 47 | + Deencapsulation.setField(job, "offsetProvider", provider); |
| 48 | + return job; |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testLoopsUntilNoMoreSplits() throws Exception { |
| 53 | + // Many small tables: one split per round, splitting completes after 5 rounds. |
| 54 | + FakeOffsetProvider provider = new FakeOffsetProvider(1, 5); |
| 55 | + newJob(provider, 3600).advanceSplitsIfNeed(); |
| 56 | + Assert.assertEquals("must keep admitting across rounds until noMoreSplits", 5, provider.rounds); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testStopsAtPendingCap() throws Exception { |
| 61 | + // Fast producer that never finishes: must stop once the FE backlog cap is crossed. |
| 62 | + FakeOffsetProvider provider = new FakeOffsetProvider(100, -1); |
| 63 | + newJob(provider, 3600).advanceSplitsIfNeed(); |
| 64 | + Assert.assertTrue("must stop once pending crosses the cap", provider.pendingSplitCount() >= CAP); |
| 65 | + Assert.assertEquals("must not overshoot beyond one round past the cap", 6, provider.rounds); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testBreaksWhenRoundProducesNothing() throws Exception { |
| 70 | + // A round that yields no new split (empty RPC / cursor moved) must break, not spin to deadline. |
| 71 | + FakeOffsetProvider provider = new FakeOffsetProvider(0, -1); |
| 72 | + newJob(provider, 3600).advanceSplitsIfNeed(); |
| 73 | + Assert.assertEquals("must break after a no-progress round", 1, provider.rounds); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testSkipsWhenAlreadyDone() throws Exception { |
| 78 | + // Entry guard: noMoreSplits up front means the loop never runs. |
| 79 | + FakeOffsetProvider provider = new FakeOffsetProvider(1, 0); |
| 80 | + newJob(provider, 3600).advanceSplitsIfNeed(); |
| 81 | + Assert.assertEquals("entry guard must skip the loop entirely", 0, provider.rounds); |
| 82 | + } |
| 83 | + |
| 84 | + /** Minimal provider whose admission behaviour is fully controllable. */ |
| 85 | + private static class FakeOffsetProvider implements SourceOffsetProvider { |
| 86 | + private int pending; |
| 87 | + private int rounds; |
| 88 | + private final int stepPerRound; // pending increment per advanceSplits call |
| 89 | + private final int roundsUntilDone; // noMoreSplits() flips true after this many rounds (-1 = never) |
| 90 | + |
| 91 | + FakeOffsetProvider(int stepPerRound, int roundsUntilDone) { |
| 92 | + this.stepPerRound = stepPerRound; |
| 93 | + this.roundsUntilDone = roundsUntilDone; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void advanceSplits() { |
| 98 | + rounds++; |
| 99 | + pending += stepPerRound; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public int pendingSplitCount() { |
| 104 | + return pending; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public boolean noMoreSplits() { |
| 109 | + return roundsUntilDone >= 0 && rounds >= roundsUntilDone; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String getSourceType() { |
| 114 | + return "fake"; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public Offset getNextOffset(StreamingJobProperties jobProps, Map<String, String> properties) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public String getShowCurrentOffset() { |
| 124 | + return ""; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public String getShowMaxOffset() { |
| 129 | + return ""; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public InsertIntoTableCommand rewriteTvfParams(InsertIntoTableCommand originCommand, Offset next, long id) { |
| 134 | + return originCommand; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void updateOffset(Offset offset) { |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void fetchRemoteMeta(Map<String, String> properties) { |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public boolean hasMoreDataToConsume() { |
| 147 | + return true; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public Offset deserializeOffset(String offset) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public Offset deserializeOffsetProperty(String offset) { |
| 157 | + return null; |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments