util.go 4.8 KB

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