irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

rsRegReplica.cpp
Go to the documentation of this file.
1 
3 /* unregDataObj.c
4  */
5 
6 #include "regReplica.h"
7 
8 #include "objMetaOpr.hpp"
9 #include "rsFileStat.hpp"
10 #include "miscServerFunct.hpp"
11 #include "rsRegReplica.hpp"
12 #include "rsFileChksum.hpp"
13 #include "rsModDataObjMeta.hpp"
15 
16 #include "irods_file_object.hpp"
19 
20 #include "boost/lexical_cast.hpp"
21 
23  rsComm_t* rsComm,
24  regReplica_t* regReplicaInp );
25 
26 namespace irods {
27  namespace reg_repl {
28 
30  const std::string& _object_path,
31  std::string& _collection_name,
32  std::string& _object_name ) {
33  namespace bfs = boost::filesystem;
34 
35  try {
36  bfs::path p(_object_path);
37  _collection_name = p.parent_path().string();
38  _object_name = p.filename().string();
39  }
40  catch(const bfs::filesystem_error& _e) {
41  THROW(SYS_INVALID_FILE_PATH, _e.what());
42  }
43  } // get_object_and_collection_from_path
44 
46  rsComm_t* _comm,
47  const std::string& _logical_path,
48  const std::string& _physical_path,
49  const std::string& _resc_hier ) {
50 
51  fileChksumInp_t chk_inp{};
52  rstrcpy(
53  chk_inp.objPath,
54  _logical_path.c_str(),
55  MAX_NAME_LEN);
56  rstrcpy(
57  chk_inp.fileName,
58  _physical_path.c_str(),
59  MAX_NAME_LEN);
60  rstrcpy(
61  chk_inp.rescHier,
62  _resc_hier.c_str(),
63  MAX_NAME_LEN);
64 
65  char* chksum{};
66  const auto chksum_err = rsFileChksum(
67  _comm,
68  &chk_inp,
69  &chksum);
70  if(DIRECT_ARCHIVE_ACCESS == chksum_err) {
71  return "";
72  }
73 
74  if(chksum_err < 0) {
75  THROW(
76  chksum_err,
77  boost::format("rsDataObjChksum failed for [%s] on [%s]") %
78  _logical_path %
79  _resc_hier);
80  }
81 
82  return chksum;
83  } // compute_checksum_for_resc
84 
86  rsComm_t* _comm,
87  const std::string& _object_path,
88  const std::string& _resource_hierarchy,
89  const std::string& _file_path ) {
90  fileStatInp_t stat_inp{};
91  rstrcpy(stat_inp.objPath, _object_path.c_str(), sizeof(stat_inp.objPath));
92  rstrcpy(stat_inp.rescHier, _resource_hierarchy.c_str(), sizeof(stat_inp.rescHier));
93  rstrcpy(stat_inp.fileName, _file_path.c_str(), sizeof(stat_inp.fileName));
94  rodsStat_t *stat_out{};
95  const auto status_rsFileStat = rsFileStat(_comm, &stat_inp, &stat_out);
96  if(status_rsFileStat < 0) {
97  THROW(
98  status_rsFileStat,
99  boost::format("rsFileStat of objPath [%s] rescHier [%s] fileName [%s] failed with [%d]") %
100  stat_inp.objPath %
101  stat_inp.rescHier %
102  stat_inp.fileName %
103  status_rsFileStat);
104  return status_rsFileStat;
105  }
106 
107  const auto size_in_vault = stat_out->st_size;
108  free(stat_out);
109  return size_in_vault;
110  } // get_file_size_from_filesystem
111 
113  rsComm_t* _comm,
114  regReplica_t* _reg_inp) {
115  dataObjInfo_t* src_info = _reg_inp->srcDataObjInfo;
116  dataObjInfo_t* dst_info = _reg_inp->destDataObjInfo;
117 
118  // Check for data_size_kw
119  keyValPair_t reg_param{};
120  auto data_size_str{getValByKey(&_reg_inp->condInput, DATA_SIZE_KW)};
121  try {
122  if (data_size_str) {
123  addKeyVal(&reg_param, DATA_SIZE_KW, data_size_str);
124  dst_info->dataSize = boost::lexical_cast<decltype(dst_info->dataSize)>(data_size_str);
125  }
126  }
127  catch (boost::bad_lexical_cast&) {
128  rodsLog(LOG_ERROR, "[%s] - bad_lexical_cast for dataSize [%s]; setting to 0", __FUNCTION__, data_size_str);
129  dst_info->dataSize = 0;
130  data_size_str = nullptr;
131  }
132 
133  if (nullptr == data_size_str) {
134  const auto dst_size = get_file_size_from_filesystem(_comm, dst_info->objPath, dst_info->rescHier, dst_info->filePath);
135  if(UNKNOWN_FILE_SZ != dst_size && dst_size != src_info->dataSize) {
136  dst_info->dataSize = dst_size;
137  const auto dst_size_str = boost::lexical_cast<std::string>(dst_size);
138  addKeyVal(&reg_param, DATA_SIZE_KW, dst_size_str.c_str());
139  }
140  }
141 
142  // optional checksum verification, should one exist
143  // on the source replica
144  std::string dst_checksum;
145  if(strlen(src_info->chksum) > 0) {
146  dst_checksum = compute_checksum_for_resc(
147  _comm,
148  dst_info->objPath,
149  dst_info->filePath,
150  dst_info->rescHier);
151  if(!dst_checksum.empty() &&
152  dst_checksum != src_info->chksum) {
153  rstrcpy(
154  dst_info->chksum,
155  dst_checksum.c_str(),
156  sizeof(dst_info->chksum));
157  addKeyVal(
158  &reg_param,
159  CHKSUM_KW,
160  dst_checksum.c_str());
161  }
162  } // checksum
163 
164  if(reg_param.len > 0) {
165  addKeyVal(
166  &reg_param,
168  "TRUE" );
169 
170  modDataObjMeta_t mod_inp{};
171  mod_inp.dataObjInfo = dst_info;
172  mod_inp.regParam = &reg_param;
173  const auto mod_err = rsModDataObjMeta(_comm, &mod_inp);
174  if(mod_err < 0) {
175  THROW(
176  mod_err,
177  boost::format("rsModDataObjMeta failed for [%s] on resc [%s]") %
178  dst_info->objPath %
179  dst_info->rescHier);
180  }
181  } // if reg_params
182  } // verify_and_update_replica
183  } // namespace reg_repl
184 } // namespace irods
185 
186 int
187 rsRegReplica( rsComm_t *rsComm, regReplica_t *regReplicaInp ) {
188  int status;
190  dataObjInfo_t *srcDataObjInfo;
191 
192  srcDataObjInfo = regReplicaInp->srcDataObjInfo;
193 
195  rsComm,
196  MASTER_RCAT,
197  ( const char* )srcDataObjInfo->objPath,
198  &rodsServerHost );
199  if ( status < 0 || NULL == rodsServerHost ) { // JMC cppcheck - nullptr
200  return status;
201  }
202  if ( rodsServerHost->localFlag == LOCAL_HOST ) {
203  std::string svc_role;
204  irods::error ret = get_catalog_service_role(svc_role);
205  if(!ret.ok()) {
206  irods::log(PASS(ret));
207  return ret.code();
208  }
209 
210  if( irods::CFG_SERVICE_ROLE_PROVIDER == svc_role ) {
211  status = _rsRegReplica( rsComm, regReplicaInp );
212  } else if( irods::CFG_SERVICE_ROLE_CONSUMER == svc_role ) {
214  } else {
215  rodsLog(
216  LOG_ERROR,
217  "role not supported [%s]",
218  svc_role.c_str() );
220  }
221  }
222  else {
223  // Add IN_REPL_KW to prevent replication on the redirected server (the provider)
224  addKeyVal(&regReplicaInp->condInput, IN_REPL_KW, "" );
225  status = rcRegReplica( rodsServerHost->conn, regReplicaInp );
226  // Remove the keyword as we will want to replicate on this server (the consumer)
227  rmKeyVal(&regReplicaInp->condInput, IN_REPL_KW);
228  if ( status >= 0 ) {
229  regReplicaInp->destDataObjInfo->replNum = status;
230  }
231  status = _call_file_modified_for_replica( rsComm, regReplicaInp );
232  return status;
233  }
234 
235  if ( status >= 0 ) {
236  try {
237  irods::reg_repl::verify_and_update_replica(rsComm, regReplicaInp);
238  }
239  catch(const irods::exception& _e) {
240  irods::log(_e);
241  return _e.code();
242  }
243 
244  if (!getValByKey(&regReplicaInp->condInput, IN_REPL_KW)) {
245  status = _call_file_modified_for_replica( rsComm, regReplicaInp );
246  }
247  }
248 
249  return status;
250 }
251 
252 int
253 _rsRegReplica( rsComm_t *rsComm, regReplica_t *regReplicaInp ) {
254  std::string svc_role;
255  irods::error ret = get_catalog_service_role(svc_role);
256  if(!ret.ok()) {
257  irods::log(PASS(ret));
258  return ret.code();
259  }
260 
261  if( irods::CFG_SERVICE_ROLE_PROVIDER == svc_role ) {
262  int status;
263  dataObjInfo_t *srcDataObjInfo;
264  dataObjInfo_t *destDataObjInfo;
265  int savedClientAuthFlag;
266 
267  srcDataObjInfo = regReplicaInp->srcDataObjInfo;
268  destDataObjInfo = regReplicaInp->destDataObjInfo;
269  if ( getValByKey( &regReplicaInp->condInput, SU_CLIENT_USER_KW ) != NULL ) {
270  savedClientAuthFlag = rsComm->clientUser.authInfo.authFlag;
272  status = chlRegReplica( rsComm, srcDataObjInfo, destDataObjInfo,
273  &regReplicaInp->condInput );
274  /* restore it */
275  rsComm->clientUser.authInfo.authFlag = savedClientAuthFlag;
276  }
277  else {
278  status = chlRegReplica( rsComm, srcDataObjInfo, destDataObjInfo, &regReplicaInp->condInput );
279  if ( status >= 0 ) {
280  status = destDataObjInfo->replNum;
281  }
282  }
283  // =-=-=-=-=-=-=-
284  // JMC - backport 4608
286  status == CATALOG_ALREADY_HAS_ITEM_BY_THAT_NAME ) { // JMC - backport 4668, 4670
287  int status2;
288  /* register a repl with a copy with the same resource and phyPaht.
289  * could be caused by 2 staging at the same time */
290  status2 = checkDupReplica( rsComm, srcDataObjInfo->dataId,
291  destDataObjInfo->rescName,
292  destDataObjInfo->filePath );
293  if ( status2 >= 0 ) {
294  destDataObjInfo->replNum = status2; // JMC - backport 4668
295  destDataObjInfo->dataId = srcDataObjInfo->dataId;
296  return status2;
297  }
298  }
299  // =-=-=-=-=-=-=-
300  return status;
301  } else if( irods::CFG_SERVICE_ROLE_CONSUMER == svc_role ) {
302  return SYS_NO_RCAT_SERVER_ERR;
303  } else {
304  rodsLog(
305  LOG_ERROR,
306  "role not supported [%s]",
307  svc_role.c_str() );
309  }
310 }
311 
313  rsComm_t* rsComm,
314  regReplica_t* regReplicaInp ) {
315  int status = 0;
316  dataObjInfo_t* destDataObjInfo = regReplicaInp->destDataObjInfo;
317 
318  irods::file_object_ptr file_obj(
319  new irods::file_object(
320  rsComm,
321  destDataObjInfo ) );
322 
323  char* pdmo_kw = getValByKey( &regReplicaInp->condInput, IN_PDMO_KW );
324  if ( pdmo_kw != NULL ) {
325  file_obj->in_pdmo( pdmo_kw );
326  }
327 
328  char* admin_kw = getValByKey( &regReplicaInp->condInput, ADMIN_KW );
329  if ( admin_kw != NULL ) {
330  addKeyVal( (keyValPair_t*)&file_obj->cond_input(), ADMIN_KW, "" );;
331  }
332  const auto open_type{getValByKey(&regReplicaInp->condInput, OPEN_TYPE_KW)};
333  if (open_type) {
334  addKeyVal((keyValPair_t*)&file_obj->cond_input(), OPEN_TYPE_KW, open_type);
335  }
336  irods::error ret = fileModified( rsComm, file_obj );
337  if ( !ret.ok() ) {
338  std::stringstream msg;
339  msg << __FUNCTION__;
340  msg << " - Failed to signal resource that the data object \"";
341  msg << destDataObjInfo->objPath;
342  msg << "\" was registered";
343  ret = PASSMSG( msg.str(), ret );
344  irods::log( ret );
345  status = ret.code();
346  }
347 
348  return status;
349 
350 } // _call_file_modified_for_replica
rodsLog
void rodsLog(int level, const char *formatStr,...)
Definition: rodsLog.cpp:86
getValByKey
char * getValByKey(const keyValPair_t *condInput, const char *keyWord)
Definition: rcMisc.cpp:675
NULL
#define NULL
Definition: rodsDef.h:70
rsComm_t
Definition: rcConnect.h:145
addKeyVal
int addKeyVal(keyValPair_t *condInput, const char *keyWord, const char *value)
Definition: rcMisc.cpp:789
irods::CFG_SERVICE_ROLE_CONSUMER
const std::string CFG_SERVICE_ROLE_CONSUMER("consumer")
irods_configuration_keywords.hpp
rodsServerHost::localFlag
int localFlag
Definition: rodsConnect.h:68
SYS_SERVICE_ROLE_NOT_SUPPORTED
@ SYS_SERVICE_ROLE_NOT_SUPPORTED
Definition: rodsErrorTable.h:217
UNKNOWN_FILE_SZ
#define UNKNOWN_FILE_SZ
Definition: rodsDef.h:92
THROW
#define THROW(_code, _msg)
Definition: irods_exception.hpp:68
irods::reg_repl::get_object_and_collection_from_path
void get_object_and_collection_from_path(const std::string &_object_path, std::string &_collection_name, std::string &_object_name)
Definition: rsRegReplica.cpp:29
rodsServerHost::conn
rcComm_t * conn
Definition: rodsConnect.h:64
irods_file_object.hpp
PASS
#define PASS(prev_error_)
Definition: irods_error.hpp:118
CATALOG_ALREADY_HAS_ITEM_BY_THAT_NAME
@ CATALOG_ALREADY_HAS_ITEM_BY_THAT_NAME
Definition: rodsErrorTable.h:424
CHKSUM_KW
#define CHKSUM_KW
Definition: rodsKeyWdDef.h:25
rsFileStat
int rsFileStat(rsComm_t *rsComm, fileStatInp_t *fileStatInp, rodsStat_t **fileStatOut)
Definition: rsFileStat.cpp:18
irods::reg_repl::verify_and_update_replica
void verify_and_update_replica(rsComm_t *_comm, regReplica_t *_reg_inp)
Definition: rsRegReplica.cpp:112
irods::reg_repl::get_file_size_from_filesystem
rodsLong_t get_file_size_from_filesystem(rsComm_t *_comm, const std::string &_object_path, const std::string &_resource_hierarchy, const std::string &_file_path)
Definition: rsRegReplica.cpp:85
pid_age.p
p
Definition: pid_age.py:13
fileStatInp_t
Definition: fileStat.h:7
DataObjInfo::rescHier
char rescHier[(1024+64)]
Definition: objInfo.h:132
LOCAL_HOST
#define LOCAL_HOST
Definition: rodsConnect.h:44
PASSMSG
#define PASSMSG(message_, prev_error_)
Definition: irods_error.hpp:119
DataObjInfo::filePath
char filePath[(1024+64)]
Definition: objInfo.h:137
rcRegReplica
int rcRegReplica(rcComm_t *conn, regReplica_t *regReplicaInp)
Definition: rcRegReplica.cpp:23
LOG_ERROR
#define LOG_ERROR
Definition: rodsLog.h:43
DATA_SIZE_KW
#define DATA_SIZE_KW
Definition: rodsKeyWdDef.h:24
irods::exception::code
int64_t code() const
Definition: irods_exception.hpp:39
DataObjInfo::dataId
rodsLong_t dataId
Definition: objInfo.h:143
regReplica_t::condInput
keyValPair_t condInput
Definition: regReplica.h:10
regReplica_t::destDataObjInfo
dataObjInfo_t * destDataObjInfo
Definition: regReplica.h:9
rsFileStat.hpp
irods::error::code
long long code() const
Definition: irods_error.cpp:194
rodsStat
Definition: rodsType.h:52
_rsRegReplica
int _rsRegReplica(rsComm_t *rsComm, regReplica_t *regReplicaInp)
Definition: rsRegReplica.cpp:253
irods
Definition: apiHandler.hpp:35
OPEN_TYPE_KW
#define OPEN_TYPE_KW
Definition: rodsKeyWdDef.h:95
chlRegReplica
int chlRegReplica(rsComm_t *rsComm, dataObjInfo_t *srcDataObjInfo, dataObjInfo_t *dstDataObjInfo, keyValPair_t *condInput)
Definition: icatHighLevelRoutines.cpp:546
irods::CFG_SERVICE_ROLE_PROVIDER
const std::string CFG_SERVICE_ROLE_PROVIDER("provider")
IN_REPL_KW
#define IN_REPL_KW
Definition: rodsKeyWdDef.h:230
MAX_NAME_LEN
#define MAX_NAME_LEN
Definition: rodsDef.h:61
FileChksumInp
Definition: fileChksum.h:7
rsFileChksum.hpp
fileModified
irods::error fileModified(rsComm_t *_comm, irods::first_class_object_ptr _object)
Definition: fileDriver.cpp:692
SYS_NO_RCAT_SERVER_ERR
@ SYS_NO_RCAT_SERVER_ERR
Definition: rodsErrorTable.h:110
rmKeyVal
int rmKeyVal(keyValPair_t *condInput, const char *keyWord)
Definition: rcMisc.cpp:710
SU_CLIENT_USER_KW
#define SU_CLIENT_USER_KW
Definition: rodsKeyWdDef.h:86
ADMIN_KW
#define ADMIN_KW
Definition: rodsKeyWdDef.h:62
DataObjInfo::replNum
int replNum
Definition: objInfo.h:140
LOCAL_PRIV_USER_AUTH
#define LOCAL_PRIV_USER_AUTH
Definition: rodsUser.h:36
irods.pypyodbc.status
status
Definition: pypyodbc.py:467
rsFileChksum
int rsFileChksum(rsComm_t *rsComm, fileChksumInp_t *fileChksumInp, char **chksumStr)
Definition: rsFileChksum.cpp:22
irods::log
void log(const error &)
Definition: irods_log.cpp:13
irods::reg_repl::compute_checksum_for_resc
std::string compute_checksum_for_resc(rsComm_t *_comm, const std::string &_logical_path, const std::string &_physical_path, const std::string &_resc_hier)
Definition: rsRegReplica.cpp:45
rsComm_t::clientUser
userInfo_t clientUser
Definition: rcConnect.h:153
regReplica.h
CAT_SUCCESS_BUT_WITH_NO_INFO
@ CAT_SUCCESS_BUT_WITH_NO_INFO
Definition: rodsErrorTable.h:434
irods::error
Definition: irods_error.hpp:23
miscServerFunct.hpp
regReplica_t::srcDataObjInfo
dataObjInfo_t * srcDataObjInfo
Definition: regReplica.h:8
modDataObjMeta_t::dataObjInfo
dataObjInfo_t * dataObjInfo
Definition: modDataObjMeta.h:8
irods::lexical_cast
error lexical_cast(S _s, T &_t)
Definition: irods_lexical_cast.hpp:13
SYS_INVALID_FILE_PATH
@ SYS_INVALID_FILE_PATH
Definition: rodsErrorTable.h:99
getAndConnRcatHost
int getAndConnRcatHost(rsComm_t *rsComm, int rcatType, const char *rcatZoneHint, rodsServerHost_t **rodsServerHost)
Definition: rodsConnect.cpp:26
DataObjInfo::chksum
char chksum[64]
Definition: objInfo.h:135
IN_PDMO_KW
#define IN_PDMO_KW
Definition: rodsKeyWdDef.h:227
irods_hierarchy_parser.hpp
MASTER_RCAT
#define MASTER_RCAT
Definition: rodsDef.h:85
get_catalog_service_role
irods::error get_catalog_service_role(std::string &_role)
Definition: miscServerFunct.cpp:3153
irods::file_object
Definition: irods_file_object.hpp:19
rodsServerHost
Definition: rodsConnect.h:62
DataObjInfo::rescName
char rescName[64]
Definition: objInfo.h:131
irods::file_object_ptr
boost::shared_ptr< file_object > file_object_ptr
Definition: irods_file_object.hpp:145
rsModDataObjMeta
int rsModDataObjMeta(rsComm_t *rsComm, modDataObjMeta_t *modDataObjMetaInp)
Definition: rsModDataObjMeta.cpp:23
rsModDataObjMeta.hpp
irods::exception
Definition: irods_exception.hpp:15
icatHighLevelRoutines.hpp
rsRegReplica.hpp
ALL_REPL_STATUS_KW
#define ALL_REPL_STATUS_KW
Definition: rodsKeyWdDef.h:33
rsRegReplica
int rsRegReplica(rsComm_t *rsComm, regReplica_t *regReplicaInp)
Definition: rsRegReplica.cpp:187
DataObjInfo::objPath
char objPath[(1024+64)]
Definition: objInfo.h:130
checkDupReplica
int checkDupReplica(rsComm_t *rsComm, rodsLong_t dataId, char *rescName, char *filePath)
Definition: objMetaOpr.cpp:711
userInfo_t::authInfo
authInfo_t authInfo
Definition: rodsUser.h:70
_call_file_modified_for_replica
int _call_file_modified_for_replica(rsComm_t *rsComm, regReplica_t *regReplicaInp)
Definition: rsRegReplica.cpp:312
rstrcpy
char * rstrcpy(char *dest, const char *src, int maxLen)
Definition: stringOpr.cpp:51
modDataObjMeta_t
Definition: modDataObjMeta.h:7
regReplica_t
Definition: regReplica.h:7
KeyValPair
Definition: objInfo.h:120
authInfo_t::authFlag
int authFlag
Definition: rodsUser.h:42
irods::error::ok
bool ok()
Definition: irods_error.cpp:258
DataObjInfo
Definition: objInfo.h:129
DataObjInfo::dataSize
rodsLong_t dataSize
Definition: objInfo.h:134
DIRECT_ARCHIVE_ACCESS
@ DIRECT_ARCHIVE_ACCESS
Definition: rodsErrorTable.h:763
rodsLong_t
long long rodsLong_t
Definition: rodsType.h:32
objMetaOpr.hpp