2017-11-25 06:41:58 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2017 The Cartographer Authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CARTOGRAPHER_GRPC_FRAMEWORK_RPC_H
|
|
|
|
#define CARTOGRAPHER_GRPC_FRAMEWORK_RPC_H
|
|
|
|
|
|
|
|
#include <memory>
|
2017-12-04 22:28:19 +08:00
|
|
|
#include <queue>
|
2017-11-25 06:41:58 +08:00
|
|
|
#include <unordered_set>
|
|
|
|
|
2017-12-15 19:21:44 +08:00
|
|
|
#include "cartographer/common/blocking_queue.h"
|
2017-11-25 06:41:58 +08:00
|
|
|
#include "cartographer/common/mutex.h"
|
2017-11-29 21:05:31 +08:00
|
|
|
#include "cartographer_grpc/framework/execution_context.h"
|
2017-11-29 17:40:26 +08:00
|
|
|
#include "cartographer_grpc/framework/rpc_handler_interface.h"
|
2017-11-28 17:50:30 +08:00
|
|
|
#include "google/protobuf/message.h"
|
2017-11-25 06:41:58 +08:00
|
|
|
#include "grpc++/grpc++.h"
|
2017-11-28 17:50:30 +08:00
|
|
|
#include "grpc++/impl/codegen/async_stream.h"
|
2017-11-29 17:40:26 +08:00
|
|
|
#include "grpc++/impl/codegen/async_unary_call.h"
|
2017-11-28 17:50:30 +08:00
|
|
|
#include "grpc++/impl/codegen/proto_utils.h"
|
|
|
|
#include "grpc++/impl/codegen/service_type.h"
|
2017-11-25 06:41:58 +08:00
|
|
|
|
|
|
|
namespace cartographer_grpc {
|
|
|
|
namespace framework {
|
|
|
|
|
|
|
|
class Service;
|
2017-12-18 20:36:44 +08:00
|
|
|
// TODO(cschuet): Add a unittest that tests the logic of this class.
|
2017-11-25 06:41:58 +08:00
|
|
|
class Rpc {
|
|
|
|
public:
|
2017-12-14 23:30:01 +08:00
|
|
|
using WeakPtrFactory = std::function<std::weak_ptr<Rpc>(Rpc*)>;
|
2017-12-18 20:36:44 +08:00
|
|
|
enum class Event {
|
|
|
|
NEW_CONNECTION = 0,
|
|
|
|
READ,
|
|
|
|
WRITE_NEEDED,
|
|
|
|
WRITE,
|
|
|
|
FINISH,
|
|
|
|
DONE
|
|
|
|
};
|
2018-01-08 19:42:19 +08:00
|
|
|
|
|
|
|
struct EventBase {
|
|
|
|
explicit EventBase(Event event) : event(event) {}
|
|
|
|
virtual ~EventBase(){};
|
|
|
|
virtual void Handle() = 0;
|
|
|
|
|
2017-11-29 17:40:26 +08:00
|
|
|
const Event event;
|
2018-01-08 19:42:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class EventDeleter {
|
|
|
|
public:
|
|
|
|
enum Action { DELETE = 0, DO_NOT_DELETE };
|
|
|
|
|
|
|
|
// The default action 'DELETE' is used implicitly, for instance for a
|
|
|
|
// new UniqueEventPtr or a UniqueEventPtr that is created by
|
|
|
|
// 'return nullptr'.
|
|
|
|
EventDeleter() : action_(DELETE) {}
|
|
|
|
explicit EventDeleter(Action action) : action_(action) {}
|
|
|
|
void operator()(EventBase* e) {
|
|
|
|
if (e != nullptr && action_ == DELETE) {
|
|
|
|
delete e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Action action_;
|
|
|
|
};
|
|
|
|
|
|
|
|
using UniqueEventPtr = std::unique_ptr<EventBase, EventDeleter>;
|
|
|
|
using EventQueue = cartographer::common::BlockingQueue<UniqueEventPtr>;
|
|
|
|
|
|
|
|
// Flows through gRPC's CompletionQueue and then our EventQueue.
|
|
|
|
struct CompletionQueueRpcEvent : public EventBase {
|
|
|
|
CompletionQueueRpcEvent(Event event, Rpc* rpc)
|
|
|
|
: EventBase(event), rpc_ptr(rpc), ok(false), pending(false) {}
|
|
|
|
void PushToEventQueue() {
|
|
|
|
rpc_ptr->event_queue()->Push(
|
|
|
|
UniqueEventPtr(this, EventDeleter(EventDeleter::DO_NOT_DELETE)));
|
|
|
|
}
|
|
|
|
void Handle() override;
|
|
|
|
|
|
|
|
Rpc* rpc_ptr;
|
2017-12-15 19:21:44 +08:00
|
|
|
bool ok;
|
2018-01-08 19:42:19 +08:00
|
|
|
bool pending;
|
2017-11-25 06:41:58 +08:00
|
|
|
};
|
2018-01-08 19:42:19 +08:00
|
|
|
|
|
|
|
// Flows only through our EventQueue.
|
|
|
|
struct InternalRpcEvent : public EventBase {
|
|
|
|
InternalRpcEvent(Event event, std::weak_ptr<Rpc> rpc)
|
|
|
|
: EventBase(event), rpc(rpc) {}
|
|
|
|
void Handle() override;
|
|
|
|
|
|
|
|
std::weak_ptr<Rpc> rpc;
|
|
|
|
};
|
|
|
|
|
2017-11-28 17:50:30 +08:00
|
|
|
Rpc(int method_index, ::grpc::ServerCompletionQueue* server_completion_queue,
|
2017-12-15 19:21:44 +08:00
|
|
|
EventQueue* event_queue, ExecutionContext* execution_context,
|
2017-12-14 23:30:01 +08:00
|
|
|
const RpcHandlerInfo& rpc_handler_info, Service* service,
|
|
|
|
WeakPtrFactory weak_ptr_factory);
|
2017-11-29 17:40:26 +08:00
|
|
|
std::unique_ptr<Rpc> Clone();
|
|
|
|
void OnRequest();
|
|
|
|
void OnReadsDone();
|
2017-12-19 22:11:29 +08:00
|
|
|
void OnFinish();
|
2017-11-29 17:40:26 +08:00
|
|
|
void RequestNextMethodInvocation();
|
|
|
|
void RequestStreamingReadIfNeeded();
|
2017-12-18 20:36:44 +08:00
|
|
|
void HandleSendQueue();
|
2017-11-29 17:40:26 +08:00
|
|
|
void Write(std::unique_ptr<::google::protobuf::Message> message);
|
2017-12-04 22:28:19 +08:00
|
|
|
void Finish(::grpc::Status status);
|
2017-11-29 17:40:26 +08:00
|
|
|
Service* service() { return service_; }
|
2017-12-14 01:01:01 +08:00
|
|
|
bool IsRpcEventPending(Event event);
|
|
|
|
bool IsAnyEventPending();
|
2017-12-15 19:21:44 +08:00
|
|
|
void SetEventQueue(EventQueue* event_queue) { event_queue_ = event_queue; }
|
|
|
|
EventQueue* event_queue() { return event_queue_; }
|
2017-12-19 04:27:03 +08:00
|
|
|
std::weak_ptr<Rpc> GetWeakPtr();
|
2017-11-25 06:41:58 +08:00
|
|
|
|
|
|
|
private:
|
2017-12-04 22:28:19 +08:00
|
|
|
struct SendItem {
|
|
|
|
std::unique_ptr<google::protobuf::Message> msg;
|
|
|
|
::grpc::Status status;
|
|
|
|
};
|
|
|
|
|
2017-11-25 06:41:58 +08:00
|
|
|
Rpc(const Rpc&) = delete;
|
|
|
|
Rpc& operator=(const Rpc&) = delete;
|
2017-11-29 17:40:26 +08:00
|
|
|
void InitializeReadersAndWriters(
|
|
|
|
::grpc::internal::RpcMethod::RpcType rpc_type);
|
2018-01-08 19:42:19 +08:00
|
|
|
CompletionQueueRpcEvent* GetRpcEvent(Event event);
|
2017-12-14 01:01:01 +08:00
|
|
|
bool* GetRpcEventState(Event event);
|
2018-01-08 19:42:19 +08:00
|
|
|
void SetRpcEventState(Event event, bool pending);
|
2017-12-18 20:36:44 +08:00
|
|
|
void EnqueueMessage(SendItem&& send_item);
|
|
|
|
void PerformFinish(std::unique_ptr<::google::protobuf::Message> message,
|
|
|
|
::grpc::Status status);
|
|
|
|
void PerformWrite(std::unique_ptr<::google::protobuf::Message> message,
|
|
|
|
::grpc::Status status);
|
2017-11-29 17:40:26 +08:00
|
|
|
|
|
|
|
::grpc::internal::AsyncReaderInterface<::google::protobuf::Message>*
|
|
|
|
async_reader_interface();
|
2017-12-04 22:28:19 +08:00
|
|
|
::grpc::internal::AsyncWriterInterface<::google::protobuf::Message>*
|
|
|
|
async_writer_interface();
|
|
|
|
|
2017-11-29 17:40:26 +08:00
|
|
|
::grpc::internal::ServerAsyncStreamingInterface* streaming_interface();
|
2017-11-25 06:41:58 +08:00
|
|
|
|
2017-11-28 17:50:30 +08:00
|
|
|
int method_index_;
|
|
|
|
::grpc::ServerCompletionQueue* server_completion_queue_;
|
2017-12-15 19:21:44 +08:00
|
|
|
EventQueue* event_queue_;
|
2017-11-29 21:05:31 +08:00
|
|
|
ExecutionContext* execution_context_;
|
2017-11-25 06:41:58 +08:00
|
|
|
RpcHandlerInfo rpc_handler_info_;
|
2017-11-29 17:40:26 +08:00
|
|
|
Service* service_;
|
2017-12-14 23:30:01 +08:00
|
|
|
WeakPtrFactory weak_ptr_factory_;
|
2017-11-25 06:41:58 +08:00
|
|
|
::grpc::ServerContext server_context_;
|
|
|
|
|
2018-01-08 19:42:19 +08:00
|
|
|
CompletionQueueRpcEvent new_connection_event_;
|
|
|
|
CompletionQueueRpcEvent read_event_;
|
|
|
|
CompletionQueueRpcEvent write_event_;
|
|
|
|
CompletionQueueRpcEvent finish_event_;
|
|
|
|
CompletionQueueRpcEvent done_event_;
|
2017-11-25 06:41:58 +08:00
|
|
|
|
|
|
|
std::unique_ptr<google::protobuf::Message> request_;
|
|
|
|
std::unique_ptr<google::protobuf::Message> response_;
|
2017-11-28 17:50:30 +08:00
|
|
|
|
2017-11-29 17:40:26 +08:00
|
|
|
std::unique_ptr<RpcHandlerInterface> handler_;
|
|
|
|
|
2017-11-30 20:18:16 +08:00
|
|
|
std::unique_ptr<::grpc::ServerAsyncResponseWriter<google::protobuf::Message>>
|
|
|
|
server_async_response_writer_;
|
2017-11-28 17:50:30 +08:00
|
|
|
std::unique_ptr<::grpc::ServerAsyncReader<google::protobuf::Message,
|
|
|
|
google::protobuf::Message>>
|
|
|
|
server_async_reader_;
|
2017-12-04 22:28:19 +08:00
|
|
|
std::unique_ptr<::grpc::ServerAsyncReaderWriter<google::protobuf::Message,
|
|
|
|
google::protobuf::Message>>
|
|
|
|
server_async_reader_writer_;
|
2017-12-19 17:27:30 +08:00
|
|
|
std::unique_ptr<::grpc::ServerAsyncWriter<google::protobuf::Message>>
|
|
|
|
server_async_writer_;
|
2017-12-04 22:28:19 +08:00
|
|
|
|
2017-12-18 20:36:44 +08:00
|
|
|
cartographer::common::Mutex send_queue_lock_;
|
2017-12-04 22:28:19 +08:00
|
|
|
std::queue<SendItem> send_queue_;
|
2017-11-25 06:41:58 +08:00
|
|
|
};
|
|
|
|
|
2018-01-08 19:42:19 +08:00
|
|
|
using EventQueue = Rpc::EventQueue;
|
|
|
|
|
2017-11-25 06:41:58 +08:00
|
|
|
// This class keeps track of all in-flight RPCs for a 'Service'. Make sure that
|
|
|
|
// all RPCs have been terminated and removed from this object before it goes out
|
|
|
|
// of scope.
|
|
|
|
class ActiveRpcs {
|
|
|
|
public:
|
|
|
|
ActiveRpcs();
|
|
|
|
~ActiveRpcs() EXCLUDES(lock_);
|
|
|
|
|
2017-12-14 23:30:01 +08:00
|
|
|
std::shared_ptr<Rpc> Add(std::unique_ptr<Rpc> rpc) EXCLUDES(lock_);
|
2017-11-25 06:41:58 +08:00
|
|
|
bool Remove(Rpc* rpc) EXCLUDES(lock_);
|
2017-12-14 23:30:01 +08:00
|
|
|
Rpc::WeakPtrFactory GetWeakPtrFactory();
|
2017-11-25 06:41:58 +08:00
|
|
|
|
|
|
|
private:
|
2017-12-14 23:30:01 +08:00
|
|
|
std::weak_ptr<Rpc> GetWeakPtr(Rpc* rpc);
|
|
|
|
|
2017-11-25 06:41:58 +08:00
|
|
|
cartographer::common::Mutex lock_;
|
2017-12-14 23:30:01 +08:00
|
|
|
std::map<Rpc*, std::shared_ptr<Rpc>> rpcs_;
|
2017-11-25 06:41:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace framework
|
|
|
|
} // namespace cartographer_grpc
|
|
|
|
|
|
|
|
#endif // CARTOGRAPHER_GRPC_FRAMEWORK_RPC_H
|