aboutsummaryrefslogtreecommitdiff
path: root/main.cc
blob: 1f7b402f364a814c15bff294fe4ca9fe67c99cd4 (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
/*
 * main.cc: the DMUCS server -- to assign compilation hosts' cpus to
 * requestors, listen for load average messages and monitoring requests,
 * etc.
 *
 * Copyright (C) 2005, 2006  Victor T. Norman
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program 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 General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "dmucs.h"
#include "dmucs_dprop.h"
#include "dmucs_msg.h"
#include "dmucs_hosts_file.h"
#include "dmucs_host.h"
#include "dmucs_db.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fstream>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include "COSMIC/HDR/sockets.h"


static void spawn_stats_thread();
static void spawn_silent_thread();
static void *doSilentSearch(void *bogus);
static void *updateStats(void *bogus);
static void usage(const char *prog);
static void handleReq(Socket *server, DmucsDb *db);
static char* peer2buf(const Socket *server, char *buf);

void addFd(Socket *sock);
void removeFd(Socket *sock);

bool debugMode = false;

std::string hostsInfoFile = HOSTS_INFO_FILE;

static std::list<Socket *> fdList;
static std::map<Socket *, DmucsDprop> dpropMap;


int
main(int argc, char *argv[])
{
    /*
     * Open a socket on which we will:
     *   o receive requests for hosts
     *	   o fork a child process in which we:
     *	     o respond with the highest-tier available cpu, and move the cpu
     *	       into the assignedCpus set.
     *       o wait for the client to close the (slave) socket, indicating
     *	       that the compilation host is done.
     *       o place the compilation cpu back into the db of available cpus
     *   o receive load average messages from hosts
     *	   o recompute the new tier value for the host.  If it is different
     *       from the current tier, move the host in the availHosts map to
     *	     the new tier.
     *   o receive status messages (available|unavailable) from hosts
     *	   o if available, get the host information from the hosts-info
     *	     file and add the host to the availHosts data structure.
     *	   o if unavailable, remove the host from whatever set it is in.
     *   o receive monitoring requests from the monitoring clients.
     * 	   o package up the data structures and send the info in the reply.
     * 
     * Command-line arguments:
     *   o -D: display debugging output.  (Assumes -s.) Optional.
     *   o -p <port>: use <port>.  Required.
     *   o -s: do not fork as a daemon.
     */


    /*
     * Process command-line arguments:
     *
     * -p <port>, --port <port>: the port number to listen on (default: 9714).
     * -D, --debug: debug mode (default: off)
     * -H, --hosts-info-file <filename>: specify the hosts info file location.
     */

    int serverPortNum = SERVER_PORT_NUM;

#ifndef HAVE_GETHOSTBYADDR_R
#ifdef HAVE_GETHOSTBYADDR
       pthread_mutex_init(&gethost_mutex, NULL);
#endif /* HAVE_GETHOSTBYADDR */
#endif /* !HAVE_GETHOSTBYADDR_R */


    for (int i = 1; i < argc; i++) {
	if (strequ("-p", argv[i]) || strequ("--port", argv[i])) {
	    if (++i >= argc) {
		usage(argv[0]);
		return -1;
	    }
	    serverPortNum = atoi(argv[i]);
	} else if (strequ("-D", argv[i]) || strequ("--debug", argv[i])) {
	    debugMode = true;
	} else if (strequ("-H", argv[i]) ||
		   strequ("--hosts-info-file", argv[i])) {
	    if (++i >= argc) {
		usage(argv[0]);
		return -1;
	    }
	    hostsInfoFile = argv[i];
	} else {
	    usage(argv[0]);
	    return -1;
	}
    }


    /*
     * Make the database.
     */
    DmucsDb *db = DmucsDb::getInstance();

    /*
     * Open the socket.
     */
    char svrstr[16];
    sprintf(svrstr, "s%d", serverPortNum);
    Socket *server = Sopen(NULL, svrstr);
    if (!server) {
	fprintf(stderr, "Could not open server on port 9714.\n");
	return -1;
    }

    /*
     * Spawn a thread to periodically search the database for hosts
     * that have been silent.  Move these hosts to the SILENT state.
     */
    spawn_silent_thread();

    /*
     * Spawn a thread to periodically collect statistics and print them
     * out.
     */
    spawn_stats_thread();


    Smaskset(server);

    /* Process requests, forever!!!  Bwa, ha, ha! */
    while (1) {

	DMUCS_DEBUG((stderr, "\n------- Server: calling select ---------\n"));
               
	int result = Smaskwait();
	DMUCS_DEBUG((stderr, "select returned %d\n", result));

	if (result > 0) {	// something is available to be read

	    std::list<Socket*>::const_iterator it;
	    for (it = fdList.begin(); it != fdList.end(); ++it) {
		if (Smaskisset(*it)) {
		    DMUCS_DEBUG((stderr,
				 "\n--- Server: Handle client request ---\n"));
		    handleReq(*it, db);
		    /* handleReq could change the list so we have to jump out
		       here. */
		    break;
		}
	    }
	    if (Smaskisset(server)) {
		Socket *sock_req = Saccept(server);
		if (sock_req == NULL) {
		    DMUCS_DEBUG((stderr, "ERROR: Saccept returns 0: %s\n",
				 strerror(errno)));
		} else {
		    addFd(sock_req);
		    handleReq(sock_req, db);
		}
	    }
	} else if (result < 0) {
	    // Error condition
	    fprintf(stderr, "ERROR: result %d\n", result);
	} else { // result == 0.
	    fprintf(stderr, "Select timeout...\n");
	}
    }

#ifndef HAVE_GETHOSTBYADDR_R
#ifdef HAVE_GETHOSTBYADDR
    pthread_mutex_destroy(&gethost_mutex);
#endif /* HAVE_GETHOSTBYADDR */
#endif /* !HAVE_GETHOSTBYADDR_R */
}




