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