-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
172 lines (144 loc) · 4.26 KB
/
types.go
File metadata and controls
172 lines (144 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package utils
import (
"fmt"
"time"
)
// NodeID uniquely identifies a node
type NodeID struct {
IP string `json:"ip"`
Port int `json:"port"`
Timestamp int64 `json:"timestamp"`
}
func (n NodeID) String() string {
return fmt.Sprintf("%s:%d:%d", n.IP, n.Port, n.Timestamp)
}
func (n NodeID) Address() string {
return fmt.Sprintf("%s:%d", n.IP, n.Port)
}
func (n NodeID) Equals(other NodeID) bool {
return n.IP == other.IP && n.Port == other.Port && n.Timestamp == other.Timestamp
}
// MemberStatus represents the status of a member
type MemberStatus int
const (
Alive MemberStatus = iota
Suspected
Failed
)
func (s MemberStatus) String() string {
switch s {
case Alive:
return "Alive"
case Suspected:
return "Suspected"
case Failed:
return "Failed"
default:
return "Unknown"
}
}
// Member represents a group member
type Member struct {
ID NodeID
Incarnation int32
Status MemberStatus
LastHeartbeat time.Time
SuspicionStart time.Time
}
// ChangeType represents types of membership changes
type ChangeType int
const (
Joined ChangeType = iota
Left
FailureDetected
StatusChanged
)
// MessageType represents different message types
type MessageType int
const (
Heartbeat MessageType = iota
Ping
Ack
IndirectPing
IndirectAck
Join
JoinResponse
Leave
Suspect
AliveMsg
Confirm
)
// Message represents a network message
type Message struct {
Type MessageType `json:"type"`
Sender NodeID `json:"sender"`
Target NodeID `json:"target,omitempty"`
Incarnation int32 `json:"incarnation"`
SeqNum uint64 `json:"seq_num,omitempty"`
Members []MemberUpdate `json:"members,omitempty"`
Timestamp int64 `json:"timestamp"`
}
// MemberUpdate represents a membership update
type MemberUpdate struct {
NodeID NodeID `json:"node_id"`
Incarnation int32 `json:"incarnation"`
Status MemberStatus `json:"status"`
Timestamp time.Time `json:"timestamp"`
}
// DetectionMode represents the failure detection mode
type DetectionMode int
const (
GossipMode DetectionMode = iota
PingAckMode
)
func ParseMode(mode string) DetectionMode {
if mode == "pingack" {
return PingAckMode
}
return GossipMode
}
// TimeoutConfig contains all timeout-related configuration
type TimeoutConfig struct {
// Failure detection timeouts
FailureTimeout time.Duration // Time before marking as suspected/failed
CleanupTimeout time.Duration // Additional time before removal (gossip only)
SuspicionTimeout time.Duration // Time before confirming failure
// Protocol-specific timeouts
GossipPeriod time.Duration // How often to gossip
ProtocolPeriod time.Duration // SWIM protocol period (pingack)
AckTimeout time.Duration // Time to wait for direct ACK
// Maintenance intervals
CheckInterval time.Duration // How often to check for failures
}
// DefaultTimeoutConfig returns a configuration with harmonized timeout values
func DefaultTimeoutConfig() TimeoutConfig {
return TimeoutConfig{
FailureTimeout: 3 * time.Second, // Standard failure detection time
CleanupTimeout: 2 * time.Second, // Additional cleanup time for gossip
SuspicionTimeout: 2 * time.Second, // Time to confirm suspicions
GossipPeriod: 1 * time.Second, // Gossip every second
ProtocolPeriod: 1 * time.Second, // SWIM period
AckTimeout: 250 * time.Millisecond, // Quick ACK timeout
CheckInterval: 200 * time.Millisecond, // Frequent checks
}
}
// OptimalTimeoutConfig returns optimized parameters for fast convergence
func OptimalTimeoutConfig() TimeoutConfig {
return TimeoutConfig{
FailureTimeout: 2 * time.Second, // Faster failure detection
CleanupTimeout: 1 * time.Second, // Quicker cleanup
SuspicionTimeout: 1500 * time.Millisecond, // Balanced suspicion time
GossipPeriod: 500 * time.Millisecond, // More frequent gossip
ProtocolPeriod: 500 * time.Millisecond, // Faster SWIM cycles
AckTimeout: 150 * time.Millisecond, // Quicker ACK timeout
CheckInterval: 100 * time.Millisecond, // More frequent checks
}
}
// Config represents configuration for initializing a node
type Config struct {
NodeID NodeID
IntroducerAddr string
IsIntroducer bool
Mode DetectionMode
Timeouts TimeoutConfig
}