The expiration time for new job artifacts in CI/CD pipelines is now 30 days (GitLab default). Previously generated artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

dcdbslurmjob.cpp 12.5 KB
Newer Older
1
2
//================================================================================
// Name        : dcdbslurmjob.cpp
Micha Müller's avatar
Micha Müller committed
3
// Author      : Michael Ott, Micha Mueller
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Copyright   : Leibniz Supercomputing Centre
// Description : Main file of the dcdbslurmjob command line utility
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2011-2019 Leibniz Supercomputing Centre
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================

Micha Müller's avatar
Micha Müller committed
27
#include "../../common/include/globalconfiguration.h"
28
#include "timestamp.h"
Micha Müller's avatar
Micha Müller committed
29
#include <boost/algorithm/string.hpp>
30
#include <boost/regex.hpp>
Micha Müller's avatar
Micha Müller committed
31
32
33
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <cstdlib>
34
#include <dcdb/connection.h>
Alessio Netti's avatar
Alessio Netti committed
35
#include <dcdb/jobdatastore.h>
Micha Müller's avatar
Micha Müller committed
36
37
#include <iostream>
#include <mosquitto.h>
38
39
40
#include "dcdb/version.h"
#include "version.h"

Alessio Netti's avatar
Alessio Netti committed
41

42
43
44
45
46
47
48
49
50
51
52
53
#define SLURMJOBTIMEOUT 60000000000


int msgId = -1;
bool done = false;

void publishCallback(struct mosquitto *mosq, void *obj, int mid) {
	if(msgId != -1 && mid == msgId)
		done = true;
}


Alessio Netti's avatar
Alessio Netti committed
54
55
56
57
58
/*
 * Print usage information
 */
void usage() {
    std::cout << "Usage:" << std::endl;
Micha Müller's avatar
Micha Müller committed
59
    std::cout << "  dcdbslurmjob [-b<host>] [-t<timestamp>] [-n<nodelist>] [-j<jobid>] [-i<userid>] start|stop" << std::endl;
60
    std::cout << "  dcdbslurmjob [-c<host>] [-u<username>] [-p<password>] [-t<timestamp>] [-n<nodelist>] [-j<jobid>] [-i<userid>] start|stop" << std::endl;
Alessio Netti's avatar
Alessio Netti committed
61
62
63
64
    std::cout << "  dcdbslurmjob -h" << std::endl;
    std::cout << std::endl;

    std::cout << "Options:" << std::endl;
Micha Müller's avatar
Micha Müller committed
65
    std::cout << "  -b<host>      MQTT broker              [default: 127.0.0.1:1883]" << std::endl;
66
67
68
    std::cout << "  -c<host>      Cassandra host           [default: 127.0.0.1:9042]" << std::endl;
    std::cout << "  -u<username>  Cassandra username       [default: none]" << std::endl;
    std::cout << "  -p<password>  Cassandra password       [default: none]" << std::endl;
Alessio Netti's avatar
Alessio Netti committed
69
70
71
72
73
74
75
    std::cout << "  -t<timestamp> Timestamp value          [default: now]" << std::endl;
    std::cout << "  -n<nodelist>  Comma-separated nodelist [default: SLURM_JOB_NODELIST]" << std::endl;
    std::cout << "  -j<jobid>     Numerical job id         [default: SLURM_JOB_ID var]" << std::endl;
    std::cout << "  -i<userid>    Numerical user id        [default: SLURM_JOB_USER var]" << std::endl;
    std::cout << std::endl;
    std::cout << "  -h            This help page" << std::endl;
    std::cout << std::endl;
76
    std::cout << "Options -b and -c|u|p are mutual exclusive! If both are specified, the latter takes precedence. By default MQTT broker is specified." << std::endl;
Alessio Netti's avatar
Alessio Netti committed
77
}
78
79
80
81

std::string getEnv(const char* var) {
    char* str = std::getenv(var);
    if (str != NULL) {
Alessio Netti's avatar
Alessio Netti committed
82
	    return std::string(str);
83
    } else {
Alessio Netti's avatar
Alessio Netti committed
84
85
86
87
	    return std::string("");
    }
}

