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/net/DatabaseDownloader.h>
27 #include <ripple/rpc/ShardVerificationScheduler.h>
28 
29 #include <boost/asio/basic_waitable_timer.hpp>
30 #include <boost/filesystem.hpp>
31 
32 namespace ripple {
33 namespace test {
34 class ShardArchiveHandler_test;
35 }
36 namespace RPC {
37 
40 {
41 public:
42  using TimerOpCounter =
45 
46  static boost::filesystem::path
47  getDownloadDirectory(Config const& config);
48 
51 
52  // Create a ShardArchiveHandler only if
53  // the state database is present, indicating
54  // that recovery is needed.
57 
59 
60  virtual ~ShardArchiveHandler() = default;
61 
62  [[nodiscard]] bool
63  init();
64 
65  bool
67 
69  bool
70  start();
71 
72  void
73  release();
74 
75 private:
76  ShardArchiveHandler() = delete;
79  operator=(ShardArchiveHandler&&) = delete;
81  operator=(ShardArchiveHandler const&) = delete;
82 
83  [[nodiscard]] bool
85 
86  void
87  onStop() override;
88 
95  bool
96  add(std::uint32_t shardIndex,
97  parsedURL&& url,
99 
100  // Begins the download and import of the next archive.
101  bool
103 
104  // Callback used by the downloader to notify completion of a download.
105  void
106  complete(boost::filesystem::path dstPath);
107 
108  // Extract a downloaded archive and import it into the shard store.
109  void
110  process(boost::filesystem::path const& dstPath);
111 
112  // Remove the archive being processed.
113  void
115 
116  void
118 
119  bool
121  std::string const& errorMsg,
122  std::lock_guard<std::mutex> const& lock);
123 
124  bool
126 
128  // m_ is used to protect access to downloader_,
129  // archives_, process_ and to protect setting and
130  // destroying sqliteDB_.
132  std::mutex mutable m_;
135  bool process_;
138 
141  boost::filesystem::path const downloadDir_;
142  boost::asio::basic_waitable_timer<std::chrono::steady_clock> timer_;
146 };
147 
149 // The RecoveryHandler is an empty class that is constructed by
150 // the application when the ShardArchiveHandler's state database
151 // is present at application start, indicating that the handler
152 // needs to perform recovery. However, if recovery isn't needed
153 // at application start, and the user subsequently submits a request
154 // to download shards, we construct a ShardArchiveHandler rather
155 // than a RecoveryHandler to process the request. With this approach,
156 // type verification can be employed to determine whether the
157 // ShardArchiveHandler was constructed in recovery mode by the
158 // application, or as a response to a user submitting a request to
159 // download shards.
162 {
163 public:
164  RecoveryHandler(Application& app, Stoppable& parent);
165 };
166 
167 } // namespace RPC
168 } // namespace ripple
169 
170 #endif
ripple::Application
Definition: Application.h:101
ripple::RPC::ShardArchiveHandler::getDownloadDirectory
static boost::filesystem::path getDownloadDirectory(Config const &config)
Definition: ShardArchiveHandler.cpp:37
ripple::RPC::ShardArchiveHandler::downloader_
std::shared_ptr< DatabaseDownloader > downloader_
Definition: ShardArchiveHandler.h:133
ripple::RPC::ShardArchiveHandler::downloadDir_
const boost::filesystem::path downloadDir_
Definition: ShardArchiveHandler.h:141
ripple::RPC::ShardArchiveHandler::remove
void remove(std::lock_guard< std::mutex > const &)
Definition: ShardArchiveHandler.cpp:532
ripple::RPC::ShardArchiveHandler::timerCounter_
TimerOpCounter timerCounter_
Definition: ShardArchiveHandler.h:144
ripple::RPC::ShardArchiveHandler::verificationScheduler_
ShardVerificationScheduler verificationScheduler_
Definition: ShardArchiveHandler.h:145
ripple::RPC::ShardArchiveHandler::operator=
ShardArchiveHandler & operator=(ShardArchiveHandler &&)=delete
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:601
ripple::RPC::ShardArchiveHandler::~ShardArchiveHandler
virtual ~ShardArchiveHandler()=default
ripple::RPC::ShardArchiveHandler::start
bool start()
Starts downloading and importing archives.
Definition: ShardArchiveHandler.cpp:256
ripple::RPC::ShardArchiveHandler::add
bool add(std::uint32_t shardIndex, std::pair< parsedURL, std::string > &&url)
Definition: ShardArchiveHandler.cpp:217
ripple::parsedURL
Definition: StringUtilities.h:100
std::pair
ripple::RPC::ShardArchiveHandler::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: ShardArchiveHandler.cpp:193
ripple::RPC::ShardArchiveHandler::doRelease
void doRelease(std::lock_guard< std::mutex > const &)
Definition: ShardArchiveHandler.cpp:557
std::lock_guard
STL class.
ripple::RPC::ShardArchiveHandler::initFromDB
bool initFromDB(std::lock_guard< std::mutex > const &)
Definition: ShardArchiveHandler.cpp:137
ripple::RPC::ShardArchiveHandler::onClosureFailed
bool onClosureFailed(std::string const &errorMsg, std::lock_guard< std::mutex > const &lock)
Definition: ShardArchiveHandler.cpp:588
ripple::RPC::ShardArchiveHandler::timer_
boost::asio::basic_waitable_timer< std::chrono::steady_clock > timer_
Definition: ShardArchiveHandler.h:142
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:201
ripple::RPC::ShardArchiveHandler::release
void release()
Definition: ShardArchiveHandler.cpp:308
ripple::RPC::ShardArchiveHandler::process
void process(boost::filesystem::path const &dstPath)
Definition: ShardArchiveHandler.cpp:493
ripple::Config
Definition: Config.h:67
ripple::RPC::ShardArchiveHandler::sqliteDB_
std::unique_ptr< DatabaseCon > sqliteDB_
Definition: ShardArchiveHandler.h:136
ripple::ClosureCounter< void, boost::system::error_code const & >
ripple::RPC::ShardArchiveHandler::complete
void complete(boost::filesystem::path dstPath)
Definition: ShardArchiveHandler.cpp:412
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
ripple::RPC::ShardVerificationScheduler
Definition: ShardVerificationScheduler.h:29
std::map
STL class.
ripple::RPC::ShardArchiveHandler::makeShardArchiveHandler
static std::unique_ptr< ShardArchiveHandler > makeShardArchiveHandler(Application &app, Stoppable &parent)
Definition: ShardArchiveHandler.cpp:48
ripple::RPC::ShardArchiveHandler::j_
const beast::Journal j_
Definition: ShardArchiveHandler.h:140
ripple::RPC::RecoveryHandler::RecoveryHandler
RecoveryHandler(Application &app, Stoppable &parent)
Definition: ShardArchiveHandler.cpp:607
ripple::RPC::ShardArchiveHandler::tryMakeRecoveryHandler
static std::unique_ptr< ShardArchiveHandler > tryMakeRecoveryHandler(Application &app, Stoppable &parent)
Definition: ShardArchiveHandler.cpp:56
ripple::RPC::RecoveryHandler
Definition: ShardArchiveHandler.h:161
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:315
ripple::RPC::ShardArchiveHandler::archives_
std::map< std::uint32_t, parsedURL > archives_
Definition: ShardArchiveHandler.h:134
ripple::RPC::ShardArchiveHandler::app_
Application & app_
Definition: ShardArchiveHandler.h:139
ripple::RPC::ShardArchiveHandler
Handles the download and import of one or more shard archives.
Definition: ShardArchiveHandler.h:39
ripple::RPC::ShardArchiveHandler::ShardArchiveHandler
ShardArchiveHandler()=delete
std::mutex
STL class.
ripple::test::ShardArchiveHandler_test
Definition: ShardArchiveHandler_test.cpp:36
ripple::RPC::ShardArchiveHandler::m_
std::mutex m_
Definition: ShardArchiveHandler.h:132
ripple::RPC::ShardArchiveHandler::jobCounter_
JobCounter jobCounter_
Definition: ShardArchiveHandler.h:143
std::unique_ptr
STL class.
ripple::RPC::ShardArchiveHandler::process_
bool process_
Definition: ShardArchiveHandler.h:135
ripple::RPC::ShardArchiveHandler::init
bool init()
Definition: ShardArchiveHandler.cpp:91