Skip to content

Commit b60e200

Browse files
committed
reapi: use enum to pass match op
Problem: reapi has several layers of bindings that use strings to pass the match option. Strings require validation and use more space than is required. Use match_op_t enum for reapi insead. Move match_op_t definition to its own file and add function to convert match_op_t to string for resource module match call back.
1 parent 758c3e9 commit b60e200

File tree

12 files changed

+103
-56
lines changed

12 files changed

+103
-56
lines changed

resource/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(RESOURCE_HEADERS
2222
traversers/dfu_impl.hpp
2323
policies/base/dfu_match_cb.hpp
2424
policies/base/matcher.hpp
25+
policies/base/match_op.h
2526
readers/resource_namespace_remapper.hpp
2627
readers/resource_reader_base.hpp
2728
readers/resource_spec_grug.hpp

resource/policies/base/match_op.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef MATCH_OP_H
2+
#define MATCH_OP_H
3+
4+
5+
typedef enum match_op_t { MATCH_UNKNOWN,
6+
MATCH_ALLOCATE,
7+
MATCH_ALLOCATE_W_SATISFIABILITY,
8+
MATCH_ALLOCATE_ORELSE_RESERVE,
9+
MATCH_SATISFIABILITY } match_op_t;
10+
11+
static const char *match_op_to_string (match_op_t match_op) {
12+
switch (match_op) {
13+
case MATCH_ALLOCATE: return "allocate";
14+
case MATCH_ALLOCATE_ORELSE_RESERVE: return "allocate_orelse_reserve";
15+
case MATCH_ALLOCATE_W_SATISFIABILITY: return "allocate_with_satisfiability";
16+
case MATCH_SATISFIABILITY: return "satisfiability";
17+
default: return "error";
18+
}
19+
}
20+
21+
static bool match_op_valid (match_op_t match_op) {
22+
23+
if ( (match_op != MATCH_ALLOCATE) &&
24+
(match_op != MATCH_ALLOCATE_W_SATISFIABILITY) &&
25+
(match_op != MATCH_ALLOCATE_ORELSE_RESERVE) &&
26+
(match_op != MATCH_SATISFIABILITY) ) {
27+
28+
return false;
29+
}
30+
31+
return true;
32+
}
33+
34+
#endif //MATCH_OP_H

resource/policies/base/matcher.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "resource/libjobspec/jobspec.hpp"
2020
#include "resource/schema/data_std.hpp"
2121
#include "resource/planner/c/planner.h"
22+
#include "resource/policies/base/match_op.h"
2223

2324
namespace Flux {
2425
namespace resource_model {
@@ -27,11 +28,6 @@ const std::string ANY_RESOURCE_TYPE = "*";
2728

2829
enum match_score_t { MATCH_UNMET = 0, MATCH_MET = 1 };
2930

30-
enum class match_op_t { MATCH_ALLOCATE,
31-
MATCH_ALLOCATE_W_SATISFIABILITY,
32-
MATCH_ALLOCATE_ORELSE_RESERVE,
33-
MATCH_SATISFIABILITY };
34-
3531
/*! Base matcher data class.
3632
* Provide idioms to specify the target subsystems and
3733
* resource relationship types which then allow for filtering the graph

resource/reapi/bindings/c++/reapi.hpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,16 @@ class reapi_t {
9999
* detail. However, when it is used within a Flux's
100100
* service module, it is expected to be a pointer
101101
* to a flux_t object.
102-
* \param orelse_reserve
103-
* Boolean: if false, only allocate; otherwise, first try
104-
* to allocate and if that fails, reserve.
102+
* \param match_op match_op_t: set to specify the specific match option
103+
* from 1 of 4 choices:
104+
* MATCH_ALLOCATE: try to allocate now and fail if resources
105+
* aren't available.
106+
* MATCH_ALLOCATE_ORELSE_RESERVE : Try to allocate and reseve
107+
* if resources aren't available now.
108+
* MATCH_SATISFIABILITY: Do a satisfiablity check and do not
109+
* allocate.
110+
* MATCH_ALLOCATE_W_SATISFIABILITY: try to allocate and run
111+
* satisfiability check if resources are not available.
105112
* \param jobspec jobspec string.
106113
* \param jobid jobid of the uint64_t type.
107114
* \param reserved Boolean into which to return true if this job has been
@@ -131,9 +138,15 @@ class reapi_t {
131138
* detail. However, when it is used within a Flux's
132139
* service module, it is expected to be a pointer
133140
* to a flux_t object.
134-
* \param orelse_reserve
135-
* Boolean: if false, only allocate; otherwise, first try
136-
* to allocate and if that fails, reserve.
141+
* \param match_op match_op_t: set to specify the specific match option
142+
* from 1 of 4 choices:
143+
* MATCH_ALLOCATE: try to allocate now and fail if resources
144+
* aren't available.
145+
* MATCH_ALLOCATE_ORELSE_RESERVE : Try to allocate and reseve
146+
* if resources aren't available now.
147+
* MATCH_SATISFIABILITY: Do a satisfiablity check and do not
148+
* allocate.
149+
* MATCH_ALLOCATE_W_SATISFIABILITY: try to allocate and run
137150
* \param jobs JSON array of jobspecs.
138151
* \param adapter queue_adapter_base_t object that provides
139152
* a set of callback methods to be called each time

resource/reapi/bindings/c++/reapi_cli.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern "C" {
2626
#include "resource/jobinfo/jobinfo.hpp"
2727
#include "resource/policies/dfu_match_policy_factory.hpp"
2828
#include "resource/traversers/dfu.hpp"
29+
#include "resource/policies/base/match_op.h"
2930

3031
namespace Flux {
3132
namespace resource_model {

resource/reapi/bindings/c++/reapi_cli_impl.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ int reapi_cli_t::match_allocate (void *h, match_op_t match_op,
6666
std::stringstream o;
6767
bool matched = false;
6868

69+
if (!match_op_valid (match_op) ) {
70+
m_err_msg += __FUNCTION__;
71+
m_err_msg += ": ERROR: Invalid Match Option: "
72+
+ std::string (match_op_to_string (match_op)) + "\n";
73+
rc = -1;
74+
goto out;
75+
}
76+
6977
try {
7078
Flux::Jobspec::Jobspec job {jobspec};
7179

resource/reapi/bindings/c++/reapi_module.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ extern "C" {
2121
#include <cstdint>
2222
#include <string>
2323
#include "resource/reapi/bindings/c++/reapi.hpp"
24+
#include "resource/policies/base/match_op.h"
2425

2526
namespace Flux {
2627
namespace resource_model {
2728
namespace detail {
2829

2930
class reapi_module_t : public reapi_t {
3031
public:
31-
static int match_allocate (void *h, const char *cmd,
32+
static int match_allocate (void *h, match_op_t match_op,
3233
const std::string &jobspec,
3334
const uint64_t jobid, bool &reserved,
3435
std::string &R, int64_t &at, double &ov);
@@ -39,7 +40,7 @@ class reapi_module_t : public reapi_t {
3940
static int match_allocate_multi (void *h, bool orelse_reserve,
4041
const char *jobs,
4142
queue_adapter_base_t *adapter);
42-
static int match_allocate_multi (void *h, const char *cmd,
43+
static int match_allocate_multi (void *h, match_op_t match_op,
4344
const char *jobs,
4445
queue_adapter_base_t *adapter);
4546
static int update_allocate (void *h, const uint64_t jobid,

resource/reapi/bindings/c++/reapi_module_impl.hpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Flux {
2525
namespace resource_model {
2626
namespace detail {
2727

28-
int reapi_module_t::match_allocate (void *h, const char *cmd,
28+
int reapi_module_t::match_allocate (void *h, match_op_t match_op,
2929
const std::string &jobspec,
3030
const uint64_t jobid, bool &reserved,
3131
std::string &R, int64_t &at, double &ov)
@@ -36,6 +36,7 @@ int reapi_module_t::match_allocate (void *h, const char *cmd,
3636
flux_future_t *f = NULL;
3737
const char *rset = NULL;
3838
const char *status = NULL;
39+
const char *cmd = match_op_to_string (match_op);
3940

4041
if (!fh || jobspec == "" || jobid > INT64_MAX) {
4142
errno = EINVAL;
@@ -73,10 +74,10 @@ int reapi_module_t::match_allocate (void *h, bool orelse_reserve,
7374
const uint64_t jobid, bool &reserved,
7475
std::string &R, int64_t &at, double &ov)
7576
{
76-
const char *cmd = (orelse_reserve)? "allocate_orelse_reserve"
77-
: "allocate_with_satisfiability";
77+
match_op_t match_op = (orelse_reserve)? match_op_t::MATCH_ALLOCATE_ORELSE_RESERVE
78+
: match_op_t::MATCH_ALLOCATE_W_SATISFIABILITY;
7879

79-
return match_allocate (h, cmd, jobspec, jobid, reserved, R, at, ov);
80+
return match_allocate (h, match_op, jobspec, jobid, reserved, R, at, ov);
8081

8182
}
8283

@@ -111,13 +112,14 @@ void match_allocate_multi_cont (flux_future_t *f, void *arg)
111112
}
112113

113114
int reapi_module_t::match_allocate_multi (void *h,
114-
const char *cmd,
115+
match_op_t match_op,
115116
const char *jobs,
116117
queue_adapter_base_t *adapter)
117118
{
118119
int rc = -1;
119120
flux_t *fh = static_cast<flux_t *> (h);
120121
flux_future_t *f = nullptr;
122+
const char *cmd = match_op_to_string (match_op);
121123

122124
if (!fh) {
123125
errno = EINVAL;
@@ -147,9 +149,10 @@ int reapi_module_t::match_allocate_multi (void *h,
147149
const char *jobs,
148150
queue_adapter_base_t *adapter)
149151
{
150-
const char *cmd = orelse_reserve ? "allocate_orelse_reserve"
151-
: "allocate_with_satisfiability";
152-
return match_allocate_multi (h, cmd, jobs, adapter);
152+
match_op_t match_op = (orelse_reserve)? match_op_t::MATCH_ALLOCATE_ORELSE_RESERVE
153+
: match_op_t::MATCH_ALLOCATE_W_SATISFIABILITY;
154+
155+
return match_allocate_multi (h, match_op, jobs, adapter);
153156
}
154157

155158
int reapi_module_t::update_allocate (void *h, const uint64_t jobid,

resource/reapi/bindings/c/reapi_cli.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,33 +91,21 @@ extern "C" int reapi_cli_initialize (reapi_cli_ctx_t *ctx, const char *rgraph,
9191
}
9292

9393
extern "C" int reapi_cli_match (reapi_cli_ctx_t *ctx,
94-
const char *match_op, const char *jobspec,
94+
match_op_t match_op, const char *jobspec,
9595
uint64_t *jobid, bool *reserved,
9696
char **R, int64_t *at, double *ov)
9797
{
9898
int rc = -1;
9999
std::string R_buf = "";
100100
char *R_buf_c = nullptr;
101-
match_op_t op;
102101

103102
if (!ctx || !ctx->rqt) {
104103
errno = EINVAL;
105104
goto out;
106105
}
107106

108-
if (!strcmp (match_op,"allocate"))
109-
op = match_op_t::MATCH_ALLOCATE;
110-
else if (!strcmp (match_op, "allocate_with_satisfiability"))
111-
op = match_op_t::MATCH_ALLOCATE_W_SATISFIABILITY;
112-
else if (!strcmp (match_op, "allocate_orelse_reserve"))
113-
op = match_op_t::MATCH_ALLOCATE_ORELSE_RESERVE;
114-
else if (!strcmp (match_op, "satisfiability"))
115-
op = match_op_t::MATCH_SATISFIABILITY;
116-
else
117-
goto out;
118-
119107
*jobid = ctx->rqt->get_job_counter ();
120-
if ((rc = reapi_cli_t::match_allocate (ctx->rqt, op,
108+
if ((rc = reapi_cli_t::match_allocate (ctx->rqt, match_op,
121109
jobspec, *jobid, *reserved,
122110
R_buf, *at, *ov)) < 0) {
123111
goto out;
@@ -141,8 +129,8 @@ extern "C" int reapi_cli_match_allocate (reapi_cli_ctx_t *ctx,
141129
uint64_t *jobid, bool *reserved,
142130
char **R, int64_t *at, double *ov)
143131
{
144-
const char *match_op = orelse_reserve ? "allocate_orelse_reserve" :
145-
"allocate";
132+
match_op_t match_op = orelse_reserve ? match_op_t::MATCH_ALLOCATE_ORELSE_RESERVE :
133+
match_op_t::MATCH_ALLOCATE;
146134

147135
return reapi_cli_match (ctx, match_op, jobspec, jobid, reserved,
148136
R, at, ov);
@@ -152,7 +140,7 @@ extern "C" int reapi_cli_match_satisfy (reapi_cli_ctx_t *ctx,
152140
const char *jobspec,
153141
bool *sat, double *ov)
154142
{
155-
const char *match_op = "satisfiability";
143+
match_op_t match_op = match_op_t::MATCH_SATISFIABILITY;
156144
uint64_t jobid;
157145
bool reserved;
158146
char *R;

resource/reapi/bindings/c/reapi_cli.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern "C" {
1919
#include "config.h"
2020
#endif
2121
#include <flux/core.h>
22+
#include "resource/policies/base/match_op.h"
2223

2324
typedef struct reapi_cli_ctx reapi_cli_ctx_t;
2425

@@ -46,15 +47,15 @@ int reapi_cli_initialize (reapi_cli_ctx_t *ctx, const char *rgraph,
4647
* the selected match policy.
4748
*
4849
* \param ctx reapi_cli_ctx_t context object
49-
* \param match_op const char *: set to specify the specific match option
50+
* \param match_op match_op_t: set to specify the specific match option
5051
* from 1 of 4 choices:
51-
* allocate: try to allocate now and fail if resources
52+
* MATCH_ALLOCATE: try to allocate now and fail if resources
5253
* aren't available.
53-
* allocate_orelse_reserve : Try to allocate and reserve if
54-
* resources aren't available now.
55-
* satisfiability: Do a satisfiablity check and do not
54+
* MATCH_ALLOCATE_ORELSE_RESERVE : Try to allocate and reserve
55+
* if resources aren't available now.
56+
* MATCH_SATISFIABILITY: Do a satisfiablity check and do not
5657
* allocate.
57-
* allocate_with_satisfiability: try to allocate and run
58+
* MATCH_ALLOCATE_W_SATISFIABILITY: try to allocate and run
5859
* satisfiability check if resources are not available.
5960
* \param jobspec jobspec string.
6061
* \param jobid jobid of the uint64_t type.
@@ -69,7 +70,7 @@ int reapi_cli_initialize (reapi_cli_ctx_t *ctx, const char *rgraph,
6970
* the match operation.
7071
* \return 0 on success; -1 on error.
7172
*/
72-
int reapi_cli_match (reapi_cli_ctx_t *ctx, const char *match_op,
73+
int reapi_cli_match (reapi_cli_ctx_t *ctx, match_op_t match_op,
7374
const char *jobspec, uint64_t *jobid,
7475
bool *reserved,
7576
char **R, int64_t *at, double *ov);

0 commit comments

Comments
 (0)