-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlibirods_rule_engine_plugin-policy_engine-data_replication.cpp
More file actions
224 lines (186 loc) · 6.77 KB
/
libirods_rule_engine_plugin-policy_engine-data_replication.cpp
File metadata and controls
224 lines (186 loc) · 6.77 KB
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
#include <irods/policy_composition_framework_policy_engine.hpp>
#include <irods/policy_composition_framework_parameter_capture.hpp>
#include <irods/irods_hierarchy_parser.hpp>
#include <irods/irods_server_api_call.hpp>
#include <irods/physPath.hpp>
#include <irods/apiNumber.h>
#include "parameter_substitution.hpp"
namespace
{
// clang-format off
namespace pc = irods::policy_composition;
namespace kw = irods::policy_composition::keywords;
namespace pe = irods::policy_composition::policy_engine;
using json = nlohmann::json;
// clang-format on
auto destination_replica_exists(rsComm_t* comm, const std::string& resource, const std::string& logical_path)
{
namespace fs = irods::experimental::filesystem;
fs::path path{logical_path};
const auto coll_name = path.parent_path();
const auto data_name = path.object_name();
auto leaf_bundle = pe::compute_leaf_bundle(resource);
auto qstr{boost::str(
boost::format("SELECT DATA_REPL_NUM WHERE COLL_NAME = '%s' AND DATA_NAME = '%s' AND DATA_REPL_STATUS = '1' "
"AND RESC_ID IN (%s)") %
coll_name.string() % data_name.string() % leaf_bundle)};
irods::query qobj{comm, qstr};
return (qobj.size() > 0);
} // destination_replica_exists
auto replicate_object_to_resource(
rsComm_t* _comm,
const std::string& _user_name,
const std::string& _logical_path,
const std::string& _source_resource,
const std::string& _destination_resource)
{
if (destination_replica_exists(_comm, _destination_resource, _logical_path)) {
return 0;
}
dataObjInp_t data_obj_inp{};
rstrcpy(data_obj_inp.objPath, _logical_path.c_str(), MAX_NAME_LEN);
data_obj_inp.createMode = getDefFileMode();
addKeyVal(&data_obj_inp.condInput, RESC_NAME_KW, _source_resource.c_str());
addKeyVal(&data_obj_inp.condInput, DEST_RESC_NAME_KW, _destination_resource.c_str());
if (_comm->clientUser.authInfo.authFlag >= LOCAL_PRIV_USER_AUTH) {
addKeyVal(&data_obj_inp.condInput, ADMIN_KW, "true");
}
transferStat_t* trans_stat{};
auto repl_fcn = [&](auto& comm) {
auto ret = irods::server_api_call(DATA_OBJ_REPL_AN, _comm, &data_obj_inp, &trans_stat);
free(trans_stat);
clearDataObjInp(&data_obj_inp);
return ret;
};
return pc::exec_as_user(*_comm, _user_name, repl_fcn);
} // replicate_object_to_resource
auto replication_policy(const pe::context ctx, pe::arg_type out)
{
auto comm = ctx.rei->rsComm;
auto [user_name, logical_path, source_resource, destination_resource] =
capture_parameters(ctx.parameters, tag_first_resc);
destination_resource = destination_resource.empty()
? pc::get(ctx.configuration, "destination_resource", std::string{})
: destination_resource;
pe::client_message(
{{"0.usage",
fmt::format(
"{} requires user_name, logical_path, source_resource, destination_resource or "
"source_to_destination_map",
ctx.policy_name)},
{"1.user_name", user_name},
{"2.logical_path", logical_path},
{"3.source_resource", source_resource},
{"4.destination_resource", destination_resource}});
if (!destination_resource.empty()) {
pe::client_message(
{{"0.message", fmt::format("{} destination_resource is not emtpy", ctx.policy_name)},
{"1.message",
fmt::format(
"{} replicating {} from {} to {}",
ctx.policy_name,
logical_path,
source_resource,
destination_resource)}});
// direct call invocation
int err =
replicate_object_to_resource(comm, user_name, logical_path, source_resource, destination_resource);
if (err < 0) {
return ERROR(
err,
boost::format("failed to replicate [%s] from [%s] to [%s]") % logical_path % source_resource %
destination_resource);
}
}
else {
pe::client_message(
{{"0.message",
fmt::format(
"{} destination_resource is emtpy, requires source_to_destination_map", ctx.policy_name)}});
if (ctx.configuration.find("source_to_destination_map") == ctx.configuration.end()) {
THROW(
SYS_INVALID_INPUT_PARAM,
boost::format("%s - destination_resource or source_to_destination_map not provided") %
ctx.policy_name);
}
auto src_dst_map{ctx.configuration.at("source_to_destination_map")};
if (!src_dst_map.contains(source_resource)) {
pc::logger::info(
"{}: irods_policy_data_replication - source resource is not present in map [{}]",
__func__,
source_resource);
return SUCCESS();
}
auto dst_resc_arr{src_dst_map.at(source_resource)};
auto destination_resources = dst_resc_arr.get<std::vector<std::string>>();
irods::error ret{SUCCESS()};
for (auto& dest : destination_resources) {
pe::client_message(
{{"0.message",
fmt::format(
"{} replicating {} from {} to {}", ctx.policy_name, logical_path, source_resource, dest)}});
int err = replicate_object_to_resource(comm, user_name, logical_path, source_resource, dest);
if (err < 0) {
ret = PASSMSG(
boost::str(
boost::format("failed to replicate [%s] from [%s] to [%s]") % logical_path %
source_resource),
ret);
}
}
if (!ret.ok()) {
return ret;
}
}
return SUCCESS();
} // replication_policy
} // namespace
const char usage[] = R"(
{
"$id": "https://schemas.irods.org/policy-composition/data_replication.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": ""
"input_interfaces": [
{
"name" : "event_handler-collection_modified",
"description" : "",
"json_schema" : ""
},
{
"name" : "event_handler-data_object_modified",
"description" : "",
"json_schema" : ""
},
{
"name" : "event_handler-metadata_modified",
"description" : "",
"json_schema" : ""
},
{
"name" : "event_handler-user_modified",
"description" : "",
"json_schema" : ""
},
{
"name" : "event_handler-resource_modified",
"description" : "",
"json_schema" : ""
},
{
"name" : "direct_invocation",
"description" : "",
"json_schema" : ""
},
{
"name" : "query_results"
"description" : "",
"json_schema" : ""
},
],
"output_json_for_validation" : ""
}
)";
extern "C" pe::plugin_pointer_type plugin_factory(const std::string& _plugin_name, const std::string&)
{
return pe::make(_plugin_name, "irods_policy_data_replication", usage, replication_policy);
} // plugin_factory