@@ -4,73 +4,30 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"log"
7
- "strconv"
8
- "strings"
9
- "time"
10
7
11
- "github.com/google/uuid"
12
8
"github.com/pkg/errors"
13
9
)
14
10
15
11
const (
12
+ rmbTimeout = 40
16
13
FarmerBotVersionAction = "farmerbot.farmmanager.version"
17
14
FarmerBotFindNodeAction = "farmerbot.nodemanager.findnode"
18
- FarmerBotRMBFunction = "execute_job"
19
15
)
20
16
21
- type FarmerBotAction struct {
22
- Guid string `json:"guid"`
23
- TwinID uint32 `json:"twinid"`
24
- Action string `json:"action"`
25
- Args FarmerBotArgs `json:"args"`
26
- Result FarmerBotArgs `json:"result"`
27
- State string `json:"state"`
28
- Start uint64 `json:"start"`
29
- End uint64 `json:"end"`
30
- GracePeriod uint32 `json:"grace_period"`
31
- Error string `json:"error"`
32
- Timeout uint32 `json:"timeout"`
33
- SourceTwinID uint32 `json:"src_twinid"`
34
- SourceAction string `json:"src_action"`
35
- Dependencies []string `json:"dependencies"`
36
- }
37
-
38
- type FarmerBotArgs struct {
39
- Args []Args `json:"args"`
40
- Params []Params `json:"params"`
41
- }
42
-
43
- type Args struct {
44
- RequiredHRU * uint64 `json:"required_hru,omitempty"`
45
- RequiredSRU * uint64 `json:"required_sru,omitempty"`
46
- RequiredCRU * uint64 `json:"required_cru,omitempty"`
47
- RequiredMRU * uint64 `json:"required_mru,omitempty"`
48
- NodeExclude []uint32 `json:"node_exclude,omitempty"`
49
- Dedicated * bool `json:"dedicated,omitempty"`
50
- PublicConfig * bool `json:"public_config,omitempty"`
51
- PublicIPs * uint32 `json:"public_ips"`
52
- Certified * bool `json:"certified,omitempty"`
53
- }
54
-
55
- type Params struct {
56
- Key string `json:"key"`
57
- Value interface {} `json:"value"`
58
- }
59
-
60
17
func (s * Scheduler ) hasFarmerBot (ctx context.Context , farmID uint32 ) bool {
61
- args := []Args {}
62
- params := []Params {}
63
- data := buildFarmerBotAction (farmID , uint32 (s .twinID ), args , params , FarmerBotVersionAction )
18
+ ctx , cancel := context .WithTimeout (ctx , rmbTimeout )
19
+ defer cancel ()
64
20
65
21
info , err := s .getFarmInfo (ctx , farmID )
66
22
if err != nil {
67
23
return false
68
24
}
69
25
70
26
dst := info .farmerTwinID
71
- var output FarmerBotAction
72
27
73
- err = s .rmbClient .Call (ctx , dst , FarmerBotRMBFunction , data , & output )
28
+ service := fmt .Sprintf ("farmerbot-%d" , farmID )
29
+ var version string
30
+ err = s .rmbClient .CallWithSession (ctx , info .farmerTwinID , & service , FarmerBotVersionAction , nil , & version )
74
31
if err != nil {
75
32
log .Printf ("error while pinging farmerbot on farm %d with farmer twin %d. %s" , farmID , dst , err .Error ())
76
33
}
@@ -79,94 +36,75 @@ func (s *Scheduler) hasFarmerBot(ctx context.Context, farmID uint32) bool {
79
36
}
80
37
81
38
func (n * Scheduler ) farmerBotSchedule (ctx context.Context , r * Request ) (uint32 , error ) {
82
- info , err := n .getFarmInfo (ctx , r .FarmId )
83
- if err != nil {
84
- return 0 , errors .Wrapf (err , "failed to get farm %d info" , r .FarmId )
85
- }
86
- params := buildFarmerBotParams (r )
87
- args := buildFarmerBotArgs (r )
88
- data := buildFarmerBotAction (info .farmerTwinID , uint32 (n .twinID ), args , params , FarmerBotFindNodeAction )
89
- output := FarmerBotAction {}
39
+ ctx , cancel := context .WithTimeout (ctx , rmbTimeout )
40
+ defer cancel ()
90
41
91
- err = n .rmbClient . Call (ctx , info . farmerTwinID , FarmerBotRMBFunction , data , & output )
42
+ info , err : = n .getFarmInfo (ctx , r . FarmID )
92
43
if err != nil {
93
- return 0 , err
94
- }
95
- if len (output .Result .Params ) < 1 {
96
- return 0 , fmt .Errorf ("cannot find an eligible node on farm %d" , r .FarmId )
44
+ return 0 , errors .Wrapf (err , "failed to get farm %d info" , r .FarmID )
97
45
}
98
- nodeId , err := strconv .ParseUint (output .Result .Params [0 ].Value .(string ), 10 , 32 )
99
- if err != nil {
46
+
47
+ data := buildNodeOptions (r )
48
+ var nodeID uint32
49
+
50
+ service := fmt .Sprintf ("farmerbot-%d" , r .FarmID )
51
+ if err := n .rmbClient .CallWithSession (ctx , info .farmerTwinID , & service , FarmerBotFindNodeAction , data , & nodeID ); err != nil {
100
52
return 0 , err
101
53
}
102
- log .Printf ("got a node with id %d" , nodeId )
103
- return uint32 (nodeId ), nil
54
+
55
+ log .Printf ("got a node with id %d" , nodeID )
56
+ return nodeID , nil
104
57
}
105
58
106
- func buildFarmerBotArgs (r * Request ) []Args {
107
- return []Args {}
59
+ type NodeFilterOption struct {
60
+ NodesExcluded []uint32 `json:"nodes_excluded,omitempty"`
61
+ Certified bool `json:"certified,omitempty"`
62
+ Dedicated bool `json:"dedicated,omitempty"`
63
+ PublicConfig bool `json:"public_config,omitempty"`
64
+ PublicIPs uint64 `json:"public_ips,omitempty"`
65
+ HRU uint64 `json:"hru,omitempty"` // in GB
66
+ SRU uint64 `json:"sru,omitempty"` // in GB
67
+ CRU uint64 `json:"cru,omitempty"`
68
+ MRU uint64 `json:"mru,omitempty"` // in GB
108
69
}
109
- func buildFarmerBotParams (r * Request ) []Params {
110
- params := []Params {}
70
+
71
+ func buildNodeOptions (r * Request ) NodeFilterOption {
72
+ options := NodeFilterOption {}
111
73
if r .Capacity .HRU != 0 {
112
- params = append ( params , Params { Key : "required_hru" , Value : r .Capacity .HRU } )
74
+ options . HRU = r .Capacity .HRU / ( 1024 * 1024 * 1024 )
113
75
}
114
76
115
77
if r .Capacity .SRU != 0 {
116
- params = append ( params , Params { Key : "required_sru" , Value : r .Capacity .SRU } )
78
+ options . SRU = r .Capacity .SRU / ( 1024 * 1024 * 1024 )
117
79
}
118
80
119
81
if r .Capacity .MRU != 0 {
120
- params = append ( params , Params { Key : "required_mru" , Value : r .Capacity .MRU } )
82
+ options . MRU = r .Capacity .MRU / ( 1024 * 1024 * 1024 )
121
83
}
122
84
123
85
if r .Capacity .CRU != 0 {
124
- params = append ( params , Params { Key : "required_cru" , Value : r .Capacity .CRU })
86
+ options . CRU = r .Capacity .CRU
125
87
}
126
88
127
89
if len (r .NodeExclude ) != 0 {
128
- value := strings .Trim (strings .Join (strings .Fields (fmt .Sprint (r .NodeExclude )), "," ), "" )
129
- params = append (params , Params {Key : "node_exclude" , Value : value })
90
+ options .NodesExcluded = append (options .NodesExcluded , r .NodeExclude ... )
130
91
}
131
92
132
93
if r .Dedicated {
133
- params = append ( params , Params { Key : "dedicated" , Value : r .Dedicated })
94
+ options . Dedicated = r .Dedicated
134
95
}
135
96
136
97
if r .PublicConfig {
137
- params = append ( params , Params { Key : "public_config" , Value : r .PublicConfig })
98
+ options . PublicConfig = r .PublicConfig
138
99
}
139
100
140
101
if r .PublicIpsCount > 0 {
141
- params = append ( params , Params { Key : "public_ips" , Value : r .PublicIpsCount } )
102
+ options . PublicIPs = uint64 ( r .PublicIpsCount )
142
103
}
143
104
144
105
if r .Certified {
145
- params = append ( params , Params { Key : "certified" , Value : r .Certified })
106
+ options . Certified = r .Certified
146
107
}
147
- return params
148
- }
149
108
150
- func buildFarmerBotAction (farmerTwinID uint32 , sourceTwinID uint32 , args []Args , params []Params , action string ) FarmerBotAction {
151
- return FarmerBotAction {
152
- Guid : uuid .NewString (),
153
- TwinID : farmerTwinID ,
154
- Action : action ,
155
- Args : FarmerBotArgs {
156
- Args : args ,
157
- Params : params ,
158
- },
159
- Result : FarmerBotArgs {
160
- Args : []Args {},
161
- Params : []Params {},
162
- },
163
- State : "init" ,
164
- Start : uint64 (time .Now ().Unix ()),
165
- End : 0 ,
166
- GracePeriod : 0 ,
167
- Error : "" ,
168
- Timeout : 6000 ,
169
- SourceTwinID : sourceTwinID ,
170
- Dependencies : []string {},
171
- }
109
+ return options
172
110
}
0 commit comments