-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevents.go
172 lines (152 loc) · 4 KB
/
events.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
package main
import (
"io"
"math/rand"
"net/http"
"time"
"github.com/golang/protobuf/proto"
log "github.com/Sirupsen/logrus"
"github.com/jimenez/mesos-APIproto/mesosproto"
)
func sendEvent(encoder icoder, eventType mesosproto.Event_Type, event *mesosproto.Event) error {
event.Type = &eventType
if event.Type.String() == "UPDATE" {
log.WithFields(log.Fields{"Type": event.Type.String(), "TaskState": event.Update.GetStatus().GetState().String()}).Info("Sending event")
} else {
log.WithFields(log.Fields{"Type": event.Type.String()}).Info("Sending event")
}
return encoder.Encode(event)
}
func createOffer(frameworkID *mesosproto.FrameworkID) *mesosproto.Offer {
slaveId, _ := generateID()
offerId, _ := generateID()
hostname := "slave.host" + slaveId
return &mesosproto.Offer{
Id: &mesosproto.OfferID{Value: &offerId},
FrameworkId: frameworkID,
SlaveId: &mesosproto.SlaveID{Value: &slaveId},
Hostname: &hostname,
Resources: resources(),
}
}
func sendTasksUpdates(f *Framework, frameworkId *mesosproto.FrameworkID) {
for {
select {
case <-f.connection:
return
case <-time.After(time.Duration(rand.Intn(10)) * time.Second):
for ID, _ := range f.GetTasks() {
taskID := &mesosproto.TaskID{Value: &ID}
event := generateEventUpdate((mesosproto.TaskState)(mesosproto.TaskState_value[mesosproto.TaskState_name[rand.Int31n(3)]]), taskID)
f.send(event)
}
}
}
}
func sendOffers(f *Framework, frameworkID *mesosproto.FrameworkID) {
for {
select {
case <-f.connection:
return
case <-time.After(time.Duration(rand.Intn(10)) * time.Second):
if frameworks.OffersSize() >= *size {
continue
}
offer := createOffer(frameworkID)
event_type := mesosproto.Event_OFFERS
event := &mesosproto.Event{
Type: &event_type,
Offers: &mesosproto.Event_Offers{
Offers: []*mesosproto.Offer{offer},
},
}
f.send(event)
f.AddOffer(offer.Id.GetValue())
}
}
}
type icoder interface {
Encode(v interface{}) error
}
type protobufEncoder struct {
w io.Writer
}
func (pe *protobufEncoder) Encode(v interface{}) error {
data, err := proto.Marshal(v.(proto.Message))
if err != nil {
return err
}
_, err = pe.w.Write(data)
return err
}
func events(frameworkInfo *mesosproto.FrameworkInfo, encoder icoder, res http.ResponseWriter) {
var (
mchan chan *mesosproto.Event
ID string
f *Framework
failover = *timeout
)
res.Header().Set("Connection", "keep-alive")
res.Header().Set("Accept", "application/json")
res.Header().Set("Content-Type", "application/json")
if frameworkInfo.GetFailoverTimeout() != 0 {
failover = frameworkInfo.GetFailoverTimeout()
}
if frameworkInfo.GetId() != nil {
ID = frameworkInfo.Id.GetValue()
//Check that this framework hasn't subsribed yet
f = frameworks.Get(ID)
if f == nil {
http.Error(res, "Unknown framework", 403)
return
}
if f.hasChan() {
http.Error(res, "Framework already subscribed", 403)
return
}
// Resubscribing framework
mchan = f.newChan(res, failover)
// Sending subcribed for resubscribtion
err := sendEvent(encoder, mesosproto.Event_SUBSCRIBED, &mesosproto.Event{
Subscribed: &mesosproto.Event_Subscribed{
FrameworkId: frameworkInfo.Id,
},
})
if err != nil {
http.Error(res, err.Error(), 500)
return
}
} else {
//Create and subscribe framework to chan
ID, err := generateID()
if err != nil {
http.Error(res, err.Error(), 500)
return
}
f = frameworks.New(ID)
mchan = f.newChan(res, failover)
frameworkInfo.Id = &mesosproto.FrameworkID{
Value: &ID,
}
err = sendEvent(encoder, mesosproto.Event_SUBSCRIBED, &mesosproto.Event{
Subscribed: &mesosproto.Event_Subscribed{
FrameworkId: frameworkInfo.Id,
},
})
if err != nil {
http.Error(res, err.Error(), 500)
return
}
}
go sendOffers(f, frameworkInfo.Id)
go sendTasksUpdates(f, frameworkInfo.Id)
for {
mess := <-mchan
if mess == nil {
return
}
if err := sendEvent(encoder, *mess.Type, mess); err != nil {
log.Error(err)
}
}
}