probing_status.go 2.5 KB

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