-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlibirods_rule_engine_plugin-policy_engine-data_retention.cpp
More file actions
419 lines (331 loc) · 12.9 KB
/
libirods_rule_engine_plugin-policy_engine-data_retention.cpp
File metadata and controls
419 lines (331 loc) · 12.9 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <irods/policy_composition_framework_policy_engine.hpp>
#include <irods/policy_composition_framework_parameter_capture.hpp>
#include <irods/apiNumber.h>
#include <irods/irods_server_api_call.hpp>
#include <irods/irods_resource_manager.hpp>
#include <irods/irods_hierarchy_parser.hpp>
#include <nlohmann/json.hpp>
#include <algorithm>
#include <iostream>
#include "parameter_substitution.hpp"
extern irods::resource_manager resc_mgr;
namespace
{
// clang-format off
namespace pc = irods::policy_composition;
namespace fs = irods::experimental::filesystem;
namespace kw = irods::policy_composition::keywords;
namespace pe = irods::policy_composition::policy_engine;
using string_vector = std::vector<std::string>;
using string_tuple_vector = std::vector<std::tuple<std::string, std::string>>;
// clang-format on
namespace retention_mode
{
static const std::string remove_all{"remove_all_replicas"};
static const std::string trim_single{"trim_single_replica"};
}; //namespace retention_mode
auto mode_is_supported(const std::string& m) -> bool
{
return (m == retention_mode::remove_all || m == retention_mode::trim_single);
}
auto get_source_resource(rsComm_t* comm, const std::string& logical_path, const std::string& destination_resource)
-> std::string
{
fs::path path{logical_path};
const auto coll_name = path.parent_path();
const auto data_name = path.object_name();
// query for list of all participating resources
auto qstr{boost::str(
boost::format("SELECT RESC_NAME WHERE COLL_NAME = '%s' AND DATA_NAME = '%s'") % coll_name.string() %
data_name.string())};
irods::query qobj{comm, qstr};
// if there are more than two replicas and no source resource has been
// specificied, this is a usage error
if (qobj.size() > 2) {
THROW(
SYS_INVALID_INPUT_PARAM,
fmt::format("Multiple replicas found with no specified source resource for [{}]", logical_path));
}
for (auto&& rn : qobj) {
if (rn[0] != destination_resource) {
return rn[0];
}
}
THROW(SYS_INVALID_INPUT_PARAM, fmt::format("No source replica found for [{}]", logical_path));
} // get_source_resource
auto get_replica_number_for_resource(
rsComm_t* comm,
const std::string& logical_path,
const std::string& source_resource) -> std::string
{
fs::path path{logical_path};
const auto coll_name = path.parent_path();
const auto data_name = path.object_name();
// query for list of all participating resources
auto qstr{boost::str(
boost::format("SELECT DATA_REPL_NUM WHERE COLL_NAME = '%s' AND DATA_NAME = '%s' AND RESC_NAME = '%s'") %
coll_name.string() % data_name.string() % source_resource)};
irods::query qobj{comm, qstr};
return qobj.size() > 0 ? qobj.front()[0] : "INVALID_REPLICA_NUMBER";
} // get_replica_number_for_resource
auto remove_data_object(
int api_index,
rsComm_t* comm,
const std::string& user_name,
const std::string& logical_path,
const std::string& source_resource = {}) -> int
{
dataObjInp_t obj_inp{};
memset(&obj_inp, 0, sizeof(obj_inp));
rstrcpy(obj_inp.objPath, logical_path.c_str(), sizeof(obj_inp.objPath));
if (comm->clientUser.authInfo.authFlag >= LOCAL_PRIV_USER_AUTH) {
addKeyVal(&obj_inp.condInput, ADMIN_KW, "true");
}
addKeyVal(&obj_inp.condInput, COPIES_KW, "1");
std::string repl_num{};
if (!source_resource.empty()) {
repl_num = get_replica_number_for_resource(comm, logical_path, source_resource);
addKeyVal(&obj_inp.condInput, REPL_NUM_KW, repl_num.c_str());
}
auto trim_fcn = [&](auto& comm) {
auto res{irods::server_api_call(api_index, &comm, &obj_inp)};
clearDataObjInp(&obj_inp);
return res;
};
return pc::exec_as_user(*comm, user_name, trim_fcn);
} // remove_data_object
auto get_leaf_resources_for_object(rsComm_t* comm, const std::string& logical_path) -> string_vector
{
namespace fs = irods::experimental::filesystem;
fs::path path{logical_path};
const auto coll_name = path.parent_path();
const auto data_name = path.object_name();
// query for list of all participating resources
auto qstr{boost::str(
boost::format("SELECT RESC_NAME WHERE COLL_NAME = '%s' AND DATA_NAME = '%s'") % coll_name.string() %
data_name.string())};
irods::query qobj{comm, qstr};
string_vector resources{};
for (auto r : qobj) {
resources.push_back(r[0]);
}
return resources;
} // get_leaf_resources_for_object
auto get_leaf_resource_for_root(rsComm_t* comm, const std::string& source_resource, const std::string& logical_path)
-> std::string
{
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(source_resource);
auto qstr{boost::str(
boost::format("SELECT RESC_NAME WHERE COLL_NAME = '%s' AND DATA_NAME = '%s' AND RESC_ID IN (%s)") %
coll_name.string() % data_name.string() % leaf_bundle)};
irods::query qobj{comm, qstr};
return qobj.size() > 0 ? qobj.front()[0] : std::string{};
} // get_leaf_resource_for_root
auto get_root_resources_for_leaves(rsComm_t* comm, const string_vector& leaf_resources)
{
std::vector<std::tuple<std::string, std::string>> roots_and_leaves;
for (auto&& l : leaf_resources) {
std::string hier{};
irods::error err = resc_mgr.get_hier_to_root_for_resc(l, hier);
irods::hierarchy_parser p(hier);
roots_and_leaves.push_back(std::make_tuple(p.first_resc(), l));
}
return roots_and_leaves;
} // get_root_resources_for_leaves
auto filter_roots_and_leaves_by_whitelist(
const string_vector& whitelist,
const string_tuple_vector& roots_and_leaves)
{
if (whitelist.empty()) {
return roots_and_leaves;
}
string_tuple_vector tmp{};
for (auto&& rl : roots_and_leaves) {
if (std::find(whitelist.begin(), whitelist.end(), std::get<0>(rl)) != whitelist.end()) {
tmp.push_back(rl);
}
}
return tmp;
} // filter_roots_and_leaves_by_whitelist
bool resource_has_preservation_metadata(rsComm_t* comm, const std::string& attribute, const std::string& resource)
{
auto qstr = boost::str(
boost::format("SELECT META_RESC_ATTR_VALUE WHERE RESC_NAME = '%s' AND META_RESC_ATTR_NAME = '%s'") %
resource % attribute);
irods::query qobj{comm, qstr};
return qobj.size() != 0;
} // resource_has_preservation_metadata
auto filter_roots_and_leaves_by_preservation_metadata(
rsComm_t* comm,
const std::string& attribute,
const string_tuple_vector& roots_and_leaves)
{
string_tuple_vector tmp{};
for (auto&& rl : roots_and_leaves) {
if (!resource_has_preservation_metadata(comm, attribute, std::get<0>(rl))) {
tmp.push_back(rl);
}
}
return tmp;
} // filter_roots_and_leaves_by_preservation_metadata
auto determine_resource_list_for_unlink(
rsComm_t* comm,
const std::string& attribute,
const std::string& logical_path,
const string_vector& whitelist)
{
// need to convert leaves to roots and then determine if
// 1. it is in the white list
// 2. if it has preservation metadata
auto leaf_resources = get_leaf_resources_for_object(comm, logical_path);
auto roots_and_leaves = get_root_resources_for_leaves(comm, leaf_resources);
roots_and_leaves = filter_roots_and_leaves_by_whitelist(whitelist, roots_and_leaves);
roots_and_leaves = filter_roots_and_leaves_by_preservation_metadata(comm, attribute, roots_and_leaves);
// gather remaining leaves
string_vector tmp{};
for (auto&& rl : roots_and_leaves) {
tmp.push_back(std::get<1>(rl));
}
// if identical to original list then we unlink, not trim
auto unlink = (leaf_resources == tmp);
return std::make_tuple(unlink, tmp);
} // determine_resource_list_for_unlink
auto object_can_be_trimmed(
rsComm_t* comm,
const std::string& attribute,
const std::string& source_resource,
const string_vector& whitelist)
{
if (!whitelist.empty()) {
if (std::find(whitelist.begin(), whitelist.end(), source_resource) == whitelist.end()) {
return false;
}
}
return !resource_has_preservation_metadata(comm, attribute, source_resource);
} // object_can_be_trimmed
auto data_retention_policy(const pe::context& ctx, pe::arg_type out)
{
auto mode = pc::get(ctx.configuration, "mode", std::string{});
if (!mode_is_supported(mode)) {
return ERROR(SYS_INVALID_INPUT_PARAM, boost::format("retention mode is not supported [%s]") % mode);
}
auto [user_name, logical_path, source_resource, destination_resource] =
capture_parameters(ctx.parameters, tag_first_resc);
pe::client_message(
{{"0.usage",
fmt::format(
"{} requires user_name, logical_path, source_resource, or destination_resource and mode",
ctx.policy_name)},
{"1.user_name", user_name},
{"2.logical_path", logical_path},
{"3.source_resource", source_resource},
{"4.destination_resource", destination_resource},
{"5.mode", mode}});
auto comm = ctx.rei->rsComm;
auto whitelist = pc::get(ctx.configuration, "resource_white_list", json::array());
auto attribute = pc::get(ctx.configuration, kw::attribute, std::string{"irods::retention::preserve_replicas"});
if (mode == retention_mode::remove_all) {
pe::client_message({{"0.message", fmt::format("{} mode is removing all replicas", ctx.policy_name)}});
auto [unlink, resources_to_remove] =
determine_resource_list_for_unlink(comm, attribute, logical_path, whitelist);
pe::client_message({{"0.message", fmt::format("{} unlink flag is {}", ctx.policy_name, unlink)}});
// removing all replicas requires a call to unlink, cannot trim
if (unlink) {
pe::client_message(
{{"0.message", fmt::format("{} removing data object {}", ctx.policy_name, unlink, logical_path)}});
const auto ret = remove_data_object(DATA_OBJ_UNLINK_AN, comm, user_name, logical_path);
if (ret < 0) {
return ERROR(
ret, boost::format("failed to remove [%s] from [%s]") % logical_path % source_resource);
}
}
// trim a specific list of replicas determined by policy
else {
for (const auto& src : resources_to_remove) {
pe::client_message(
{{"0.message",
fmt::format("{} trimming replica {} from {}", ctx.policy_name, unlink, logical_path, src)}});
const auto ret = remove_data_object(DATA_OBJ_TRIM_AN, comm, user_name, logical_path, src);
if (ret < 0) {
return ERROR(ret, boost::format("failed to remove [%s] from [%s]") % logical_path % src);
}
} // for src
}
}
// trim single replica
else {
if (source_resource.empty()) {
source_resource = get_source_resource(comm, logical_path, destination_resource);
}
pe::client_message(
{{"0.message",
fmt::format("{} mode is trimming single replica from {}", ctx.policy_name, source_resource)}});
if (object_can_be_trimmed(comm, attribute, source_resource, whitelist)) {
auto leaf_name = std::string{};
leaf_name = get_leaf_resource_for_root(comm, source_resource, logical_path);
pe::client_message(
{{"0.message",
fmt::format("{} trimming single replica {} from {}", ctx.policy_name, logical_path, leaf_name)}});
const auto ret = remove_data_object(DATA_OBJ_TRIM_AN, comm, user_name, logical_path, leaf_name);
if (ret < 0) {
return ERROR(
ret, boost::format("failed to remove [%s] from [%s]") % logical_path % source_resource);
}
}
}
return SUCCESS();
} // data_retention_policy
} // namespace
const char usage[] = R"(
{
"$id": "https://schemas.irods.org/policy-composition/data_retention.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_retention", usage, data_retention_policy);
} // plugin_factory