-
Notifications
You must be signed in to change notification settings - Fork 375
/
edas_event.go
233 lines (196 loc) · 7.18 KB
/
edas_event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package edas
import (
"context"
"strings"
"sync"
"time"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/scheduler/events"
"github.com/erda-project/erda/modules/scheduler/events/eventtypes"
"github.com/erda-project/erda/modules/scheduler/executor/executortypes"
"github.com/erda-project/erda/pkg/discover"
"github.com/erda-project/erda/pkg/jsonstore/storetypes"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func (m *EDAS) registerEventChanAndLocalStore(evCh chan *eventtypes.StatusEvent, stopCh chan struct{}, lstore *sync.Map) {
o11 := discover.Orchestrator()
eventAddr := "http://" + o11 + "/api/events/runtimes/actions/sync"
name := string(m.name)
logrus.Infof("edas registerEventChanAndLocalStore, name: %s", name)
// watch event handler for a specific etcd directory
syncRuntimeToLstore := func(key string, value interface{}, t storetypes.ChangeType) error {
runtimeName := etcdKeyToMapKey(key)
if len(runtimeName) == 0 {
return nil
}
// Deal with the delete event first
if t == storetypes.Del {
_, ok := lstore.Load(runtimeName)
if ok {
var e events.RuntimeEvent
e.RuntimeName = runtimeName
e.IsDeleted = true
lstore.Delete(runtimeName)
m.notifier.Send(e, events.WithSender(name+events.SUFFIX_EDAS), events.WithDest(map[string]interface{}{"HTTP": []string{eventAddr}}))
}
return nil
}
run := value.(*apistructs.ServiceGroup)
// Filter events that do not belong to this executor
if run.Executor != name {
return nil
}
switch t {
// Add or update event
case storetypes.Add, storetypes.Update:
lstore.Store(runtimeName, *run)
default:
logrus.Errorf("unknown store type, try to skip, type: %s, name: %s", t, runtimeName)
return nil
}
if strings.Contains(name, "EDAS") {
logrus.Infof("edas executor(%s) lstore stored key: %s", name, key)
}
return nil
}
// Correspond the name of the registered executor and its event channel
getEvChanFn := func(executorName executortypes.Name) (chan *eventtypes.StatusEvent, chan struct{}, *sync.Map, error) {
logrus.Infof("in RegisterEvChan executor(%s)", name)
if string(executorName) == name {
return evCh, stopCh, lstore, nil
}
return nil, nil, nil, errors.Errorf("this is for %s executor, not %s", executorName, name)
}
executortypes.RegisterEvChan(executortypes.Name(name), getEvChanFn, syncRuntimeToLstore)
}
// Currently edas does not use an event-driven mechanism, so it uses polling to simulate each time
func (m *EDAS) WaitEvent(lstore *sync.Map, stopCh chan struct{}) {
o11 := discover.Orchestrator()
eventAddr := "http://" + o11 + "/api/events/runtimes/actions/sync"
logrus.Infof("executor(%s) in waitEvent", m.name)
initStore := func(k string, v interface{}) error {
reKey := etcdKeyToMapKey(k)
if len(reKey) == 0 {
return nil
}
r := v.(*apistructs.ServiceGroup)
if r.Executor != string(m.name) {
return nil
}
lstore.Store(reKey, *r)
return nil
}
em := events.GetEventManager()
if err := em.MemEtcdStore.ForEach(context.Background(), "/dice/service/", apistructs.ServiceGroup{}, initStore); err != nil {
logrus.Errorf("executor(%s) foreach initStore error: %v", m.name, err)
}
keys := make([]string, 0)
f := func(k, v interface{}) bool {
r := v.(apistructs.ServiceGroup)
// double check
if r.Executor != string(m.name) {
return true
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
var err error
c := make(chan struct{}, 1)
go func() {
start := time.Now()
defer func() {
logrus.Infof("edas executor(%s) get status for key(%s) took %v", m.name, k.(string), time.Since(start))
}()
_, err = m.Status(ctx, r)
c <- struct{}{}
}()
keys = append(keys, k.(string))
//logrus.Debugf("in edas loop inside f before status, key: %s", k.(string))
select {
case <-c:
if err != nil {
logrus.Errorf("executor(%s)'s key(%s) for edas get status error: %v", m.name, k, err)
return true
}
var e events.RuntimeEvent
e.EventType = events.EVENTS_TOTAL
e.RuntimeName = k.(string)
e.ServiceStatuses = make([]events.ServiceStatus, len(r.Services))
for i, srv := range r.Services {
e.ServiceStatuses[i].ServiceName = srv.Name
e.ServiceStatuses[i].Replica = srv.Scale
e.ServiceStatuses[i].ServiceStatus = convertServiceStatus(srv.Status)
// Status is empty string
if len(e.ServiceStatuses[i].ServiceStatus) == 0 {
e.ServiceStatuses[i].ServiceStatus = convertServiceStatus(apistructs.StatusProgressing)
}
//Get the service status through the Status interface of edas. Currently there is no instance information attached.
//If the upper layer sets the number of instances of a service to 0, then the edas service is set to a healthy state
if e.ServiceStatuses[i].Replica == 0 {
e.ServiceStatuses[i].ServiceStatus = string(apistructs.StatusHealthy)
}
/*
e.ServiceStatuses[i].InstanceStatuses = make([]events.InstanceStatus, len(srv.InstanceInfos))
for j := range srv.InstanceInfos {
e.ServiceStatuses[i].InstanceStatuses[j].Ip = srv.InstanceInfos[j].Ip
e.ServiceStatuses[i].InstanceStatuses[j].ID = srv.InstanceInfos[j].Id
if srv.InstanceInfos[j].Status == "Running" {
e.ServiceStatuses[i].InstanceStatuses[j].InstanceStatus = "Healthy"
} else {
//TODO. modify this
e.ServiceStatuses[i].InstanceStatuses[j].InstanceStatus = srv.InstanceInfos[j].Status
}
}
*/
}
go m.notifier.Send(e, events.WithSender(string(m.name)+events.SUFFIX_EDAS), events.WithDest(map[string]interface{}{"HTTP": []string{eventAddr}}))
return true
case <-ctx.Done():
logrus.Errorf("executor(%s)'s key(%s) get status timeout", m.name, k)
return true
}
}
for {
select {
case <-stopCh:
logrus.Errorf("edas executor(%s) got stop chan message", m.name)
return
case <-time.After(10 * time.Second):
lstore.Range(f)
}
logrus.Infof("executor(%s) edas keys list: %v", m.name, keys)
keys = nil
}
}
// Treat the original key in etcd
// /dice/service/services/staging-77 -> "services/staging-77"
func etcdKeyToMapKey(eKey string) string {
fields := strings.Split(eKey, "/")
if l := len(fields); l > 2 {
return fields[l-2] + "/" + fields[l-1]
}
return ""
}
// Modification of the status of the service obtained through the Status interface through edas
func convertServiceStatus(serviceStatus apistructs.StatusCode) string {
switch serviceStatus {
case apistructs.StatusReady:
return string(apistructs.StatusHealthy)
case apistructs.StatusProgressing:
return string(apistructs.StatusUnHealthy)
default:
return string(apistructs.StatusUnknown)
}
}