Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] Fix transactional stream load with warehouse property not work rightly #56464

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wxl24life
Copy link
Contributor

Why I'm doing:

What I'm doing:

Fixes #issue

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.4
    • 3.3
    • 3.2
    • 3.1
    • 3.0

@wxl24life wxl24life requested review from a team as code owners March 3, 2025 03:00
}
throw new StarRocksException("No compute node alive in warehouse: " + warehouseId);
}

public Long seqChooseBackendOrComputeId() throws StarRocksException {
List<Long> backendIds = seqChooseBackendIds(1, true, false, null);
if (CollectionUtils.isNotEmpty(backendIds)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most risky bug in this code is:
A null pointer exception could be thrown if GlobalStateMgr.getCurrentState().getWarehouseMgr().getWarehouse(warehouseId) returns null.

You can modify the code like this:

public Long seqChooseComputeIdFromWarehouse(long warehouseId) throws StarRocksException {
    Warehouse warehouse = GlobalStateMgr.getCurrentState().getWarehouseMgr().getWarehouse(warehouseId);
    if (warehouse == null) {
        throw new StarRocksException("Warehouse not found for ID: " + warehouseId);
    }
    List<ComputeNode> aliveComputeNodes =
            GlobalStateMgr.getCurrentState().getWarehouseMgr().getAliveComputeNodes(warehouseId);
    if (CollectionUtils.isNotEmpty(aliveComputeNodes)) {
        List<Long> computeNodes = seqChooseNodeIds(1, false, null, aliveComputeNodes);
        if (CollectionUtils.isNotEmpty(computeNodes)) {
            return computeNodes.get(0);
        }
    }
    throw new StarRocksException("No compute node alive in warehouse: " + warehouseId);
}

// txnNodeMap is LRU cache, it atomic remove unused entry
accessTxnNodeMapWithWriteLock(txnNodeMap -> txnNodeMap.put(label, chosenNodeId));
accessTxnNodeMapWithWriteLock(txnNodeMap -> txnNodeMap.put(label, nodeId));
} else {
nodeId = accessTxnNodeMapWithReadLock(txnNodeMap -> txnNodeMap.get(label));
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most risky bug in this code is:
Potential NullPointerException when warehouseName is empty.

You can modify the code like this:

private Long getNodeId(TransactionOperation txnOperation, String label, String warehouseName) throws StarRocksException {
    Long nodeId = null;  // Initialize to null for safety
    // save label->be hashmap when begin transaction, so that subsequent operator can send to same BE
    if (TXN_BEGIN.equals(txnOperation)) {
        if (StringUtils.isNotEmpty(warehouseName)) {
            Warehouse warehouse = GlobalStateMgr.getCurrentState().getWarehouseMgr().getWarehouse(warehouseName);
            if (warehouse != null) {  // Check if the warehouse exists
                nodeId = GlobalStateMgr.getCurrentState().getNodeMgr()
                        .getClusterInfo().getNodeSelector().seqChooseComputeIdFromWarehouse(warehouse.getId());
            }
            // Handle case where warehouse is not found
            if (nodeId == null) {
                throw new StarRocksException("Warehouse " + warehouseName + " is not valid");
            }
        }
        if (nodeId == null) {  // If nodeId was not set, fall back to default behavior
            nodeId = GlobalStateMgr.getCurrentState().getNodeMgr()
                    .getClusterInfo().getNodeSelector().seqChooseBackendOrComputeId();
        }
        // txnNodeMap is LRU cache, it atomic remove unused entry
        accessTxnNodeMapWithWriteLock(txnNodeMap -> txnNodeMap.put(label, nodeId));
    } else {
        nodeId = accessTxnNodeMapWithReadLock(txnNodeMap -> txnNodeMap.get(label));
    }

    if (nodeId == null) {
        throw new StarRocksException("Could not obtain a valid node ID for label: " + label);
    }

    return nodeId;
}

Copy link

sonarqubecloud bot commented Mar 3, 2025

Copy link

github-actions bot commented Mar 3, 2025

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

github-actions bot commented Mar 3, 2025

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

return computeNodes.get(0);
}
}
throw new StarRocksException("No compute node alive in warehouse: " + warehouseId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new StarRocksException("No compute node alive in warehouse: " + warehouseId);
throw new StarRocksException("No compute node alive in warehouse: " + warehouse.getName());

@@ -250,7 +250,6 @@ public static Optional<Long> getWarehouseIdByNodeId(SystemInfoService systemInfo
LOG.warn("failed to get warehouse id by node id: {}", nodeId);
return Optional.empty();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated code change.

@@ -49,7 +49,8 @@ public ResultWrapper handle(BaseRequest request, BaseResponse response) throws S
Long timeoutMillis = txnOperationParams.getTimeoutMillis();
String label = txnOperationParams.getLabel();
Channel channel = txnOperationParams.getChannel();
LOG.info("Handle transaction with channel info, label: {}", label);
LOG.info("Handle transaction with channel info, label: {}, warehouse: {}", label,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this log may not be accurate, because only the begin txn will take the warehouse name from parameter, subsequent ops are not using the warehousename parameter at all.

@kevincai kevincai requested a review from banmoy March 4, 2025 02:21
Long chosenNodeId = GlobalStateMgr.getCurrentState().getNodeMgr()
.getClusterInfo().getNodeSelector().seqChooseBackendOrComputeId();
nodeId = chosenNodeId;
if (StringUtils.isNotEmpty(warehouseName)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can provide a utility function to unify the same logic in LoadAction

@banmoy
Copy link
Contributor

banmoy commented Mar 4, 2025

should add some tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants