rippled
ShardArchiveHandler.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_RPC_SHARDARCHIVEHANDLER_H_INCLUDED
21 #define RIPPLE_RPC_SHARDARCHIVEHANDLER_H_INCLUDED
22 
23 #include <ripple/app/main/Application.h>
24 #include <ripple/basics/BasicConfig.h>
25 #include <ripple/basics/StringUtilities.h>
26 #include <ripple/core/DatabaseCon.h>
27 #include <ripple/net/DatabaseDownloader.h>
28 #include <ripple/rpc/ShardVerificationScheduler.h>
29 
30 #include <boost/asio/basic_waitable_timer.hpp>
31 #include <boost/filesystem.hpp>
32 
33 namespace ripple {
34 #ifdef ENABLE_TESTS
35 namespace test {
36 class ShardArchiveHandler_test;
37 }
38 #endif // ENABLE_TESTS
39 namespace RPC {
40 
43 {
44 public:
45  using TimerOpCounter =
47 #ifdef ENABLE_TESTS
48  friend class test::ShardArchiveHandler_test;
49 #endif // ENABLE_TESTS
50 
51  static boost::filesystem::path
52  getDownloadDirectory(Config const& config);
53 
56 
57  // Create a ShardArchiveHandler only if
58  // the state database is present, indicating
59  // that recovery is needed.
62 
63  explicit ShardArchiveHandler(Application& app);
64 
65  virtual ~ShardArchiveHandler() = default;
66 
67  [[nodiscard]] bool
68  init();
69 
70  bool
72 
74  bool
75  start();
76 
77  void
78  stop();
79 
80  void
81  release();
82 
83 private:
84  ShardArchiveHandler() = delete;
87  operator=(ShardArchiveHandler&&) = delete;
89  operator=(ShardArchiveHandler const&) = delete;
90 
91  [[nodiscard]] bool
93 
100  bool
101  add(std::uint32_t shardIndex,
102  parsedURL&& url,
104 
105  // Begins the download and import of the next archive.
106  bool
108 
109  // Callback used by the downloader to notify completion of a download.
110  void
111  complete(boost::filesystem::path dstPath);
112 
113  // Extract a downloaded archive and import it into the shard store.
114  void
115  process(boost::filesystem::path const& dstPath);
116 
117  // Remove the archive being processed.
118  void
120 
121  void
123 
124  bool
126  std::string const& errorMsg,
127  std::lock_guard<std::mutex> const& lock);
128 
129  bool
131 
133  // m_ is used to protect access to downloader_,
134  // archives_, process_ and to protect setting and
135  // destroying sqlDB_.
137  std::mutex mutable m_;
141  bool process_;
144 
147  boost::filesystem::path const downloadDir_;
148  boost::asio::basic_waitable_timer<std::chrono::steady_clock> timer_;
152 };
153 
155 // The RecoveryHandler is an empty class that is constructed by
156 // the application when the ShardArchiveHandler's state database
157 // is present at application start, indicating that the handler
158 // needs to perform recovery. However, if recovery isn't needed
159 // at application start, and the user subsequently submits a request
160 // to download shards, we construct a ShardArchiveHandler rather
161 // than a RecoveryHandler to process the request. With this approach,
162 // type verification can be employed to determine whether the
163 // ShardArchiveHandler was constructed in recovery mode by the
164 // application, or as a response to a user submitting a request to
165 // download shards.
168 {
169 public:
170  explicit RecoveryHandler(Application& app);
171 };
172 
173 } // namespace RPC
174 } // namespace ripple
175 
176 #endif
ripple::Application
Definition: Application.h:115
ripple::RPC::ShardArchiveHandler::getDownloadDirectory
static boost::filesystem::path getDownloadDirectory(Config const &config)
Definition: ShardArchiveHandler.cpp:38
ripple::RPC::ShardArchiveHandler::downloader_
std::shared_ptr< DatabaseDownloader > downloader_
Definition: ShardArchiveHandler.h:139
ripple::RPC::ShardArchiveHandler::downloadDir_
const boost::filesystem::path downloadDir_
Definition: ShardArchiveHandler.h:147
ripple::RPC::ShardArchiveHandler::remove
void remove(std::lock_guard< std::mutex > const &)
Definition: ShardArchiveHandler.cpp:512
ripple::RPC::ShardArchiveHandler::timerCounter_
TimerOpCounter timerCounter_
Definition: ShardArchiveHandler.h:150
ripple::RPC::ShardArchiveHandler::verificationScheduler_
ShardVerificationScheduler verificationScheduler_
Definition: ShardArchiveHandler.h:151
ripple::RPC::ShardArchiveHandler::operator=
ShardArchiveHandler & operator=(ShardArchiveHandler &&)=delete
ripple::RPC::ShardArchiveHandler::stop
void stop()
Definition: ShardArchiveHandler.cpp:177
ripple::RPC::ShardArchiveHandler::makeShardArchiveHandler
static std::unique_ptr< ShardArchiveHandler > makeShardArchiveHandler(Application &app)
Definition: ShardArchiveHandler.cpp:50
std::string
STL class.
std::shared_ptr
STL class.
ripple::RPC::ShardArchiveHandler::removeAndProceed
bool removeAndProceed(std::lock_guard< std::mutex > const &lock)
Definition: ShardArchiveHandler.cpp:574
ripple::RPC::ShardArchiveHandler::~ShardArchiveHandler
virtual ~ShardArchiveHandler()=default
ripple::RPC::ShardArchiveHandler::start
bool start()
Starts downloading and importing archives.
Definition: ShardArchiveHandler.cpp:236
ripple::RPC::ShardArchiveHandler::add
bool add(std::uint32_t shardIndex, std::pair< parsedURL, std::string > &&url)
Definition: ShardArchiveHandler.cpp:200
ripple::parsedURL
Definition: StringUtilities.h:116
std::pair
ripple::RPC::ShardArchiveHandler::doRelease
void doRelease(std::lock_guard< std::mutex > const &)
Definition: ShardArchiveHandler.cpp:534
std::lock_guard
STL class.
ripple::RPC::RecoveryHandler::RecoveryHandler
RecoveryHandler(Application &app)
Definition: ShardArchiveHandler.cpp:580
ripple::RPC::ShardArchiveHandler::initFromDB
bool initFromDB(std::lock_guard< std::mutex > const &)
Definition: ShardArchiveHandler.cpp:132
ripple::RPC::ShardArchiveHandler::onClosureFailed
bool onClosureFailed(std::string const &errorMsg, std::lock_guard< std::mutex > const &lock)
Definition: ShardArchiveHandler.cpp:561
ripple::RPC::ShardArchiveHandler::stopping_
std::atomic_bool stopping_
Definition: ShardArchiveHandler.h:138
ripple::RPC::ShardArchiveHandler::timer_
boost::asio::basic_waitable_timer< std::chrono::steady_clock > timer_
Definition: ShardArchiveHandler.h:148
ripple::RPC::ShardArchiveHandler::release
void release()
Definition: ShardArchiveHandler.cpp:288
ripple::RPC::ShardArchiveHandler::process
void process(boost::filesystem::path const &dstPath)
Definition: ShardArchiveHandler.cpp:473
ripple::Config
Definition: Config.h:89
ripple::ClosureCounter< void, boost::system::error_code const & >
ripple::RPC::ShardArchiveHandler::complete
void complete(boost::filesystem::path dstPath)
Definition: ShardArchiveHandler.cpp:392
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
std::atomic_bool
ripple::RPC::ShardVerificationScheduler
Definition: ShardVerificationScheduler.h:29
std::map
STL class.
ripple::RPC::ShardArchiveHandler::j_
const beast::Journal j_
Definition: ShardArchiveHandler.h:146
ripple::RPC::RecoveryHandler
Definition: ShardArchiveHandler.h:167
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RPC::ShardArchiveHandler::next
bool next(std::lock_guard< std::mutex > const &l)
Definition: ShardArchiveHandler.cpp:295
ripple::RPC::ShardArchiveHandler::tryMakeRecoveryHandler
static std::unique_ptr< ShardArchiveHandler > tryMakeRecoveryHandler(Application &app)
Definition: ShardArchiveHandler.cpp:56
ripple::RPC::ShardArchiveHandler::archives_
std::map< std::uint32_t, parsedURL > archives_
Definition: ShardArchiveHandler.h:140
ripple::RPC::ShardArchiveHandler::app_
Application & app_
Definition: ShardArchiveHandler.h:145
ripple::RPC::ShardArchiveHandler
Handles the download and import of one or more shard archives.
Definition: ShardArchiveHandler.h:42
ripple::RPC::ShardArchiveHandler::ShardArchiveHandler
ShardArchiveHandler()=delete
std::mutex
STL class.
ripple::test::ShardArchiveHandler_test
Definition: ShardArchiveHandler_test.cpp:37
ripple::RPC::ShardArchiveHandler::m_
std::mutex m_
Definition: ShardArchiveHandler.h:137
ripple::RPC::ShardArchiveHandler::jobCounter_
JobCounter jobCounter_
Definition: ShardArchiveHandler.h:149
std::unique_ptr
STL class.
ripple::RPC::ShardArchiveHandler::process_
bool process_
Definition: ShardArchiveHandler.h:141
ripple::RPC::ShardArchiveHandler::init
bool init()
Definition: ShardArchiveHandler.cpp:90
ripple::RPC::ShardArchiveHandler::sqlDB_
std::unique_ptr< DatabaseCon > sqlDB_
Definition: ShardArchiveHandler.h:142