Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
fileio.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2023 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file ored/utilties/fileio.cpp
20 \brief Wrapper class for retrying file IO operations
21 \ingroup
22*/
23
24#include <boost/filesystem/operations.hpp>
25#include <chrono>
29#include <thread>
30#include <vector>
31
32namespace ore {
33namespace data {
34
35using QuantLib::Real;
36using QuantLib::Size;
37
38// Defaults
39static Size _s_maxRetries = 7;
40static Real _s_backoff = 0.5;
41static Real _s_maxBackoff = 30;
42
43Size FileIO::maxRetries() { return _s_maxRetries; }
44
45Real FileIO::backoff() { return _s_backoff; }
46
47Real FileIO::maxBackoff() { return _s_maxBackoff; }
48
50 LOG("Setting FileOpen max retries to " << n);
51 _s_maxRetries = n;
52}
53
54void FileIO::setBackoff(Real b) {
55 LOG("Setting FileOpen backoff to " << b);
56 _s_backoff = b;
57}
58
60 LOG("Setting FileOpen max backoff to " << m);
61 _s_maxBackoff = m;
62}
63
64FILE* FileIO::fopen(const char* filename, const char* mode) {
65 FILE* fp;
66 Real currentBackoff = backoff();
67
68 for (Size i = 0; i <= maxRetries(); i++) {
69 if (i > 0) {
70 auto em = EventMessage("Error opening file '" + std::string(filename) + "'. Retrying...", "exception_message");
71 em.set("retry_count", i);
72 Real backoffMillis = currentBackoff * 1000;
73 em.set("retry_interval", backoffMillis);
74 em.log();
75 std::this_thread::sleep_for(std::chrono::duration<Real>(currentBackoff));
76 Real nextBackoff = currentBackoff * 2;
77 currentBackoff = (nextBackoff >= maxBackoff()) ? maxBackoff() : nextBackoff;
78 }
79
80 fp = std::fopen(filename, mode);
81 if (fp)
82 break;
83 }
84
85 return fp;
86}
87
88bool FileIO::create_directories(const path& p) {
89 bool res = false;
90 Real currentBackoff = backoff();
91
92 for (Size i = 0; i <= maxRetries(); i++) {
93 if (i > 0) {
94 auto em = EventMessage("Error creating directory '" + p.string() + "'. Retrying...", "exception_message");
95 em.set("retry_count", i);
96 Real backoffMillis = currentBackoff * 1000;
97 em.set("retry_interval", backoffMillis);
98 em.log();
99 std::this_thread::sleep_for(std::chrono::duration<Real>(currentBackoff));
100 Real nextBackoff = currentBackoff * 2;
101 currentBackoff = (nextBackoff >= maxBackoff()) ? maxBackoff() : nextBackoff;
102 }
103
104 try {
105 res = boost::filesystem::create_directories(p);
106 if (res)
107 break;
108 } catch (...) {
109 }
110 }
111
112 return res;
113}
114
115bool FileIO::remove_all(const path& p) {
116 bool res = false;
117 Real currentBackoff = backoff();
118
119 for (Size i = 0; i <= maxRetries(); i++) {
120 if (i > 0) {
121 auto em = EventMessage("Error emptying directory '" + p.string() + "'. Retrying...", "exception_message");
122 em.set("retry_count", i);
123 Real backoffMillis = currentBackoff * 1000;
124 em.set("retry_interval", backoffMillis);
125 em.log();
126 std::this_thread::sleep_for(std::chrono::duration<Real>(currentBackoff));
127 Real nextBackoff = currentBackoff * 2;
128 currentBackoff = (nextBackoff >= maxBackoff()) ? maxBackoff() : nextBackoff;
129 }
130
131 try {
132 res = boost::filesystem::remove_all(p);
133 if (res)
134 break;
135 } catch (...) {
136 }
137 }
138
139 return res;
140}
141
142} // namespace data
143} // namespace ore
static void setMaxBackoff(QuantLib::Real)
Definition: fileio.cpp:59
static QuantLib::Real backoff()
Definition: fileio.cpp:45
static void setBackoff(QuantLib::Real)
Definition: fileio.cpp:54
static void setMaxRetries(QuantLib::Size)
Definition: fileio.cpp:49
static bool remove_all(const path &)
Retry wrapper for boost::filesystem::remove_all.
Definition: fileio.cpp:115
static QuantLib::Real maxBackoff()
Definition: fileio.cpp:47
static QuantLib::Size maxRetries()
The maximum number of retries, defaults to 7.
Definition: fileio.cpp:43
static bool create_directories(const path &)
Retry wrapper for boost::filesystem::create_directories.
Definition: fileio.cpp:88
static FILE * fopen(const char *, const char *)
Retry wrapper for std::fopen.
Definition: fileio.cpp:64
Wrapper class for retrying file IO operations.
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
#define LOG(text)
Logging Macro (Level = Notice)
Definition: log.hpp:552
Serializable Credit Default Swap.
Definition: namespaces.docs:23
string conversion utilities