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 
109 public:
110  explicit GRPCServerImpl(Application& app);
111 
112  GRPCServerImpl(const GRPCServerImpl&) = delete;
113 
115  operator=(const GRPCServerImpl&) = delete;
116 
117  void
118  shutdown();
119 
120  // setup the server and listeners
121  // returns true if server started successfully
122  bool
123  start();
124 
125  // the main event loop
126  void
127  handleRpcs();
128 
129  // Create a CallData object for each RPC. Return created objects in vector
131  setupListeners();
132 
133 private:
134  // Class encompasing the state and logic needed to serve a request.
135  template <class Request, class Response>
136  class CallData
137  : public Processor,
138  public std::enable_shared_from_this<CallData<Request, Response>>
139  {
140  private:
141  // The means of communication with the gRPC runtime for an asynchronous
142  // server.
143  org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService& service_;
144 
145  // The producer-consumer queue for asynchronous server notifications.
146  grpc::ServerCompletionQueue& cq_;
147 
148  // Context for the rpc, allowing to tweak aspects of it such as the use
149  // of compression, authentication, as well as to send metadata back to
150  // the client.
151  grpc::ServerContext ctx_;
152 
153  // true if finished processing request
154  // Note, this variable does not need to be atomic, since it is
155  // currently only accessed from one thread. However, isFinished(),
156  // which returns the value of this variable, is public facing. In the
157  // interest of avoiding future concurrency bugs, we make it atomic.
159 
161 
162  // What we get from the client.
163  Request request_;
164 
165  // What we send back to the client.
166  Response reply_;
167 
168  // The means to get back to the client.
169  grpc::ServerAsyncResponseWriter<Response> responder_;
170 
171  // Function that creates a listener for specific request type
173 
174  // Function that processes a request
176 
177  // Condition required for this RPC
179 
180  // Load type for this RPC
182 
183  public:
184  virtual ~CallData() = default;
185 
186  // Take in the "service" instance (in this case representing an
187  // asynchronous server) and the completion queue "cq" used for
188  // asynchronous communication with the gRPC runtime.
189  explicit CallData(
190  org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService& service,
191  grpc::ServerCompletionQueue& cq,
192  Application& app,
193  BindListener<Request, Response> bindListener,
195  RPC::Condition requiredCondition,
196  Resource::Charge loadType);
197 
198  CallData(const CallData&) = delete;
199 
200  CallData&
201  operator=(const CallData&) = delete;
202 
203  virtual void
204  process() override;
205 
206  virtual bool
207  isFinished() override;
208 
210  clone() override;
211 
212  private:
213  // process the request. Called inside the coroutine passed to JobQueue
214  void
216 
217  // return load type of this RPC
219  getLoadType();
220 
221  // return the Role required for this RPC
222  // for now, we are only supporting RPC's that require Role::USER for
223  // gRPC
224  Role
225  getRole();
226 
227  // register endpoint with ResourceManager and return usage
229  getUsage();
230 
231  }; // CallData
232 
233 }; // GRPCServerImpl
234 
236 {
237 public:
238  explicit GRPCServer(Application& app) : impl_(app){};
239 
240  GRPCServer(const GRPCServer&) = delete;
241 
242  GRPCServer&
243  operator=(const GRPCServer&) = delete;
244 
245  void
246  run();
247 
248  ~GRPCServer();
249 
250 private:
253  bool running_;
254 };
255 } // namespace ripple
256 #endif
ripple::GRPCServerImpl::CallData::reply_
Response reply_
Definition: GRPCServer.h:166
ripple::Application
Definition: Application.h:85
ripple::Processor
Definition: GRPCServer.h:41
std::string
STL class.
std::shared_ptr
STL class.
ripple::GRPCServer::impl_
GRPCServerImpl impl_
Definition: GRPCServer.h:251
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:160
ripple::Processor::process
virtual void process()=0
ripple::GRPCServerImpl::start
bool start()
Definition: GRPCServer.cpp:412
std::vector
STL class.
ripple::GRPCServerImpl::shutdown
void shutdown()
Definition: GRPCServer.cpp:233
ripple::GRPCServerImpl::CallData::request_
Request request_
Definition: GRPCServer.h:163
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:146
ripple::GRPCServer::running_
bool running_
Definition: GRPCServer.h:253
ripple::GRPCServerImpl::CallData::requiredCondition_
RPC::Condition requiredCondition_
Definition: GRPCServer.h:178
ripple::GRPCServerImpl::server_
std::unique_ptr< grpc::Server > server_
Definition: GRPCServer.h:82
std::function
ripple::GRPCServerImpl::handleRpcs
void handleRpcs()
Definition: GRPCServer.cpp:256
ripple::GRPCServerImpl::CallData::isFinished
virtual bool isFinished() override
Definition: GRPCServer.cpp:174
ripple::GRPCServerImpl
Definition: GRPCServer.h:70
ripple::GRPCServerImpl::CallData::finished_
std::atomic_bool finished_
Definition: GRPCServer.h:158
ripple::GRPCServerImpl::CallData::handler_
Handler< Request, Response > handler_
Definition: GRPCServer.h:175
ripple::Processor::isFinished
virtual bool isFinished()=0
ripple::GRPCServer::thread_
std::thread thread_
Definition: GRPCServer.h:252
ripple::GRPCServerImpl::CallData::service_
org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService & service_
Definition: GRPCServer.h:143
ripple::GRPCServerImpl::cq_
std::unique_ptr< grpc::ServerCompletionQueue > cq_
Definition: GRPCServer.h:75
ripple::GRPCServerImpl::CallData::getUsage
Resource::Consumer getUsage()
Definition: GRPCServer.cpp:195
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:203
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
ripple::RPC::Condition
Condition
Definition: Handler.h:39
std::atomic_bool
ripple::GRPCServerImpl::CallData::getLoadType
Resource::Charge getLoadType()
Definition: GRPCServer.cpp:181
ripple::GRPCServerImpl::CallData::bindListener_
BindListener< Request, Response > bindListener_
Definition: GRPCServer.h:172
ripple::GRPCServerImpl::CallData::loadType_
Resource::Charge loadType_
Definition: GRPCServer.h:181
ripple::GRPCServer::~GRPCServer
~GRPCServer()
Definition: GRPCServer.cpp:448
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:151
ripple::GRPCServer
Definition: GRPCServer.h:235
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:136
ripple::Processor::clone
virtual std::shared_ptr< Processor > clone()=0
ripple::GRPCServer::GRPCServer
GRPCServer(Application &app)
Definition: GRPCServer.h:238
ripple::GRPCServer::run
void run()
Definition: GRPCServer.cpp:436
ripple::GRPCServerImpl::app_
Application & app_
Definition: GRPCServer.h:84
ripple::GRPCServerImpl::CallData::getRole
Role getRole()
Definition: GRPCServer.cpp:188
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:169