rippled
RipplePathFind.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2014 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/LedgerMaster.h>
21 #include <ripple/app/paths/PathRequests.h>
22 #include <ripple/net/RPCErr.h>
23 #include <ripple/resource/Fees.h>
24 #include <ripple/rpc/Context.h>
25 #include <ripple/rpc/impl/LegacyPathFind.h>
26 #include <ripple/rpc/impl/RPCHelpers.h>
27 
28 namespace ripple {
29 
30 // This interface is deprecated.
32 {
33  if (context.app.config().PATH_SEARCH_MAX == 0)
34  return rpcError (rpcNOT_SUPPORTED);
35 
37 
39  Json::Value jvResult;
40 
41  if (! context.app.config().standalone() &&
42  ! context.params.isMember(jss::ledger) &&
43  ! context.params.isMember(jss::ledger_index) &&
44  ! context.params.isMember(jss::ledger_hash))
45  {
46  // No ledger specified, use pathfinding defaults
47  // and dispatch to pathfinding engine
48  if (context.app.getLedgerMaster().getValidatedLedgerAge() >
50  {
51  return rpcError (rpcNO_NETWORK);
52  }
53 
54  PathRequest::pointer request;
55  lpLedger = context.ledgerMaster.getClosedLedger();
56 
57  // It doesn't look like there's much odd happening here, but you should
58  // be aware this code runs in a JobQueue::Coro, which is a coroutine.
59  // And we may be flipping around between threads. Here's an overview:
60  //
61  // 1. We're running doRipplePathFind() due to a call to
62  // ripple_path_find. doRipplePathFind() is currently running
63  // inside of a JobQueue::Coro using a JobQueue thread.
64  //
65  // 2. doRipplePathFind's call to makeLegacyPathRequest() enqueues the
66  // path-finding request. That request will (probably) run at some
67  // indeterminate future time on a (probably different) JobQueue
68  // thread.
69  //
70  // 3. As a continuation from that path-finding JobQueue thread, the
71  // coroutine we're currently running in (!) is posted to the
72  // JobQueue. Because it is a continuation, that post won't
73  // happen until the path-finding request completes.
74  //
75  // 4. Once the continuation is enqueued, and we have reason to think
76  // the path-finding job is likely to run, then the coroutine we're
77  // running in yield()s. That means it surrenders its thread in
78  // the JobQueue. The coroutine is suspended, but ready to run,
79  // because it is kept resident by a shared_ptr in the
80  // path-finding continuation.
81  //
82  // 5. If all goes well then path-finding runs on a JobQueue thread
83  // and executes its continuation. The continuation posts this
84  // same coroutine (!) to the JobQueue.
85  //
86  // 6. When the JobQueue calls this coroutine, this coroutine resumes
87  // from the line below the coro->yield() and returns the
88  // path-finding result.
89  //
90  // With so many moving parts, what could go wrong?
91  //
92  // Just in terms of the JobQueue refusing to add jobs at shutdown
93  // there are two specific things that can go wrong.
94  //
95  // 1. The path-finding Job queued by makeLegacyPathRequest() might be
96  // rejected (because we're shutting down).
97  //
98  // Fortunately this problem can be addressed by looking at the
99  // return value of makeLegacyPathRequest(). If
100  // makeLegacyPathRequest() cannot get a thread to run the path-find
101  // on, then it returns an empty request.
102  //
103  // 2. The path-finding job might run, but the Coro::post() might be
104  // rejected by the JobQueue (because we're shutting down).
105  //
106  // We handle this case by resuming (not posting) the Coro.
107  // By resuming the Coro, we allow the Coro to run to completion
108  // on the current thread instead of requiring that it run on a
109  // new thread from the JobQueue.
110  //
111  // Both of these failure modes are hard to recreate in a unit test
112  // because they are so dependent on inter-thread timing. However
113  // the failure modes can be observed by synchronously (inside the
114  // rippled source code) shutting down the application. The code to
115  // do so looks like this:
116  //
117  // context.app.signalStop();
118  // while (! context.app.getJobQueue().jobCounter().joined()) { }
119  //
120  // The first line starts the process of shutting down the app.
121  // The second line waits until no more jobs can be added to the
122  // JobQueue before letting the thread continue.
123  //
124  // May 2017
125  jvResult = context.app.getPathRequests().makeLegacyPathRequest (
126  request,
127  [&context] () {
128  // Copying the shared_ptr keeps the coroutine alive up
129  // through the return. Otherwise the storage under the
130  // captured reference could evaporate when we return from
131  // coroCopy->resume(). This is not strictly necessary, but
132  // will make maintenance easier.
133  std::shared_ptr<JobQueue::Coro> coroCopy {context.coro};
134  if (!coroCopy->post())
135  {
136  // The post() failed, so we won't get a thread to let
137  // the Coro finish. We'll call Coro::resume() so the
138  // Coro can finish on our thread. Otherwise the
139  // application will hang on shutdown.
140  coroCopy->resume();
141  }
142  },
143  context.consumer, lpLedger, context.params);
144  if (request)
145  {
146  context.coro->yield();
147  jvResult = request->doStatus (context.params);
148  }
149 
150  return jvResult;
151  }
152 
153  // The caller specified a ledger
154  jvResult = RPC::lookupLedger (lpLedger, context);
155  if (! lpLedger)
156  return jvResult;
157 
158  RPC::LegacyPathFind lpf (isUnlimited (context.role), context.app);
159  if (! lpf.isOk ())
160  return rpcError (rpcTOO_BUSY);
161 
162  auto result = context.app.getPathRequests().doLegacyPathRequest (
163  context.consumer, lpLedger, context.params);
164 
165  for (auto &fieldName : jvResult.getMemberNames ())
166  result[fieldName] = std::move (jvResult[fieldName]);
167 
168  return result;
169 }
170 
171 } // ripple
ripple::rpcNO_NETWORK
@ rpcNO_NETWORK
Definition: ErrorCodes.h:67
ripple::PathRequests::doLegacyPathRequest
Json::Value doLegacyPathRequest(Resource::Consumer &consumer, std::shared_ptr< ReadView const > const &inLedger, Json::Value const &request)
Definition: PathRequests.cpp:261
ripple::RPC::JsonContext
Definition: Context.h:52
ripple::rpcNOT_SUPPORTED
@ rpcNOT_SUPPORTED
Definition: ErrorCodes.h:133
std::shared_ptr
STL class.
ripple::doRipplePathFind
Json::Value doRipplePathFind(RPC::JsonContext &)
Definition: RipplePathFind.cpp:31
ripple::RPC::Context::loadType
Resource::Charge & loadType
Definition: Context.h:43
ripple::RPC::Context::ledgerMaster
LedgerMaster & ledgerMaster
Definition: Context.h:45
ripple::PathRequests::makeLegacyPathRequest
Json::Value makeLegacyPathRequest(PathRequest::pointer &req, std::function< void(void)> completion, Resource::Consumer &consumer, std::shared_ptr< ReadView const > const &inLedger, Json::Value const &request)
Definition: PathRequests.cpp:227
ripple::rpcTOO_BUSY
@ rpcTOO_BUSY
Definition: ErrorCodes.h:57
ripple::RPC::Context::role
Role role
Definition: Context.h:47
ripple::RPC::lookupLedger
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext &context, Json::Value &result)
Look up a ledger from a request and fill a Json::Result with the data representing a ledger.
Definition: RPCHelpers.cpp:484
ripple::Config::PATH_SEARCH_MAX
int PATH_SEARCH_MAX
Definition: Config.h:155
ripple::RPC::Context::consumer
Resource::Consumer & consumer
Definition: Context.h:46
ripple::Application::getLedgerMaster
virtual LedgerMaster & getLedgerMaster()=0
ripple::Application::config
virtual Config & config()=0
ripple::Config::standalone
bool standalone() const
Definition: Config.h:204
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
ripple::RPC::Context::coro
std::shared_ptr< JobQueue::Coro > coro
Definition: Context.h:48
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:961
ripple::Application::getPathRequests
virtual PathRequests & getPathRequests()=0
ripple::rpcError
Json::Value rpcError(int iError, Json::Value jvResult)
Definition: RPCErr.cpp:28
ripple::isUnlimited
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition: Role.cpp:86
ripple::LedgerMaster::getValidatedLedgerAge
std::chrono::seconds getValidatedLedgerAge()
Definition: LedgerMaster.cpp:276
ripple::LedgerMaster::getClosedLedger
std::shared_ptr< Ledger const > getClosedLedger()
Definition: LedgerMaster.h:89
Json::Value::getMemberNames
Members getMemberNames() const
Return a list of the member names.
Definition: json_value.cpp:979
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RPC::LegacyPathFind::isOk
bool isOk() const
Definition: LegacyPathFind.h:37
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:63
ripple::Resource::feeHighBurdenRPC
const Charge feeHighBurdenRPC
ripple::RPC::Tuning::maxValidatedLedgerAge
constexpr auto maxValidatedLedgerAge
Definition: rpc/impl/Tuning.h:58
ripple::RPC::LegacyPathFind
Definition: LegacyPathFind.h:31
Json::Value
Represents a JSON value.
Definition: json_value.h:141