static void
spawn_silent_thread()
{
    pthread_attr_t tattr;
    pthread_attr_init(&tattr);
    /* We don't care about joining up this thread with its parent -- it
       won't matter because both will die off together -- when the
       server is killed. */
    pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
    pthread_t thread_id;
    if (pthread_create(&thread_id, &tattr, doSilentSearch,
		       (void *) NULL) != 0) {
	perror("pthread_create");
	return;
    }
}


static void
spawn_stats_thread()
{
    pthread_attr_t tattr;
    pthread_attr_init(&tattr);
    /* We don't care about joining up this thread with its parent -- it
       won't matter because both will die off together -- when the
       server is killed. */
    pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
    pthread_t thread_id;
    if (pthread_create(&thread_id, &tattr, updateStats, (void *) NULL) != 0) {
	perror("pthread_create");
	return;
    }
}


static void *
doSilentSearch(void *bogus /* not used */)
{
    while (1) {
	struct timeval t = { 60L, 0L };		// 60 seconds.
	select(0, NULL, NULL, NULL, &t);
	DmucsDb::getInstance()->handleSilentHosts();
    }
}


static void *
updateStats(void *bogus /* not used */)
{
    int served, max, avail;
    char buf[32];
    while (1) {
	DmucsDb::getInstance()->getStatsFromDb(&served, &max, &avail);
	time_t t = time(NULL);
	(void) ctime_r(&t, buf);
	/* There is a newline on the end of buf -- remove it. */
	buf[strlen(buf) - 1] = '\0';
	fprintf(stderr, "[%s] Hosts Served: %d  Max/Avail: %d/%d\n",
		buf, served, max, avail);

	struct timeval sleepTime = { 60L, 0L };		// 60 seconds.
	select(0, NULL, NULL, NULL, &sleepTime);
    }
}


static void
handleReq(Socket *sock_req, DmucsDb *db)
{
    char buf[BUFSIZE];

    DMUCS_DEBUG((stderr, "New request from %s\n", peer2buf(sock_req, buf)));

    if (Sgets(buf, BUFSIZE, sock_req) == NULL) {
	DMUCS_DEBUG((stderr, "Socket closed: %s\n", peer2buf(sock_req, buf)));
	db->releaseCpu(sock_req);
	removeFd(sock_req);
	return;
    }

    DmucsMsg *msg = DmucsMsg::parseMsg(sock_req, buf);
    if (msg == NULL) {
	fprintf(stderr, "Got bad message on socket.  Continuing.\n");
	removeFd(sock_req);
	return;
    }

    msg->handle(sock_req, buf);
    delete msg;
}


static char *
peer2buf(const Socket *sock, char *buf)
{
    struct sockaddr sck;
    socklen_t s = sizeof(sck);
    getpeername(sock->skt, &sck, &s);
    struct sockaddr_in *sin = (struct sockaddr_in *) &sck;
       
    sprintf(buf, "%s:%d", inet_ntoa(sin->sin_addr), sin->sin_port);
    return buf;     
}


void
addFd(Socket *sock)
{
    fdList.push_back(sock);
    Smaskset(sock);
}


void
removeFd(Socket *sock)
{
    Smaskunset(sock);
    fdList.remove(sock);
    Sclose(sock);
}



static void
usage(const char *prog)
{
    fprintf(stderr, "Usage: %s [-p|--port <port>] [-D|--debug] "
	    "[-H|--hosts-info-file <file>]\n\n", prog);
}