summaryrefslogtreecommitdiff
path: root/src/master.c
blob: 276166799a7e6301224a39a45c1ae10224b04d51 (plain)
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
349
350
351
352
353
354
355
356
357
358
359
360
#include "master.h"

#include "pthread.h"

#include "nanomsg/nn.h"
#include "nanomsg/survey.h"

#include "json-c/json.h"

#include <stdio.h>
#include <string.h>

static pthread_t master_thread;
static int master_sock;
static int master_must_terminate = 0;

coordinator_t coordinator[MAX_COORDINATORS];

static void recompute_coordinator_states( void )
{
	time_t now;
	time( &now );
	for( int pos = 0; pos < MAX_COORDINATORS; pos++ ) {
		coordinator_t *c = &coordinator[pos];
		if( c->used ) {
			double d = difftime( now, c->lastAlive );
			fprintf( stderr, "%s %lld %lld %g\n", c->host, (long long)c->lastAlive, (long long)now, d );
			if( difftime( now, c->lastAlive ) > MAX_COORDINATOR_AGE ) {
				c->alive = false;
			}
		}
	}
}

static int register_coordinator( json_object *obj )
{
	json_object *host_obj;
	json_object_object_get_ex( obj, "host", &host_obj );
	const char *host = json_object_get_string( host_obj );

	json_object *os_obj;
	json_object_object_get_ex( obj, "os", &os_obj );
	const char *os = json_object_get_string( os_obj );

	json_object *arch_obj;
	json_object_object_get_ex( obj, "arch", &arch_obj );
	const char *arch = json_object_get_string( arch_obj );
	
	json_object *cpus_obj;
	json_object_object_get_ex( obj, "cpus", &cpus_obj );
	unsigned int cpus = json_object_get_int( cpus_obj );
	
	int pos;
	for( pos = 0; pos < MAX_COORDINATORS; pos++ ) {
		coordinator_t *coord = &coordinator[pos];
		if( coord->used &&
			strcmp( coord->host, host ) == 0 ) {
				time( &coord->lastAlive );
				coord->alive = true;
				fprintf( stderr, "master: already registered coordinator from host '%s' (%d)\n", host, pos );
			return 0;
		}
	}
	
	for( pos = 0; pos < MAX_COORDINATORS && coordinator[pos].used; pos++ );
		
	if( pos == MAX_COORDINATORS ) {
		fprintf( stderr, "Can't accept more coordinators, limit reached!\n" );
		return -1;
	}
	coordinator_t *coord = &coordinator[pos];

	printf( "master: registering coordinator from host '%s' (%d)\n", host, pos );
	
	coord->host = strdup( host );
	coord->os = strdup( os );
	coord->arch = strdup( arch );
	coord->cpus = cpus;
	coord->alive = true;
	time( &coord->lastAlive );
	coord->used = true;
	
	json_object *worker_array;
	json_object_object_get_ex( obj, "workers", &worker_array );	
	coord->nof_workers = json_object_array_length( worker_array );

	if( coord->nof_workers >= MAX_WORKERS ) {
		fprintf( stderr, "Can't define more workers per coordinator, limit reached!\n" );		
		return -1;
	}
	
	for( int i = 0; i < coord->nof_workers; i++ ) {
		json_object *worker_obj;
		worker_obj= json_object_array_get_idx( worker_array, i );
		
		json_object *name_obj;
		json_object_object_get_ex( worker_obj, "name", &name_obj );
		const char *name = json_object_get_string( name_obj );

		json_object *mode_obj;
		json_object_object_get_ex( worker_obj, "mode", &mode_obj );
		const char *mode = json_object_get_string( mode_obj );

		json_object *command_obj;
		json_object_object_get_ex( worker_obj, "command", &command_obj );
		const char *command = json_object_get_string( command_obj );
		
		worker_t *w = &coord->worker[i];
		
		w->name = strdup( name );
		w->mode = worker_execution_mode_from_str( mode );
		if( command != NULL ) {
			w->command = strdup( command );
		} else {
			w->mode = WORKER_EXECUTION_DISABLED;
		}
	}
	
	return 0;
}

static char *alloc_message( const char *msg )
{
	char *buf = strdup( msg );
	
	// crashes in survey mode!
	//~ size_t len = strlen( msg );
	//~ char *buf = (char *)nn_allocmsg( len + 1, 0 );
	//~ strncpy( buf, msg, len );

	return buf;
}

static char *create_discover_request( )
{
	json_object *obj = json_object_new_object( );
	json_object *op = json_object_new_string( "discover" );
	json_object_object_add( obj, "op", op );
	json_object *role = json_object_new_string( "master" );
	json_object_object_add( obj, "role", role );

	/* produce message as string, caller must free it */
	const char *msg = json_object_to_json_string( obj );
	char *res = alloc_message( msg );
	
	json_object_put( obj );
	
	return res;
}

static char *create_start_request( const char *name )
{
	json_object *obj = json_object_new_object( );
	json_object *op = json_object_new_string( "start" );
	json_object_object_add( obj, "op", op );
	json_object *role = json_object_new_string( "master" );
	json_object_object_add( obj, "role", role );
	json_object *worker = json_object_new_string( name );
	json_object_object_add( obj, "worker", worker );

	/* produce message as string, caller must free it */
	const char *msg = json_object_to_json_string( obj );
	char *res = alloc_message( msg );
           
	json_object_put( obj );
	
	return res;
}

#define MAX_NOF_COMMANDS 12

static int nof_queued_commands = 0;

char *queued_command[MAX_NOF_COMMANDS];

