rippled
GRPCServer.h
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 #ifndef RIPPLE_CORE_GRPCSERVER_H_INCLUDED
21 #define RIPPLE_CORE_GRPCSERVER_H_INCLUDED
22 
23 #include <ripple/app/main/Application.h>
24 #include <ripple/core/JobQueue.h>
25 #include <ripple/core/Stoppable.h>
26 #include <ripple/net/InfoSub.h>
27 #include <ripple/protocol/ErrorCodes.h>
28 #include <ripple/resource/Charge.h>
29 #include <ripple/rpc/Context.h>
30 #include <ripple/rpc/GRPCHandlers.h>
31 #include <ripple/rpc/Role.h>
32 #include <ripple/rpc/impl/Handler.h>
33 #include <ripple/rpc/impl/RPCHelpers.h>
34 #include <ripple/rpc/impl/Tuning.h>
35 
36 #include "org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h"
37 #include <grpcpp/grpcpp.h>
38 
39 namespace ripple {
40 
41 // Interface that CallData implements
42 class Processor
43 {
44 public:
45  virtual ~Processor() = default;
46 
47  Processor() = default;
48 
49  Processor(const Processor&) = delete;
50 
51  Processor&
52  operator=(const Processor&) = delete;
53 
54  // process a request that has arrived. Can only be called once per instance
55  virtual void
56  process() = 0;
57 
58  // create a new instance of this CallData object, with the same type
59  //(same template parameters) as original. This is called when a CallData
60  // object starts processing a request. Creating a new instance allows the
61  // server to handle additional requests while the first is being processed
63  clone() = 0;
64 
65  // true if this object has finished processing the request. Object will be
66  // deleted once this function returns true
67  virtual bool
68  isFinished() = 0;
69 };
70 
71 class GRPCServerImpl final
72 {
73 private:
74  // CompletionQueue returns events that have occurred, or events that have
75  // been cancelled
77 
79 
80  // The gRPC service defined by the .proto files
81  org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService service_;
82 
84 
86 
88 
90 
91  // typedef for function to bind a listener
92  // This is always of the form:
93  // org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService::Request[RPC NAME]
94  template <class Request, class Response>
95  using BindListener = std::function<void(
96  org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService&,
97  grpc::ServerContext*,
98  Request*,
99  grpc::ServerAsyncResponseWriter<Response>*,
100  grpc::CompletionQueue*,
101  grpc::ServerCompletionQueue*,
102  void*)>;
103 
104  // typedef for actual handler (that populates a response)
105  // handlers are defined in rpc/GRPCHandlers.h
106  template <class Request, class Response>
109  // This implementation is currently limited to v1 of the API
110  static unsigned constexpr apiVersion = 1;
111 
112 public:
113  explicit GRPCServerImpl(Application& app);
114 
115  GRPCServerImpl(const GRPCServerImpl&) = delete;
116 
118  operator=(const GRPCServerImpl&) = delete;
119 
120  void
121  shutdown();
122 
123  // setup the server and listeners
124  // returns true if server started successfully
125  bool
126  start();
127 
128  // the main event loop
129  void
130  handleRpcs();
131 
132  // Create a CallData object for each RPC. Return created objects in vector
134  setupListeners();
135 
136 private:
137  // Class encompasing the state and logic needed to serve a request.
138  template <class Request, class Response>
139  class CallData
140  : public Processor,
141  public std::enable_shared_from_this<CallData<Request, Response>>
142  {
143  private:
144  // The means of communication with the gRPC runtime for an asynchronous
145  // server.
146  org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService& service_;
147 
148  // The producer-consumer queue for asynchronous server notifications.
149  grpc::ServerCompletionQueue& cq_;
150 
151  // Context for the rpc, allowing to tweak aspects of it such as the use
152  // of compression, authentication, as well as to send metadata back to
153  // the client.
154  grpc::ServerContext ctx_;
155 
156  // true if finished processing request
157  // Note, this variable does not need to be atomic, since it is
158  // currently only accessed from one thread. However, isFinished(),
159  // which returns the value of this variable, is public facing. In the
160  // interest of avoiding future concurrency bugs, we make it atomic.
162 
164 
165  // What we get from the client.
166  Request request_;
167 
168  // What we send back to the client.
169  Response reply_;
170 
171  // The means to get back to the client.
172  grpc::ServerAsyncResponseWriter<Response> responder_;
173 
174  // Function that creates a listener for specific request type
176 
177  // Function that processes a request
179 
180  // Condition required for this RPC
182 
183  // Load type for this RPC
185 
186  public:
187  virtual ~CallData() = default;
188 
189  // Take in the "service" instance (in this case representing an
190  // asynchronous server) and the completion queue "cq" used for
191  // asynchronous communication with the gRPC runtime.
192  explicit CallData(
193  org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService& service,
194  grpc::ServerCompletionQueue& cq,
195  Application& app,
196  BindListener<Request, Response> bindListener,
198  RPC::Condition requiredCondition,
199  Resource::Charge loadType);
200 
201  CallData(const CallData&) = delete;
202 
203  CallData&
204  operator=(const CallData&) = delete;
205 
206  virtual void
207  process() override;
208 
209  virtual bool
210  isFinished() override;
211 
213  clone() override;
214 
215  private:
216  // process the request. Called inside the coroutine passed to JobQueue
217  void
219 
220  // return load type of this RPC
222  getLoadType();
223 
224  // return the Role required for this RPC
225  // for now, we are only supporting RPC's that require Role::USER for
226  // gRPC
227  Role
228  getRole();
229 
230  // register endpoint with ResourceManager and return usage
232  getUsage();
233 
234  }; // CallData
235 
236 }; // GRPCServerImpl
237 
238 class GRPCServer : public Stoppable
239 {
240 public:
241  explicit GRPCServer(Application& app, Stoppable& parent)
242  : Stoppable("GRPCServer", parent), impl_(app)
243  {
244  }
245 
246  GRPCServer(const GRPCServer&) = delete;
247 
248  GRPCServer&
249  operator=(const GRPCServer&) = delete;
250 
251  void
252  onStart() override;
253 
254  void
255  onStop() override;
256 
257  ~GRPCServer() override;
258 
259 private:
262  bool running_ = false;
263 };
264 } // namespace ripple
265 #endif
ripple::GRPCServerImpl::CallData::reply_
Response reply_
Definition: GRPCServer.h:169
ripple::GRPCServer::onStart
void onStart() override
Override called during start.
Definition: GRPCServer.cpp:442
ripple::Application
Definition: Application.h:97
ripple::Processor
Definition: GRPCServer.h:42
std::string
STL class.
std::shared_ptr
STL class.
ripple::GRPCServer::impl_
GRPCServerImpl impl_
Definition: GRPCServer.h:260
ripple::GRPCServerImpl::CallData::clone
std::shared_ptr< Processor > clone() override
Definition: GRPCServer.cpp:69
ripple::GRPCServerImpl::CallData::operator=
CallData & operator=(const CallData &)=delete
ripple::GRPCServerImpl::CallData::app_
Application & app_
Definition: GRPCServer.h:163
ripple::GRPCServerImpl::apiVersion
static constexpr unsigned apiVersion
Definition: GRPCServer.h:110
ripple::Processor::process
virtual void process()=0
ripple::GRPCServerImpl::start
bool start()
Definition: GRPCServer.cpp:418
std::vector
STL class.
ripple::GRPCServerImpl::shutdown
void shutdown()
Definition: GRPCServer.cpp:237
ripple::GRPCServerImpl::CallData::request_
Request request_
Definition: GRPCServer.h:166
ripple::GRPCServerImpl::service_
org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService service_
Definition: GRPCServer.h:81
ripple::GRPCServerImpl::requests_
std::vector< std::shared_ptr< Processor > > requests_
Definition: GRPCServer.h:78
ripple::GRPCServerImpl::CallData::cq_
grpc::ServerCompletionQueue & cq_
Definition: GRPCServer.h:149
ripple::GRPCServer::running_
bool running_
Definition: GRPCServer.h:262
ripple::GRPCServerImpl::CallData::requiredCondition_
RPC::Condition requiredCondition_
Definition: GRPCServer.h:181
ripple::GRPCServerImpl::server_
std::unique_ptr< grpc::Server > server_
Definition: GRPCServer.h:83
std::function
ripple::GRPCServerImpl::handleRpcs
void handleRpcs()
Definition: GRPCServer.cpp:259
ripple::GRPCServerImpl::CallData::isFinished
virtual bool isFinished() override
Definition: GRPCServer.cpp:178
ripple::GRPCServerImpl
Definition: GRPCServer.h:71
ripple::GRPCServerImpl::CallData::finished_
std::atomic_bool finished_
Definition: GRPCServer.h:161
ripple::GRPCServerImpl::CallData::handler_
Handler< Request, Response > handler_
Definition: GRPCServer.h:178
ripple::Processor::isFinished
virtual bool isFinished()=0
ripple::GRPCServer::thread_
std::thread thread_
Definition: GRPCServer.h:261
ripple::GRPCServerImpl::CallData::service_
org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService & service_
Definition: GRPCServer.h:146
ripple::GRPCServerImpl::cq_
std::unique_ptr< grpc::ServerCompletionQueue > cq_
Definition: GRPCServer.h:76
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:201
ripple::GRPCServerImpl::CallData::getUsage
Resource::Consumer getUsage()
Definition: GRPCServer.cpp:199
std::thread
STL class.
ripple::GRPCServer::operator=
GRPCServer & operator=(const GRPCServer &)=delete
ripple::RPC::GRPCContext
Definition: Context.h:70
ripple::Processor::operator=
Processor & operator=(const Processor &)=delete
std::enable_shared_from_this
ripple::GRPCServerImpl::operator=
GRPCServerImpl & operator=(const GRPCServerImpl &)=delete
ripple::GRPCServerImpl::GRPCServerImpl
GRPCServerImpl(Application &app)
Definition: GRPCServer.cpp:207
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::RPC::Condition
Condition
Definition: Handler.h:39
std::atomic_bool
ripple::GRPCServerImpl::CallData::getLoadType
Resource::Charge getLoadType()
Definition: GRPCServer.cpp:185
ripple::GRPCServerImpl::CallData::bindListener_
BindListener< Request, Response > bindListener_
Definition: GRPCServer.h:175
ripple::GRPCServerImpl::CallData::loadType_
Resource::Charge loadType_
Definition: GRPCServer.h:184
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Processor::Processor
Processor()=default
ripple::GRPCServerImpl::setupListeners
std::vector< std::shared_ptr< Processor > > setupListeners()
Definition: GRPCServer.cpp:330
ripple::GRPCServerImpl::CallData::ctx_
grpc::ServerContext ctx_
Definition: GRPCServer.h:154
ripple::GRPCServer
Definition: GRPCServer.h:238
ripple::Resource::Consumer
An endpoint that consumes resources.
Definition: Consumer.h:33
ripple::Resource::Charge
A consumption charge.
Definition: Charge.h:30
ripple::GRPCServerImpl::CallData
Definition: GRPCServer.h:139
ripple::Processor::clone
virtual std::shared_ptr< Processor > clone()=0
ripple::GRPCServer::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: GRPCServer.cpp:456
ripple::GRPCServerImpl::app_
Application & app_
Definition: GRPCServer.h:85
ripple::GRPCServer::GRPCServer
GRPCServer(Application &app, Stoppable &parent)
Definition: GRPCServer.h:241
ripple::GRPCServerImpl::CallData::getRole
Role getRole()
Definition: GRPCServer.cpp:192
ripple::GRPCServerImpl::CallData::~CallData
virtual ~CallData()=default
ripple::Processor::~Processor
virtual ~Processor()=default
std::unique_ptr< grpc::ServerCompletionQueue >
ripple::GRPCServerImpl::journal_
beast::Journal journal_
Definition: GRPCServer.h:89
ripple::GRPCServer::~GRPCServer
~GRPCServer() override
Definition: GRPCServer.cpp:468
ripple::Role
Role
Indicates the level of administrative permission to grant.
Definition: Role.h:40
ripple::GRPCServerImpl::CallData::CallData
CallData(org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService &service, grpc::ServerCompletionQueue &cq, Application &app, BindListener< Request, Response > bindListener, Handler< Request, Response > handler, RPC::Condition requiredCondition, Resource::Charge loadType)
Definition: GRPCServer.cpp:44
ripple::GRPCServerImpl::CallData::process
virtual void process() override
Definition: GRPCServer.cpp:83
ripple::GRPCServerImpl::serverAddress_
std::string serverAddress_
Definition: GRPCServer.h:87
ripple::GRPCServerImpl::CallData::responder_
grpc::ServerAsyncResponseWriter< Response > responder_
Definition: GRPCServer.h:172