wangzhigang1999 opened a new issue, #7389:
URL: https://github.com/apache/kyuubi/issues/7389

   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   
   
   ### Search before asking
   
   - [x] I have searched in the 
[issues](https://github.com/apache/kyuubi/issues?q=is%3Aissue) and found no 
similar issues.
   
   
   ### Describe the bug
   
   ### Description
   
   In `JDBCMetadataStore#transformMetadataState`, the SQL parameter binding 
order is swapped, causing the method to silently fail during state transitions.
   
   ### Root Cause
   
   The SQL query is defined as:
   ```sql
   UPDATE metadata SET state = ? WHERE identifier = ? AND state = ?
   ```
   
   The placeholders expect parameters in the order: `(targetState, identifier, 
fromState)`.
   
   However, the code currently passes them as:
   ```java
   withUpdateCount(connection, query, fromState, identifier, targetState) { ... 
}
   ```
   
   This results in the following incorrect behavior:
   1. **SET clause**: Sets `state` to `fromState` (the old state) instead of 
`targetState`.
   2. **WHERE clause**: Checks if `state` equals `targetState` (the new state) 
instead of `fromState`.
   
   This effectively reverses the intended Compare-And-Swap (CAS) logic.
   
   ### Impact
   
   For example, when `MetadataManager#cancelUnscheduledBatch` calls:
   ```java
   transformMetadataState(batchId, "INITIALIZED", "CANCELED")
   ```
   
   The executed SQL becomes:
   ```sql
   UPDATE metadata 
   SET state = 'INITIALIZED' 
   WHERE identifier = ? AND state = 'CANCELED'
   ```
   
   Since the record is currently in the `INITIALIZED` state, the `WHERE state = 
'CANCELED'` condition matches **0 rows**. Consequently:
   - The update does not happen.
   - The method returns `false`.
   - The cancel request fails silently without any exception or error log.
   
   ### Steps to Reproduce
   
   1. Insert a metadata record with `state = "INITIALIZED"`.
   2. Call `transformMetadataState(id, "INITIALIZED", "CANCELED")`.
   3. Observe that the method returns `false`.
   4. Query the database record: the state remains `"INITIALIZED"`.
   
   ### Expected Behavior
   
   The parameters should be bound as `(targetState, identifier, fromState)`. 
The SQL should execute as:
   ```sql
   UPDATE metadata 
   SET state = 'CANCELED' 
   WHERE identifier = ? AND state = 'INITIALIZED'
   ```
   This should successfully update the state and return `true` (or a positive 
update count).
   
   ### Proposed Fix
   
   Swap the first and last arguments in the `withUpdateCount` call within 
`transformMetadataState`:
   
   ```java
   // Current (Incorrect)
   withUpdateCount(connection, query, fromState, identifier, targetState)
   
   // Fixed
   withUpdateCount(connection, query, targetState, identifier, fromState)
   ```
   
   ### Affects Version(s)
   
   1.8.0
   
   ### Kyuubi Server Log Output
   
   ```logtalk
   
   ```
   
   ### Kyuubi Engine Log Output
   
   ```logtalk
   
   ```
   
   ### Kyuubi Server Configurations
   
   ```yaml
   
   ```
   
   ### Kyuubi Engine Configurations
   
   ```yaml
   
   ```
   
   ### Additional context
   
   _No response_
   
   ### Are you willing to submit PR?
   
   - [ ] Yes. I would be willing to submit a PR with guidance from the Kyuubi 
community to fix.
   - [ ] No. I cannot submit a PR at this time.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to