Skip to content

Commit d71b844

Browse files
ivanmurashkofacebook-github-bot
authored andcommitted
Glass connection warm-up
Summary: The diff implement a connection warm-up mechanism in the GlassAccess constructor that sends the `getStatus` request to establish the connection. The diff also logs the timing. Note: based on the tests with binary built with mode/opt flag file - the initialisation takes ~1.5sec. With mode/dev it will take up to 30sec Reviewed By: Wilfred Differential Revision: D78978796 fbshipit-source-id: 7f67feb963930a181e0cfaebb372d32364428692
1 parent 7403086 commit d71b844

6 files changed

Lines changed: 89 additions & 1 deletion

File tree

glean/client/swift/Clock.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#include "glean/client/swift/Clock.h"
10+
11+
namespace facebook::glean::swift {
12+
13+
Clock::Clock() : start_(std::chrono::high_resolution_clock::now()) {}
14+
15+
int64_t Clock::duration() const {
16+
auto now = std::chrono::high_resolution_clock::now();
17+
return std::chrono::duration_cast<std::chrono::milliseconds>(now - start_)
18+
.count();
19+
}
20+
21+
} // namespace facebook::glean::swift

glean/client/swift/Clock.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#pragma once
10+
11+
#include <chrono>
12+
13+
namespace facebook::glean::swift {
14+
15+
class Clock {
16+
public:
17+
Clock();
18+
~Clock() = default;
19+
20+
// Returns the duration in milliseconds since the Clock was created
21+
int64_t duration() const;
22+
23+
private:
24+
std::chrono::high_resolution_clock::time_point start_;
25+
};
26+
27+
} // namespace facebook::glean::swift

glean/client/swift/GlassAccess.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <cstdlib>
1515
#include <memory>
1616
#include <stdexcept>
17+
#include "glean/client/swift/Clock.h"
1718

1819
using namespace facebook;
1920
using apache::thrift::RpcOptions;
@@ -24,6 +25,7 @@ GlassAccess::GlassAccess() : client(nullptr), hgRoot_(getHgRoot()) {}
2425

2526
std::optional<protocol::LocationList> GlassAccess::usrToDefinition(
2627
const std::string& usr) {
28+
facebook::glean::swift::Clock clock;
2729
std::string msg;
2830

2931
// Create USRToDefinitionRequest
@@ -42,6 +44,9 @@ std::optional<protocol::LocationList> GlassAccess::usrToDefinition(
4244
co_return co_await client->co_usrToDefinition(rpcOptions, request, ro);
4345
});
4446

47+
auto duration = clock.duration();
48+
LOG(INFO) << "usrToDefinition request took " << duration << " milliseconds";
49+
4550
if (!result.has_value()) {
4651
return std::nullopt;
4752
}
@@ -123,3 +128,32 @@ std::string GlassAccess::getHgRoot() {
123128

124129
return result;
125130
}
131+
132+
void GlassAccess::warmUpConnection() {
133+
facebook::glean::swift::Clock clock;
134+
135+
LOG(INFO) << "Warming up Glass connection...";
136+
137+
try {
138+
// Use getStatus to establish the connection
139+
std::string msg;
140+
141+
// Call the Glass service getStatus
142+
this->runGlassMethod<fb303::cpp2::fb_status>(
143+
"getStatus", msg, [&]() -> folly::coro::Task<fb303::cpp2::fb_status> {
144+
RpcOptions rpcOptions;
145+
rpcOptions.setTimeout(std::chrono::milliseconds(GlassTimeoutMs));
146+
co_return co_await client->co_getStatus(rpcOptions);
147+
});
148+
149+
auto duration = clock.duration();
150+
151+
LOG(INFO) << "Glass connection established successfully in " << duration
152+
<< " milliseconds";
153+
} catch (const std::exception& e) {
154+
auto duration = clock.duration();
155+
156+
LOG(WARNING) << "Glass connection warm-up failed after " << duration
157+
<< " milliseconds: " << e.what();
158+
}
159+
}

glean/client/swift/GlassAccess.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <memory>
1313
#include "glean/client/swift/IGlassAccess.h"
1414
#include "glean/glass/if/gen-cpp2/GlassServiceAsyncClient.h"
15-
#include "glean/glass/if/gen-cpp2/glass_types.h"
1615

1716
class GlassAccess : public IGlassAccess {
1817
public:
@@ -29,6 +28,7 @@ class GlassAccess : public IGlassAccess {
2928
std::string& msg,
3029
folly::Function<folly::coro::Task<T>()> f);
3130

31+
void warmUpConnection();
3232
std::unique_ptr<apache::thrift::Client<::glean::GlassService>> client;
3333

3434
private:

glean/client/swift/GlassAccessLocal.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ GlassAccessLocal::GlassAccessLocal() {
1616
client = facebook::corp2prod::client::getClientFactory()
1717
.getClientUnique<apache::thrift::Client<::glean::GlassService>>(
1818
connectionTier);
19+
20+
// Warm up the connection after client is initialized
21+
warmUpConnection();
1922
}

glean/client/swift/GlassAccessRemote.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ GlassAccessRemote::GlassAccessRemote() {
2020
servicerouter::cpp2::getClientFactory()
2121
.getSRClientUnique<apache::thrift::Client<::glean::GlassService>>(
2222
connectionTier, params);
23+
24+
// Warm up the connection after client is initialized
25+
warmUpConnection();
2326
}

0 commit comments

Comments
 (0)