-
Notifications
You must be signed in to change notification settings - Fork 5
/
publishing_utilities.cpp
417 lines (360 loc) · 14.9 KB
/
publishing_utilities.cpp
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
#include <irods/irods_server_properties.hpp>
#include <irods/irods_re_plugin.hpp>
#include "utilities.hpp"
#include "publishing_utilities.hpp"
#include <irods/irods_query.hpp>
#include <irods/irods_virtual_path.hpp>
#include <irods/rsExecMyRule.hpp>
#include <irods/rsOpenCollection.hpp>
#include <irods/rsReadCollection.hpp>
#include <irods/rsCloseCollection.hpp>
#include <irods/rsModAVUMetadata.hpp>
#define IRODS_FILESYSTEM_ENABLE_SERVER_SIDE_API
#include <irods/filesystem.hpp>
#include <boost/any.hpp>
#include <boost/regex.hpp>
#include <boost/exception/all.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <random>
#include <nlohmann/json.hpp>
int _delayExec(
const char *inActionCall,
const char *recoveryActionCall,
const char *delayCondition,
ruleExecInfo_t *rei );
namespace irods {
namespace publishing {
publisher::publisher(
ruleExecInfo_t* _rei,
const std::string& _instance_name) :
rei_(_rei)
, comm_(_rei->rsComm)
, config_(_instance_name) {
} // publisher
void publisher::schedule_publishing_policy(
const std::string& _json,
const std::string& _params) {
const int delay_err = _delayExec(
_json.c_str(),
"",
_params.c_str(),
rei_);
if(delay_err < 0) {
THROW(
delay_err,
"delayExec failed");
}
} // schedule_publishing_policy
bool publisher::metadata_exists_on_collection(
const std::string& _collection_name,
const std::string& _attribute,
const std::string& _value,
const std::string& _units ) {
try {
std::string query_str {
boost::str(
boost::format("SELECT META_COLL_ATTR_VALUE, META_COLL_ATTR_UNITS WHERE META_COLL_ATTR_NAME = '%s' and COLL_NAME = '%s'") %
_attribute %
_collection_name) };
query<rsComm_t> qobj{rei_->rsComm, query_str, 1};
if(qobj.size() == 0) {
return false;
}
for(auto results : qobj) {
if(results[0] == _value &&
results[1] == _units) {
return true;
}
}
return false;
}
catch( irods::exception& _e) {
return false;
}
} // metadata_exists_on_collection
bool publisher::metadata_exists_on_object(
const std::string& _object_path,
const std::string& _attribute,
const std::string& _value,
const std::string& _units ) {
namespace fs = irods::experimental::filesystem;
try {
fs::path p{_object_path};
std::string coll_name = p.parent_path().string();
std::string data_name = p.object_name().string();
std::string query_str {
boost::str(
boost::format("SELECT META_DATA_ATTR_VALUE, META_DATA_ATTR_UNITS WHERE META_DATA_ATTR_NAME = '%s' and COLL_NAME = '%s' and DATA_NAME = '%s'")
% _attribute
% coll_name
% data_name) };
query<rsComm_t> qobj{rei_->rsComm, query_str, 1};
if(qobj.size() == 0) {
return false;
}
for(auto results : qobj) {
if(results[0] == _value &&
results[1] == _units) {
return true;
}
}
return false;
}
catch( irods::exception& _e) {
return false;
}
} // metadata_exists_on_object
bool publisher::publishing_metadata_exists_in_path(
const std::string& _path) {
namespace fs = irods::experimental::filesystem;
namespace fsvr = irods::experimental::filesystem::server;
rsComm_t& comm = *rei_->rsComm;
try {
fs::path full_path{_path};
if(fsvr::is_data_object(comm, full_path) &&
object_is_published(_path)) {
return true;
}
else if(collection_is_published(_path)) {
return true;
}
auto coll = full_path.parent_path();
while(!coll.empty()) {
try {
if(collection_is_published(coll.string())) {
return true;
}
coll = coll.parent_path();
}
catch(const std::exception& _e) {
rodsLog(
LOG_ERROR,
"publishing_metadata_exists_in_path failed [%s]",
_e.what());
}
catch(const irods::exception& _e) {
rodsLog(
LOG_ERROR,
"publishing_metadata_exists_in_path failed [%s]",
_e.what());
}
} // while
return false;
}
catch(const std::exception& _e) {
rodsLog(
LOG_ERROR,
"publishing_metadata_exists_in_path failed [%s]",
_e.what());
}
catch(const irods::exception& _e) {
rodsLog(
LOG_ERROR,
"publishing_metadata_exists_in_path failed [%s]",
_e.what());
}
return false;
} // publishing_metadata_exists_in_path
void publisher::schedule_collection_publishing_event(
const std::string& _collection_name,
const std::string& _publisher,
const std::string& _user_name) {
rodsLog(
config_.log_level,
"irods::publishing::collection publishing collection [%s] with publisher [%s]",
_collection_name.c_str(),
_publisher.c_str());
using json = nlohmann::json;
json rule_obj;
rule_obj["rule-engine-operation"] = policy::collection::publish;
rule_obj["rule-engine-instance-name"] = config_.instance_name_;
rule_obj["collection-name"] = _collection_name;
rule_obj["user-name"] = _user_name;
rule_obj["publisher"] = _publisher;
rule_obj["publish-type"] = publish_type::collection;
const auto delay_err = _delayExec(
rule_obj.dump().c_str(),
"",
generate_delay_execution_parameters().c_str(),
rei_);
if(delay_err < 0) {
THROW(
delay_err,
boost::format("queue collection publishing failed for [%s] publisher [%s]") %
_collection_name %
_publisher);
}
rodsLog(
config_.log_level,
"irods::publishing::collection publishing collection [%s] with [%s]",
_collection_name.c_str(),
_publisher.c_str());
} // schedule_collection_publishing_event
void publisher::schedule_object_publishing_event(
const std::string& _object_path,
const std::string& _user_name,
const std::string& _publisher) {
try {
schedule_policy_event_for_object(
policy::object::publish,
_object_path,
_user_name,
_publisher,
publish_type::object,
generate_delay_execution_parameters());
}
catch(const irods::exception& _e) {
rodsLog(
LOG_ERROR,
"failed [%s]",
_e.what());
}
} // schedule_object_publishing_event
std::string publisher::generate_delay_execution_parameters() {
std::string params{config_.delay_parameters + "<INST_NAME>" + config_.instance_name_ + "</INST_NAME>"};
int min_time{1};
try {
min_time = boost::lexical_cast<int>(config_.minimum_delay_time);
}
catch(const boost::bad_lexical_cast&) {}
int max_time{30};
try {
max_time = boost::lexical_cast<int>(config_.maximum_delay_time);
}
catch(const boost::bad_lexical_cast&) {}
std::string sleep_time{"1"};
try {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(min_time, max_time);
sleep_time = boost::lexical_cast<std::string>(dis(gen));
}
catch(const boost::bad_lexical_cast&) {}
params += "<PLUSET>"+sleep_time+"s</PLUSET>";
rodsLog(
config_.log_level,
"irods::storage_tiering :: delay params min [%d] max [%d] computed [%s]",
min_time,
max_time,
params.c_str());
return params;
} // generate_delay_execution_parameters
bool publisher::object_is_published(
const std::string& _object_path) {
boost::filesystem::path p{_object_path};
std::string coll_name = p.parent_path().string();
std::string data_name = p.filename().string();
std::string query_str {
boost::str(boost::format(
"SELECT META_DATA_ATTR_VALUE WHERE META_DATA_ATTR_NAME = '%s' and DATA_NAME = '%s' AND COLL_NAME = '%s'")
% config_.publish
% data_name
% coll_name)};
try {
query<rsComm_t> qobj{comm_, query_str, 1};
if(qobj.size() == 0) {
return false;
}
}
catch(const std::exception&) {
return false;
}
catch(const irods::exception&) {
return false;
}
return true;
} // object_is_published
bool publisher::collection_is_published(
const std::string& _collection_name) {
std::string query_str {
boost::str(boost::format(
"SELECT META_COLL_ATTR_VALUE WHERE META_COLL_ATTR_NAME = '%s' and COLL_NAME = '%s'")
% config_.publish
% _collection_name)};
try {
query<rsComm_t> qobj{comm_, query_str, 1};
if(qobj.size() == 0) {
return false;
}
}
catch(const std::exception&) {
return false;
}
catch(const irods::exception&) {
return false;
}
return true;
} // collection_is_published
publisher::metadata_results publisher::get_metadata_for_data_object(
const std::string& _object_path,
const std::string& _meta_attr_name) {
boost::filesystem::path p{_object_path};
std::string coll_name = p.parent_path().string();
std::string data_name = p.filename().string();
std::string query_str {
boost::str(boost::format(
"SELECT META_DATA_ATTR_VALUE, META_DATA_ATTR_UNITS WHERE META_DATA_ATTR_NAME = '%s' and DATA_NAME = '%s' AND COLL_NAME = '%s'")
% _meta_attr_name
% data_name
% coll_name) };
query<rsComm_t> qobj{comm_, query_str};
metadata_results ret_val;
for(const auto& row : qobj) {
ret_val.push_back(std::make_pair(row[0], row[1]));
}
return ret_val;
} // get_metadata_for_data_object
publisher::metadata_results publisher::get_metadata_for_collection(
const std::string& _meta_attr_name,
const std::string& _collection) {
std::string query_str {
boost::str(boost::format(
"SELECT META_COLL_ATTR_VALUE, META_COLL_ATTR_UNITS WHERE META_COLL_ATTR_NAME = '%s' and COLL_NAME = '%s'")
% _meta_attr_name
% _collection) };
query<rsComm_t> qobj{comm_, query_str, 1};
metadata_results ret_val;
for(const auto& row : qobj) {
ret_val.push_back(std::make_pair(row[0], row[1]));
}
return ret_val;
} // get_metadata_for_collection
void publisher::schedule_policy_event_for_object(
const std::string& _event,
const std::string& _object_path,
const std::string& _user_name,
const std::string& _publisher,
const std::string& _publish_type,
const std::string& _data_movement_params) {
using json = nlohmann::json;
json rule_obj;
rule_obj["rule-engine-operation"] = _event;
rule_obj["rule-engine-instance-name"] = config_.instance_name_;
rule_obj["object-path"] = _object_path;
rule_obj["user-name"] = _user_name;
rule_obj["publisher"] = _publisher;
rule_obj["publish-type"] = _publish_type;
const auto delay_err = _delayExec(
rule_obj.dump().c_str(),
"",
_data_movement_params.c_str(),
rei_);
if(delay_err < 0) {
THROW(
delay_err,
boost::format("queue publishing event failed for object [%s] publisher [%s] type [%s]") %
_object_path %
_publisher %
_publish_type);
}
rodsLog(
config_.log_level,
"irods::publishing::publisher publishing object [%s] with [%s] type [%s]",
_object_path.c_str(),
_publisher.c_str(),
_publish_type.c_str());
} // schedule_policy_event_for_object
} // namespace publishing
}; // namespace irods