ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FROG.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FROG.cc
1 #include "FROG.h"
2 
3 #include <phool/phool.h>
4 
5 #include <odbc++/connection.h>
6 #include <odbc++/drivermanager.h>
7 #include <odbc++/resultset.h>
8 #include <odbc++/statement.h> // for Statement
9 #include <odbc++/types.h> // for SQLException
10 
11 #include <boost/tokenizer.hpp>
12 
13 #include <chrono>
14 #include <cstdlib>
15 #include <fstream>
16 #include <iostream>
17 #include <string>
18 #include <thread>
19 
20 const char *
21 FROG::location(const std::string &logical_name)
22 {
23  pfn = logical_name;
24  if (logical_name.empty() || logical_name.find("/") != std::string::npos)
25  {
26  if (Verbosity() > 0)
27  {
28  if (logical_name.empty())
29  {
30  std::cout << "FROG: empty string as filename" << std::endl;
31  }
32  else if (logical_name.find("/") != std::string::npos)
33  {
34  std::cout << "FROG: found / in filename, assuming it contains a full path" << std::endl;
35  }
36  }
37  return pfn.c_str();
38  }
39  try
40  {
41  std::string gsearchpath(getenv("GSEARCHPATH"));
42  if (Verbosity() > 0)
43  {
44  std::cout << "FROG: GSEARCHPATH: " << gsearchpath << std::endl;
45  }
46  boost::char_separator<char> sep(":");
47  boost::tokenizer<boost::char_separator<char> > tok(gsearchpath, sep);
48  for (auto &iter : tok)
49  {
50  if (iter == "PG")
51  {
52  if (Verbosity() > 1)
53  {
54  std::cout << "Searching FileCatalog for disk resident file "
55  << logical_name << std::endl;
56  }
57  if (PGSearch(logical_name))
58  {
59  if (Verbosity() > 1)
60  {
61  std::cout << "Found " << logical_name << " in FileCatalog, returning "
62  << pfn << std::endl;
63  }
64  break;
65  }
66  }
67  else if (iter == "DCACHE")
68  {
69  if (Verbosity() > 1)
70  {
71  std::cout << "Searching FileCatalog for dCache file "
72  << logical_name << std::endl;
73  }
74  if (dCacheSearch(logical_name))
75  {
76  if (Verbosity() > 1)
77  {
78  std::cout << "Found " << logical_name << " in dCache, returning "
79  << pfn << std::endl;
80  }
81  break;
82  }
83  }
84  else if (iter == "XROOTD")
85  {
86  if (Verbosity() > 1)
87  {
88  std::cout << "Searching FileCatalog for XRootD file "
89  << logical_name << std::endl;
90  }
91  if (XRootDSearch(logical_name))
92  {
93  if (Verbosity() > 1)
94  {
95  std::cout << "Found " << logical_name << " in XRootD, returning "
96  << pfn << std::endl;
97  }
98  break;
99  }
100  }
101  else if (iter == "LUSTRE")
102  {
103  if (Verbosity() > 1)
104  {
105  std::cout << "Searching FileCatalog for Lustre file "
106  << logical_name << std::endl;
107  }
108  if (LustreSearch(logical_name))
109  {
110  if (Verbosity() > 1)
111  {
112  std::cout << "Found " << logical_name << " in Lustre, returning "
113  << pfn << std::endl;
114  }
115  break;
116  }
117  }
118  else if (iter == "MINIO")
119  {
120  if (Verbosity() > 1)
121  {
122  std::cout << "Searching FileCatalog for Lustre file via MinIO"
123  << logical_name << std::endl;
124  }
125  if (MinIOSearch(logical_name))
126  {
127  if (Verbosity() > 1)
128  {
129  std::cout << "Found " << logical_name << " in Lustre, returning MinIO URL "
130  << pfn << std::endl;
131  }
132  break;
133  }
134  }
135  else // assuming this is a file path
136  {
137  if (Verbosity() > 0)
138  {
139  std::cout << "Trying path " << iter << std::endl;
140  }
141  std::string fullfile = iter + "/" + logical_name;
142  if (localSearch(fullfile))
143  {
144  break;
145  }
146  }
147  }
148  }
149  catch (...)
150  {
151  if (Verbosity() > 0)
152  {
153  std::cout << "FROG: GSEARCHPATH not set " << std::endl;
154  }
155  }
156  Disconnect();
157  return pfn.c_str();
158 }
159 
160 bool FROG::localSearch(const std::string &logical_name)
161 {
162  if (std::ifstream(logical_name))
163  {
164  pfn = logical_name;
165  return true;
166  }
167  return false;
168 }
169 
171 {
172  if (m_OdbcConnection)
173  {
174  return true;
175  }
176  int icount = 0;
177  do
178  {
179  try
180  {
181  m_OdbcConnection = odbc::DriverManager::getConnection("FileCatalog", "argouser", "Brass_Ring");
182  return true;
183  }
184  catch (odbc::SQLException &e)
185  {
186  std::cout << PHWHERE
187  << " Exception caught during DriverManager::getConnection" << std::endl;
188  std::cout << "Message: " << e.getMessage() << std::endl;
189  }
190  icount++;
191  std::this_thread::sleep_for(std::chrono::seconds(30)); // sleep 30 seconds before retry
192  } while (icount < 5);
193  return false;
194 }
195 
197 {
198  delete m_OdbcConnection;
199  m_OdbcConnection = nullptr;
200 }
201 
202 bool FROG::PGSearch(const std::string &lname)
203 {
204  bool bret = false;
205  if (!GetConnection())
206  {
207  return bret;
208  }
209  std::string sqlquery = "SELECT full_file_path from files where lfn='" + lname + "' and full_host_name <> 'hpss' and full_host_name <> 'dcache' and full_host_name <> 'lustre'";
210 
211  odbc::Statement *stmt = m_OdbcConnection->createStatement();
212  odbc::ResultSet *rs = stmt->executeQuery(sqlquery);
213 
214  if (rs->next())
215  {
216  pfn = rs->getString(1);
217  bret = true;
218  }
219  delete rs;
220  delete stmt;
221  return bret;
222 }
223 
224 bool FROG::dCacheSearch(const std::string &lname)
225 {
226  bool bret = false;
227  if (!GetConnection())
228  {
229  return bret;
230  }
231  std::string sqlquery = "SELECT full_file_path from files where lfn='" + lname + "' and full_host_name = 'dcache'";
232 
233  odbc::Statement *stmt = m_OdbcConnection->createStatement();
234  odbc::ResultSet *rs = stmt->executeQuery(sqlquery);
235 
236  if (rs->next())
237  {
238  std::string dcachefile = rs->getString(1);
239  if (std::ifstream(dcachefile))
240  {
241  pfn = "dcache:" + dcachefile;
242  bret = true;
243  }
244  }
245  delete rs;
246  delete stmt;
247  return bret;
248 }
249 
250 bool FROG::XRootDSearch(const std::string &lname)
251 {
252  bool bret = false;
253  if (!GetConnection())
254  {
255  return bret;
256  }
257  std::string sqlquery = "SELECT full_file_path from files where lfn='" + lname + "' and full_host_name = 'dcache'";
258 
259  odbc::Statement *stmt = m_OdbcConnection->createStatement();
260  odbc::ResultSet *rs = stmt->executeQuery(sqlquery);
261 
262  if (rs->next())
263  {
264  std::string xrootdfile = rs->getString(1);
265  if (std::ifstream(xrootdfile))
266  {
267  pfn = "root://dcsphdoor02.rcf.bnl.gov:1095" + xrootdfile;
268  bret = true;
269  }
270  }
271  delete rs;
272  delete stmt;
273  return bret;
274 }
275 
276 bool FROG::LustreSearch(const std::string &lname)
277 {
278  bool bret = false;
279  if (!GetConnection())
280  {
281  return bret;
282  }
283  std::string sqlquery = "SELECT full_file_path from files where lfn='" + lname + "' and full_host_name = 'lustre'";
284 
285  odbc::Statement *stmt = m_OdbcConnection->createStatement();
286  odbc::ResultSet *rs = stmt->executeQuery(sqlquery);
287 
288  if (rs->next())
289  {
290  pfn = rs->getString(1);
291  bret = true;
292  }
293  delete rs;
294  delete stmt;
295  return bret;
296 }
297 
298 bool FROG::MinIOSearch(const std::string &lname)
299 {
300  bool bret = false;
301  if (!GetConnection())
302  {
303  return bret;
304  }
305  std::string sqlquery = "SELECT full_file_path from files where lfn='" + lname + "' and full_host_name = 'lustre'";
306 
307  odbc::Statement *stmt = m_OdbcConnection->createStatement();
308  odbc::ResultSet *rs = stmt->executeQuery(sqlquery);
309 
310  if (rs->next())
311  {
312  pfn = rs->getString(1);
313  std::string toreplace("/sphenix/lustre01/sphnxpro");
314  size_t strpos = pfn.find(toreplace);
315  if (strpos == std::string::npos)
316  {
317  std::cout << " could not locate " << toreplace
318  << " in full file path " << pfn << std::endl;
319  exit(1);
320  }
321  else if (strpos > 0)
322  {
323  std::cout << "full file path " << pfn
324  << "does not start with " << toreplace << std::endl;
325  exit(1);
326 
327  }
328  pfn.replace(pfn.begin(),pfn.begin()+toreplace.size(),"s3://dcsphst004.rcf.bnl.gov:9000");
329  bret = true;
330  }
331  delete rs;
332  delete stmt;
333  return bret;
334 }