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  auto nObj = NodeObject::createObject(type, std::move(data), hash);
35  backend_->store(nObj);
36  storeStats(1, nObj->getData().size());
37 }
38 
39 void
41 {
42  if (cache_)
43  cache_->sweep();
44 }
45 
48  uint256 const& hash,
50  FetchReport& fetchReport)
51 {
52  std::shared_ptr<NodeObject> nodeObject{
53  cache_ ? cache_->fetch(hash) : nullptr};
54  if (!nodeObject)
55  {
56  JLOG(j_.trace())
57  << "DatabaseNodeImp::fetchNodeObject - record not in cache";
58  Status status;
59 
60  try
61  {
62  status = backend_->fetch(hash.data(), &nodeObject);
63  }
64  catch (std::exception const& e)
65  {
66  JLOG(j_.fatal()) << "Exception, " << e.what();
67  Rethrow();
68  }
69 
70  switch (status)
71  {
72  case ok:
74  if (nodeObject)
75  {
76  fetchSz_ += nodeObject->getData().size();
77  if (cache_)
78  cache_->canonicalize_replace_client(hash, nodeObject);
79  }
80  break;
81  case notFound:
82  break;
83  case dataCorrupt:
84  JLOG(j_.fatal()) << "Corrupt NodeObject #" << hash;
85  break;
86  default:
87  JLOG(j_.warn()) << "Unknown status=" << status;
88  break;
89  }
90  }
91  else
92  {
93  JLOG(j_.trace())
94  << "DatabaseNodeImp::fetchNodeObject - record in cache";
95  }
96 
97  if (nodeObject)
98  fetchReport.wasFound = true;
99 
100  return nodeObject;
101 }
102 
105 {
107  using namespace std::chrono;
108  auto const before = steady_clock::now();
110  std::vector<uint256 const*> cacheMisses;
111  uint64_t hits = 0;
112  uint64_t fetches = 0;
113  for (size_t i = 0; i < hashes.size(); ++i)
114  {
115  auto const& hash = hashes[i];
116  // See if the object already exists in the cache
117  auto nObj = cache_ ? cache_->fetch(hash) : nullptr;
118  ++fetches;
119  if (!nObj)
120  {
121  // Try the database
122  indexMap[&hash] = i;
123  cacheMisses.push_back(&hash);
124  }
125  else
126  {
127  results[i] = nObj;
128  // It was in the cache.
129  ++hits;
130  }
131  }
132 
133  JLOG(j_.debug()) << "DatabaseNodeImp::fetchBatch - cache hits = "
134  << (hashes.size() - cacheMisses.size())
135  << " - cache misses = " << cacheMisses.size();
136  auto dbResults = backend_->fetchBatch(cacheMisses).first;
137 
138  for (size_t i = 0; i < dbResults.size(); ++i)
139  {
140  auto nObj = dbResults[i];
141  size_t index = indexMap[cacheMisses[i]];
142  results[index] = nObj;
143  auto const& hash = hashes[index];
144 
145  if (nObj)
146  {
147  // Ensure all threads get the same object
148  if (cache_)
149  cache_->canonicalize_replace_client(hash, nObj);
150  }
151  else
152  {
153  JLOG(j_.error())
154  << "DatabaseNodeImp::fetchBatch - "
155  << "record not found in db or cache. hash = " << strHex(hash);
156  }
157  }
158 
159  auto fetchDurationUs =
160  std::chrono::duration_cast<std::chrono::microseconds>(
161  steady_clock::now() - before)
162  .count();
163  updateFetchMetrics(fetches, hits, fetchDurationUs);
164  return results;
165 }
166 
167 } // namespace NodeStore
168 } // 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
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
ripple::NodeStore::DatabaseNodeImp::fetchNodeObject
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t, FetchReport &fetchReport) override
Definition: DatabaseNodeImp.cpp:47
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
ripple::NodeStore::Database::fetchSz_
std::atomic< std::uint32_t > fetchSz_
Definition: Database.h:306
ripple::NodeStore::FetchReport
Contains information about a fetch operation.
Definition: ripple/nodestore/Scheduler.h:32
ripple::NodeStore::Database::fetchHitCount_
std::atomic< std::uint32_t > fetchHitCount_
Definition: Database.h:305
ripple::base_uint::data
pointer data()
Definition: base_uint.h:114
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:74
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:104
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