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
|
package internal
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"time"
)
type peerReport struct {
LastUpdated string `json:"lastUpdated"`
}
func peerActive(ctx context.Context, conf config) (bool, string) {
if conf.PeerURL == "" {
return true, "Peer failover: disabled (PeerURL not set)"
}
hostname, err := os.Hostname()
if err != nil {
return true, fmt.Sprintf("Peer failover: hostname lookup failed (%v); staying active", err)
}
return peerActiveAt(ctx, conf, time.Now(), hostname, fetchPeerLastUpdated)
}
func peerActiveAt(
ctx context.Context,
conf config,
now time.Time,
hostname string,
fetch func(context.Context, string) (time.Time, error),
) (bool, string) {
if conf.PeerURL == "" {
return true, "Peer failover: disabled (PeerURL not set)"
}
primary := conf.PeerPrimaryName
if primary == "" {
primary = hostname
}
secondary := conf.PeerSecondaryName
if secondary == "" {
if parsedURL, err := url.Parse(conf.PeerURL); err == nil && parsedURL.Hostname() != "" {
secondary = parsedURL.Hostname()
}
}
if primary == "" || secondary == "" {
return true, "Peer failover: missing peer names; staying active"
}
if hostname != primary && hostname != secondary {
return true, fmt.Sprintf("Peer failover: local hostname %s not in [%s, %s]; staying active",
hostname, primary, secondary)
}
staleThresholdS := conf.PeerStaleThresholdS
if staleThresholdS == 0 {
staleThresholdS = 600
}
lastUpdated, err := fetch(ctx, conf.PeerURL)
if err != nil {
return true, fmt.Sprintf("Peer failover: peer check failed (%v); staying active", err)
}
age := now.Sub(lastUpdated)
if age > time.Duration(staleThresholdS)*time.Second {
return true, fmt.Sprintf("Peer failover: peer stale (%v > %ds); staying active",
age, staleThresholdS)
}
master := scheduledMaster(primary, secondary, now)
if hostname == master {
return true, fmt.Sprintf("Peer failover: peer healthy; scheduled master is %s", master)
}
return false, fmt.Sprintf("Peer failover: peer healthy; scheduled master is %s", master)
}
func scheduledMaster(primary, secondary string, now time.Time) string {
week := weekNumberSunday(now)
if week%2 == 0 {
return secondary
}
return primary
}
// weekNumberSunday matches strftime %U (Sunday-based week number, 00-53).
func weekNumberSunday(t time.Time) int {
tUTC := t.In(time.UTC)
yearStart := time.Date(tUTC.Year(), 1, 1, 0, 0, 0, 0, time.UTC)
// Find the first Sunday on or after Jan 1.
daysUntilSunday := (7 - int(yearStart.Weekday())) % 7
firstSunday := yearStart.AddDate(0, 0, daysUntilSunday)
if tUTC.Before(firstSunday) {
return 0
}
daysSinceFirstSunday := int(tUTC.Sub(firstSunday).Hours() / 24)
return 1 + (daysSinceFirstSunday / 7)
}
func fetchPeerLastUpdated(ctx context.Context, peerURL string) (time.Time, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, peerURL, nil)
if err != nil {
return time.Time{}, err
}
client := http.Client{
Timeout: 5 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
return time.Time{}, err
}
defer resp.Body.Close()
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return time.Time{}, fmt.Errorf("unexpected status %d", resp.StatusCode)
}
var report peerReport
if err := json.NewDecoder(resp.Body).Decode(&report); err != nil {
return time.Time{}, err
}
if report.LastUpdated == "" {
return time.Time{}, fmt.Errorf("missing lastUpdated")
}
lastUpdated, err := time.Parse(time.RFC3339, report.LastUpdated)
if err != nil {
return time.Time{}, err
}
return lastUpdated, nil
}
|