Skip to content

Commit e217ae0

Browse files
wangzhigang1999pan3793
authored andcommitted
[KYUUBI #7389] Fix wrong parameter binding order in JDBCMetadataStore#transformMetadataState
Closes #7389 The SQL placeholders expect (targetState, identifier, fromState) but the code passed (fromState, identifier, targetState), causing the UPDATE to set state back to fromState and match on targetState instead. This made cancelUnscheduledBatch silently fail. ### Why are the changes needed? In `JDBCMetadataStore#transformMetadataState`, the SQL is: ```sql UPDATE metadata SET state = ? WHERE identifier = ? AND state = ? ``` The placeholders expect `(targetState, identifier, fromState)`, but the code passes `(fromState, identifier, targetState)`: ```scala withUpdateCount(connection, query, fromState, identifier, targetState) ``` This causes `cancelUnscheduledBatch("INITIALIZED" -> "CANCELED")` to execute: ```sql UPDATE metadata SET state = 'INITIALIZED' WHERE identifier = ? AND state = 'CANCELED' ``` Which matches no rows and silently returns `false`. The bug has existed since v1.8.0 ([KYUUBI #4790]). ### How was this patch tested? Added a unit test `transformMetadataState should transition state correctly` in `JDBCMetadataStoreSuite` that: 1. Inserts a metadata record with `state = "INITIALIZED"` 2. Calls `transformMetadataState(id, "INITIALIZED", "CANCELED")` 3. Asserts the method returns `true` 4. Asserts the persisted state is `"CANCELED"` ### Was this patch authored or co-authored using generative AI tooling? No. Closes #7390 from wangzhigang1999/fix/transformMetadataState-param-order. Closes #7389 e3142a5 [wangzhigang] [KYUUBI] Fix wrong parameter binding order in JDBCMetadataStore#transformMetadataState Authored-by: wangzhigang <wzg443064@alibaba-inc.com> Signed-off-by: Cheng Pan <chengpan@apache.org> (cherry picked from commit bb67bd6) Signed-off-by: Cheng Pan <chengpan@apache.org>
1 parent ae4fba4 commit e217ae0

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

kyuubi-server/src/main/scala/org/apache/kyuubi/server/metadata/jdbc/JDBCMetadataStore.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class JDBCMetadataStore(conf: KyuubiConf) extends MetadataStore with Logging {
237237
targetState: String): Boolean = {
238238
val query = s"UPDATE $METADATA_TABLE SET state = ? WHERE identifier = ? AND state = ?"
239239
JdbcUtils.withConnection { connection =>
240-
withUpdateCount(connection, query, fromState, identifier, targetState) { updateCount =>
240+
withUpdateCount(connection, query, targetState, identifier, fromState) { updateCount =>
241241
updateCount == 1
242242
}
243243
}

kyuubi-server/src/test/scala/org/apache/kyuubi/server/metadata/jdbc/JDBCMetadataStoreSuite.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,32 @@ class JDBCMetadataStoreSuite extends KyuubiFunSuite {
244244
}
245245
}
246246

247+
test("transformMetadataState should transition state correctly") {
248+
val batchId = UUID.randomUUID().toString
249+
val batchMetadata = Metadata(
250+
identifier = batchId,
251+
sessionType = SessionType.BATCH,
252+
realUser = "kyuubi",
253+
username = "kyuubi",
254+
ipAddress = "127.0.0.1",
255+
state = "INITIALIZED",
256+
resource = "intern",
257+
className = "org.apache.kyuubi.SparkWC",
258+
requestName = "test_transform",
259+
createTime = System.currentTimeMillis(),
260+
engineType = "SPARK")
261+
262+
jdbcMetadataStore.insertMetadata(batchMetadata)
263+
264+
val result = jdbcMetadataStore.transformMetadataState(batchId, "INITIALIZED", "CANCELED")
265+
assert(result, "should successfully transition from INITIALIZED to CANCELED")
266+
267+
val metadata = jdbcMetadataStore.getMetadata(batchId)
268+
assert(metadata.state == "CANCELED", s"state should be CANCELED but was ${metadata.state}")
269+
270+
jdbcMetadataStore.cleanupMetadataByIdentifier(batchId)
271+
}
272+
247273
test("throw exception if update count is 0") {
248274
val metadata = Metadata(identifier = UUID.randomUUID().toString, state = "RUNNING")
249275
intercept[KyuubiException] {

0 commit comments

Comments
 (0)