GitHub user tew-axiom added a comment to the discussion: Proposal: support 
Master-Slave architecture among components

# Some initial ideas for a "Gorm + DB" solution

## Lease Design

Based on the following considerations:
- Need to ensure only one lease record exists per component
- Define lease expiration conditions

We need a table with roughly the following structure:

```sql
CREATE TABLE component_lease (
    -- Primary key: component name (e.g., "discovery", "engine")
    component_name VARCHAR(64) PRIMARY KEY,
    -- Current holder identifier (pod_name or hostname+pid)
    holder_id VARCHAR(128) NOT NULL,
    -- Lease acquisition time (millisecond timestamp)
    lease_time BIGINT NOT NULL,
    -- Last renewal time (millisecond timestamp)
    renew_time BIGINT NOT NULL,
    -- Optimistic lock version number
    version INT NOT NULL DEFAULT 0,
    -- Lease TTL (seconds)
    ttl INT NOT NULL DEFAULT 30,
);
```

Now we can perform leader election through the following approaches or 
strategies, as illustrated:
<img width="320" height="638" alt="截屏2026-01-13 20 48 59" 
src="https://github.com/user-attachments/assets/6e1c037e-fc0b-4aa6-a713-94a68c42d17b";
 />

1. **Initial Competition** - Attempt to INSERT lease record
   
   - Success → Become Leader
   - Failure (unique key conflict) → Proceed to next step

2. **Seize Expired Lease** - UPDATE WHERE expiration condition
   For example:
   
   ```sql
   ... WHERE component_name = 'discovery' AND renew_time < (NOW() - ttl * 1000)
   ```
   
   At this point, we can determine the current node's role based on the 
following feedback:
   
   - RowsAffected > 0 → Become Leader
   - RowsAffected = 0 → Become Follower

3. **Periodic Heartbeat**
   
   - **Leader**: Renew every TTL/3 interval (e.g., if TTL=30s, renew every 10s)
   - **Follower**: Attempt to compete every TTL/3 interval

4. **Loss of Leadership**
   
   - Renewal failure (database exception/lease preemption) → Stop business 
logic → Transition to Follower

## Other Details

- If the database is master-slave, how to ensure lease operates normally? Force 
routing to master? How to handle master failure? ProxySQL?
- Lease lifecycle management itself, and integration with existing component 
lifecycle
- Edge cases:
  - Boundary handling when multiple nodes start simultaneously
  - Use database timestamps instead of server/application time to avoid clock 
skew

GitHub link: 
https://github.com/apache/dubbo-admin/discussions/1380#discussioncomment-15485594

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: 
[email protected]


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

Reply via email to