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 namespace test {
35 class ShardArchiveHandler_test;
36 }
37 namespace RPC {
38 
41 {
42 public:
43  using TimerOpCounter =
46 
47  static boost::filesystem::path
48  getDownloadDirectory(Config const& config);
49 
52 
53  // Create a ShardArchiveHandler only if
54  // the state database is present, indicating
55  // that recovery is needed.
58 
59  explicit ShardArchiveHandler(Application& app);
60 
61  virtual ~ShardArchiveHandler() = default;
62 
63  [[nodiscard]] bool
64  init();
65 
66  bool
68 
70  bool
71  start();
72 
73  void
74  stop();
75 
76  void
77  release();
78 
79 private:
80  ShardArchiveHandler() = delete;
83  operator=(ShardArchiveHandler&&) = delete;
85  operator=(ShardArchiveHandler const&) = delete;
86 
87  [[nodiscard]] bool
89 
96  bool
97  add(std::uint32_t shardIndex,
98  parsedURL&& url,
100 
101  // Begins the download and import of the next archive.
102  bool
104 
105  // Callback used by the downloader to notify completion of a download.
106  void
107  complete(boost::filesystem::path dstPath);
108 
109  // Extract a downloaded archive and import it into the shard store.
110  void
111  process(boost::filesystem::path const& dstPath);
112 
113  // Remove the archive being processed.
114  void
116 
117  void
119 
120  bool
122  std::string const& errorMsg,
123  std::lock_guard<std::mutex> const& lock);
124 
125  bool
127 
129  // m_ is used to protect access to downloader_,
130  // archives_, process_ and to protect setting and
131  // destroying sqlDB_.
133  std::mutex mutable m_;
137  bool process_;
140 
143  boost::filesystem::path const downloadDir_;
144  boost::asio::basic_waitable_timer<std::chrono::steady_clock> timer_;
148 };
149 
151 // The RecoveryHandler is an empty class that is constructed by
152 // the application when the ShardArchiveHandler's state database
153 // is present at application start, indicating that the handler
154 // needs to perform recovery. However, if recovery isn't needed
155 // at application start, and the user subsequently submits a request
156 // to download shards, we construct a ShardArchiveHandler rather
157 // than a RecoveryHandler to process the request. With this approach,
158 // type verification can be employed to determine whether the
159 // ShardArchiveHandler was constructed in recovery mode by the
160 // application, or as a response to a user submitting a request to
161 // download shards.
164 {
165 public:
166  explicit RecoveryHandler(Application& app);
167 };
168 
169 } // namespace RPC
170 } // namespace ripple
171 
172 #endif
ripple::Application
Definition: Application.h:103
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:135
ripple::RPC::ShardArchiveHandler::downloadDir_
const boost::filesystem::path downloadDir_
Definition: ShardArchiveHandler.h:143
ripple::RPC::ShardArchiveHandler::remove
void remove(std::lock_guard< std::mutex > const &)
Definition: ShardArchiveHandler.cpp:511
ripple::RPC::ShardArchiveHandler::timerCounter_
TimerOpCounter timerCounter_
Definition: ShardArchiveHandler.h:146
ripple::RPC::ShardArchiveHandler::verificationScheduler_
ShardVerificationScheduler verificationScheduler_
Definition: ShardArchiveHandler.h:147
ripple::RPC::ShardArchiveHandler::operator=
ShardArchiveHandler & operator=(ShardArchiveHandler &&)=delete
ripple::RPC::ShardArchiveHandler::stop
void stop()
Definition: ShardArchiveHandler.cpp:176
ripple::RPC::ShardArchiveHandler::makeShardArchiveHandler
static std::unique_ptr< ShardArchiveHandler > makeShardArchiveHandler(Application &app)
Definition: ShardArchiveHandler.cpp:49
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:573
ripple::RPC::ShardArchiveHandler::~ShardArchiveHandler
virtual ~ShardArchiveHandler()=default
ripple::RPC::ShardArchiveHandler::start
bool start()
Starts downloading and importing archives.
Definition: ShardArchiveHandler.cpp:235
ripple::RPC::ShardArchiveHandler::add
bool add(std::uint32_t shardIndex, std::pair< parsedURL, std::string > &&url)
Definition: ShardArchiveHandler.cpp:199
ripple::parsedURL
Definition: StringUtilities.h:100
std::pair
ripple::RPC::ShardArchiveHandler::doRelease
void doRelease(std::lock_guard< std::mutex > const &)
Definition: ShardArchiveHandler.cpp:533
std::lock_guard
STL class.
ripple::RPC::RecoveryHandler::RecoveryHandler
RecoveryHandler(Application &app)
Definition: ShardArchiveHandler.cpp:579
ripple::RPC::ShardArchiveHandler::initFromDB
bool initFromDB(std::lock_guard< std::mutex > const &)
Definition: ShardArchiveHandler.cpp:131
ripple::RPC::ShardArchiveHandler::onClosureFailed
bool onClosureFailed(std::string const &errorMsg, std::lock_guard< std::mutex > const &lock)
Definition: ShardArchiveHandler.cpp:560
ripple::RPC::ShardArchiveHandler::stopping_
std::atomic_bool stopping_
Definition: ShardArchiveHandler.h:134
ripple::RPC::ShardArchiveHandler::timer_
boost::asio::basic_waitable_timer< std::chrono::steady_clock > timer_
Definition: ShardArchiveHandler.h:144
ripple::RPC::ShardArchiveHandler::release
void release()
Definition: ShardArchiveHandler.cpp:287
ripple::RPC::ShardArchiveHandler::process
void process(boost::filesystem::path const &dstPath)
Definition: ShardArchiveHandler.cpp:472
ripple::Config
Definition: Config.h:68
ripple::ClosureCounter< void, boost::system::error_code const & >
ripple::RPC::ShardArchiveHandler::complete
void complete(boost::filesystem::path dstPath)
Definition: ShardArchiveHandler.cpp:391
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:142
ripple::RPC::RecoveryHandler
Definition: ShardArchiveHandler.h:163
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:294
ripple::RPC::ShardArchiveHandler::tryMakeRecoveryHandler
static std::unique_ptr< ShardArchiveHandler > tryMakeRecoveryHandler(Application &app)
Definition: ShardArchiveHandler.cpp:55
ripple::RPC::ShardArchiveHandler::archives_
std::map< std::uint32_t, parsedURL > archives_
Definition: ShardArchiveHandler.h:136
ripple::RPC::ShardArchiveHandler::app_
Application & app_
Definition: ShardArchiveHandler.h:141
ripple::RPC::ShardArchiveHandler
Handles the download and import of one or more shard archives.
Definition: ShardArchiveHandler.h:40
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:133
ripple::RPC::ShardArchiveHandler::jobCounter_
JobCounter jobCounter_
Definition: ShardArchiveHandler.h:145
std::unique_ptr
STL class.
ripple::RPC::ShardArchiveHandler::process_
bool process_
Definition: ShardArchiveHandler.h:137
ripple::RPC::ShardArchiveHandler::init
bool init()
Definition: ShardArchiveHandler.cpp:89
ripple::RPC::ShardArchiveHandler::sqlDB_
std::unique_ptr< DatabaseCon > sqlDB_
Definition: ShardArchiveHandler.h:138