|
17 | 17 |
|
18 | 18 | package org.apache.doris.connector; |
19 | 19 |
|
| 20 | +import org.apache.doris.catalog.TableIf; |
20 | 21 | import org.apache.doris.connector.spi.ConnectorSession; |
21 | 22 | import org.apache.doris.connector.spi.ConnectorStatementScope; |
22 | 23 | import org.apache.doris.connector.spi.ConnectorWriteOps; |
|
40 | 41 | import java.util.ArrayList; |
41 | 42 | import java.util.Arrays; |
42 | 43 | import java.util.List; |
| 44 | +import java.util.concurrent.TimeUnit; |
43 | 45 | import java.util.concurrent.atomic.AtomicInteger; |
44 | 46 | import java.util.function.Supplier; |
45 | 47 |
|
@@ -156,22 +158,44 @@ public void planningValueMustHoldNothingToClose() { |
156 | 158 | } |
157 | 159 |
|
158 | 160 | @Test |
159 | | - public void statementContextEndsThePlanningOfItsScope() { |
160 | | - // NereidsPlanner.plan ends connector planning through the statement context; a statement no connector has |
161 | | - // touched has no scope to end. MUTATION: endConnectorPlanning not reaching the scope -> red. |
| 161 | + public void releasingPlannerResourcesEndsThePlanningOfTheScope() { |
| 162 | + // NereidsPlanner.planWithLock releases the planner resources when planning is over, which ends connector |
| 163 | + // planning; a statement no connector has touched has no scope to end. MUTATION: releasePlannerResources not |
| 164 | + // ending connector planning -> red. |
162 | 165 | StatementContext ctx = new StatementContext(); |
163 | | - ctx.endConnectorPlanning(); |
| 166 | + ctx.releasePlannerResources(); |
164 | 167 | ConnectorStatementScope scope = ctx.getOrCreateConnectorStatementScope(); |
165 | 168 | Object memo = scope.computeIfAbsentForPlanning("memo", Object::new); |
166 | 169 | Object table = scope.computeIfAbsent("table", Object::new); |
167 | 170 |
|
168 | | - ctx.endConnectorPlanning(); |
| 171 | + ctx.releasePlannerResources(); |
169 | 172 |
|
170 | 173 | Assertions.assertSame(scope, ctx.getOrCreateConnectorStatementScope(), "ending planning keeps the scope"); |
171 | 174 | Assertions.assertSame(table, scope.computeIfAbsent("table", Object::new)); |
172 | 175 | Assertions.assertNotSame(memo, scope.computeIfAbsentForPlanning("memo", Object::new)); |
173 | 176 | } |
174 | 177 |
|
| 178 | + @Test |
| 179 | + public void releasingPlannerResourcesEndsPlanningWhenAResourceFailsToClose() { |
| 180 | + // A table lock that fails to unlock still reaches the caller, after the planning values are gone. |
| 181 | + // MUTATION: ending connector planning only after the release succeeded -> the memo survives -> red. |
| 182 | + StatementContext ctx = new StatementContext(); |
| 183 | + ConnectorStatementScope scope = ctx.getOrCreateConnectorStatementScope(); |
| 184 | + Object memo = scope.computeIfAbsentForPlanning("memo", Object::new); |
| 185 | + TableIf table = Mockito.mock(TableIf.class); |
| 186 | + Mockito.when(table.needReadLockWhenPlan()).thenReturn(true); |
| 187 | + Mockito.when(table.tryReadLock(Mockito.anyLong(), Mockito.any(TimeUnit.class))).thenReturn(true); |
| 188 | + Mockito.when(table.getNameWithFullQualifiers()).thenReturn("internal.db.t"); |
| 189 | + IllegalStateException unlockFailure = new IllegalStateException("unlock failed"); |
| 190 | + Mockito.doThrow(unlockFailure).when(table).readUnlock(); |
| 191 | + ctx.getTables().put(Arrays.asList("internal", "db", "t"), table); |
| 192 | + ctx.lock(); |
| 193 | + |
| 194 | + Assertions.assertSame(unlockFailure, |
| 195 | + Assertions.assertThrows(IllegalStateException.class, ctx::releasePlannerResources)); |
| 196 | + Assertions.assertNotSame(memo, scope.computeIfAbsentForPlanning("memo", Object::new)); |
| 197 | + } |
| 198 | + |
175 | 199 | @Test |
176 | 200 | public void statementContextScopeIsStableThenResetsForReusedPreparedContext() { |
177 | 201 | // StatementContext lazily builds one scope and reuses it across the statement (getOrCreate...). A prepared |
|
0 commit comments