probing_status.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rafthttp
  15. import (
  16. "time"
  17. "github.com/prometheus/client_golang/prometheus"
  18. "github.com/xiang90/probing"
  19. "go.uber.org/zap"
  20. )
  21. const (
  22. // RoundTripperNameRaftMessage is the name of round-tripper that sends
  23. // all other Raft messages, other than "snap.Message".
  24. RoundTripperNameRaftMessage = "ROUND_TRIPPER_RAFT_MESSAGE"
  25. // RoundTripperNameSnapshot is the name of round-tripper that sends merged snapshot message.
  26. RoundTripperNameSnapshot = "ROUND_TRIPPER_SNAPSHOT"
  27. )
  28. var (
  29. // proberInterval must be shorter than read timeout.
  30. // Or the connection will time-out.
  31. proberInterval = ConnReadTimeout - time.Second
  32. statusMonitoringInterval = 30 * time.Second
  33. statusErrorInterval = 5 * time.Second
  34. )
  35. func addPeerToProber(lg *zap.Logger, p probing.Prober, id string, us []string, roundTripperName string, rttSecProm *prometheus.HistogramVec) {
  36. hus := make([]string, len(us))
  37. for i := range us {
  38. hus[i] = us[i] + ProbingPrefix
  39. }
  40. p.AddHTTP(id, proberInterval, hus)
  41. s, err := p.Status(id)
  42. if err != nil {
  43. if lg != nil {
  44. lg.Warn("failed to add peer into prober", zap.String("remote-peer-id", id))
  45. } else {
  46. plog.Errorf("failed to add peer %s into prober", id)
  47. }
  48. return
  49. }
  50. go monitorProbingStatus(lg, s, id, roundTripperName, rttSecProm)
  51. }
  52. func monitorProbingStatus(lg *zap.Logger, s probing.Status, id string, roundTripperName string, rttSecProm *prometheus.HistogramVec) {
  53. // set the first interval short to log error early.
  54. interval := statusErrorInterval
  55. for {
  56. select {
  57. case <-time.After(interval):
  58. if !s.Health() {
  59. if lg != nil {
  60. lg.Warn(
  61. "prober detected unhealthy status",
  62. zap.String("round-tripper-name", roundTripperName),
  63. zap.String("remote-peer-id", id),
  64. zap.Duration("rtt", s.SRTT()),
  65. zap.Error(s.Err()),
  66. )
  67. } else {
  68. plog.Warningf("health check for peer %s could not connect: %v", id, s.Err())
  69. }
  70. interval = statusErrorInterval
  71. } else {
  72. interval = statusMonitoringInterval
  73. }
  74. if s.ClockDiff() > time.Second {
  75. if lg != nil {
  76. lg.Warn(
  77. "prober found high clock drift",
  78. zap.String("round-tripper-name", roundTripperName),
  79. zap.String("remote-peer-id", id),
  80. zap.Duration("clock-drift", s.ClockDiff()),
  81. zap.Duration("rtt", s.SRTT()),
  82. zap.Error(s.Err()),
  83. )
  84. } else {
  85. plog.Warningf("the clock difference against peer %s is too high [%v > %v]", id, s.ClockDiff(), time.Second)
  86. }
  87. }
  88. rttSecProm.WithLabelValues(id).Observe(s.SRTT().Seconds())
  89. case <-s.StopNotify():
  90. return
  91. }
  92. }
  93. }