jobaction.cpp 7.64 KB
Newer Older
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
//================================================================================
// Name        : jobaction.cpp
// Author      : Alessio Netti
// Contact     : info@dcdb.it
// Copyright   : Leibniz Supercomputing Centre
// Description : Implementation for performing actions on the DCDB Database
//================================================================================

//================================================================================
// 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.
//================================================================================

#include <iostream>
#include <string>

#include <boost/lexical_cast.hpp>

#include "jobaction.h"

/*
 * Print the help for the SENSOR command
 */
void JobAction::printHelp(int argc, char* argv[])
{
    /*            01234567890123456789012345678901234567890123456789012345678901234567890123456789 */
    std::cout << "JOB command help" << std::endl << std::endl;
    std::cout << "The JOB command has the following options:" << std::endl;
    std::cout << "   SHOW <jobid>                - Shows information for a certain <jobid>" << std::endl;
    std::cout << "   LIST                        - Lists all job IDs stored in the database" << std::endl;
    std::cout << "   RUNNING                     - Lists all currently running jobs" << std::endl;
    std::cout << "   PENDING                     - Lists all jobs that have not yet started" << std::endl;
    std::cout << "   FINISHED                    - Lists all jobs that have already terminated" << std::endl;
}

/*
 * Execute any of the Job commands
 */
int JobAction::executeCommand(int argc, char* argv[], int argvidx, const char* hostname)
{
    /* Independent from the command, we need to connect to the server */
    connection = new DCDB::Connection();
    connection->setHostname(hostname);
    if (!connection->connect()) {
        std::cerr << "Cannot connect to Cassandra database." << std::endl;
        return EXIT_FAILURE;
    }

    /* Check what we need to do (argv[argvidx] contains "JOB") */
    argvidx++;
    if (argvidx >= argc) {
        std::cout << "The JOB command needs at least two parameters." << std::endl;
        std::cout << "Run with 'HELP JOB' to see the list of possible JOB commands." << std::endl;
        goto executeCommandError;
    }

    if (strcasecmp(argv[argvidx], "SHOW") == 0) {
        /* SHOW needs two more parameters */
        if (argvidx+1 >= argc) {
            std::cout << "SHOW needs one more parameter!" << std::endl;
            goto executeCommandError;
        }
        doShow(argv[argvidx+1]);
    }
    else if (strcasecmp(argv[argvidx], "LIST") == 0) {
        doList();
    }
    else if (strcasecmp(argv[argvidx], "RUNNING") == 0) {
        doRunning();
    }
    else if (strcasecmp(argv[argvidx], "PENDING") == 0) {
        doPending();
    }
    else if (strcasecmp(argv[argvidx], "FINISHED") == 0) {
        doFinished();
    }
    else {
        std::cout << "Invalid JOB command: " << argv[argvidx] << std::endl;
        goto executeCommandError;
    }

    /* Clean up */
    connection->disconnect();
    delete connection;
    return EXIT_SUCCESS;

    executeCommandError:
    connection->disconnect();
    delete connection;
    return EXIT_FAILURE;
}

void JobAction::doShow(std::string jobId) {
    DCDB::JobDataStore jobDataStore(connection);
    DCDB::JobData jobData;
    DCDB::JDError err = jobDataStore.getJobById(jobData, jobId);
    std::list<std::string>::iterator nIt;
    
    switch (err) {
        case DCDB::JD_OK:
115
116
117
118
119
120
121
            jobData.startTime.convertToLocal();
            jobData.endTime.convertToLocal();
            std::cout << "Job ID:     " << jobId << std::endl;
            std::cout << "User ID:    " << jobData.userId << std::endl;
            std::cout << "Start Time: " << jobData.startTime.getString() << " (" << jobData.startTime.getRaw() << ")" << std::endl;
            std::cout << "End Time:   " << jobData.endTime.getString() << " (" << jobData.endTime.getRaw() << ")" << std::endl;
            std::cout << "Node List:  ";
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
            nIt = jobData.nodes.begin();
            if(nIt != jobData.nodes.end()) {
                std::cout << *nIt;
                ++nIt;
                while(nIt != jobData.nodes.end()) {
                    std::cout << ", " << *nIt;
                    ++nIt;
                }
            }
            std::cout << std::endl;
            break;
        case DCDB::JD_JOBKEYNOTFOUND:
            std::cout << "Job key not found: " << jobId << std::endl;
            break;
        case DCDB::JD_JOBIDNOTFOUND:
            std::cout << "Job ID not found: " << jobId << std::endl;
            break;
        default:
            std::cout << "Internal error." << std::endl;
    }
}

