alarms.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2016 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 alarm manages health status alarms in etcd.
  15. package alarm
  16. import (
  17. "sync"
  18. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  19. "github.com/coreos/etcd/mvcc/backend"
  20. "github.com/coreos/etcd/pkg/types"
  21. "github.com/coreos/pkg/capnslog"
  22. )
  23. var (
  24. alarmBucketName = []byte("alarm")
  25. plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "alarm")
  26. )
  27. type BackendGetter interface {
  28. Backend() backend.Backend
  29. }
  30. type alarmSet map[types.ID]*pb.AlarmMember
  31. // AlarmStore persists alarms to the backend.
  32. type AlarmStore struct {
  33. mu sync.Mutex
  34. types map[pb.AlarmType]alarmSet
  35. bg BackendGetter
  36. }
  37. func NewAlarmStore(bg BackendGetter) (*AlarmStore, error) {
  38. ret := &AlarmStore{types: make(map[pb.AlarmType]alarmSet), bg: bg}
  39. err := ret.restore()
  40. return ret, err
  41. }
  42. func (a *AlarmStore) Activate(id types.ID, at pb.AlarmType) *pb.AlarmMember {
  43. a.mu.Lock()
  44. defer a.mu.Unlock()
  45. newAlarm := &pb.AlarmMember{MemberID: uint64(id), Alarm: at}
  46. if m := a.addToMap(newAlarm); m != newAlarm {
  47. return m
  48. }
  49. v, err := newAlarm.Marshal()
  50. if err != nil {
  51. plog.Panicf("failed to marshal alarm member")
  52. }
  53. b := a.bg.Backend()
  54. b.BatchTx().Lock()
  55. b.BatchTx().UnsafePut(alarmBucketName, v, nil)
  56. b.BatchTx().Unlock()
  57. return newAlarm
  58. }
  59. func (a *AlarmStore) Deactivate(id types.ID, at pb.AlarmType) *pb.AlarmMember {
  60. a.mu.Lock()
  61. defer a.mu.Unlock()
  62. t := a.types[at]
  63. if t == nil {
  64. t = make(alarmSet)
  65. a.types[at] = t
  66. }
  67. m := t[id]
  68. if m == nil {
  69. return nil
  70. }
  71. delete(t, id)
  72. v, err := m.Marshal()
  73. if err != nil {
  74. plog.Panicf("failed to marshal alarm member")
  75. }
  76. b := a.bg.Backend()
  77. b.BatchTx().Lock()
  78. b.BatchTx().UnsafeDelete(alarmBucketName, v)
  79. b.BatchTx().Unlock()
  80. return m
  81. }
  82. func (a *AlarmStore) Get(at pb.AlarmType) (ret []*pb.AlarmMember) {
  83. a.mu.Lock()
  84. defer a.mu.Unlock()
  85. if at == pb.AlarmType_NONE {
  86. for _, t := range a.types {
  87. for _, m := range t {
  88. ret = append(ret, m)
  89. }
  90. }
  91. return ret
  92. }
  93. for _, m := range a.types[at] {
  94. ret = append(ret, m)
  95. }
  96. return ret
  97. }
  98. func (a *AlarmStore) restore() error {
  99. b := a.bg.Backend()
  100. tx := b.BatchTx()
  101. tx.Lock()
  102. tx.UnsafeCreateBucket(alarmBucketName)
  103. err := tx.UnsafeForEach(alarmBucketName, func(k, v []byte) error {
  104. var m pb.AlarmMember
  105. if err := m.Unmarshal(k); err != nil {
  106. return err
  107. }
  108. a.addToMap(&m)
  109. return nil
  110. })
  111. tx.Unlock()
  112. b.ForceCommit()
  113. return err
  114. }
  115. func (a *AlarmStore) addToMap(newAlarm *pb.AlarmMember) *pb.AlarmMember {
  116. t := a.types[newAlarm.Alarm]
  117. if t == nil {
  118. t = make(alarmSet)
  119. a.types[newAlarm.Alarm] = t
  120. }
  121. m := t[types.ID(newAlarm.MemberID)]
  122. if m != nil {
  123. return m
  124. }
  125. t[types.ID(newAlarm.MemberID)] = newAlarm
  126. return newAlarm
  127. }