Browse Source
- Rename TbRpcService.save -> update so the create/update pairing is self-documenting (matches createAsync/updateAsync); update all actor call sites and unit tests. - Extract the (hash & 0x7FFFFFFF) % n striping into a shared HashPartitioner helper used by both TbSqlBlockingQueueWrapper and TbRpcService, so the end-to-end submission-order invariant has a single source of truth. - Collapse the insert/update split in RpcInsertRepository.saveOrUpdate into a single partitioningBy pass. - Remove the unused TbRpcService.findRpcById (and its now-orphaned import). - Restore the prime-thread-count guidance on the RPC batch_threads / callback_threads yml comments.pull/15853/head
7 changed files with 63 additions and 21 deletions
@ -0,0 +1,41 @@ |
|||
/** |
|||
* Copyright © 2016-2026 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.common.util; |
|||
|
|||
/** |
|||
* Maps a hash code to a non-negative partition index in {@code [0, partitions)}. |
|||
* <p> |
|||
* Centralises the {@code (hashCode & 0x7FFFFFFF) % partitions} striping so that components which must |
|||
* agree on a partition for the same key derive it identically. In particular, batched RPC persistence |
|||
* relies on this: {@code TbSqlBlockingQueueWrapper} picks the DB write partition and {@code TbRpcService} |
|||
* picks the post-persist callback stripe, both keyed off the rpcId hash. If those two ever disagreed on |
|||
* how the index is computed, the submission-order guarantee (e.g. RPC_QUEUED before RPC_DELIVERED) would |
|||
* silently break — keeping the math in one place removes that risk. |
|||
*/ |
|||
public final class HashPartitioner { |
|||
|
|||
private HashPartitioner() { |
|||
} |
|||
|
|||
/** |
|||
* @param hashCode hash of the partition key |
|||
* @param partitions number of partitions; must be {@code > 0} |
|||
* @return a non-negative partition index in {@code [0, partitions)} |
|||
*/ |
|||
public static int resolvePartition(int hashCode, int partitions) { |
|||
return (hashCode & 0x7FFFFFFF) % partitions; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue