hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
storage_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_STORAGE_IMPL_HPP
7 #define IROHA_STORAGE_IMPL_HPP
8 
9 #include "ametsuchi/storage.hpp"
10 
11 #include <atomic>
12 #include <cmath>
13 #include <shared_mutex>
14 
15 #include <soci/soci.h>
16 #include <boost/optional.hpp>
24 #include "logger/logger_fwd.hpp"
26 
27 namespace iroha {
28  namespace ametsuchi {
29 
30  class FlatFile;
31  class FailoverCallbackFactory;
32 
34  explicit ConnectionContext(std::unique_ptr<KeyValueStorage> block_store);
35 
36  std::unique_ptr<KeyValueStorage> block_store;
37  };
38 
39  class StorageImpl : public Storage {
40  protected:
41  static expected::Result<bool, std::string> createDatabaseIfNotExist(
42  const std::string &dbname,
43  const std::string &options_str_without_dbname);
44 
45  static expected::Result<ConnectionContext, std::string> initConnections(
46  std::string block_store_dir, logger::LoggerPtr log);
47 
48  static expected::Result<std::shared_ptr<soci::connection_pool>,
49  std::string>
50  initPostgresConnection(std::string &options_str, size_t pool_size);
51 
52  public:
53  static expected::Result<std::shared_ptr<StorageImpl>, std::string> create(
54  std::string block_store_dir,
55  std::string postgres_connection,
56  std::shared_ptr<shared_model::interface::CommonObjectsFactory>
57  factory,
58  std::shared_ptr<shared_model::interface::BlockJsonConverter>
59  converter,
60  std::shared_ptr<shared_model::interface::PermissionToString>
61  perm_converter,
62  std::unique_ptr<BlockStorageFactory> block_storage_factory,
63  std::unique_ptr<ReconnectionStrategyFactory>
64  reconnection_strategy_factory,
65  logger::LoggerManagerTreePtr log_manager,
66  size_t pool_size = 10);
67 
68  expected::Result<std::unique_ptr<TemporaryWsv>, std::string>
69  createTemporaryWsv() override;
70 
71  expected::Result<std::unique_ptr<MutableStorage>, std::string>
72  createMutableStorage() override;
73 
74  boost::optional<std::shared_ptr<PeerQuery>> createPeerQuery()
75  const override;
76 
77  boost::optional<std::shared_ptr<BlockQuery>> createBlockQuery()
78  const override;
79 
80  boost::optional<std::shared_ptr<QueryExecutor>> createQueryExecutor(
81  std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
82  std::shared_ptr<shared_model::interface::QueryResponseFactory>
83  response_factory) const override;
84 
85  bool insertBlock(
86  std::shared_ptr<const shared_model::interface::Block> block) override;
87 
88  expected::Result<void, std::string> insertPeer(
89  const shared_model::interface::Peer &peer) override;
90 
91  expected::Result<std::unique_ptr<MutableStorage>, std::string>
92  createMutableStorage(BlockStorageFactory &storage_factory) override;
93 
94  void reset() override;
95 
96  expected::Result<void, std::string> resetWsv() override;
97 
98  void resetPeers() override;
99 
100  void dropStorage() override;
101 
102  void freeConnections() override;
103 
104  boost::optional<std::unique_ptr<LedgerState>> commit(
105  std::unique_ptr<MutableStorage> mutable_storage) override;
106 
107  boost::optional<std::unique_ptr<LedgerState>> commitPrepared(
108  std::shared_ptr<const shared_model::interface::Block> block) override;
109 
110  std::shared_ptr<WsvQuery> getWsvQuery() const override;
111 
112  std::shared_ptr<BlockQuery> getBlockQuery() const override;
113 
114  rxcpp::observable<std::shared_ptr<const shared_model::interface::Block>>
115  on_commit() override;
116 
117  void prepareBlock(std::unique_ptr<TemporaryWsv> wsv) override;
118 
119  ~StorageImpl() override;
120 
121  protected:
122  struct PoolWrapper {
123  PoolWrapper(std::shared_ptr<soci::connection_pool>,
124  std::unique_ptr<FailoverCallbackFactory>);
125 
126  PoolWrapper(PoolWrapper &&) = default;
127  ~PoolWrapper() = default;
128 
129  std::shared_ptr<soci::connection_pool> connection_pool_;
130  std::unique_ptr<FailoverCallbackFactory> failover_callback_factory_;
131  };
132 
133  StorageImpl(PostgresOptions postgres_options,
134  std::unique_ptr<KeyValueStorage> block_store,
135  PoolWrapper pool_wrapper,
136  std::shared_ptr<shared_model::interface::CommonObjectsFactory>
137  factory,
138  std::shared_ptr<shared_model::interface::BlockJsonConverter>
139  converter,
140  std::shared_ptr<shared_model::interface::PermissionToString>
141  perm_converter,
142  std::unique_ptr<BlockStorageFactory> block_storage_factory,
143  size_t pool_size,
144  bool enable_prepared_blocks,
145  const std::string &prepared_block_name,
146  logger::LoggerManagerTreePtr log_manager);
147 
148  // db info
150 
151  private:
159  bool storeBlock(
160  std::shared_ptr<const shared_model::interface::Block> block);
161 
165  void tryRollback(soci::session &session);
166 
167  std::unique_ptr<KeyValueStorage> block_store_;
168 
169  PoolWrapper pool_wrapper_;
170 
172  std::shared_ptr<soci::connection_pool> &connection_;
173 
174  std::shared_ptr<shared_model::interface::CommonObjectsFactory> factory_;
175 
176  rxcpp::composite_subscription notifier_lifetime_;
177  rxcpp::subjects::subject<
178  std::shared_ptr<const shared_model::interface::Block>>
179  notifier_;
180 
181  std::shared_ptr<shared_model::interface::BlockJsonConverter> converter_;
182 
183  std::shared_ptr<shared_model::interface::PermissionToString>
184  perm_converter_;
185 
186  std::unique_ptr<BlockStorageFactory> block_storage_factory_;
187 
188  logger::LoggerManagerTreePtr log_manager_;
189  logger::LoggerPtr log_;
190 
191  mutable std::shared_timed_mutex drop_mutex_;
192 
193  const size_t pool_size_;
194 
195  bool prepared_blocks_enabled_;
196 
197  std::atomic<bool> block_is_prepared_;
198 
199  std::string prepared_block_name_;
200 
201  protected:
202  static const std::string &reset_;
203  static const std::string &reset_peers_;
204  static const std::string &init_;
205  };
206  } // namespace ametsuchi
207 } // namespace iroha
208 
209 #endif // IROHA_STORAGE_IMPL_HPP
Definition: postgres_options.hpp:20
static const std::string & reset_peers_
Definition: storage_impl.hpp:203
Definition: peer.hpp:18
static const std::string & init_
Definition: storage_impl.hpp:204
static const std::string & reset_
Definition: storage_impl.hpp:202
std::shared_ptr< Logger > LoggerPtr
Definition: logger_fwd.hpp:22
const PostgresOptions postgres_options_
Definition: storage_impl.hpp:149
Definition: storage.hpp:37
std::unique_ptr< FailoverCallbackFactory > failover_callback_factory_
Definition: storage_impl.hpp:130
Definition: block_query.hpp:14
Definition: storage_impl.hpp:122
Definition: storage_impl.hpp:33
std::shared_ptr< LoggerManagerTree > LoggerManagerTreePtr
Definition: logger_manager_fwd.hpp:14
ConnectionContext(std::unique_ptr< KeyValueStorage > block_store)
Definition: storage_impl.cpp:269
std::unique_ptr< KeyValueStorage > block_store
Definition: storage_impl.hpp:36
Definition: block_storage_factory.hpp:18
Definition: storage_impl.hpp:39
std::shared_ptr< soci::connection_pool > connection_pool_
Definition: storage_impl.hpp:129