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