144
145
146
147
148
149
150
151
void JobAction::printList(std::list<DCDB::JobData>& jobList) {
    std::cout << "Job ID, User ID, Start Time, End Time, #Nodes" << std::endl;
    for(const auto &j : jobList) {
	std::cout << j.jobId << "," << j.userId << "," << j.startTime.getRaw() << "," << j.endTime.getRaw() << "," << j.nodes.size() << std::endl;
    }
    std::cout << std::endl;
}

152
153
154
155
156
157
158
159
void JobAction::doList() {
    DCDB::JobDataStore jobDataStore(connection);
    DCDB::TimeStamp tsEnd((uint64_t)LLONG_MAX);
    DCDB::TimeStamp tsStart((uint64_t)0);
    std::list<DCDB::JobData> jobList;
    DCDB::JDError err = jobDataStore.getJobsInIntervalIncl(jobList, tsStart, tsEnd);
    switch (err) {
        case DCDB::JD_OK:
160
	    printList(jobList);
161
162
163
164
165
166
            break;
        default:
            std::cout << "Internal error." << std::endl;
    }
}

167
void JobAction::doPending() {
168
    DCDB::JobDataStore jobDataStore(connection);
169
170
171
    DCDB::TimeStamp tsNow;
    DCDB::TimeStamp tsEnd((uint64_t)LLONG_MAX);
    DCDB::TimeStamp tsStart((uint64_t)0);
172
    std::list<DCDB::JobData> jobList;
173
    DCDB::JDError err = jobDataStore.getJobsInIntervalIncl(jobList, tsStart, tsEnd);
174
175
    switch (err) {
        case DCDB::JD_OK:
176
177
	    printList(jobList);
	    break;
178
179
180
181
182
        default:
            std::cout << "Internal error." << std::endl;
    }
}

183
void JobAction::doRunning() {
184
    DCDB::JobDataStore jobDataStore(connection);
185
186
    DCDB::TimeStamp tsEnd;
    DCDB::TimeStamp tsStart(tsEnd.getRaw() - JOB_ACTION_OFFSET);
187
    std::list<DCDB::JobData> jobList;
188
    DCDB::JDError err = jobDataStore.getJobsInIntervalRunning(jobList, tsStart, tsEnd);
189
190
    switch (err) {
        case DCDB::JD_OK:
191
192
	    printList(jobList);
	    break;
193
194
195
196
197
198
199
200
201
202
203
204
205
206
        default:
            std::cout << "Internal error." << std::endl;
    }
}

void JobAction::doFinished() {
    DCDB::JobDataStore jobDataStore(connection);
    DCDB::TimeStamp tsNow;
    DCDB::TimeStamp tsEnd(tsNow.getRaw() - JOB_ACTION_OFFSET);
    DCDB::TimeStamp tsStart((uint64_t)0);
    std::list<DCDB::JobData> jobList;
    DCDB::JDError err = jobDataStore.getJobsInIntervalExcl(jobList, tsStart, tsEnd);
    switch (err) {
        case DCDB::JD_OK:
207
208
	    printList(jobList);
	    break;
209
210
211
212
        default:
            std::cout << "Internal error." << std::endl;
    }
}