static pthread_mutex_t queued_command_mutex;

static int enqueue_request( char *cmd )
{
	pthread_mutex_lock( &queued_command_mutex );

	if( nof_queued_commands >= MAX_NOF_COMMANDS ) {
		fprintf( stderr, "ERROR: internal command queue overflow!\n" );
		pthread_mutex_unlock( &queued_command_mutex );
		return -1;
	}
	
	queued_command[nof_queued_commands] = cmd;
	nof_queued_commands++;

	pthread_mutex_unlock( &queued_command_mutex );
	
	return 0;
}

static char *get_next_request( )
{
	pthread_mutex_lock( &queued_command_mutex );
	char *cmd;
	if( nof_queued_commands > 0 ) {
		cmd = queued_command[0];
		--nof_queued_commands;
		memmove( &queued_command[0], &queued_command[1], nof_queued_commands * sizeof( char * ) );
		pthread_mutex_unlock( &queued_command_mutex );
		return cmd;
	} else {
		pthread_mutex_unlock( &queued_command_mutex );
		return create_discover_request( );
	}
}

static void *master_func( void *thread_data )
{
	char *control = (char *)thread_data;

	pthread_mutex_init( &queued_command_mutex, NULL );
	
	if( ( master_sock = nn_socket( AF_SP, NN_SURVEYOR ) ) < 0 ) {
		fprintf( stderr, "master, nn_socket( AF_SP, NN_SURVEYOR ) error: %s (%d)\n", strerror( errno ), errno );
		abort( );
	}
	
	int deadline = 5000;
	if( nn_setsockopt( master_sock, NN_SURVEYOR, NN_SURVEYOR_DEADLINE, &deadline, sizeof( deadline ) ) < 0 ) {
		fprintf( stderr, "master, nn_setsockopt error: %s (%d)\n", strerror( errno ), errno );
		abort( );
	}
	
	(void)nn_bind( master_sock, control );

	printf( "master connected to %s\n", control );

NEXT_COMMAND:
	;
	char *msg = get_next_request( );
	int msg_size = strlen( msg ) + 1;
		
	printf( "master send: %s\n", msg );
	int bytes = nn_send( master_sock, msg, msg_size /* NN_MSG */, 0 );
	if( bytes < 0 ) {
		if( errno == ETERM ) {
			master_must_terminate = 1;
		} else {
			fprintf( stderr, "ERROR: nn_send returned %d: %s (%d)\n",
				bytes, nn_strerror( errno ), errno );
		}
	} else if( bytes != msg_size ) {
		fprintf( stderr, "ERROR: truncated message!" );
	}
	// breaks in coordinator, so the memory is passed between threads?
	free( msg ); msg = NULL;

	while( !master_must_terminate ) {
		
		printf( "master idle: %d\n", master_must_terminate );
		
		recompute_coordinator_states( );
		
		char *answer = NULL;
		bytes = nn_recv( master_sock, &answer, NN_MSG, 0 );
		if( master_must_terminate ) continue;
		if( bytes >= 0 ) {
			json_object *recv_obj = json_tokener_parse( answer );
			const char *recv_obj_str = json_object_to_json_string( recv_obj );
			printf( "master received: %s\n", recv_obj_str );
			json_object *op_obj;
			json_object_object_get_ex( recv_obj, "op", &op_obj );
			const char *op = json_object_get_string( op_obj );

			if( strcmp( op, "register" ) == 0 ) {
				json_object *role_obj;
				json_object_object_get_ex( recv_obj, "role", &role_obj );
				const char *role = json_object_get_string( role_obj );
				if( strcmp( role, "coordinator" ) == 0 ) {
					if( register_coordinator( recv_obj ) < 0 ) {
						fprintf( stderr, "Terminating master because out of resources!\n" );
						master_must_terminate = 1;
						continue;
					}
				}
			}
			
			json_object_put( recv_obj );
			nn_freemsg( answer );
		}
		if( bytes < 0 ) {
			/* documentation says we should get a ETIMEDOUT, but in
			 * fact we get EFSM here, change if fixed in nanomsg */
			if( errno == EFSM /* ETIMEDOUT */ ) {
				goto NEXT_COMMAND;
			} else if( errno == EAGAIN || errno == EINTR ) {
				continue;
			} else if( errno == ETERM ) {
				master_must_terminate = 1;
				continue;
			} else {
				fprintf( stderr, "ERROR: nn_recv returned %d: %s (%d)\n",
					bytes, nn_strerror( errno ), errno );
			}
		}
	}

	(void)nn_shutdown( master_sock, 0 );

	puts( "master disconnected" );
	
	return NULL;
}

int master_init( const char *control )
{
	pthread_attr_t attr;
	int res;
	
	master_must_terminate = 0;

	memset( coordinator, 0, sizeof( coordinator_t * ) * MAX_COORDINATORS );

	res = pthread_attr_init( &attr );
	if( res != 0 ) {
		return 1;
	}
	
	res = pthread_create( &master_thread, &attr, master_func, (void *)control );
	if( res != 0 ) {
		return 1;
	}
		
	return 0;
}

void master_terminate( int terminate_nanomsg )
{
	master_must_terminate = 1;

	if( terminate_nanomsg ) {
		nn_term( );
	}
}

int master_free( )
{
	void *result;
	int res;
	
	res = pthread_join( master_thread, &result );
	if( res != 0 ) {
		return 1;
	}

	return 0;	
}

int master_start_worker( const char *name )
{
	char *msg = create_start_request( name );
	int res = enqueue_request( msg );

	return res;
}