probing_status.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. )
  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. const (
  28. // RoundTripperNameRaftMessage is the name of round-tripper that sends
  29. // all other Raft messages, other than "snap.Message".
  30. RoundTripperNameRaftMessage = "ROUND_TRIPPER_RAFT_MESSAGE"
  31. // RoundTripperNameSnapshot is the name of round-tripper that sends merged snapshot message.
  32. RoundTripperNameSnapshot = "ROUND_TRIPPER_SNAPSHOT"
  33. )
  34. func addPeerToProber(p probing.Prober, id string, us []string, roundTripperName string, rttSecProm *prometheus.HistogramVec) {
  35. hus := make([]string, len(us))
  36. for i := range us {
  37. hus[i] = us[i] + ProbingPrefix
  38. }
  39. p.AddHTTP(id, proberInterval, hus)
  40. s, err := p.Status(id)
  41. if err != nil {
  42. plog.Errorf("failed to add peer %s into prober", id)
  43. } else {
  44. go monitorProbingStatus(s, id, roundTripperName, rttSecProm)
  45. }
  46. }
  47. func monitorProbingStatus(s probing.Status, id string, roundTripperName string, rttSecProm *prometheus.HistogramVec) {
  48. // set the first interval short to log error early.
  49. interval := statusErrorInterval
  50. for {
  51. select {
  52. case <-time.After(interval):
  53. if !s.Health() {
  54. plog.Warningf("health check for peer %s could not connect: %v (prober %q)", id, s.Err(), roundTripperName)
  55. interval = statusErrorInterval
  56. } else {
  57. interval = statusMonitoringInterval
  58. }
  59. if s.ClockDiff() > time.Second {
  60. plog.Warningf("the clock difference against peer %s is too high [%v > %v] (prober %q)", id, s.ClockDiff(), time.Second, roundTripperName)
  61. }
  62. rttSecProm.WithLabelValues(id).Observe(s.SRTT().Seconds())
  63. case <-s.StopNotify():
  64. return
  65. }
  66. }
  67. }