probing_status.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // RoundTripperNameSnapshot is the name of round-tripper that sends merged snapshot message.
  23. RoundTripperNameSnapshot = "ROUND_TRIPPER_SNAPSHOT"
  24. )
  25. var (
  26. // proberInterval must be shorter than read timeout.
  27. // Or the connection will time-out.
  28. proberInterval = ConnReadTimeout - time.Second
  29. statusMonitoringInterval = 30 * time.Second
  30. statusErrorInterval = 5 * time.Second
  31. )
  32. func addPeerToProber(lg *zap.Logger, p probing.Prober, id string, us []string, roundTripperName string, rttSecProm *prometheus.HistogramVec) {
  33. hus := make([]string, len(us))
  34. for i := range us {
  35. hus[i] = us[i] + ProbingPrefix
  36. }
  37. p.AddHTTP(id, proberInterval, hus)
  38. s, err := p.Status(id)
  39. if err != nil {
  40. if lg != nil {
  41. lg.Warn("failed to add peer into prober", zap.String("remote-peer-id", id))
  42. } else {
  43. plog.Errorf("failed to add peer %s into prober", id)
  44. }
  45. return
  46. }
  47. go monitorProbingStatus(lg, s, id, roundTripperName, rttSecProm)
  48. }
  49. func monitorProbingStatus(lg *zap.Logger, s probing.Status, id string, roundTripperName string, rttSecProm *prometheus.HistogramVec) {
  50. // set the first interval short to log error early.
  51. interval := statusErrorInterval
  52. for {
  53. select {
  54. case <-time.After(interval):
  55. if !s.Health() {
  56. if lg != nil {
  57. lg.Warn(
  58. "prober detected unhealthy status",
  59. zap.String("round-tripper-name", roundTripperName),
  60. zap.String("remote-peer-id", id),
  61. zap.Duration("rtt", s.SRTT()),
  62. zap.Error(s.Err()),
  63. )
  64. } else {
  65. plog.Warningf("health check for peer %s could not connect: %v", id, s.Err())
  66. }
  67. interval = statusErrorInterval
  68. } else {
  69. interval = statusMonitoringInterval
  70. }
  71. if s.ClockDiff() > time.Second {
  72. if lg != nil {
  73. lg.Warn(
  74. "prober found high clock drift",
  75. zap.String("round-tripper-name", roundTripperName),
  76. zap.String("remote-peer-id", id),
  77. zap.Duration("clock-drift", s.SRTT()),
  78. zap.Duration("rtt", s.ClockDiff()),
  79. zap.Error(s.Err()),
  80. )
  81. } else {
  82. plog.Warningf("the clock difference against peer %s is too high [%v > %v]", id, s.ClockDiff(), time.Second)
  83. }
  84. }
  85. rttSecProm.WithLabelValues(id).Observe(s.SRTT().Seconds())
  86. case <-s.StopNotify():
  87. return
  88. }
  89. }
  90. }