-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodehandle.go
211 lines (190 loc) · 5.98 KB
/
nodehandle.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
/*
Copyright (C) 2003-2011 Institute for Systems Biology
Seattle, Washington, USA.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import (
"encoding/json"
"time"
)
type NodeHandle struct {
NodeId string
Uri string
Hostname string
Master *Master
Con Connection
MaxJobs chan int
Running chan int
Update chan int
BroadcastChan chan *WorkerMessage
}
func NewNodeHandle(n *Connection, m *Master) *NodeHandle {
logger.Debug("NewNodeHandle(%v)", n.isWorker)
con := *n
id := UniqueId()
nh := NodeHandle{NodeId: id,
Uri: "/nodes/" + id,
Hostname: con.Socket.RemoteAddr().String(),
Master: m,
Con: con,
MaxJobs: make(chan int, 1),
Running: make(chan int, 1),
Update: make(chan int, 10),
BroadcastChan: make(chan *WorkerMessage, 0)}
//wait for worker handshake TODO: should this be in monitor???
nh.Running <- 0
logger.Debug("NewNodeHandle(%v) waiting for first message", n.isWorker)
msg := <-nh.Con.InChan
if msg.Type == HELLO {
logger.Printf("Node Hello Body:%v", msg.Body)
val, err := NewHelloMsgBody(msg.Body)
if err != nil {
logger.Warn(err)
return nil
}
nh.MaxJobs <- val.JobCapacity
if val.UniqueId != "" {
nh.NodeId = val.UniqueId
nh.Uri = "/nodes/" + val.UniqueId
<-nh.Running
nh.Running <- val.RunningJobs
}
if val.RunningJobs != 0 {
<-nh.Running
nh.Running <- val.RunningJobs
}
} else {
logger.Debug("%v didn't say hello as first message.", nh.Hostname)
return nil
}
logger.Debug("%v says hello and asks for %v jobs.", nh.Hostname, msg.Body)
return &nh
}
func (nh *NodeHandle) Stats() (processes int, running int) {
//logger.Debug("Stats()")
running = <-nh.Running
nh.Running <- running
processes = <-nh.MaxJobs
nh.MaxJobs <- processes
return
}
func (nh *NodeHandle) ReSize(newMaxJobs int) {
logger.Debug("ReSize(%d)", newMaxJobs)
<-nh.MaxJobs
nh.MaxJobs <- newMaxJobs
}
// turns job into JSON and send to connections outbox. Seems to sleep or deadlock if left alone to long so the worker checks-in every 60 seconds.
func (nh *NodeHandle) SendJob(j *WorkerJob) {
job := *j
logger.Debug("SendJob(%v): %v", job, nh.Hostname)
jobjson, err := json.Marshal(job)
if err != nil {
logger.Warn(err)
}
msg := WorkerMessage{Type: START, Body: string(jobjson)}
nh.Con.OutChan <- msg
running := <-nh.Running
nh.Running <- running + 1
logger.Debug("assigning [%v, %d]", nh.Hostname, running)
nh.Master.GetSub(job.SubId).SubmittedChan <- &SubmitedWorkerJob{j, nh.Hostname}
}
func (nh *NodeHandle) Monitor() {
logger.Debug("Monitor(): [%v]", nh.Hostname)
//control loop
for {
processes, running := nh.Stats()
//logger.Debug("[%v %d %d]", nh.Hostname, processes, running)
switch {
case running < processes:
//logger.Debug("waiting for job or message [%v, %d]", nh.Hostname, running)
select {
case bcMsg := <-nh.BroadcastChan:
logger.Debug("broadcasting [%v, %v]", nh.Hostname, *bcMsg)
nh.Con.OutChan <- *bcMsg
case job := <-nh.Master.jobChan:
nh.SendJob(job)
case <-nh.Update:
case <-time.After(time.Second):
}
default:
//logger.Debug("waiting for message [%v, %v]", nh.Hostname, running)
select {
case bcMsg := <-nh.BroadcastChan:
logger.Debug("broadcasting [%v, %v]", nh.Hostname, *bcMsg)
nh.Con.OutChan <- *bcMsg
case <-nh.Update:
case <-time.After(1 * time.Second):
}
}
}
}
func (nh *NodeHandle) MonitorIO() {
logger.Debug("MonitorIO(): [%v]", nh.Hostname)
for {
msg := <-nh.Con.InChan
nh.HandleWorkerMessage(&msg)
}
}
//handle worker messages and updates the value in nh.Running if appropriate
func (nh *NodeHandle) HandleWorkerMessage(msg *WorkerMessage) {
//logger.Debug("message from: %v", nh.Hostname)
switch msg.Type {
default:
case CHECKIN:
logger.Debug("CHECKIN [%v]", nh.Hostname)
case COUT:
//logger.Debug("COUT [%v]", nh.Hostname)
blocked := true
for blocked == true {
select {
case nh.Master.GetSub(msg.SubId).CoutFileChan <- msg.Body:
blocked = false
case <-time.After(1 * time.Second):
logger.Printf("Sending COUT to subid %v blocked for more then 1 second.", msg.SubId)
}
}
case CERROR:
//logger.Debug("CERROR [%v]", nh.Hostname)
blocked := true
for blocked == true {
select {
case nh.Master.GetSub(msg.SubId).CerrFileChan <- msg.Body:
blocked = false
case <-time.After(1 * time.Second):
logger.Printf("Sending CERROR to subid %v blocked for more then 1 second.", msg.SubId)
}
}
case JOBFINISHED:
go func() {
logger.Debug("JOBFINISHED [%v]", nh.Hostname)
running := <-nh.Running
nh.Running <- running - 1
logger.Debug("JOBFINISHED [%v, %v, %v]", nh.Hostname, msg.Body, running)
nh.Master.GetSub(msg.SubId).FinishedChan <- NewWorkerJob(msg.Body)
nh.Update <- 1
logger.Printf("JOBFINISHED [%v, %v, %v]", nh.Hostname, msg.Body, running)
}()
case JOBERROR:
go func() {
logger.Debug("JOBERROR %v", nh.Hostname)
running := <-nh.Running
nh.Running <- running - 1
logger.Debug("JOBERROR running [%v, %v, %v]", nh.Hostname, msg.Body, running)
nh.Master.GetSub(msg.SubId).ErrorChan <- NewWorkerJob(msg.Body)
nh.Update <- 1
logger.Printf("JOBERROR finished sent: [%v, %v, %v]", nh.Hostname, msg.Body, running)
}()
}
//logger.Debug("message handled: %v", nh.Hostname)
}