util.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 etcdserver
  15. import (
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "time"
  20. "github.com/golang/protobuf/proto"
  21. "go.etcd.io/etcd/etcdserver/api/membership"
  22. "go.etcd.io/etcd/etcdserver/api/rafthttp"
  23. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  24. "go.etcd.io/etcd/pkg/types"
  25. "go.uber.org/zap"
  26. )
  27. // isConnectedToQuorumSince checks whether the local member is connected to the
  28. // quorum of the cluster since the given time.
  29. func isConnectedToQuorumSince(transport rafthttp.Transporter, since time.Time, self types.ID, members []*membership.Member) bool {
  30. return numConnectedSince(transport, since, self, members) >= (len(members)/2)+1
  31. }
  32. // isConnectedSince checks whether the local member is connected to the
  33. // remote member since the given time.
  34. func isConnectedSince(transport rafthttp.Transporter, since time.Time, remote types.ID) bool {
  35. t := transport.ActiveSince(remote)
  36. return !t.IsZero() && t.Before(since)
  37. }
  38. // isConnectedFullySince checks whether the local member is connected to all
  39. // members in the cluster since the given time.
  40. func isConnectedFullySince(transport rafthttp.Transporter, since time.Time, self types.ID, members []*membership.Member) bool {
  41. return numConnectedSince(transport, since, self, members) == len(members)
  42. }
  43. // numConnectedSince counts how many members are connected to the local member
  44. // since the given time.
  45. func numConnectedSince(transport rafthttp.Transporter, since time.Time, self types.ID, members []*membership.Member) int {
  46. connectedNum := 0
  47. for _, m := range members {
  48. if m.ID == self || isConnectedSince(transport, since, m.ID) {
  49. connectedNum++
  50. }
  51. }
  52. return connectedNum
  53. }
  54. // longestConnected chooses the member with longest active-since-time.
  55. // It returns false, if nothing is active.
  56. func longestConnected(tp rafthttp.Transporter, membs []types.ID) (types.ID, bool) {
  57. var longest types.ID
  58. var oldest time.Time
  59. for _, id := range membs {
  60. tm := tp.ActiveSince(id)
  61. if tm.IsZero() { // inactive
  62. continue
  63. }
  64. if oldest.IsZero() { // first longest candidate
  65. oldest = tm
  66. longest = id
  67. }
  68. if tm.Before(oldest) {
  69. oldest = tm
  70. longest = id
  71. }
  72. }
  73. if uint64(longest) == 0 {
  74. return longest, false
  75. }
  76. return longest, true
  77. }
  78. type notifier struct {
  79. c chan struct{}
  80. err error
  81. }
  82. func newNotifier() *notifier {
  83. return &notifier{
  84. c: make(chan struct{}),
  85. }
  86. }
  87. func (nc *notifier) notify(err error) {
  88. nc.err = err
  89. close(nc.c)
  90. }
  91. func warnOfExpensiveRequest(lg *zap.Logger, now time.Time, reqStringer fmt.Stringer, respMsg proto.Message, err error) {
  92. var resp string
  93. if !isNil(respMsg) {
  94. resp = fmt.Sprintf("size:%d", proto.Size(respMsg))
  95. }
  96. warnOfExpensiveGenericRequest(lg, now, reqStringer, "", resp, err)
  97. }
  98. func warnOfExpensiveReadOnlyTxnRequest(lg *zap.Logger, now time.Time, r *pb.TxnRequest, txnResponse *pb.TxnResponse, err error) {
  99. reqStringer := pb.NewLoggableTxnRequest(r)
  100. var resp string
  101. if !isNil(txnResponse) {
  102. var resps []string
  103. for _, r := range txnResponse.Responses {
  104. switch op := r.Response.(type) {
  105. case *pb.ResponseOp_ResponseRange:
  106. resps = append(resps, fmt.Sprintf("range_response_count:%d", len(op.ResponseRange.Kvs)))
  107. default:
  108. // only range responses should be in a read only txn request
  109. }
  110. }
  111. resp = fmt.Sprintf("responses:<%s> size:%d", strings.Join(resps, " "), proto.Size(txnResponse))
  112. }
  113. warnOfExpensiveGenericRequest(lg, now, reqStringer, "read-only range ", resp, err)
  114. }
  115. func warnOfExpensiveReadOnlyRangeRequest(lg *zap.Logger, now time.Time, reqStringer fmt.Stringer, rangeResponse *pb.RangeResponse, err error) {
  116. var resp string
  117. if !isNil(rangeResponse) {
  118. resp = fmt.Sprintf("range_response_count:%d size:%d", len(rangeResponse.Kvs), proto.Size(rangeResponse))
  119. }
  120. warnOfExpensiveGenericRequest(lg, now, reqStringer, "read-only range ", resp, err)
  121. }
  122. func warnOfExpensiveGenericRequest(lg *zap.Logger, now time.Time, reqStringer fmt.Stringer, prefix string, resp string, err error) {
  123. d := time.Since(now)
  124. if d > warnApplyDuration {
  125. if lg != nil {
  126. lg.Warn(
  127. "apply request took too long",
  128. zap.Duration("took", d),
  129. zap.Duration("expected-duration", warnApplyDuration),
  130. zap.String("prefix", prefix),
  131. zap.String("request", reqStringer.String()),
  132. zap.String("response", resp),
  133. zap.Error(err),
  134. )
  135. } else {
  136. var result string
  137. if err != nil {
  138. result = fmt.Sprintf("error:%v", err)
  139. } else {
  140. result = resp
  141. }
  142. plog.Warningf("%srequest %q with result %q took too long (%v) to execute", prefix, reqStringer.String(), result, d)
  143. }
  144. slowApplies.Inc()
  145. }
  146. }
  147. func isNil(msg proto.Message) bool {
  148. return msg == nil || reflect.ValueOf(msg).IsNil()
  149. }