Skip to content

Commit fb48f78

Browse files
ivanmurashkofacebook-github-bot
authored andcommitted
Communication with Glass via ServiceRouter
Summary: This diff updates the BUCK file for the Glean Glass client in Swift, adding dependencies for ServiceRouter and folly/coro. It also modifies the GlassAccess class to use ServiceRouter for communication with the Glass service, and implements the usrToDefinition method. With the diff we get a functional Swift Glass Client application that can be run on DevServers and OnDemand servers. It's important to note that it cannot be built on local (Darwin) and the required changes will be implemented later in subsequent diffs Reviewed By: dmpolukhin Differential Revision: D78901802 fbshipit-source-id: b80b8c67dd1e09c3e26bbcf56f6ee92ffa71e0cd
1 parent 4c0d829 commit fb48f78

2 files changed

Lines changed: 102 additions & 3 deletions

File tree

glean/client/swift/GlassAccess.cpp

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,95 @@
77
*/
88

99
#include "glean/client/swift/GlassAccess.h"
10+
#include <folly/ExceptionString.h>
11+
#include <folly/String.h>
12+
#include <folly/coro/BlockingWait.h>
13+
#include <glog/logging.h>
14+
#include "servicerouter/client/cpp2/ServiceRouter.h"
15+
16+
using namespace facebook;
17+
using apache::thrift::RpcOptions;
18+
19+
const auto GlassTimeoutMs = 900;
20+
21+
GlassAccess::GlassAccess() {
22+
auto params = facebook::servicerouter::ClientParams().setProcessingTimeoutMs(
23+
std::chrono::milliseconds(10000));
24+
25+
std::string connectionTier = "glean.glass";
26+
client =
27+
servicerouter::cpp2::getClientFactory()
28+
.getSRClientUnique<apache::thrift::Client<::glean::GlassService>>(
29+
connectionTier, params);
30+
}
1031

1132
std::optional<protocol::LocationList> GlassAccess::usrToDefinition(
1233
const std::string& usr) {
13-
// Default implementation - always returns nullopt
14-
return std::nullopt;
34+
std::string msg;
35+
36+
// Create USRToDefinitionRequest
37+
::glean::USRToDefinitionRequest request;
38+
request.usr_ref() = usr;
39+
request.repo_name_ref() = "fbsource";
40+
41+
// Call the Glass service to get symbol definition
42+
const auto result = this->runGlassMethod<::glean::USRSymbolDefinition>(
43+
"usrToDefinition",
44+
msg,
45+
[&]() -> folly::coro::Task<::glean::USRSymbolDefinition> {
46+
::glean::RequestOptions ro;
47+
RpcOptions rpcOptions;
48+
rpcOptions.setTimeout(std::chrono::milliseconds(GlassTimeoutMs));
49+
co_return co_await client->co_usrToDefinition(rpcOptions, request, ro);
50+
});
51+
52+
if (!result.has_value()) {
53+
return std::nullopt;
54+
}
55+
56+
// Convert Glass result to protocol::LocationList
57+
protocol::LocationList locations;
58+
const auto& definition = result.value();
59+
60+
// Extract location from USRSymbolDefinition
61+
const auto& gleanLocation = definition.location_ref().value();
62+
63+
// Convert glean::LocationRange to protocol::Location
64+
// Access the range fields directly
65+
const auto& range = gleanLocation.range_ref().value();
66+
67+
protocol::Position start(
68+
static_cast<int>(range.lineBegin_ref().value()),
69+
static_cast<int>(range.columnBegin_ref().value()));
70+
protocol::Position end(
71+
static_cast<int>(range.lineEnd_ref().value()),
72+
static_cast<int>(range.columnEnd_ref().value()));
73+
protocol::Range protocolRange(start, end);
74+
75+
// Create URI from repository and filepath using URI escaping
76+
std::string filepath = gleanLocation.filepath_ref().value();
77+
std::string uri = "file://" + folly::uriEscape<std::string>(filepath);
78+
protocol::Location location(uri, protocolRange);
79+
80+
locations.push_back(location);
81+
82+
return locations;
83+
}
84+
85+
template <typename T>
86+
std::optional<T> GlassAccess::runGlassMethod(
87+
const std::string& method,
88+
std::string& msg,
89+
folly::Function<folly::coro::Task<T>()> f) {
90+
try {
91+
return std::make_optional(folly::coro::blockingWait(f()));
92+
} catch (::glean::ServerException& ex) {
93+
msg += "::glean::ServerException(" + ex.message().value() + ");";
94+
LOG(ERROR) << "EXCEPTION searching " << msg << "\n";
95+
} catch (std::exception& ex) {
96+
msg += folly::exceptionStr(ex) + ";";
97+
LOG(ERROR) << "EXCEPTION running " << method << ":" << ex.what() << ":"
98+
<< msg << "\n";
99+
}
100+
return {};
15101
}

glean/client/swift/GlassAccess.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,26 @@
88

99
#pragma once
1010

11+
#include <folly/coro/Task.h>
12+
#include <memory>
1113
#include "glean/client/swift/IGlassAccess.h"
14+
#include "glean/glass/if/gen-cpp2/GlassServiceAsyncClient.h"
15+
#include "glean/glass/if/gen-cpp2/glass_types.h"
1216

1317
class GlassAccess : public IGlassAccess {
1418
public:
15-
GlassAccess() = default;
19+
GlassAccess();
1620
~GlassAccess() override = default;
1721

1822
std::optional<protocol::LocationList> usrToDefinition(
1923
const std::string& usr) override;
24+
25+
private:
26+
template <typename T>
27+
std::optional<T> runGlassMethod(
28+
const std::string& method,
29+
std::string& msg,
30+
folly::Function<folly::coro::Task<T>()> f);
31+
32+
std::unique_ptr<apache::thrift::Client<::glean::GlassService>> client;
2033
};

0 commit comments

Comments
 (0)