Skip to content

Commit

Permalink
resource-query: add support for partial-cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
milroy committed Jun 24, 2024
1 parent a34e25e commit 1277524
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
84 changes: 84 additions & 0 deletions resource/utilities/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ command_t commands[] = {
"resource-query> find status=down and sched-now=allocated" },
{ "cancel", "c", cmd_cancel, "Cancel an allocation or reservation: "
"resource-query> cancel jobid" },
{ "partial-cancel", "pc", cmd_partial_cancel, "Partially release an allocation: "
"resource-query> partial-cancel jobid (file format: jgf | rv1exec) R_to_cancel.file" },
{ "set-property", "p", cmd_set_property, "Add a property to a resource: "
"resource-query> set-property resource PROPERTY=VALUE" },
{ "get-property", "g", cmd_get_property, "Get all properties of a resource: "
Expand Down Expand Up @@ -84,6 +86,26 @@ static int do_remove (std::shared_ptr<resource_context_t> &ctx, int64_t jobid)
return rc;
}

static int do_partial_remove (std::shared_ptr<resource_context_t> &ctx,
std::shared_ptr<resource_reader_base_t> &reader,
int64_t jobid, const std::string &R_cancel)
{
int rc = -1;
bool full_cancel = false;

if ( (rc = ctx->traverser->remove (R_cancel, reader, (int64_t)jobid,
full_cancel)) == 0) {
if (full_cancel && (ctx->jobs.find (jobid) != ctx->jobs.end ())) {
std::shared_ptr<job_info_t> info = ctx->jobs[jobid];
info->state = job_lifecycle_t::CANCELED;
}
} else {
std::cout << ctx->traverser->err_message ();
ctx->traverser->clear_err_message ();
}
return rc;
}

static void print_sat_info (std::shared_ptr<resource_context_t> &ctx,
std::ostream &out, bool sat, double elapse,
unsigned int pre, unsigned int post)
Expand Down Expand Up @@ -600,6 +622,68 @@ int cmd_cancel (std::shared_ptr<resource_context_t> &ctx,
return 0;
}

int cmd_partial_cancel (std::shared_ptr<resource_context_t> &ctx,
std::vector<std::string> &args)
{
int rc = -1;
std::stringstream buffer{};
std::string jobid_str = args[1];
std::string reader = args[2];
std::ifstream cancel_file (args[3]);
std::shared_ptr<resource_reader_base_t> rd;

if (args.size () != 4) {
std::cerr << "ERROR: malformed command" << std::endl;
return 0;
}

uint64_t jobid = (uint64_t)std::strtoll (jobid_str.c_str (), NULL, 10);

if (!(reader == "jgf" || reader == "rv1exec")) {
std::cerr << "ERROR: unsupported reader " << args[2] << std::endl;
goto done;
}

if (!cancel_file) {
std::cerr << "ERROR: can't open " << args[3] << std::endl;
goto done;
}
buffer << cancel_file.rdbuf ();
cancel_file.close ();

if (reader == "rv1exec") {
if ( (rd = create_resource_reader ("rv1exec")) == nullptr) {
std::cerr << "ERROR: can't create rv1exec reader " << std::endl;
goto done;
}
} else { // must be JGF
if ( (rd = create_resource_reader ("jgf")) == nullptr) {
std::cerr << "ERROR: can't create rv1exec reader " << std::endl;
goto done;
}
}

if (ctx->allocations.find (jobid) != ctx->allocations.end ()) {
if ( (rc = do_partial_remove (ctx, rd, jobid, buffer.str ())) == 0)
ctx->allocations.erase (jobid);
} else if (ctx->reservations.find (jobid) != ctx->reservations.end ()) {
std::cerr << "ERROR: reservations not currently supported by partial cancel"
<< std::endl;
goto done;
} else {
std::cerr << "ERROR: nonexistent job " << jobid << std::endl;
goto done;
}

if (rc != 0) {
std::cerr << "ERROR: error encountered while removing job "
<< jobid << std::endl;
}

done:
return 0;
}

int cmd_set_property (std::shared_ptr<resource_context_t> &ctx,
std::vector<std::string> &args)
{
Expand Down
2 changes: 2 additions & 0 deletions resource/utilities/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ int cmd_find (std::shared_ptr<resource_context_t> &ctx,
std::vector<std::string> &args);
int cmd_cancel (std::shared_ptr<resource_context_t> &ctx,
std::vector<std::string> &args);
int cmd_partial_cancel (std::shared_ptr<resource_context_t> &ctx,
std::vector<std::string> &args);
int cmd_set_property (std::shared_ptr<resource_context_t> &ctx,
std::vector<std::string> &args);
int cmd_get_property (std::shared_ptr<resource_context_t> &ctx,
Expand Down

0 comments on commit 1277524

Please sign in to comment.