Skip to content

Commit

Permalink
singleton manager: use ASSERT_IS_MAIN_OR_TEST_THREAD rather than comp…
Browse files Browse the repository at this point in the history
…aring tid directly (envoyproxy#34766)

* singleton manager: use ASSERT_IS_MAIN_OR_TEST_THREAD rather than comparing tid directly

Signed-off-by: wbpcode <wbphub@live.com>

* fix test

Signed-off-by: wbpcode <wbphub@live.com>

* fix test

Signed-off-by: wbpcode <wbphub@live.com>

---------

Signed-off-by: wbpcode <wbphub@live.com>
Co-authored-by: wbpcode <wbphub@live.com>
Signed-off-by: Neal Soni <Nealsoni00@gmail.com>
  • Loading branch information
2 people authored and Nealsoni00 committed Jun 18, 2024
1 parent 8d67b1c commit b2bf826
Show file tree
Hide file tree
Showing 22 changed files with 32 additions and 41 deletions.
2 changes: 1 addition & 1 deletion source/common/singleton/manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Envoy {
namespace Singleton {

InstanceSharedPtr ManagerImpl::get(const std::string& name, SingletonFactoryCb cb, bool pin) {
ASSERT(run_tid_ == thread_factory_.currentThreadId());
ASSERT_IS_MAIN_OR_TEST_THREAD();

ENVOY_BUG(Registry::FactoryRegistry<Registration>::getFactory(name) != nullptr,
"invalid singleton name '" + name + "'. Make sure it is registered.");
Expand Down
6 changes: 1 addition & 5 deletions source/common/singleton/manager_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@ namespace Singleton {
*/
class ManagerImpl : public Manager, NonCopyable {
public:
explicit ManagerImpl(Thread::ThreadFactory& thread_factory)
: thread_factory_(thread_factory), run_tid_(thread_factory.currentThreadId()) {}
ManagerImpl() = default;

// Singleton::Manager
InstanceSharedPtr get(const std::string& name, SingletonFactoryCb cb, bool pin) override;

private:
absl::node_hash_map<std::string, std::weak_ptr<Instance>> singletons_;
std::vector<InstanceSharedPtr> pinned_singletons_;

Thread::ThreadFactory& thread_factory_;
const Thread::ThreadId run_tid_;
};

} // namespace Singleton
Expand Down
2 changes: 0 additions & 2 deletions source/server/config_validation/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "source/common/listener_manager/listener_info_impl.h"
#include "source/common/local_info/local_info_impl.h"
#include "source/common/protobuf/utility.h"
#include "source/common/singleton/manager_impl.h"
#include "source/common/stats/tag_producer_impl.h"
#include "source/common/tls/context_manager_impl.h"
#include "source/common/version/version.h"
Expand Down Expand Up @@ -60,7 +59,6 @@ ValidationInstance::ValidationInstance(
api_(new Api::ValidationImpl(thread_factory, store, time_system, file_system,
random_generator_, bootstrap_, process_context)),
dispatcher_(api_->allocateDispatcher("main_thread")),
singleton_manager_(new Singleton::ManagerImpl(api_->threadFactory())),
access_log_manager_(options.fileFlushIntervalMsec(), *api_, *dispatcher_, access_log_lock,
store),
grpc_context_(stats_store_.symbolTable()), http_context_(stats_store_.symbolTable()),
Expand Down
5 changes: 3 additions & 2 deletions source/server/config_validation/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "source/common/router/rds_impl.h"
#include "source/common/runtime/runtime_impl.h"
#include "source/common/secret/secret_manager_impl.h"
#include "source/common/singleton/manager_impl.h"
#include "source/common/thread_local/thread_local_impl.h"
#include "source/server/config_validation/admin.h"
#include "source/server/config_validation/api.h"
Expand Down Expand Up @@ -95,7 +96,7 @@ class ValidationInstance final : Logger::Loggable<Logger::Id::main>,
void shutdown() override;
bool isShutdown() override { return false; }
void shutdownAdmin() override {}
Singleton::Manager& singletonManager() override { return *singleton_manager_; }
Singleton::Manager& singletonManager() override { return singleton_manager_; }
OverloadManager& overloadManager() override { return *overload_manager_; }
OverloadManager& nullOverloadManager() override { return *null_overload_manager_; }
bool healthCheckFailed() override { return false; }
Expand Down Expand Up @@ -172,7 +173,7 @@ class ValidationInstance final : Logger::Loggable<Logger::Id::main>,
std::unique_ptr<Ssl::ContextManager> ssl_context_manager_;
Event::DispatcherPtr dispatcher_;
std::unique_ptr<Server::ValidationAdmin> admin_;
Singleton::ManagerPtr singleton_manager_;
Singleton::ManagerImpl singleton_manager_;
std::unique_ptr<Runtime::Loader> runtime_;
Random::RandomGeneratorImpl random_generator_;
Configuration::MainImpl config_;
Expand Down
2 changes: 0 additions & 2 deletions source/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include "source/common/runtime/runtime_impl.h"
#include "source/common/runtime/runtime_keys.h"
#include "source/common/signal/fatal_error_handler.h"
#include "source/common/singleton/manager_impl.h"
#include "source/common/stats/stats_matcher_impl.h"
#include "source/common/stats/tag_producer_impl.h"
#include "source/common/stats/thread_local_store.h"
Expand Down Expand Up @@ -97,7 +96,6 @@ InstanceBase::InstanceBase(Init::Manager& init_manager, const Options& options,
dispatcher_(api_->allocateDispatcher("main_thread")),
access_log_manager_(options.fileFlushIntervalMsec(), *api_, *dispatcher_, access_log_lock,
store),
singleton_manager_(new Singleton::ManagerImpl(api_->threadFactory())),
handler_(getHandler(*dispatcher_)), worker_factory_(thread_local_, *api_, hooks),
mutex_tracer_(options.mutexTracingEnabled() ? &Envoy::MutexTracerImpl::getOrCreateTracer()
: nullptr),
Expand Down
5 changes: 3 additions & 2 deletions source/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "source/common/router/context_impl.h"
#include "source/common/runtime/runtime_impl.h"
#include "source/common/secret/secret_manager_impl.h"
#include "source/common/singleton/manager_impl.h"
#include "source/common/upstream/health_discovery_service.h"

#ifdef ENVOY_ADMIN_FUNCTIONALITY
Expand Down Expand Up @@ -282,7 +283,7 @@ class InstanceBase : Logger::Loggable<Logger::Id::main>,
void shutdown() override;
bool isShutdown() final { return shutdown_; }
void shutdownAdmin() override;
Singleton::Manager& singletonManager() override { return *singleton_manager_; }
Singleton::Manager& singletonManager() override { return singleton_manager_; }
bool healthCheckFailed() override;
const Options& options() override { return options_; }
time_t startTimeCurrentEpoch() override { return start_time_; }
Expand Down Expand Up @@ -383,7 +384,7 @@ class InstanceBase : Logger::Loggable<Logger::Id::main>,
Event::DispatcherPtr dispatcher_;
AccessLog::AccessLogManagerImpl access_log_manager_;
std::shared_ptr<Admin> admin_;
Singleton::ManagerPtr singleton_manager_;
Singleton::ManagerImpl singleton_manager_;
Network::ConnectionHandlerPtr handler_;
std::unique_ptr<Runtime::Loader> runtime_;
ProdWorkerFactory worker_factory_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class HttpServerPropertiesCacheManagerTest : public testing::Test,
manager_ = factory_->get();
}

Singleton::ManagerImpl singleton_manager_{Thread::threadFactoryForTest()};
Singleton::ManagerImpl singleton_manager_;
NiceMock<Server::Configuration::MockFactoryContext> context_;
testing::NiceMock<ThreadLocal::MockInstance> tls_;
std::unique_ptr<Http::HttpServerPropertiesCacheManagerFactoryImpl> factory_;
Expand Down
10 changes: 5 additions & 5 deletions test/common/singleton/manager_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace {

// Must be a dedicated function so that TID is within the death test.
static void deathTestWorker() {
ManagerImpl manager(Thread::threadFactoryForTest());
ManagerImpl manager;

manager.get(
"foo", [] { return nullptr; }, false);
Expand All @@ -39,7 +39,7 @@ TEST(SingletonRegistration, category) {
}

TEST(SingletonManagerImplTest, Basic) {
ManagerImpl manager(Thread::threadFactoryForTest());
ManagerImpl manager;

std::shared_ptr<TestSingleton> singleton = std::make_shared<TestSingleton>();
EXPECT_EQ(singleton, manager.get(
Expand All @@ -53,7 +53,7 @@ TEST(SingletonManagerImplTest, Basic) {
}

TEST(SingletonManagerImplTest, NonConstructingGetTyped) {
ManagerImpl manager(Thread::threadFactoryForTest());
ManagerImpl manager;

// Access without first constructing should be null.
EXPECT_EQ(nullptr, manager.getTyped<TestSingleton>("test_singleton"));
Expand All @@ -73,7 +73,7 @@ TEST(SingletonManagerImplTest, NonConstructingGetTyped) {
TEST(SingletonManagerImplTest, PinnedSingleton) {

{
ManagerImpl manager(Thread::threadFactoryForTest());
ManagerImpl manager;
TestSingleton* singleton_ptr{};

// Register a singleton and get it.
Expand All @@ -94,7 +94,7 @@ TEST(SingletonManagerImplTest, PinnedSingleton) {
}

{
ManagerImpl manager(Thread::threadFactoryForTest());
ManagerImpl manager;
TestSingleton* singleton_ptr{};

// Register a pinned singleton and get it.
Expand Down
2 changes: 1 addition & 1 deletion test/common/upstream/test_cluster_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class TestClusterManagerFactory : public ClusterManagerFactory {
NiceMock<Server::MockAdmin>& admin_ = server_context_.admin_;
NiceMock<Secret::MockSecretManager> secret_manager_;
NiceMock<AccessLog::MockAccessLogManager>& log_manager_ = server_context_.access_log_manager_;
Singleton::ManagerImpl singleton_manager_{Thread::threadFactoryForTest()};
Singleton::ManagerImpl singleton_manager_;
NiceMock<ProtobufMessage::MockValidationVisitor> validation_visitor_;
NiceMock<Random::MockRandomGenerator> random_;
Api::ApiPtr api_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class AsyncFileHandleHelpers {
class AsyncFileHandleTest : public testing::Test, public AsyncFileHandleHelpers {
public:
void SetUp() override {
singleton_manager_ = std::make_unique<Singleton::ManagerImpl>(Thread::threadFactoryForTest());
singleton_manager_ = std::make_unique<Singleton::ManagerImpl>();
factory_ = AsyncFileManagerFactory::singleton(singleton_manager_.get());
envoy::extensions::common::async_files::v3::AsyncFileManagerConfig config;
config.mutable_thread_pool()->set_thread_count(1);
Expand All @@ -77,7 +77,7 @@ class AsyncFileHandleWithMockPosixTest : public testing::Test, public AsyncFileH
void SetUp() override {
EXPECT_CALL(mock_posix_file_operations_, supportsAllPosixFileOperations())
.WillRepeatedly(Return(true));
singleton_manager_ = std::make_unique<Singleton::ManagerImpl>(Thread::threadFactoryForTest());
singleton_manager_ = std::make_unique<Singleton::ManagerImpl>();
factory_ = AsyncFileManagerFactory::singleton(singleton_manager_.get());
envoy::extensions::common::async_files::v3::AsyncFileManagerConfig config;
config.mutable_thread_pool()->set_thread_count(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using ::testing::StrictMock;
class AsyncFileManagerFactoryTest : public ::testing::Test {
public:
void SetUp() override {
singleton_manager_ = std::make_unique<Singleton::ManagerImpl>(Thread::threadFactoryForTest());
singleton_manager_ = std::make_unique<Singleton::ManagerImpl>();
factory_ = AsyncFileManagerFactory::singleton(singleton_manager_.get());
EXPECT_CALL(mock_posix_file_operations_, supportsAllPosixFileOperations())
.WillRepeatedly(Return(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class AsyncFileActionBlockedUntilReleased : public AsyncFileActionWithResult<boo
class AsyncFileManagerTest : public testing::Test {
public:
void SetUp() override {
singleton_manager_ = std::make_unique<Singleton::ManagerImpl>(Thread::threadFactoryForTest());
singleton_manager_ = std::make_unique<Singleton::ManagerImpl>();
factory_ = AsyncFileManagerFactory::singleton(singleton_manager_.get());
}

Expand Down Expand Up @@ -179,7 +179,7 @@ class AsyncFileManagerSingleThreadTest : public AsyncFileManagerTest {
void SetUp() override {
envoy::extensions::common::async_files::v3::AsyncFileManagerConfig config;
config.mutable_thread_pool()->set_thread_count(1);
singleton_manager_ = std::make_unique<Singleton::ManagerImpl>(Thread::threadFactoryForTest());
singleton_manager_ = std::make_unique<Singleton::ManagerImpl>();
auto factory = AsyncFileManagerFactory::singleton(singleton_manager_.get());
manager_ = factory->getAsyncFileManager(config);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AsyncFileManagerWithMockFilesTest : public ::testing::Test {
.WillRepeatedly(Return(true));
envoy::extensions::common::async_files::v3::AsyncFileManagerConfig config;
config.mutable_thread_pool()->set_thread_count(1);
singleton_manager_ = std::make_unique<Singleton::ManagerImpl>(Thread::threadFactoryForTest());
singleton_manager_ = std::make_unique<Singleton::ManagerImpl>();
factory_ = AsyncFileManagerFactory::singleton(singleton_manager_.get());
manager_ = factory_->getAsyncFileManager(config, &mock_posix_file_operations_);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ DEFINE_PROTO_FUZZER(
// default time system in GlobalTimeSystem.
dispatcher.time_system_ = std::make_unique<Event::SimulatedTimeSystem>();
Stats::IsolatedStoreImpl stats_store;
Singleton::ManagerImpl singleton_manager(Thread::threadFactoryForTest());
Singleton::ManagerImpl singleton_manager;
static NiceMock<Runtime::MockLoader> runtime;
Event::MockTimer* fill_timer = new Event::MockTimer(&dispatcher);
envoy::extensions::filters::network::local_ratelimit::v3::LocalRateLimit proto_config =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace LocalRateLimitFilter {

class LocalRateLimitTestBase : public testing::Test, public Event::TestUsingSimulatedTime {
public:
LocalRateLimitTestBase() : singleton_manager_(Thread::threadFactoryForTest()) {}
LocalRateLimitTestBase() = default;

uint64_t initialize(const std::string& filter_yaml, bool expect_timer_create = true) {
envoy::extensions::filters::network::local_ratelimit::v3::LocalRateLimit proto_config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ CacheConfig varyAllowListConfig() {

class MockSingletonManager : public Singleton::ManagerImpl {
public:
MockSingletonManager() : Singleton::ManagerImpl(Thread::threadFactoryForTest()) {
MockSingletonManager() {
// By default just act like a real SingletonManager, but allow overrides.
ON_CALL(*this, get(_, _, _))
.WillByDefault(std::bind(&MockSingletonManager::realGet, this, std::placeholders::_1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HttpServerPropertiesCacheManagerTest : public testing::Test,
singleton_manager_, tls_, data);
manager_ = factory_->get();
}
Singleton::ManagerImpl singleton_manager_{Thread::threadFactoryForTest()};
Singleton::ManagerImpl singleton_manager_;
NiceMock<Server::Configuration::MockFactoryContext> context_;
testing::NiceMock<ThreadLocal::MockInstance> tls_;
std::unique_ptr<Http::HttpServerPropertiesCacheManagerFactoryImpl> factory_;
Expand Down
4 changes: 2 additions & 2 deletions test/extensions/transport_sockets/alts/config_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace {

TEST(UpstreamAltsConfigTest, CreateSocketFactory) {
NiceMock<MockTransportSocketFactoryContext> factory_context;
Singleton::ManagerImpl singleton_manager{Thread::threadFactoryForTest()};
Singleton::ManagerImpl singleton_manager;
EXPECT_CALL(factory_context.server_context_, singletonManager())
.WillRepeatedly(ReturnRef(singleton_manager));
UpstreamAltsTransportSocketConfigFactory factory;
Expand All @@ -39,7 +39,7 @@ TEST(UpstreamAltsConfigTest, CreateSocketFactory) {

TEST(DownstreamAltsConfigTest, CreateSocketFactory) {
NiceMock<MockTransportSocketFactoryContext> factory_context;
Singleton::ManagerImpl singleton_manager{Thread::threadFactoryForTest()};
Singleton::ManagerImpl singleton_manager;
EXPECT_CALL(factory_context.server_context_, singletonManager())
.WillRepeatedly(ReturnRef(singleton_manager));
DownstreamAltsTransportSocketConfigFactory factory;
Expand Down
3 changes: 1 addition & 2 deletions test/mocks/server/instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ using ::testing::ReturnRef;

MockInstance::MockInstance()
: secret_manager_(std::make_unique<Secret::SecretManagerImpl>(admin_.getConfigTracker())),
cluster_manager_(timeSource()),
singleton_manager_(new Singleton::ManagerImpl(Thread::threadFactoryForTest())),
cluster_manager_(timeSource()), singleton_manager_(new Singleton::ManagerImpl()),
grpc_context_(stats_store_.symbolTable()), http_context_(stats_store_.symbolTable()),
router_context_(stats_store_.symbolTable()), quic_stat_names_(stats_store_.symbolTable()),
stats_config_(std::make_shared<NiceMock<Configuration::MockStatsConfig>>()),
Expand Down
5 changes: 2 additions & 3 deletions test/mocks/server/server_factory_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ using ::testing::Return;
using ::testing::ReturnRef;

MockServerFactoryContext::MockServerFactoryContext()
: singleton_manager_(new Singleton::ManagerImpl(Thread::threadFactoryForTest())),
http_context_(store_.symbolTable()), grpc_context_(store_.symbolTable()),
router_context_(store_.symbolTable()) {
: singleton_manager_(new Singleton::ManagerImpl()), http_context_(store_.symbolTable()),
grpc_context_(store_.symbolTable()), router_context_(store_.symbolTable()) {
ON_CALL(*this, clusterManager()).WillByDefault(ReturnRef(cluster_manager_));
ON_CALL(*this, mainThreadDispatcher()).WillByDefault(ReturnRef(dispatcher_));
ON_CALL(*this, drainDecision()).WillByDefault(ReturnRef(drain_manager_));
Expand Down
3 changes: 1 addition & 2 deletions test/mocks/server/transport_socket_factory_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ namespace Configuration {
using ::testing::ReturnRef;

MockTransportSocketFactoryContext::MockTransportSocketFactoryContext()
: secret_manager_(std::make_unique<Secret::SecretManagerImpl>(config_tracker_)),
singleton_manager_(Thread::threadFactoryForTest()) {
: secret_manager_(std::make_unique<Secret::SecretManagerImpl>(config_tracker_)) {
ON_CALL(*this, serverFactoryContext()).WillByDefault(ReturnRef(server_context_));
ON_CALL(*this, clusterManager()).WillByDefault(ReturnRef(cluster_manager_));
ON_CALL(*this, messageValidationVisitor())
Expand Down
2 changes: 1 addition & 1 deletion test/mocks/upstream/cluster_manager_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class MockClusterManagerFactory : public ClusterManagerFactory {

private:
NiceMock<Secret::MockSecretManager> secret_manager_;
Singleton::ManagerImpl singleton_manager_{Thread::threadFactoryForTest()};
Singleton::ManagerImpl singleton_manager_;
};
} // namespace Upstream
} // namespace Envoy

0 comments on commit b2bf826

Please sign in to comment.