ATLAS Offline Software
Public Member Functions | Private Member Functions | Private Attributes | List of all members
SH::DiskWriterXRD Class Referencefinal

an implementation of DiskWriter for the XRD protocol More...

#include <DiskWriterXRD.h>

Inheritance diagram for SH::DiskWriterXRD:
Collaboration diagram for SH::DiskWriterXRD:

Public Member Functions

void testInvariant () const
 test the invariant of this object More...
 
 DiskWriterXRD (const std::string &val_path)
 standard constructor More...
 
 ~DiskWriterXRD ()
 standard destructor More...
 
std::string path () const
 the path where this file can be accessed or the empty string if it is not known (yet). More...
 
TFile * file ()
 the file we are writing to More...
 
void close ()
 closes the file we are writing to More...
 

Private Member Functions

virtual std::string getPath () const override
 the path where this file can be accessed or the empty string if it is not known (yet). More...
 
virtual TFile * getFile () override
 the file we are writing to More...
 
virtual void doClose () override
 closes the file we are writing to More...
 

Private Attributes

std::string m_tmp
 the temporary path being used More...
 
std::string m_path
 the path being used More...
 
std::unique_ptr< TFile > m_file
 the actual file object More...
 

Detailed Description

an implementation of DiskWriter for the XRD protocol

See also
DiskOutputXRD

Definition at line 21 of file DiskWriterXRD.h.

Constructor & Destructor Documentation

◆ DiskWriterXRD()

SH::DiskWriterXRD::DiskWriterXRD ( const std::string &  val_path)

standard constructor

Guarantee
strong
Failures
out of memory I
Precondition
val_path.find ("root://") == 0

Definition at line 42 of file DiskWriterXRD.cxx.

44  : m_path (val_path)
45  {
46  RCU_REQUIRE (val_path.find ("root://") == 0);
47 
48  const char *tmpdir = getenv ("TMPDIR");
49  std::size_t hash {0};
50  boost::hash_combine (hash, std::hash<pid_t>() (getpid()));
51  std::size_t tries = 0;
52  while (m_file == nullptr || !m_file->IsOpen())
53  {
54  if (++ tries == 10)
55  throw std::runtime_error ("infinite loop trying to create tempory file for DiskWriterXRD");
56 
57  auto time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
58  boost::hash_combine (hash, std::hash<decltype(time)>() (time));
59  std::size_t hash16 {hash};
60  while (hash16 > 0xffff)
61  hash16 = (hash16&0xffff) ^ (hash16 >> 16);
62 
63  std::ostringstream str;
64  if (tmpdir)
65  str << tmpdir;
66  else
67  str << "/tmp";
68  str << "/SH-XRD-" << m_path.substr (m_path.rfind ("/")+1)
69  << "-" << std::format("{:04x}", hash16);
70  m_tmp = str.str();
71  if (gSystem->AccessPathName (m_tmp.c_str()) != 0)
72  m_file.reset (TFile::Open (m_tmp.c_str(), "CREATE"));
73  }
74 
75  RCU_NEW_INVARIANT (this);
76  }

◆ ~DiskWriterXRD()

SH::DiskWriterXRD::~DiskWriterXRD ( )

standard destructor

Guarantee
no-fail

Definition at line 80 of file DiskWriterXRD.cxx.

82  {
83  RCU_DESTROY_INVARIANT (this);
84 
85  if (m_file != nullptr)
86  {
87  try
88  {
89  close ();
90  } catch (std::exception& e)
91  {
92  std::cerr << "exception closing file " << m_path << ": "
93  << e.what() << std::endl;
94 
95  } catch (...)
96  {
97  std::cerr << "unknown exception closing file " << m_path << std::endl;
98  }
99  }
100  }

Member Function Documentation

◆ close()

void SH::DiskWriter::close ( )
inherited

closes the file we are writing to

Guarantee
basic
Failures
i/o errors
file already closed

