[llvm-branch-commits] [clang-tools-extra] dcdef5b - [clangd] Log remote index connectivity status

Kirill Bobyrev via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Dec 15 09:35:02 PST 2020


Author: Kirill Bobyrev
Date: 2020-12-15T18:29:48+01:00
New Revision: dcdef5b5b3df457566e7faf61e1e5789c42528d1

URL: https://github.com/llvm/llvm-project/commit/dcdef5b5b3df457566e7faf61e1e5789c42528d1
DIFF: https://github.com/llvm/llvm-project/commit/dcdef5b5b3df457566e7faf61e1e5789c42528d1.diff

LOG: [clangd] Log remote index connectivity status

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D92198

Added: 
    

Modified: 
    clang-tools-extra/clangd/index/remote/Client.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/index/remote/Client.cpp b/clang-tools-extra/clangd/index/remote/Client.cpp
index 0387e65db7d0..4720b20402e1 100644
--- a/clang-tools-extra/clangd/index/remote/Client.cpp
+++ b/clang-tools-extra/clangd/index/remote/Client.cpp
@@ -19,14 +19,40 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Error.h"
 
+#include <atomic>
 #include <chrono>
+#include <memory>
 
 namespace clang {
 namespace clangd {
 namespace remote {
 namespace {
 
+llvm::StringRef toString(const grpc_connectivity_state &State) {
+  switch (State) {
+  case GRPC_CHANNEL_IDLE:
+    return "idle";
+  case GRPC_CHANNEL_CONNECTING:
+    return "connecting";
+  case GRPC_CHANNEL_READY:
+    return "ready";
+  case GRPC_CHANNEL_TRANSIENT_FAILURE:
+    return "transient failure";
+  case GRPC_CHANNEL_SHUTDOWN:
+    return "shutdown";
+  }
+  llvm_unreachable("Not a valid grpc_connectivity_state.");
+}
+
 class IndexClient : public clangd::SymbolIndex {
+  void updateConnectionStatus() const {
+    auto NewStatus = Channel->GetState(/*try_to_connect=*/false);
+    auto OldStatus = ConnectionStatus.exchange(NewStatus);
+    if (OldStatus != NewStatus)
+      vlog("Remote index connection [{0}]: {1} => {2}", ServerAddress,
+           toString(OldStatus), toString(NewStatus));
+  }
+
   template <typename RequestT, typename ReplyT>
   using StreamingCall = std::unique_ptr<grpc::ClientReader<ReplyT>> (
       remote::v1::SymbolIndex::Stub::*)(grpc::ClientContext *,
@@ -37,6 +63,7 @@ class IndexClient : public clangd::SymbolIndex {
   bool streamRPC(ClangdRequestT Request,
                  StreamingCall<RequestT, ReplyT> RPCCall,
                  CallbackT Callback) const {
+    updateConnectionStatus();
     bool FinalResult = false;
     trace::Span Tracer(RequestT::descriptor()->name());
     const auto RPCRequest = ProtobufMarshaller->toProtobuf(Request);
@@ -77,18 +104,21 @@ class IndexClient : public clangd::SymbolIndex {
     SPAN_ATTACH(Tracer, "Status", Reader->Finish().ok());
     SPAN_ATTACH(Tracer, "Successful", Successful);
     SPAN_ATTACH(Tracer, "Failed to parse", FailedToParse);
+    updateConnectionStatus();
     return FinalResult;
   }
 
 public:
   IndexClient(
-      std::shared_ptr<grpc::Channel> Channel, llvm::StringRef ProjectRoot,
-      llvm::StringRef Address,
+      std::shared_ptr<grpc::Channel> Channel, llvm::StringRef Address,
+      llvm::StringRef ProjectRoot,
       std::chrono::milliseconds DeadlineTime = std::chrono::milliseconds(1000))
-      : Stub(remote::v1::SymbolIndex::NewStub(Channel)),
+      : Stub(remote::v1::SymbolIndex::NewStub(Channel)), Channel(Channel),
+        ServerAddress(Address),
+        ConnectionStatus(Channel->GetState(/*try_to_connect=*/true)),
         ProtobufMarshaller(new Marshaller(/*RemoteIndexRoot=*/"",
                                           /*LocalIndexRoot=*/ProjectRoot)),
-        ServerAddress(Address), DeadlineWaitingTime(DeadlineTime) {
+        DeadlineWaitingTime(DeadlineTime) {
     assert(!ProjectRoot.empty());
   }
 
@@ -128,6 +158,9 @@ class IndexClient : public clangd::SymbolIndex {
 
 private:
   std::unique_ptr<remote::v1::SymbolIndex::Stub> Stub;
+  std::shared_ptr<grpc::Channel> Channel;
+  llvm::SmallString<256> ServerAddress;
+  mutable std::atomic<grpc_connectivity_state> ConnectionStatus;
   std::unique_ptr<Marshaller> ProtobufMarshaller;
   llvm::SmallString<256> ServerAddress;
   // Each request will be terminated if it takes too long.
@@ -140,9 +173,8 @@ std::unique_ptr<clangd::SymbolIndex> getClient(llvm::StringRef Address,
                                                llvm::StringRef ProjectRoot) {
   const auto Channel =
       grpc::CreateChannel(Address.str(), grpc::InsecureChannelCredentials());
-  Channel->GetState(true);
   return std::unique_ptr<clangd::SymbolIndex>(
-      new IndexClient(Channel, ProjectRoot, Address));
+      new IndexClient(Channel, Address, ProjectRoot));
 }
 
 } // namespace remote


        


More information about the llvm-branch-commits mailing list