rippled
DatabaseNodeImp.cpp
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 #include <ripple/app/ledger/Ledger.h>
21 #include <ripple/nodestore/impl/DatabaseNodeImp.h>
22 #include <ripple/protocol/HashPrefix.h>
23 
24 namespace ripple {
25 namespace NodeStore {
26 
27 void
29  NodeObjectType type,
30  Blob&& data,
31  uint256 const& hash,
33 {
34  storeStats(1, data.size());
35 
36  backend_->store(NodeObject::createObject(type, std::move(data), hash));
37 }
38 
39 void
41 {
42  if (cache_)
43  cache_->sweep();
44 }
45 
48  uint256 const& hash,
50  FetchReport& fetchReport,
51  bool duplicate)
52 {
53  std::shared_ptr<NodeObject> nodeObject =
54  cache_ ? cache_->fetch(hash) : nullptr;
55 
56  if (!nodeObject)
57  {
58  JLOG(j_.trace()) << "fetchNodeObject " << hash << ": record not "
59  << (cache_ ? "cached" : "found");
60 
61  Status status;
62 
63  try
64  {
65  status = backend_->fetch(hash.data(), &nodeObject);
66  }
67  catch (std::exception const& e)
68  {
69  JLOG(j_.fatal())
70  << "fetchNodeObject " << hash
71  << ": Exception fetching from backend: " << e.what();
72  Rethrow();
73  }
74 
75  switch (status)
76  {
77  case ok:
78  if (nodeObject && cache_)
79  cache_->canonicalize_replace_client(hash, nodeObject);
80  break;
81  case notFound:
82  break;
83  case dataCorrupt:
84  JLOG(j_.fatal()) << "fetchNodeObject " << hash
85  << ": nodestore data is corrupted";
86  break;
87  default:
88  JLOG(j_.warn())
89  << "fetchNodeObject " << hash
90  << ": backend returns unknown result " << status;
91  break;
92  }
93  }
94  else
95  {
96  JLOG(j_.trace()) << "fetchNodeObject " << hash
97  << ": record found in cache";
98  }
99 
100  if (nodeObject)
101  fetchReport.wasFound = true;
102 
103  return nodeObject;
104 }
105 
108 {
110  using namespace std::chrono;
111  auto const before = steady_clock::now();
113  std::vector<uint256 const*> cacheMisses;
114  uint64_t hits = 0;
115  uint64_t fetches = 0;
116  for (size_t i = 0; i < hashes.size(); ++i)
117  {
118  auto const& hash = hashes[i];
119  // See if the object already exists in the cache
120  auto nObj = cache_ ? cache_->fetch(hash) : nullptr;
121  ++fetches;
122  if (!nObj)
123  {
124  // Try the database
125  indexMap[&hash] = i;
126  cacheMisses.push_back(&hash);
127  }
128  else
129  {
130  results[i] = nObj;
131  // It was in the cache.
132  ++hits;
133  }
134  }
135 
136  JLOG(j_.debug()) << "fetchBatch - cache hits = "
137  << (hashes.size() - cacheMisses.size())
138  << " - cache misses = " << cacheMisses.size();
139  auto dbResults = backend_->fetchBatch(cacheMisses).first;
140 
141  for (size_t i = 0; i < dbResults.size(); ++i)
142  {
143  auto nObj = dbResults[i];
144  size_t index = indexMap[cacheMisses[i]];
145  results[index] = nObj;
146  auto const& hash = hashes[index];
147 
148  if (nObj)
149  {
150  // Ensure all threads get the same object
151  if (cache_)
152  cache_->canonicalize_replace_client(hash, nObj);
153  }
154  else
155  {
156  JLOG(j_.error())
157  << "fetchBatch - "
158  << "record not found in db or cache. hash = " << strHex(hash);
159  }
160  }
161 
162  auto fetchDurationUs =
163  std::chrono::duration_cast<std::chrono::microseconds>(
164  steady_clock::now() - before)
165  .count();
166  updateFetchMetrics(fetches, hits, fetchDurationUs);
167  return results;
168 }
169 
170 } // namespace NodeStore
171 } // namespace ripple
beast::Journal::fatal
Stream fatal() const
Definition: Journal.h:339
ripple::NodeStore::DatabaseNodeImp::backend_
std::shared_ptr< Backend > backend_
Definition: DatabaseNodeImp.h:137
std::shared_ptr< NodeObject >
std::exception
STL class.
beast::Journal::trace
Stream trace() const
Severity stream access functions.
Definition: Journal.h:309
ripple::NodeStore::ok
@ ok
Definition: nodestore/Types.h:45
ripple::NodeStore::DatabaseNodeImp::fetchNodeObject
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t, FetchReport &fetchReport, bool duplicate) override
Definition: DatabaseNodeImp.cpp:47
std::vector< unsigned char >
std::vector::size
T size(T... args)
ripple::NodeObjectType
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
ripple::NodeObject::createObject
static std::shared_ptr< NodeObject > createObject(NodeObjectType type, Blob &&data, uint256 const &hash)
Create an object from fields.
Definition: NodeObject.cpp:37
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
ripple::NodeStore::FetchReport
Contains information about a fetch operation.
Definition: ripple/nodestore/Scheduler.h:32
ripple::base_uint::data
pointer data()
Definition: base_uint.h:115
ripple::NodeStore::Database::updateFetchMetrics
void updateFetchMetrics(uint64_t fetches, uint64_t hits, uint64_t duration)
Definition: Database.h:344
std::vector::push_back
T push_back(T... args)
ripple::NodeStore::notFound
@ notFound
Definition: nodestore/Types.h:46
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:75
ripple::Rethrow
void Rethrow()
Rethrow the exception currently being handled.
Definition: contract.h:48
beast::Journal::error
Stream error() const
Definition: Journal.h:333
ripple::NodeStore::DatabaseNodeImp::cache_
std::shared_ptr< TaggedCache< uint256, NodeObject > > cache_
Definition: DatabaseNodeImp.h:135
ripple::NodeStore::dataCorrupt
@ dataCorrupt
Definition: nodestore/Types.h:47
std::uint32_t
ripple::NodeStore::Database::storeStats
void storeStats(std::uint64_t count, std::uint64_t sz)
Definition: Database.h:328
ripple::NodeStore::DatabaseNodeImp::store
void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t) override
Store the object.
Definition: DatabaseNodeImp.cpp:28
ripple::NodeStore::Status
Status
Return codes from Backend operations.
Definition: nodestore/Types.h:44
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeStore::Database::j_
const beast::Journal j_
Definition: Database.h:301
ripple::NodeStore::DatabaseNodeImp::sweep
void sweep() override
Remove expired entries from the positive and negative caches.
Definition: DatabaseNodeImp.cpp:40
ripple::NodeStore::FetchReport::wasFound
bool wasFound
Definition: ripple/nodestore/Scheduler.h:40
ripple::NodeStore::DatabaseNodeImp::fetchBatch
std::vector< std::shared_ptr< NodeObject > > fetchBatch(std::vector< uint256 > const &hashes)
Definition: DatabaseNodeImp.cpp:107
beast::Journal::debug
Stream debug() const
Definition: Journal.h:315
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:45
std::unordered_map
STL class.
std::exception::what
T what(T... args)
std::chrono