88
void splitNodeList(const std::string& str, DCDB::NodeList& nl)
Alessio Netti's avatar
Alessio Netti committed
89
90
{
    nl.clear();
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
    std::string s1 = str;
    boost::regex r1("([^,[]+)(\\[[0-9,-]+\\])?(,|$)", boost::regex::extended);
    boost::smatch m1;
    while (boost::regex_search(s1, m1, r1)) {
	std::string hostBase = m1[1].str();
	
	if (m1[2].str().size() == 0) {
	    nl.push_back(hostBase);
	} else {
	    std::string s2 = m1[2].str();
	    boost::regex r2("([0-9]+)-?([0-9]+)?(,|\\])", boost::regex::extended);
	    boost::smatch m2;
	    while (boost::regex_search(s2, m2, r2)) {
		if (m2[2] == "") {
		    nl.push_back(hostBase + m2[1].str());
		} else {
		    int start = atoi(m2[1].str().c_str());
		    int stop = atoi(m2[2].str().c_str());
		    for (int i=start; i<=stop; i++) {
			std::stringstream ss;
			ss << std::setw(m2[2].str().length()) << std::setfill('0') << i;
			nl.push_back(hostBase + ss.str());
		    }
		}
		s2 = m2.suffix().str();
	    }
	}
	s1 = m1.suffix().str();
119
120
121
    }
}

Micha Müller's avatar
Micha Müller committed
122
/**
123
124
125
 * Retrieves Slurm job data from environment variables and sends it to either a
 * CollectAgent or a Cassandra database. Job data can also be passed as command
 * line options.
Micha Müller's avatar
Micha Müller committed
126
 */
