gRPC和etcd客户端

时间:2018-07-18 05:59:09

标签: c++ grpc etcd

这个问题涉及etcd的具体内容,但是我认为这个问题通常与gRPC的工作有关。 我正在尝试为某些键创建etcd Watch,因为文档稀疏,所以我看了诺基亚implementation 使代码适应我的需求很容易,因此我想到了第一个版本,该版本运行良好,创建了WatchCreateRequest,并在密钥更新时触发了回调。到目前为止,一切都很好。然后,我尝试添加多个按键来观看。惨败! ClientAsyncReaderWriter在这种情况下无法读取/写入。现在到问题了。

如果我班上有以下成员

Watch::Stub watchStub;
CompletionQueue completionQueue;
ClientContext context;
std::unique_ptr<ClientAsyncReaderWriter<WatchRequest, WatchResponse>> stream;
WatchResponse reply;

,并且我想支持添加到我的班级的多个Watches,我想我必须每个手表拥有几个变量,而不是作为班级成员。 首先,我想WatchResponse reply应该是Watch中的一个。我对stream不太确定,是否应该每个Watch拿着一个?我几乎可以肯定context可以被所有Watches重用,并且100%可以肯定stubcompletionQueue可以被所有Watches重用。 所以问题是我的猜测正确吗?线程安全性如何?没有找到任何描述可以从多线程安全使用哪些对象以及必须在哪里同步访问的文档。 任何指向文档(not this one)的链接都将受到赞赏!

在将成员分成单个Watch属性之前测试代码 (我知道没有适当的关机)

using namespace grpc;
class Watcher
{
public:
    using Callback = std::function<void(const std::string&, const std::string&)>;

    Watcher(std::shared_ptr<Channel> channel) : watchStub(channel)
    {
        stream = watchStub.AsyncWatch(&context, &completionQueue, (void*) "create");
        eventPoller = std::thread([this]() { WaitForEvent(); });
    }

    void AddWatch(const std::string& key, Callback callback)
    {
        AddWatch(key, callback, false);
    }

    void AddWatches(const std::string& key, Callback callback)
    {
        AddWatch(key, callback, true);
    }

private:
    void AddWatch(const std::string& key, Callback callback, bool isRecursive)
    {
        auto insertionResult = callbacks.emplace(key, callback);
        if (!insertionResult.second) {
            throw std::runtime_error("Event handle already exist.");
        }
        WatchRequest watch_req;
        WatchCreateRequest watch_create_req;
        watch_create_req.set_key(key);
        if (isRecursive) {
            watch_create_req.set_range_end(key + "\xFF");
        }

        watch_req.mutable_create_request()->CopyFrom(watch_create_req);
        stream->Write(watch_req, (void*) insertionResult.first->first.c_str());

        stream->Read(&reply, (void*) insertionResult.first->first.c_str());
    }

    void WaitForEvent()
    {
        void* got_tag;
        bool ok = false;

        while (completionQueue.Next(&got_tag, &ok)) {
            if (ok == false) {
                break;
            }
            if (got_tag == (void*) "writes done") {
                // Signal shutdown
            }
            else if (got_tag == (void*) "create") {
            }
            else if (got_tag == (void*) "write") {
            }
            else {

                auto tag = std::string(reinterpret_cast<char*>(got_tag));
                auto findIt = callbacks.find(tag);
                if (findIt == callbacks.end()) {
                    throw std::runtime_error("Key \"" + tag + "\"not found");
                }

                if (reply.events_size()) {
                    ParseResponse(findIt->second);
                }
                stream->Read(&reply, got_tag);
            }
        }
    }

    void ParseResponse(Callback& callback)
    {
        for (int i = 0; i < reply.events_size(); ++i) {
            auto event = reply.events(i);
            auto key = event.kv().key();
            callback(event.kv().key(), event.kv().value());
        }
    }

    Watch::Stub watchStub;
    CompletionQueue completionQueue;
    ClientContext context;
    std::unique_ptr<ClientAsyncReaderWriter<WatchRequest, WatchResponse>> stream;
    WatchResponse reply;
    std::unordered_map<std::string, Callback> callbacks;
    std::thread eventPoller;
};

1 个答案:

答案 0 :(得分:1)

很抱歉,我对这里的正确Watch设计不是很确定。对我来说,不是很清楚是否要为每个Watch创建一个gRPC调用。

无论如何,每个gRPC调用都会有自己的ClientContextClientAsyncReaderWriter。但是stubCompletionQueue并不是按通话对象。

据我所知,找不到线程安全类的中心位置。您可能需要阅读API文档才能获得正确的期望。

当我编写async server load reporting service时,我自己添加同步的唯一位置是在CompletionQueue附近,这样,即使关闭了cq,也不会将新标签排队。