-
Notifications
You must be signed in to change notification settings - Fork 5
/
schedproc.go
226 lines (196 loc) · 6.12 KB
/
schedproc.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
package gomes
import (
"code.google.com/p/goprotobuf/proto"
"fmt"
mesos "github.com/vladimirvivien/gomes/mesosproto"
"io/ioutil"
"net"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"sync"
)
var schedIdMutex = new(sync.Mutex)
var schedIdCounter = 0
type schedProcID struct {
prefix string
value string
}
func newSchedProcID(addr string) schedProcID {
cntStr := strconv.Itoa(schedIdCounter)
prefix := MESOS_SCHEDULER_PREFIX + "(" + cntStr + ")"
value := prefix + "@" + addr
id := schedProcID{prefix: prefix, value: value}
schedIdMutex.Lock()
schedIdCounter = schedIdCounter + 1
schedIdMutex.Unlock()
return id
}
func (id *schedProcID) asURL() (*url.URL, error) {
return url.Parse("http://" + id.value)
}
/*
SchedHttpProcess manages http requests from the connected master.
It wraps the standard Http Server.
*/
type schedulerProcess struct {
listener net.Listener
server *http.Server
processId schedProcID
eventMsgQ chan<- interface{}
controlQ chan int32
started bool
aborted bool
}
// newSchedHttpProcess creates and starts htttp process.
func newSchedulerProcess(eventQ chan<- interface{}) (*schedulerProcess, error) {
if eventQ == nil {
return nil, fmt.Errorf("SchedulerProcess - eventQ parameber cannot be nil.")
}
serv := &http.Server{
//Addr: ":0",
//ReadTimeout:nil,
//WriteTimeout:
//ConnState : nil,
}
proc := &schedulerProcess{
server: serv,
eventMsgQ: eventQ,
controlQ: make(chan int32),
started: false,
aborted: false,
}
return proc, nil
}
// start Starts the internal http process to listen to incoming events from Master.
func (proc *schedulerProcess) start() error {
addr := fmt.Sprintf("%s:%d", localIP4String(), nextTcpPort())
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
proc.listener = listener
proc.processId = newSchedProcID(addr)
proc.registerEventHandlers()
// launch internal http.server
go func(lis net.Listener) {
err := proc.server.Serve(lis)
// TODO hack: if error.Op = 'accept' assume connection got closed
// and server.listener stopped blocking on Accept() operation.
val, ok := err.(*net.OpError)
if !ok || val.Op != "accept" {
proc.eventMsgQ <- err
}
}(proc.listener)
// ping proc.server listening at proc.listener.Addr()
rsp, err := http.Get("http://" + proc.listener.Addr().String() + "/isalive")
if err != nil {
return err
}
if rsp.StatusCode != http.StatusOK {
return NewMesosError("SchedProc.server ping failed. Server may not be running.")
}
proc.started = true
return nil
}
// stop Stops the Scheduler process and internal server.
func (proc *schedulerProcess) stop() error {
//TODO Needs a better way than this.
err := proc.listener.Close()
if err != nil {
return err
}
proc.started = false
return nil
}
// registerEventHandlers Registers http handlers for Mesos master events.
func (proc *schedulerProcess) registerEventHandlers() {
//TODO hack: only way to clear default mux.
// fix - maybe use custom mux
http.DefaultServeMux = http.NewServeMux()
http.HandleFunc("/isalive", func(rsp http.ResponseWriter, req *http.Request) {
rsp.WriteHeader(http.StatusOK)
})
http.Handle(makeProcEventPath(proc, FRAMEWORK_REGISTERED_EVENT), proc)
http.Handle(makeProcEventPath(proc, FRAMEWORK_REREGISTERED_EVENT), proc)
http.Handle(makeProcEventPath(proc, RESOURCE_OFFERS_EVENT), proc)
http.Handle(makeProcEventPath(proc, RESCIND_OFFER_EVENT), proc)
http.Handle(makeProcEventPath(proc, STATUS_UPDATE_EVENT), proc)
http.Handle(makeProcEventPath(proc, FRAMEWORK_MESSAGE_EVENT), proc)
http.Handle(makeProcEventPath(proc, LOST_SLAVE_EVENT), proc)
}
func (proc *schedulerProcess) ServeHTTP(rsp http.ResponseWriter, req *http.Request) {
code := http.StatusAccepted
var comment string = ""
// decompose incoming request path of form:
// /scheduler(?)/mesos.internal.<MessageTypeNamee>
_, internalName := path.Split(req.URL.Path) // returns mesos.internal.<MessageTypeName>
messageParts := strings.Split(internalName, ".") // last index is messageType.
// do not continue if marked aborted or stopped.
if proc.aborted || !proc.started {
comment = "SchedProc is either not started or aborted, ignoring call: " + req.URL.Path
err := NewMesosError(comment)
proc.eventMsgQ <- err
code = http.StatusMethodNotAllowed
} else
// if request path is badly formed
if len(messageParts) != 3 {
err := NewMesosError("Event posted by master is malformed:" + req.URL.Path)
proc.eventMsgQ <- err
code = http.StatusBadRequest
comment = "Request path malformed."
} else {
messageType := messageParts[2]
data, err := ioutil.ReadAll(req.Body)
if err != nil {
code = http.StatusBadRequest
comment = "Request body missing."
}
defer req.Body.Close()
// dispatch msg based on type
var msg proto.Message
switch messageType {
case FRAMEWORK_REGISTERED_EVENT:
msg = new(mesos.FrameworkRegisteredMessage)
err = proto.Unmarshal(data, msg)
case FRAMEWORK_REREGISTERED_EVENT:
msg = new(mesos.FrameworkReregisteredMessage)
err = proto.Unmarshal(data, msg)
case RESOURCE_OFFERS_EVENT:
msg = new(mesos.ResourceOffersMessage)
err = proto.Unmarshal(data, msg)
case RESCIND_OFFER_EVENT:
msg = new(mesos.RescindResourceOfferMessage)
err = proto.Unmarshal(data, msg)
case STATUS_UPDATE_EVENT:
msg = new(mesos.StatusUpdateMessage)
err = proto.Unmarshal(data, msg)
case FRAMEWORK_MESSAGE_EVENT:
msg = new(mesos.ExecutorToFrameworkMessage)
err = proto.Unmarshal(data, msg)
case LOST_SLAVE_EVENT:
msg = new(mesos.LostSlaveMessage)
err = proto.Unmarshal(data, msg)
default:
err = fmt.Errorf("Unable to parse event from master: %s unrecognized.", messageType)
code = http.StatusBadRequest
comment = err.Error()
}
if err != nil {
code = http.StatusBadRequest
comment = fmt.Sprintf("Error unmashalling %s: %s", messageType, err.Error())
proc.eventMsgQ <- NewMesosError(comment)
} else {
proc.eventMsgQ <- msg
}
}
rsp.WriteHeader(code)
if comment != "" {
fmt.Fprintln(rsp, comment)
}
}
func makeProcEventPath(proc *schedulerProcess, eventName string) string {
return fmt.Sprintf("/%s/%s%s", proc.processId.prefix, MESOS_INTERNAL_PREFIX, eventName)
}