-
Notifications
You must be signed in to change notification settings - Fork 3
/
executor.go
348 lines (278 loc) · 7.4 KB
/
executor.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package core
import (
"encoding/json"
"time"
)
const (
PENDING int = 0
APPROVED = 1
REJECTED = 2
)
type Location struct {
Long float64 `json:"long"`
Lat float64 `json:"lat"`
Description string `json:"desc"`
}
type GPU struct {
Name string `json:"name"`
Memory string `json:"mem"`
Count int `json:"count"`
NodeCount int `json:"nodecount"`
}
type Software struct {
Name string `json:"name"`
Type string `json:"type"`
Version string `json:"version"`
}
type Hardware struct {
Model string `json:"model"`
Nodes int `json:"nodes"`
CPU string `json:"cpu"`
Memory string `json:"mem"`
Storage string `json:"storage"`
GPU GPU `json:"gpu"`
}
type Capabilities struct {
Hardware Hardware `json:"hardware"`
Software Software `json:"software"`
}
type Project struct {
AllocatedCPU int64 `json:"allocatedcpu"`
UsedCPU int64 `json:"usedcpu"`
AllocatedGPU int64 `json:"allocatedgpu"`
UsedGPU int64 `json:"usedgpu"`
AllocatedStorage int64 `json:"allocatedstorage"`
UsedStorage int64 `json:"usedstorage"`
}
type Allocations struct {
Projects map[string]Project `json:"projects"`
}
type Executor struct {
ID string `json:"executorid"`
Type string `json:"executortype"`
Name string `json:"executorname"`
ColonyName string `json:"colonyname"`
State int `json:"state"`
RequireFuncReg bool `json:"requirefuncreg"`
CommissionTime time.Time `json:"commissiontime"`
LastHeardFromTime time.Time `json:"lastheardfromtime"`
Location Location `json:"location"`
Capabilities Capabilities `json:"capabilities"`
Allocations Allocations `json:"allocations"`
}
func CreateExecutor(id string,
executorType string,
name string,
colonyName string,
commissionTime time.Time,
lastHeardFromTime time.Time) *Executor {
return &Executor{ID: id,
Type: executorType,
Name: name,
ColonyName: colonyName,
State: PENDING,
RequireFuncReg: false,
CommissionTime: commissionTime,
LastHeardFromTime: lastHeardFromTime,
}
}
func CreateExecutorFromDB(id string,
executorType string,
name string,
colonyName string,
state int,
requireFuncReg bool,
commissionTime time.Time,
lastHeardFromTime time.Time) *Executor {
executor := CreateExecutor(id, executorType, name, colonyName, commissionTime, lastHeardFromTime)
executor.State = state
executor.RequireFuncReg = requireFuncReg
return executor
}
func ConvertJSONToExecutor(jsonString string) (*Executor, error) {
var executor *Executor
err := json.Unmarshal([]byte(jsonString), &executor)
if err != nil {
return nil, err
}
return executor, nil
}
func ConvertJSONToExecutorArray(jsonString string) ([]*Executor, error) {
var executors []*Executor
err := json.Unmarshal([]byte(jsonString), &executors)
if err != nil {
return executors, err
}
return executors, nil
}
func ConvertExecutorArrayToJSON(executors []*Executor) (string, error) {
jsonBytes, err := json.Marshal(executors)
if err != nil {
return "", err
}
return string(jsonBytes), nil
}
func IsExecutorArraysEqual(executors1 []*Executor, executors2 []*Executor) bool {
counter := 0
for _, executor1 := range executors1 {
for _, executor2 := range executors2 {
if executor1.Equals(executor2) {
counter++
}
}
}
if counter == len(executors1) && counter == len(executors2) {
return true
}
return false
}
func (executor *Executor) Equals(executor2 *Executor) bool {
if executor2 == nil {
return false
}
same := true
if executor.ID != executor2.ID {
same = false
}
if executor.Type != executor2.Type {
same = false
}
if executor.Name != executor2.Name {
same = false
}
if executor.ColonyName != executor2.ColonyName {
same = false
}
if executor.State != executor2.State {
same = false
}
if executor.RequireFuncReg != executor2.RequireFuncReg {
same = false
}
if executor.Location.Lat != executor2.Location.Lat {
same = false
}
if executor.Location.Long != executor2.Location.Long {
same = false
}
if executor.Location.Description != executor2.Location.Description {
same = false
}
if executor.Capabilities.Hardware.Nodes != executor2.Capabilities.Hardware.Nodes {
same = false
}
if executor.Capabilities.Hardware.CPU != executor2.Capabilities.Hardware.CPU {
same = false
}
if executor.Capabilities.Hardware.Memory != executor2.Capabilities.Hardware.Memory {
same = false
}
if executor.Capabilities.Hardware.Storage != executor2.Capabilities.Hardware.Storage {
same = false
}
if executor.Capabilities.Hardware.GPU.Name != executor2.Capabilities.Hardware.GPU.Name {
same = false
}
if executor.Capabilities.Hardware.GPU.Memory != executor2.Capabilities.Hardware.GPU.Memory {
same = false
}
if executor.Capabilities.Hardware.GPU.Count != executor2.Capabilities.Hardware.GPU.Count {
same = false
}
if executor.Capabilities.Hardware.GPU.NodeCount != executor2.Capabilities.Hardware.GPU.NodeCount {
same = false
}
if executor.Capabilities.Software.Name != executor2.Capabilities.Software.Name {
same = false
}
if executor.Capabilities.Software.Type != executor2.Capabilities.Software.Type {
same = false
}
if executor.Capabilities.Software.Version != executor2.Capabilities.Software.Version {
same = false
}
if executor.Allocations.Projects == nil && executor2.Allocations.Projects != nil {
same = false
}
if executor.Allocations.Projects != nil && executor2.Allocations.Projects == nil {
same = false
}
if executor.Allocations.Projects != nil && executor2.Allocations.Projects != nil {
if len(executor.Allocations.Projects) != len(executor2.Allocations.Projects) {
same = false
}
if !IsProjectsEqual(executor.Allocations.Projects, executor2.Allocations.Projects) {
same = false
}
}
return same
}
func IsProjectEqual(project1 Project, project2 Project) bool {
if project1.AllocatedCPU != project2.AllocatedCPU {
return false
}
if project1.UsedCPU != project2.UsedCPU {
return false
}
if project1.AllocatedGPU != project2.AllocatedGPU {
return false
}
if project1.UsedGPU != project2.UsedGPU {
return false
}
if project1.AllocatedStorage != project2.AllocatedStorage {
return false
}
if project1.UsedStorage != project2.UsedStorage {
return false
}
return true
}
func IsProjectsEqual(projects1 map[string]Project, projects2 map[string]Project) bool {
if len(projects1) != len(projects2) {
return false
}
for key, project1 := range projects1 {
if !IsProjectEqual(project1, projects2[key]) {
return false
}
}
return true
}
func (executor *Executor) IsApproved() bool {
if executor.State == APPROVED {
return true
}
return false
}
func (executor *Executor) IsRejected() bool {
if executor.State == REJECTED {
return true
}
return false
}
func (executor *Executor) IsPending() bool {
if executor.State == PENDING {
return true
}
return false
}
func (executor *Executor) Approve() {
executor.State = APPROVED
}
func (executor *Executor) Reject() {
executor.State = REJECTED
}
func (executor *Executor) SetID(id string) {
executor.ID = id
}
func (executor *Executor) SetColonyName(colonyName string) {
executor.ColonyName = colonyName
}
func (executor *Executor) ToJSON() (string, error) {
jsonBytes, err := json.Marshal(executor)
if err != nil {
return "", err
}
return string(jsonBytes), nil
}