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  pCache_->canonicalize_replace_cache(hash, nObj);
36  backend_->store(nObj);
37  nCache_->erase(hash);
38  storeStats(1, nObj->getData().size());
39 }
40 
41 bool
43  uint256 const& hash,
44  std::uint32_t ledgerSeq,
45  std::shared_ptr<NodeObject>& nodeObject)
46 {
47  // See if the object is in cache
48  nodeObject = pCache_->fetch(hash);
49  if (nodeObject || nCache_->touch_if_exists(hash))
50  return true;
51 
52  // Otherwise post a read
53  Database::asyncFetch(hash, ledgerSeq);
54  return false;
55 }
56 
57 void
59 {
60  pCache_->setTargetSize(size);
61  pCache_->setTargetAge(age);
62  nCache_->setTargetSize(size);
63  nCache_->setTargetAge(age);
64 }
65 
66 void
68 {
69  pCache_->sweep();
70  nCache_->sweep();
71 }
72 
75  uint256 const& hash,
77  FetchReport& fetchReport)
78 {
79  // See if the node object exists in the cache
80  auto nodeObject{pCache_->fetch(hash)};
81  if (!nodeObject && !nCache_->touch_if_exists(hash))
82  {
83  // Try the backend
84  fetchReport.wentToDisk = true;
85 
86  Status status;
87  try
88  {
89  status = backend_->fetch(hash.data(), &nodeObject);
90  }
91  catch (std::exception const& e)
92  {
93  JLOG(j_.fatal()) << "Exception, " << e.what();
94  Rethrow();
95  }
96 
97  switch (status)
98  {
99  case ok:
100  ++fetchHitCount_;
101  if (nodeObject)
102  fetchSz_ += nodeObject->getData().size();
103  break;
104  case notFound:
105  break;
106  case dataCorrupt:
107  JLOG(j_.fatal()) << "Corrupt NodeObject #" << hash;
108  break;
109  default:
110  JLOG(j_.warn()) << "Unknown status=" << status;
111  break;
112  }
113 
114  if (!nodeObject)
115  {
116  // Just in case a write occurred
117  nodeObject = pCache_->fetch(hash);
118  if (!nodeObject)
119  // We give up
120  nCache_->insert(hash);
121  }
122  else
123  {
124  fetchReport.wasFound = true;
125 
126  // Ensure all threads get the same object
127  pCache_->canonicalize_replace_client(hash, nodeObject);
128 
129  // Since this was a 'hard' fetch, we will log it
130  JLOG(j_.trace()) << "HOS: " << hash << " fetch: in shard db";
131  }
132  }
133 
134  return nodeObject;
135 }
136 
137 } // namespace NodeStore
138 } // namespace ripple
beast::Journal::fatal
Stream fatal() const
Definition: Journal.h:339
ripple::NodeStore::DatabaseNodeImp::backend_
std::shared_ptr< Backend > backend_
Definition: DatabaseNodeImp.h:131
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 >
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
std::chrono::seconds
ripple::NodeStore::DatabaseNodeImp::fetchNodeObject
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t, FetchReport &fetchReport) override
Definition: DatabaseNodeImp.cpp:74
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
ripple::NodeStore::Database::fetchSz_
std::atomic< std::uint32_t > fetchSz_
Definition: Database.h:252
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:251
ripple::NodeStore::DatabaseNodeImp::tune
void tune(int size, std::chrono::seconds age) override
Set the maximum number of entries and maximum cache age for both caches.
Definition: DatabaseNodeImp.cpp:58
ripple::base_uint::data
pointer data()
Definition: base_uint.h:103
ripple::NodeStore::notFound
@ notFound
Definition: nodestore/Types.h:46
ripple::base_uint< 256 >
ripple::NodeStore::DatabaseNodeImp::pCache_
std::shared_ptr< TaggedCache< uint256, NodeObject > > pCache_
Definition: DatabaseNodeImp.h:125
ripple::Rethrow
void Rethrow()
Rethrow the exception currently being handled.
Definition: contract.h:48
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:258
ripple::NodeStore::DatabaseNodeImp::asyncFetch
bool asyncFetch(uint256 const &hash, std::uint32_t ledgerSeq, std::shared_ptr< NodeObject > &nodeObject) override
Fetch an object without waiting.
Definition: DatabaseNodeImp.cpp:42
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::DatabaseNodeImp::nCache_
std::shared_ptr< KeyCache< uint256 > > nCache_
Definition: DatabaseNodeImp.h:128
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::asyncFetch
virtual bool asyncFetch(uint256 const &hash, std::uint32_t ledgerSeq, std::shared_ptr< NodeObject > &nodeObject)=0
Fetch an object without waiting.
ripple::NodeStore::Database::j_
const beast::Journal j_
Definition: Database.h:247
ripple::NodeStore::DatabaseNodeImp::sweep
void sweep() override
Remove expired entries from the positive and negative caches.
Definition: DatabaseNodeImp.cpp:67
ripple::NodeStore::FetchReport::wasFound
bool wasFound
Definition: ripple/nodestore/Scheduler.h:41
ripple::NodeStore::FetchReport::wentToDisk
bool wentToDisk
Definition: ripple/nodestore/Scheduler.h:40
std::exception::what
T what(T... args)