127
128
int main(int argc, char** argv) {
    std::cout << "dcdbslurmjob " << VERSION << std::endl << std::endl;
129
130
131
132
133
134
135

    bool                cassandra = false;
    DCDB::Connection *  dcdbConn = nullptr;
    DCDB::JobDataStore *myJobDataStore = nullptr;
    struct mosquitto *  _mosq = nullptr;

    std::string host = "127.0.0.1", cassandraPort = "9042", cassandraUser = "", cassandraPassword = "";
Micha Müller's avatar
Micha Müller committed
136
    int         brokerPort = 1883;
Alessio Netti's avatar
Alessio Netti committed
137
138
    std::string nodelist="", jobId="", userId="";
    uint64_t ts=0;
139
    
Alessio Netti's avatar
Alessio Netti committed
140
    // Defining options
141
    const char *opts = "b:c:u:p:n:t:j:i:h";
Alessio Netti's avatar
Alessio Netti committed
142
143
144
145
146
147
148
149
150
151
152

    char ret;
    while ((ret = getopt(argc, argv, opts))!=-1) {
        switch (ret)
        {
            case 'h':
                usage();
                return 0;
            default:
                break;
        }
153
154
    }
    
Alessio Netti's avatar
Alessio Netti committed
155
156
157
158
159
160
    if (argc < 2) {
        std::cerr << "At least one argument is required: start or stop" << std::endl;
        return 1;
    } else if(!boost::iequals(argv[argc-1], "start") && !boost::iequals(argv[argc-1], "stop")) {
        std::cerr << "Unsupported action: must either be start or stop" << std::endl;
        return 1;
161
162
    }
    
Alessio Netti's avatar
Alessio Netti committed
163
164
165
    optind = 1;
    while ((ret=getopt(argc, argv, opts))!=-1) {
        switch(ret) {
Micha Müller's avatar
Micha Müller committed
166
		case 'b': {
167
168
			cassandra = false;
			host = parseNetworkHost(optarg);
Micha Müller's avatar
Micha Müller committed
169
170
171
172
173
174
175
176
			std::string port = parseNetworkPort(optarg);
			if (port != "") {
				brokerPort = std::stoi(port);
			} else {
				brokerPort = 1883;
			}
			break;
		}
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
		case 'c':
			cassandra = true;
			host = parseNetworkHost(optarg);
			cassandraPort = parseNetworkPort(optarg);
			if (cassandraPort == "")
				cassandraPort = std::string("9042");
			break;
		case 'u':
			cassandra = true;
			cassandraUser = optarg;
			break;
		case 'p': {
			cassandra = true;
			cassandraPassword = optarg;
			// What does this do? Mask the password?
			size_t pwdLen = strlen(optarg);
			memset(optarg, 'x', (pwdLen >= 3) ? 3 : pwdLen);
			if (pwdLen > 3) {
				memset(optarg + 3, 0, pwdLen - 3);
			}
			break;
		}
Micha Müller's avatar
Micha Müller committed
199
	    case 'n':
Alessio Netti's avatar
Alessio Netti committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
                nodelist = optarg;
                break;
            case 't':
                ts = std::stoull(optarg);
                break;
            case 'j':
                jobId = optarg;
                break;
            case 'i':
                userId = optarg;
                break;
            case 'h':
            default:
                usage();
                return 1;
        }
    }

218
219
220
    if (cassandra) {
	    //Allocate and initialize connection to Cassandra.
	    dcdbConn = new DCDB::Connection(host, atoi(cassandraPort.c_str()), cassandraUser, cassandraPassword);
Micha Müller's avatar
Micha Müller committed
221

222
223
224
225
226
227
228
229
	    if (!dcdbConn->connect()) {
		    std::cerr << "Cannot connect to Cassandra!" << std::endl;
		    return 1;
	    }
	    myJobDataStore = new DCDB::JobDataStore(dcdbConn);
    } else {
	    //Initialize Mosquitto library and connect to broker
	    char hostname[256];
Micha Müller's avatar
Micha Müller committed
230

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
	    if (gethostname(hostname, 255) != 0) {
		    std::cerr << "Cannot get hostname!" << std::endl;
		    return 1;
	    }
	    hostname[255] = '\0';
	    mosquitto_lib_init();
	    _mosq = mosquitto_new(hostname, false, NULL);
	    if (!_mosq) {
		    perror(NULL);
		    return 1;
	    }

	    if (mosquitto_connect(_mosq, host.c_str(), brokerPort, 1000) != MOSQ_ERR_SUCCESS) {
		    std::cerr << "Could not connect to MQTT broker " << host << ":" << std::to_string(brokerPort) << std::endl;
		    return 1;
	    }
Alessio Netti's avatar
Alessio Netti committed
247
    }
Micha Müller's avatar
Micha Müller committed
248
249

    //collect job data
Alessio Netti's avatar
Alessio Netti committed
250
    DCDB::JobData jd;
Micha Müller's avatar
Micha Müller committed
251
252
    int           retCode = 0;

Alessio Netti's avatar
Alessio Netti committed
253
254
    if(ts==0)
        ts = getTimestamp();
255
    if(jobId=="") {
Alessio Netti's avatar
Alessio Netti committed
256
        jobId = getEnv("SLURM_JOB_ID");
257
258
259
260
	if(jobId=="") {
	    jobId = getEnv("SLURM_JOBID");
	}
    }
Alessio Netti's avatar
Alessio Netti committed
261
262
263
264
265
    
    if (boost::iequals(argv[argc-1], "start")) {
        
        if(userId=="")
            userId = getEnv("SLURM_JOB_USER");
266
        if(nodelist=="") {
Alessio Netti's avatar
Alessio Netti committed
267
            nodelist = getEnv("SLURM_JOB_NODELIST");
268
269
270
271
272
	    if(nodelist=="") {
		nodelist = getEnv("SLURM_NODELIST");
	    }
	}

273
274
275
276
277
278
279
280
281
282
283
284
285
	DCDB::NodeList nl;
	splitNodeList(nodelist, nl);

	std::cout << "JOBID    = " << jobId << std::endl;
        std::cout << "USER     = " << userId << std::endl;
        std::cout << "START    = " << ts << std::endl;
        std::cout << "NODELIST = " << nodelist << std::endl;
	std::cout << "NODES    =";
	for (auto &n: nl) {
	    std::cout << " " << n;
	}
	std::cout << std::endl;

Alessio Netti's avatar
Alessio Netti committed
286
        try {
287
288
            jd.jobId     = jobId;
            jd.userId    = userId;
Alessio Netti's avatar
Alessio Netti committed
289
290
291
292
            jd.startTime = DCDB::TimeStamp(ts);
            jd.endTime   = DCDB::TimeStamp((uint64_t)0);
            jd.nodes     = nl;
        } catch(const std::invalid_argument& e) {
293
294
295
296
297
298
299
300
301
		std::cerr << "Invalid input format!" << std::endl;
		retCode = 1;
		goto exit;
	}

	if (cassandra && (myJobDataStore->insertJob(jd) != DCDB::JD_OK)) {
		std::cerr << "Job data insert failed!" << std::endl;
		retCode = 1;
		goto exit;
Micha Müller's avatar
Micha Müller committed
302
	}
Alessio Netti's avatar
Alessio Netti committed
303
304
305
306
307
308
    } else if (boost::iequals(argv[argc-1], "stop")) {
        
        std::cout << "JOBID = " << jobId << std::endl;
        std::cout << "STOP  = " << ts << std::endl;
        
        try {
Micha Müller's avatar
Micha Müller committed
309
310
		jd.jobId = jobId;
		jd.endTime = DCDB::TimeStamp(ts);
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
	} catch (const std::invalid_argument &e) {
		std::cerr << "Invalid input format!" << std::endl;
		retCode = 1;
		goto exit;
	}

	if (cassandra) {
		DCDB::JobData jobStart;
		if (myJobDataStore->getJobById(jobStart, jd.jobId) != DCDB::JD_OK) {
			std::cerr << "Could not retrieve job to be updated!" << std::endl;
			retCode = 1;
			goto exit;
		}
		if (myJobDataStore->updateEndtime(jobStart.jobId, jobStart.startTime, jd.endTime) != DCDB::JD_OK) {
			std::cerr << "Could not update end time of job!" << std::endl;
			retCode = 1;
			goto exit;
		}
Micha Müller's avatar
Micha Müller committed
329
330
331
	}
    }

332
333
334
335
    //Message sent to CollectAgent is independent of start/stop. We send the
    //same JSON in either case. CA does job insert or update depending
    //on job endtime value.
    if (!cassandra) {
Micha Müller's avatar
Micha Müller committed
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
	    //create job data string in JSON format
	    std::string                 payload = "";
	    std::string                 topic = "/DCDB_JOBDATA/"; //do not change or keep in sync with simplemqttservermessage.h
	    boost::property_tree::ptree config;
	    std::ostringstream          output;
	    config.clear();
	    config.push_back(boost::property_tree::ptree::value_type("jobid", boost::property_tree::ptree(jd.jobId)));
	    config.push_back(boost::property_tree::ptree::value_type("userid", boost::property_tree::ptree(jd.userId)));
	    config.push_back(boost::property_tree::ptree::value_type("starttime", boost::property_tree::ptree(std::to_string(jd.startTime.getRaw()))));
	    config.push_back(boost::property_tree::ptree::value_type("endtime", boost::property_tree::ptree(std::to_string(jd.endTime.getRaw()))));
	    boost::property_tree::ptree nodes;
	    for (const auto &n : jd.nodes) {
		    nodes.push_back(boost::property_tree::ptree::value_type("", boost::property_tree::ptree(n)));
	    }
	    config.push_back(boost::property_tree::ptree::value_type("nodes", nodes));
	    boost::property_tree::write_json(output, config, true);
	    payload = output.str();

	    //std::cout << "Payload:\n" << payload << std::endl;
355
356
357
	    
	    mosquitto_publish_callback_set(_mosq, publishCallback);
	    uint64_t startTs = getTimestamp();
Micha Müller's avatar
Micha Müller committed
358
	    //send it to broker
359
	    if (mosquitto_publish(_mosq, &msgId, topic.c_str(), payload.length(), payload.c_str(), 1, false) != MOSQ_ERR_SUCCESS) {
360
		    std::cerr << "Broker not reachable! Job data was not published." << std::endl;
Micha Müller's avatar
Micha Müller committed
361
362
363
		    retCode = 1;
		    goto exit;
	    }
364
365
366
367
368
369
370
371
	    
	    do {
			if (mosquitto_loop(_mosq, -1, 1) != MOSQ_ERR_SUCCESS) {
				std::cerr << "Error in mosquitto_loop!" << std::endl;
				retCode = 1;
				goto exit;
			}
		} while(!done && getTimestamp() - startTs < SLURMJOBTIMEOUT);
Alessio Netti's avatar
Alessio Netti committed
372
373
    }

Micha Müller's avatar
Micha Müller committed
374
375
//hasta la vista
exit:
376
377
378
379
380
381
382
383
384
	if (cassandra) {
		delete myJobDataStore;
		dcdbConn->disconnect();
		delete dcdbConn;
	} else {
		mosquitto_disconnect(_mosq);
		mosquitto_destroy(_mosq);
		mosquitto_lib_cleanup();
	}
Micha Müller's avatar
Micha Müller committed
385
	return retCode;
386
}