Definition at line 66 of file DiskWriter.cxx.

68  {
69  RCU_CHANGE_INVARIANT (this);
70 
71  doClose ();
72  }

◆ doClose()

void SH::DiskWriterXRD::doClose ( )
overrideprivatevirtual

closes the file we are writing to

Guarantee
basic
Failures
i/o errors
file already closed
Rationale
the virtual part of DiskWriter::close

Implements SH::DiskWriter.

Definition at line 123 of file DiskWriterXRD.cxx.

125  {
126  RCU_CHANGE_INVARIANT (this);
127  RCU_REQUIRE2_SOFT (m_file != nullptr, "file already closed");
128 
129  if (m_file->IsOpen())
130  {
131  if (m_file->Write () < 0)
132  RCU_THROW_MSG ("failed to write to file: " + m_path);
133  m_file->Close ();
134  }
135 
136  std::random_device rd;
137  std::mt19937 gen (rd());
138  bool success = false;
139  unsigned tries = 0u;
140  while (!success)
141  {
142  try
143  {
144  // using the -f flag, because if this copy failed previously
145  // we need to force an overwrite. note that there would be no
146  // point in leaving this out on the first try (even though
147  // there should be no file there), because it would just fail
148  // and retry with the flag set.
150  success = true;
151  } catch (...)
152  {
153  std::cerr << "encountered error copying files to XRD path: \"" << m_path << "\"" << std::endl;
154  if (tries < 10u)
155  {
156  tries += 1;
157  // sleeping for a random period of time, to reduce the
158  // chance that the problem is that multiple jobs finishing
159  // at the same time keep overloading the server by
160  // repeatedly hitting it at the same time.
161  unsigned seconds = std::uniform_int_distribution<>(30,60) (gen);
162  std::cerr << "sleeping for " << seconds << " seconds before retrying" << std::endl;
163  std::this_thread::sleep_for (std::chrono::seconds(seconds));
164  } else
165  {
166  std::cerr << "giving up, leaving file at " << m_tmp << std::endl;
167  throw std::runtime_error ("failed to copy file to XRD");
168  }
169  }
170  }
172  m_file.reset ();
173  }

◆ file()

TFile * SH::DiskWriter::file ( )
inherited

the file we are writing to

Guarantee
strong
Failures
file already closed
Postcondition
result != 0

Definition at line 54 of file DiskWriter.cxx.

56  {
57  RCU_CHANGE_INVARIANT (this);
58 
59  TFile *result = getFile ();
60  RCU_REQUIRE (result != 0);
61  return result;
62  }

◆ getFile()

TFile * SH::DiskWriterXRD::getFile ( )
overrideprivatevirtual

the file we are writing to

Guarantee
strong
Failures
file already closed
Postcondition
result != 0
Rationale
the virtual part of DiskWriter::file

Implements SH::DiskWriter.

Definition at line 113 of file DiskWriterXRD.cxx.

115  {
116  RCU_CHANGE_INVARIANT (this);
117  RCU_REQUIRE2_SOFT (m_file != nullptr, "file already closed");
118  return m_file.get();
119  }

◆ getPath()

std::string SH::DiskWriterXRD::getPath ( ) const
overrideprivatevirtual

the path where this file can be accessed or the empty string if it is not known (yet).

Rationale
while it is not guaranteed that every writer will know how to handle this, most writers will, and it can be useful. however, not every writer will know this until after the file is closed.
Rationale
the virtual part of DiskWriter::path

Implements SH::DiskWriter.

Definition at line 104 of file DiskWriterXRD.cxx.

106  {
107  RCU_READ_INVARIANT (this);
108  return m_path;
109  }

◆ path()

std::string SH::DiskWriter::path ( ) const
inherited

the path where this file can be accessed or the empty string if it is not known (yet).

