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