mirror of
https://github.com/DeBrosOfficial/network.git
synced 2025-12-15 03:38:49 +00:00
- Added ClusterDiscoveryService to manage peer discovery and synchronization for RQLite nodes. - Introduced new configuration options for cluster synchronization intervals, peer inactivity limits, and minimum cluster size. - Enhanced validation logic to ensure proper configuration of cluster parameters. - Implemented metrics collection for cluster health and peer status, improving monitoring capabilities. - Updated RQLiteManager to integrate with the new discovery service, allowing for dynamic leadership and cluster joining logic.
110 lines
2.8 KiB
Go
110 lines
2.8 KiB
Go
package rqlite
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// getRaftLogIndex returns the current Raft log index for this node
|
|
func (r *RQLiteManager) getRaftLogIndex() uint64 {
|
|
status, err := r.getRQLiteStatus()
|
|
if err != nil {
|
|
r.logger.Debug("Failed to get Raft log index", zap.Error(err))
|
|
return 0
|
|
}
|
|
|
|
// Return the highest index we have
|
|
maxIndex := status.Store.Raft.LastLogIndex
|
|
if status.Store.Raft.AppliedIndex > maxIndex {
|
|
maxIndex = status.Store.Raft.AppliedIndex
|
|
}
|
|
if status.Store.Raft.CommitIndex > maxIndex {
|
|
maxIndex = status.Store.Raft.CommitIndex
|
|
}
|
|
|
|
return maxIndex
|
|
}
|
|
|
|
// getRQLiteStatus queries the /status endpoint for cluster information
|
|
func (r *RQLiteManager) getRQLiteStatus() (*RQLiteStatus, error) {
|
|
url := fmt.Sprintf("http://localhost:%d/status", r.config.RQLitePort)
|
|
client := &http.Client{Timeout: 5 * time.Second}
|
|
|
|
resp, err := client.Get(url)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to query status: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
body, _ := io.ReadAll(resp.Body)
|
|
return nil, fmt.Errorf("status endpoint returned %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
var status RQLiteStatus
|
|
if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
|
|
return nil, fmt.Errorf("failed to decode status: %w", err)
|
|
}
|
|
|
|
return &status, nil
|
|
}
|
|
|
|
// getRQLiteNodes queries the /nodes endpoint for cluster membership
|
|
func (r *RQLiteManager) getRQLiteNodes() (RQLiteNodes, error) {
|
|
url := fmt.Sprintf("http://localhost:%d/nodes?ver=2", r.config.RQLitePort)
|
|
client := &http.Client{Timeout: 5 * time.Second}
|
|
|
|
resp, err := client.Get(url)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to query nodes: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
body, _ := io.ReadAll(resp.Body)
|
|
return nil, fmt.Errorf("nodes endpoint returned %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
var nodes RQLiteNodes
|
|
if err := json.NewDecoder(resp.Body).Decode(&nodes); err != nil {
|
|
return nil, fmt.Errorf("failed to decode nodes: %w", err)
|
|
}
|
|
|
|
return nodes, nil
|
|
}
|
|
|
|
// getRQLiteLeader returns the current leader address
|
|
func (r *RQLiteManager) getRQLiteLeader() (string, error) {
|
|
status, err := r.getRQLiteStatus()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
leaderAddr := status.Store.Raft.LeaderAddr
|
|
if leaderAddr == "" {
|
|
return "", fmt.Errorf("no leader found")
|
|
}
|
|
|
|
return leaderAddr, nil
|
|
}
|
|
|
|
// isNodeReachable tests if a specific node is responding
|
|
func (r *RQLiteManager) isNodeReachable(httpAddress string) bool {
|
|
url := fmt.Sprintf("http://%s/status", httpAddress)
|
|
client := &http.Client{Timeout: 3 * time.Second}
|
|
|
|
resp, err := client.Get(url)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
return resp.StatusCode == http.StatusOK
|
|
}
|
|
|