Rationale
while it is not guaranteed that every writer will know how to handle this, most writers will, and it can be useful. however, not every writer will know this until after the file is closed.

Definition at line 45 of file DiskWriter.cxx.

47  {
48  RCU_READ_INVARIANT (this);
49  return getPath ();
50  }

◆ testInvariant()

void SH::DiskWriterXRD::testInvariant ( ) const

test the invariant of this object

Guarantee
no-fail

Definition at line 35 of file DiskWriterXRD.cxx.

37  {
38  }

Member Data Documentation

◆ m_file

std::unique_ptr<TFile> SH::DiskWriterXRD::m_file
private

the actual file object

Definition at line 86 of file DiskWriterXRD.h.

◆ m_path

std::string SH::DiskWriterXRD::m_path
private

the path being used

Definition at line 82 of file DiskWriterXRD.h.

◆ m_tmp

std::string SH::DiskWriterXRD::m_tmp
private

the temporary path being used

Definition at line 78 of file DiskWriterXRD.h.


The documentation for this class was generated from the following files:
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
get_generator_info.result
result
Definition: get_generator_info.py:21
vtune_athena.format
format
Definition: vtune_athena.py:14
RCU_REQUIRE
#define RCU_REQUIRE(x)
Definition: Assert.h:208
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
master.gen
gen
Definition: master.py:32
SH::DiskWriter::getPath
virtual std::string getPath() const =0
the path where this file can be accessed or the empty string if it is not known (yet).
SH::DiskWriterXRD::m_file
std::unique_ptr< TFile > m_file
the actual file object
Definition: DiskWriterXRD.h:86
python.handimod.now
now
Definition: handimod.py:675
calibdata.exception
exception
Definition: calibdata.py:496
python.LArCalib_HVCorrConfig.seconds
seconds
Definition: LArCalib_HVCorrConfig.py:86
beamspotman.tmpdir
string tmpdir
Definition: beamspotman.py:412
SH::DiskWriterXRD::m_path
std::string m_path
the path being used
Definition: DiskWriterXRD.h:82
SH::DiskWriter::getFile
virtual TFile * getFile()=0
the file we are writing to
RCU_REQUIRE2_SOFT
#define RCU_REQUIRE2_SOFT(x, y)
Definition: Assert.h:155
SCT_ConditionsAlgorithms::CoveritySafe::getenv
std::string getenv(const std::string &variableName)
get an environment variable
Definition: SCT_ConditionsUtilities.cxx:17
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
RCU_DESTROY_INVARIANT
#define RCU_DESTROY_INVARIANT(x)
Definition: Assert.h:235
python.CaloScaleNoiseConfig.str
str
Definition: CaloScaleNoiseConfig.py:78
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
RCU::Shell::exec
void exec(const std::string &cmd)
effects: execute the given command guarantee: strong failures: out of memory II failures: system fail...
Definition: ShellExec.cxx:29
SH::DiskWriter::doClose
virtual void doClose()=0
closes the file we are writing to
RCU_CHANGE_INVARIANT
#define RCU_CHANGE_INVARIANT(x)
Definition: Assert.h:231
str
Definition: BTagTrackIpAccessor.cxx:11
RCU_THROW_MSG
#define RCU_THROW_MSG(message)
Definition: PrintMsg.h:58
SH::DiskWriter::close
void close()
closes the file we are writing to
Definition: DiskWriter.cxx:67
RCU_READ_INVARIANT
#define RCU_READ_INVARIANT(x)
Definition: Assert.h:229
RCU::Shell::quote
std::string quote(const std::string &name)
effects: quote the given name to protect it from the shell returns: the quoted name guarantee: strong...
Definition: ShellExec.cxx:75
SH::DiskWriterXRD::m_tmp
std::string m_tmp
the temporary path being used
Definition: DiskWriterXRD.h:78
RCU_NEW_INVARIANT
#define RCU_NEW_INVARIANT(x)
Definition: Assert.h:233