...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package rafthttp
16
17 import (
18 "time"
19
20 "github.com/prometheus/client_golang/prometheus"
21 "github.com/xiang90/probing"
22 "go.uber.org/zap"
23 )
24
25 const (
26
27
28 RoundTripperNameRaftMessage = "ROUND_TRIPPER_RAFT_MESSAGE"
29
30 RoundTripperNameSnapshot = "ROUND_TRIPPER_SNAPSHOT"
31 )
32
33 var (
34
35
36 proberInterval = ConnReadTimeout - time.Second
37 statusMonitoringInterval = 30 * time.Second
38 statusErrorInterval = 5 * time.Second
39 )
40
41 func addPeerToProber(lg *zap.Logger, p probing.Prober, id string, us []string, roundTripperName string, rttSecProm *prometheus.HistogramVec) {
42 hus := make([]string, len(us))
43 for i := range us {
44 hus[i] = us[i] + ProbingPrefix
45 }
46
47 p.AddHTTP(id, proberInterval, hus)
48
49 s, err := p.Status(id)
50 if err != nil {
51 if lg != nil {
52 lg.Warn("failed to add peer into prober", zap.String("remote-peer-id", id), zap.Error(err))
53 }
54 return
55 }
56
57 go monitorProbingStatus(lg, s, id, roundTripperName, rttSecProm)
58 }
59
60 func monitorProbingStatus(lg *zap.Logger, s probing.Status, id string, roundTripperName string, rttSecProm *prometheus.HistogramVec) {
61
62 interval := statusErrorInterval
63 for {
64 select {
65 case <-time.After(interval):
66 if !s.Health() {
67 if lg != nil {
68 lg.Warn(
69 "prober detected unhealthy status",
70 zap.String("round-tripper-name", roundTripperName),
71 zap.String("remote-peer-id", id),
72 zap.Duration("rtt", s.SRTT()),
73 zap.Error(s.Err()),
74 )
75 }
76 interval = statusErrorInterval
77 } else {
78 interval = statusMonitoringInterval
79 }
80 if s.ClockDiff() > time.Second {
81 if lg != nil {
82 lg.Warn(
83 "prober found high clock drift",
84 zap.String("round-tripper-name", roundTripperName),
85 zap.String("remote-peer-id", id),
86 zap.Duration("clock-drift", s.ClockDiff()),
87 zap.Duration("rtt", s.SRTT()),
88 zap.Error(s.Err()),
89 )
90 }
91 }
92 rttSecProm.WithLabelValues(id).Observe(s.SRTT().Seconds())
93
94 case <-s.StopNotify():
95 return
96 }
97 }
98 }
99
View as plain text