YoWuwuuuw opened a new pull request, #7550:
URL: https://github.com/apache/incubator-seata/pull/7550
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.
-->
<!-- Please make sure you have read and understood the contributing
guidelines -->
- [ ] I have registered the PR [changes](../changes).
### Ⅰ. Describe what this PR did
This PR is developed based on
pr[#7533](https://github.com/apache/incubator-seata/pull/7533), and should be
reviewed after the previous PR is merged.
It implements a comprehensive routing filter system for Apache Seata,
enhancing routing intelligence and flexibility in multi-node, cross-region, and
multi-environment deployments.
### Ⅱ. Does this pull request fix one issue?
<!-- If that, add "fixes #xxx" below in the next line, for example, fixes
#97. -->
fixes #7547
### Ⅲ. Why don't you add test cases (unit test/integration test)?
Basically covered
### Ⅳ. Describe how to verify it
### Ⅴ. Special notes for reviews
#### ① Configuration
##### 1. Client-Side Configuration
```yaml
seata:
client:
routing:
enabled: true
debug: false
fallback: true
# Router chain order
chain:
order: "RegionRouter,MetadataRouter"
# Region router settings
region-router:
enabled: true
topN: 5
# Metadata router settings
metadata-router:
enabled: true
expression: "(version >= 2.3) | (env = dev)"
# Client location (for region routing)
location:
lat: "39.9042"
lng: "116.4074"
```
##### 2. Server-Side Configuration
```yaml
seata:
registry:
metadata:
lat: "39.9042" # Server latitude
lng: "116.4074" # Server longitude
weight: "100"
version: "2.0"
env: "prod"
region: "cn-bj"
```
---
#### ②Router Chain Design
##### 1. Default Router Chain
The default chain processes routers sequentially, where each router filters
the result from the previous router:
```plain
Service Instances
↓
RegionRouter (filters by geographic distance)
↓
MetadataRouter (filters by metadata expressions)
↓
Filtered Candidates
```
##### 2. Primary-Backup Router Chain
Implements a failover mechanism with primary and backup routing strategies:
```yaml
seata:
client:
routing:
primary-backup:
enabled: true
order: "RegionRouter,MetadataRouter"
```
**Behavior:**
+ Uses primary chain first
+ If primary chain produces empty result, switches to backup chain
+ Provides high availability for critical deployments
---
#### ③Routing Filters
##### 1. Region Router (Geographic Distance Filter)
Filters server instances based on geographic distance from client location.
**Features:**
+ Calculates Haversine distance between client and server coordinates
+ Selects top-N closest servers
+ Gracefully handles missing location information
+ Currently requires manual configuration of client/server coordinates
**Future Enhancement:**
+ Integration with GeoIP2 or similar services for automatic client location
detection
+ Automatic server location discovery from IP geolocation databases
##### 2. Metadata Router (Expression-Based Filter)
Supports advanced expression-based filtering using server metadata.
**Expression Syntax:**
+ Exact match: `key = value`
+ Comparison operators: `>=`, `<=`, `<`, `>`, `!=`
+ Logical operators: `&` (AND), `|` (OR)
+ Parentheses for precedence
**Expression Examples:**
```plain
(region = cn-bj) | (region = cn-hz)
(version >= 1.5)
version >= 1.5
```
**Logic Implementation:**
+ Multiple routers can be combined with AND logic
+ Single router supports OR logic within expressions
+ Complex expressions parsed and evaluated at runtime
##### 3. Custom Router (SPI Extension)
Supports custom routing logic via SPI mechanism:
```java
public interface StateRouter<T> {
BitList<T> route(BitList<T> servers, RoutingContext ctx, boolean
debugMode, Holder<RouterSnapshotNode<T>> snapshotHolder);
boolean isRuntime();
String buildSnapshot();
}
```
**Usage:**
+ Implement custom routing logic
+ Register via SPI mechanism
+ Integrate into router chain via configuration
---
#### ④Fallback Mechanism
Ensures high availability when routing produces empty results:
```yaml
seata:
client:
routing:
fallback: true
```
**Scenarios:**
+ When any router filters out all service instances
+ When client location information is unavailable
+ To prevent transaction failures
**Behavior:**
+ `fallback = true`: Returns all servers when routing produces empty result
+ `fallback = false`: Returns empty list when routing produces empty result
---
#### ⑤Debug Mechanism
Enables detailed logging of routing decisions:
```yaml
seata:
client:
routing:
debug: true
snapshot: true
```
**Debug Output Example:**
```plain
[RoutingChain] Service = 'default', Total servers = 5
[RegionRouter] config: regionTopN = 3, matched = 3, selected = [192.168.1.1,
192.168.1.3, 192.168.1.6]
[MetadataRouter] expression = "version >= 1.2 & env = dev", matched = 2,
selected = [192.168.1.3, 192.168.1.6]
Final candidates: [192.168.1.3, 192.168.1.6]
```
---
#### ⑥Performance Optimizations
### BitList Structure
High-performance filtering using BitSet to avoid full array copies during
filtering operations.
##### Snapshot Logging
Efficient debug output generation with minimal performance impact.
---
#### ⑦Future Extensibility
##### 1. Automatic Location Detection
+ Integration with GeoIP2 or MaxMind services
+ Automatic client location detection from IP addresses
+ Server location auto-discovery from network topology
##### 2. Dynamic Configuration
+ Hot-reload of routing expressions
+ Runtime router chain management
+ Visual UI editor for routing expressions
##### 3. Advanced Features
+ Machine learning-based routing optimization
+ Real-time performance metrics integration
+ Multi-cloud routing strategies
--
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]