ATLAS Offline Software
Loading...
Searching...
No Matches
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
 DiskWriterXRD (const std::string &val_path)
 standard constructor
 ~DiskWriterXRD ()
 standard destructor
std::string path () const
 the path where this file can be accessed or the empty string if it is not known (yet).
TFile * file ()
 the file we are writing to
void close ()
 closes the file we are writing to

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).
virtual TFile * getFile () override
 the file we are writing to
virtual void doClose () override
 closes the file we are writing to

Private Attributes

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

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 }
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:233
#define RCU_REQUIRE(x)
Definition Assert.h:208
std::string m_tmp
the temporary path being used
std::string m_path
the path being used
std::unique_ptr< TFile > m_file
the actual file object
time(flags, cells_name, *args, **kw)
std::string getenv(const std::string &variableName)
get an environment variable

◆ ~DiskWriterXRD()

SH::DiskWriterXRD::~DiskWriterXRD ( )

standard destructor

Guarantee
no-fail

Definition at line 80 of file DiskWriterXRD.cxx.

82 {
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 }
#define RCU_DESTROY_INVARIANT(x)
Definition Assert.h:235
void close()
closes the file we are writing to

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 {
70
71 doClose ();
72 }
#define RCU_CHANGE_INVARIANT(x)
Definition Assert.h:231
virtual void doClose()=0
closes the file we are writing to

◆ 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 {
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 }
#define RCU_REQUIRE2_SOFT(x, y)
Definition Assert.h:155
#define RCU_THROW_MSG(message)
Definition PrintMsg.h:58
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
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
@ u
Enums for curvilinear frames.
Definition ParamDefs.h:77

◆ 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 {
58
59 TFile *result = getFile ();
60 RCU_REQUIRE (result != 0);
61 return result;
62 }
virtual TFile * getFile()=0
the file we are writing to

◆ 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 {
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 }
#define RCU_READ_INVARIANT(x)
Definition Assert.h:229

◆ 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 }
virtual std::string getPath() const =0
the path where this file can be accessed or the empty string if it is not known (yet).

◆ 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: