-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode-list.c
362 lines (312 loc) · 9.71 KB
/
node-list.c
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
361
362
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <string.h>
#include <stdlib.h> // malloc()
#include <xtend/dsv.h> // xt_dsv_read_field()
#include <xtend/file.h>
#include <xtend/string.h> // strlcpy() on Linux
#include "node-list-private.h"
#include "network.h"
#include "lpjs.h"
#include "misc.h"
/***************************************************************************
* Use auto-c2man to generate a man page from this comment
*
* Name:
* -
*
* Library:
* #include <>
* -l
*
* Description:
*
* Arguments:
*
* Returns:
*
* Examples:
*
* Files:
*
* Environment
*
* See also:
*
* History:
* Date Name Modification
* 2024-02-23 Jason Bacon Begin
***************************************************************************/
node_list_t *node_list_new(void)
{
node_list_t *new_list;
if ( (new_list = malloc(sizeof(node_list_t))) == NULL )
{
lpjs_log("%s(): Error: malloc() failed.\n", __FUNCTION__);
exit(EX_UNAVAILABLE);
}
node_list_init(new_list);
return new_list;
}
/***************************************************************************
* Description:
* Constructor for node_list_t
*
* History:
* Date Name Modification
* 2021-09-24 Jason Bacon Begin
***************************************************************************/
void node_list_init(node_list_t *node_list)
{
node_list->head_node = NULL;
node_list->compute_node_count = 0;
}
/***************************************************************************
* Description:
* Update state and specs of a node after receiving info, e.g. from
* lpjs_compd
*
* History:
* Date Name Modification
* 2021-10-02 Jason Bacon Begin
***************************************************************************/
void node_list_update_compute(node_list_t *node_list, node_t *node)
{
size_t c;
char *hostname;
// Note: FQDN is required
hostname = node_get_hostname(node);
for (c = 0; c < node_list->compute_node_count; ++c)
{
if ( strcmp(node_get_hostname(node_list->compute_nodes[c]), hostname) == 0 )
{
// lpjs_debug("Updating compute node %zu %s\n", c, NODE_HOSTNAME(node_list->compute_nodes[c]));
node_set_state(node_list->compute_nodes[c], "up");
// processors and phys_MiB may have been set in config file
// Don't overwrite them with auto-detected specs
if ( node_get_processors(node_list->compute_nodes[c]) == 0 )
node_set_processors(node_list->compute_nodes[c], node_get_processors(node));
if ( node_get_phys_MiB(node_list->compute_nodes[c]) == 0 )
node_set_phys_MiB(node_list->compute_nodes[c], node_get_phys_MiB(node));
node_set_zfs(node_list->compute_nodes[c], node_get_zfs(node));
node_set_os(node_list->compute_nodes[c], strdup(node_get_os(node)));
node_set_arch(node_list->compute_nodes[c], strdup(node_get_arch(node)));
node_set_msg_fd(node_list->compute_nodes[c], node_get_msg_fd(node));
node_set_last_ping(node_list->compute_nodes[c], node_get_last_ping(node));
return;
}
}
}
/***************************************************************************
* Description:
* Send current node list to msg_fd in human-readable format
*
* History:
* Date Name Modification
* 2021-09-26 Jason Bacon Begin
***************************************************************************/
void node_list_send_status(int msg_fd, node_list_t *node_list)
{
unsigned c,
processors_up,
processors_up_used,
processors_down;
unsigned long mem_up,
mem_up_used,
mem_down;
char temp[LPJS_MSG_LEN_MAX + 1],
outgoing_msg[LPJS_MSG_LEN_MAX + 1];
outgoing_msg[0] = '\0';
snprintf(temp, LPJS_MSG_LEN_MAX,
NODE_STATUS_HEADER_FORMAT, "Hostname", "State",
"Procs", "Used", "PhysMiB", "Used", "OS", "Arch");
strlcat(outgoing_msg, temp, LPJS_MSG_LEN_MAX + 1);
processors_up = processors_up_used = processors_down = 0;
mem_up = mem_up_used = mem_down = 0;
for (c = 0; c < node_list->compute_node_count; ++c)
{
node_status_to_str(node_list->compute_nodes[c], temp, LPJS_MSG_LEN_MAX + 1);
strlcat(outgoing_msg, temp, LPJS_MSG_LEN_MAX + 1);
if ( strcmp(node_get_state(node_list->compute_nodes[c]), "up") == 0 )
{
processors_up += node_get_processors(node_list->compute_nodes[c]);
processors_up_used += node_get_processors_used(node_list->compute_nodes[c]);
mem_up += node_get_phys_MiB(node_list->compute_nodes[c]);
mem_up_used += node_get_phys_MiB_used(node_list->compute_nodes[c]);
}
else
{
processors_down += node_get_processors(node_list->compute_nodes[c]);
mem_down += node_get_phys_MiB(node_list->compute_nodes[c]);
}
}
// lpjs_debug("Sending summary...\n");
snprintf(temp, LPJS_MSG_LEN_MAX + 1,
"\n" NODE_STATUS_FORMAT, "Total", "up",
processors_up, processors_up_used, mem_up, mem_up_used, "-", "-");
strlcat(outgoing_msg, temp, LPJS_MSG_LEN_MAX + 1);
snprintf(temp, LPJS_MSG_LEN_MAX,
NODE_STATUS_FORMAT, "Total", "down",
processors_down, 0, mem_down, (size_t)0, "-", "-");
strlcat(outgoing_msg, temp, LPJS_MSG_LEN_MAX + 1);
// Only dispatchd calls this function, so wait for client to close first
if ( lpjs_send_munge(msg_fd, outgoing_msg,
lpjs_dispatchd_safe_close) != LPJS_MSG_SENT )
{
lpjs_log("%s(): Error: Failed to send node list info.\n", __FUNCTION__);
lpjs_dispatchd_safe_close(msg_fd);
return; // FIXME: Define return codes
}
/*
* Closing the listener first results in "address already in use"
* errors on restart. Send an EOT character to signal the end of
* transmission, so the client can close first and avoid a wait
* state for the socket.
*/
if ( lpjs_send_munge(msg_fd, LPJS_EOT_MSG, lpjs_dispatchd_safe_close)
!= LPJS_MSG_SENT )
lpjs_log("%s(): Error: Failed to send EOT.\n", __FUNCTION__);
else
lpjs_log("%s(): EOT sent.\n", __FUNCTION__);
}
/***************************************************************************
* Use auto-c2man to generate a man page from this comment
*
* Name:
* -
*
* Library:
* #include <>
* -l
*
* Description:
*
* Arguments:
*
* Returns:
*
* Examples:
*
* Files:
*
* Environment
*
* See also:
*
* History:
* Date Name Modification
* 2024-02-24 Jason Bacon Begin
***************************************************************************/
int node_list_add_compute_node(node_list_t *node_list, node_t *node)
{
if ( node_list->compute_node_count == LPJS_MAX_NODES )
{
lpjs_log("%s(): Error: LPJS_MAX_NODES reached. Cannot add new node.\n", __FUNCTION__);
return -1;
}
// lpjs_debug("%s(): Adding %s\n", __FUNCTION__, node_get_hostname(node));
node_list->compute_nodes[node_list->compute_node_count++] = node;
return 0; // FIXME: Define return codes
}
node_t *node_list_find_hostname(node_list_t *node_list, const char *hostname)
{
int c;
node_t *node;
for (c = 0; c < node_list->compute_node_count; ++c)
{
node = node_list->compute_nodes[c];
// lpjs_debug("%s(): Checking %s\n", __FUNCTION__, node_get_hostname(node));
if ( strcmp(node_get_hostname(node), hostname) == 0 )
return node;
}
// hostname not found
return NULL;
}
/***************************************************************************
* Use auto-c2man to generate a man page from this comment
*
* Name:
* -
*
* Library:
* #include <>
* -l
*
* Description:
*
* Arguments:
*
* Returns:
*
* Examples:
*
* Files:
*
* Environment
*
* See also:
*
* History:
* Date Name Modification
* 2024-05-10 Jason Bacon Begin
***************************************************************************/
int node_list_set_state(node_list_t *node_list, char *arg_string,
uid_t munge_uid, int msg_fd)
{
char *p,
*state,
*node_name;
node_t *node;
if ( (munge_uid != 0) && (munge_uid != getuid()) )
{
if ( lpjs_send_munge(msg_fd, "Only root or the user running lpjs_dispatchd can change node states.\n"
LPJS_EOT_MSG,
lpjs_dispatchd_safe_close) != LPJS_MSG_SENT )
{
lpjs_log("%s(): Error: Failed to send node status.\n", __FUNCTION__);
lpjs_dispatchd_safe_close(msg_fd);
// return 1
}
return 1;
}
p = arg_string;
state = strsep(&p, " ");
// Point state to static data so it will survive function exit
if ( strcmp(state, "paused") == 0 )
state = "paused";
else if ( strcmp(state, "updating") == 0 )
state = "updating";
else
state = "up";
node_name = strsep(&p, " ");
// nodes.c ensures that "all" is the only argument
if ( strcmp(node_name, "all") == 0 )
{
lpjs_debug("Setting all nodes to %s...\n", state);
for (size_t c = 0; c < node_list->compute_node_count; ++c)
node_set_state(node_list->compute_nodes[c], state);
}
else
{
while ( node_name != NULL )
{
node = node_list_find_hostname(node_list, node_name);
if ( node == NULL )
lpjs_log("%s(): Error: Node %s not found.\n", __FUNCTION__, node_name);
else
{
lpjs_debug("Setting node %s to %s...\n", node_name, state);
// FIXME: Save state and restore upon dispatchd restart
node_set_state(node, state);
}
node_name = strsep(&p, " ");
}
}
if ( lpjs_send_munge(msg_fd, LPJS_EOT_MSG, lpjs_dispatchd_safe_close)
!= LPJS_MSG_SENT )
lpjs_log("%s(): Error: Failed to send EOT.\n", __FUNCTION__);
else
lpjs_log("%s(): EOT sent.\n", __FUNCTION__);
return 0; // FIXME: Define return codes
}