Skip to content

Commit bb67bd6

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>
1 parent f9f0b1a commit bb67bd6

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
@@ -235,7 +235,7 @@ class JDBCMetadataStore(conf: KyuubiConf) extends MetadataStore with Logging {
235235
targetState: String): Boolean = {
236236
val query = s"UPDATE $METADATA_TABLE SET state = ? WHERE identifier = ? AND state = ?"
237237
JdbcUtils.withConnection { connection =>
238-
withUpdateCount(connection, query, fromState, identifier, targetState) { updateCount =>
238+
withUpdateCount(connection, query, targetState, identifier, fromState) { updateCount =>
239239
updateCount == 1
240240
}
241241
}

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
@@ -248,6 +248,32 @@ class JDBCMetadataStoreSuite extends KyuubiFunSuite {
248248
}
249249
}
250250

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

0 commit comments

Comments
 (0)