diff --git a/clang-tools-extra/clangd/index/remote/Client.cpp b/clang-tools-extra/clangd/index/remote/Client.cpp --- a/clang-tools-extra/clangd/index/remote/Client.cpp +++ b/clang-tools-extra/clangd/index/remote/Client.cpp @@ -6,10 +6,10 @@ // //===----------------------------------------------------------------------===// -#include - #include "Client.h" #include "Service.grpc.pb.h" +#include "grpc/impl/codegen/connectivity_state.h" +#include "grpcpp/channel.h" #include "index/Index.h" #include "marshalling/Marshalling.h" #include "support/Logger.h" @@ -17,14 +17,33 @@ #include "clang/Basic/Version.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Error.h" +#include #include +#include +#include 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 { template using StreamingCall = std::unique_ptr> ( @@ -36,6 +55,7 @@ bool streamRPC(ClangdRequestT Request, StreamingCall RPCCall, CallbackT Callback) const { + auto OldStatus = Channel->GetState(/*try_to_connect=*/false); bool FinalResult = false; trace::Span Tracer(RequestT::descriptor()->name()); const auto RPCRequest = ProtobufMarshaller->toProtobuf(Request); @@ -68,14 +88,18 @@ SPAN_ATTACH(Tracer, "Status", Reader->Finish().ok()); SPAN_ATTACH(Tracer, "Successful", Successful); SPAN_ATTACH(Tracer, "Failed to parse", FailedToParse); + vlog("Remote index ({0}): {1} -> {2}", ServerAddress, toString(OldStatus), + toString(Channel->GetState(/*try_to_connect=*/false))); return FinalResult; } public: IndexClient( - std::shared_ptr Channel, llvm::StringRef ProjectRoot, + std::shared_ptr 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), ProtobufMarshaller(new Marshaller(/*RemoteIndexRoot=*/"", /*LocalIndexRoot=*/ProjectRoot)), DeadlineWaitingTime(DeadlineTime) { @@ -118,6 +142,8 @@ private: std::unique_ptr Stub; + std::shared_ptr Channel; + llvm::SmallString<256> ServerAddress; std::unique_ptr ProtobufMarshaller; // Each request will be terminated if it takes too long. std::chrono::milliseconds DeadlineWaitingTime; @@ -129,9 +155,9 @@ llvm::StringRef ProjectRoot) { const auto Channel = grpc::CreateChannel(Address.str(), grpc::InsecureChannelCredentials()); - Channel->GetState(true); + Channel->GetState(/*try_to_connect=*/true); return std::unique_ptr( - new IndexClient(Channel, ProjectRoot)); + new IndexClient(Channel, Address, ProjectRoot)); } } // namespace remote