Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/envoy/event/dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ struct DispatcherStats {
ALL_DISPATCHER_STATS(GENERATE_HISTOGRAM_STRUCT)
};

using DispatcherStatsPtr = std::unique_ptr<DispatcherStats>;

/**
* Callback invoked when a dispatcher post() runs.
*/
using PostCb = std::function<void()>;

using PostCbSharedPtr = std::shared_ptr<PostCb>;

/**
* Abstract event dispatching loop.
*/
Expand Down
2 changes: 1 addition & 1 deletion source/common/event/dispatcher_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class DispatcherImpl : Logger::Loggable<Logger::Id::main>,
const std::string name_;
Api::Api& api_;
std::string stats_prefix_;
std::unique_ptr<DispatcherStats> stats_;
DispatcherStatsPtr stats_;
Thread::ThreadId run_tid_;
Buffer::WatermarkFactoryPtr buffer_factory_;
LibeventScheduler base_scheduler_;
Expand Down
6 changes: 4 additions & 2 deletions source/common/stats/thread_local_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,10 @@ StatType& ThreadLocalStoreImpl::ScopeImpl::safeMakeStat(
}

template <class StatType>
absl::optional<std::reference_wrapper<const StatType>>
ThreadLocalStoreImpl::ScopeImpl::findStatLockHeld(
using StatTypeOptConstRef = absl::optional<std::reference_wrapper<const StatType>>;

template <class StatType>
StatTypeOptConstRef<StatType> ThreadLocalStoreImpl::ScopeImpl::findStatLockHeld(
StatName name, StatNameHashMap<RefcountPtr<StatType>>& central_cache_map) const {
auto iter = central_cache_map.find(name);
if (iter == central_cache_map.end()) {
Expand Down
8 changes: 7 additions & 1 deletion source/common/stats/thread_local_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <chrono>
#include <cstdint>
#include <list>
#include <memory>
#include <string>

#include "envoy/stats/tag.h"
Expand Down Expand Up @@ -380,6 +381,9 @@ class ThreadLocalStoreImpl : Logger::Loggable<Logger::Id::stats>, public StoreRo
MakeStatFn<StatType> make_stat, StatRefMap<StatType>* tls_cache,
StatNameHashSet* tls_rejected_stats, StatType& null_stat);

template <class StatType>
using StatTypeOptConstRef = absl::optional<std::reference_wrapper<const StatType>>;

/**
* Looks up an existing stat, populating the local cache if necessary. Does
* not check the TLS or rejects, and does not create a stat if it does not
Expand All @@ -390,7 +394,7 @@ class ThreadLocalStoreImpl : Logger::Loggable<Logger::Id::stats>, public StoreRo
* @return a reference to the stat, if it exists.
*/
template <class StatType>
absl::optional<std::reference_wrapper<const StatType>>
StatTypeOptConstRef<StatType>
findStatLockHeld(StatName name,
StatNameHashMap<RefcountPtr<StatType>>& central_cache_map) const;

Expand Down Expand Up @@ -463,5 +467,7 @@ class ThreadLocalStoreImpl : Logger::Loggable<Logger::Id::stats>, public StoreRo
StatNameSetPtr well_known_tags_;
};

using ThreadLocalStoreImplPtr = std::unique_ptr<ThreadLocalStoreImpl>;

} // namespace Stats
} // namespace Envoy
18 changes: 9 additions & 9 deletions source/common/thread_local/thread_local_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ SlotPtr InstanceImpl::allocateSlot() {
ASSERT(!shutdown_);

if (free_slot_indexes_.empty()) {
std::unique_ptr<SlotImpl> slot(new SlotImpl(*this, slots_.size()));
SlotImplPtr slot(new SlotImpl(*this, slots_.size()));
auto wrapper = std::make_unique<Bookkeeper>(*this, std::move(slot));
slots_.push_back(wrapper->slot_.get());
return wrapper;
}
const uint32_t idx = free_slot_indexes_.front();
free_slot_indexes_.pop_front();
ASSERT(idx < slots_.size());
std::unique_ptr<SlotImpl> slot(new SlotImpl(*this, idx));
SlotImplPtr slot(new SlotImpl(*this, idx));
slots_[idx] = slot.get();
return std::make_unique<Bookkeeper>(*this, std::move(slot));
}
Expand All @@ -56,7 +56,7 @@ ThreadLocalObjectSharedPtr InstanceImpl::SlotImpl::get() {
return thread_local_data_.data_[index_];
}

InstanceImpl::Bookkeeper::Bookkeeper(InstanceImpl& parent, std::unique_ptr<SlotImpl>&& slot)
InstanceImpl::Bookkeeper::Bookkeeper(InstanceImpl& parent, SlotImplPtr&& slot)
: parent_(parent), slot_(std::move(slot)),
ref_count_(/*not used.*/ nullptr,
[slot = slot_.get(), &parent = this->parent_](uint32_t* /* not used */) {
Expand Down Expand Up @@ -117,7 +117,7 @@ void InstanceImpl::registerThread(Event::Dispatcher& dispatcher, bool main_threa

// Puts the slot into a deferred delete container, the slot will be destructed when its out-going
// callback reference count goes to 0.
void InstanceImpl::recycle(std::unique_ptr<SlotImpl>&& slot) {
void InstanceImpl::recycle(SlotImplPtr&& slot) {
ASSERT(std::this_thread::get_id() == main_thread_id_);
ASSERT(slot != nullptr);
auto* slot_addr = slot.get();
Expand Down Expand Up @@ -194,11 +194,11 @@ void InstanceImpl::runOnAllThreads(Event::PostCb cb, Event::PostCb all_threads_c
// for programming simplicity here.
cb();

std::shared_ptr<Event::PostCb> cb_guard(new Event::PostCb(cb),
[this, all_threads_complete_cb](Event::PostCb* cb) {
main_thread_dispatcher_->post(all_threads_complete_cb);
delete cb;
});
Event::PostCbSharedPtr cb_guard(new Event::PostCb(cb),
[this, all_threads_complete_cb](Event::PostCb* cb) {
main_thread_dispatcher_->post(all_threads_complete_cb);
delete cb;
});

for (Event::Dispatcher& dispatcher : registered_threads_) {
dispatcher.post([cb_guard]() -> void { (*cb_guard)(); });
Expand Down
13 changes: 9 additions & 4 deletions source/common/thread_local/thread_local_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <atomic>
#include <cstdint>
#include <list>
#include <memory>
#include <vector>

#include "envoy/thread_local/thread_local.h"
Expand Down Expand Up @@ -50,10 +51,12 @@ class InstanceImpl : Logger::Loggable<Logger::Id::main>, public NonCopyable, pub
const uint64_t index_;
};

using SlotImplPtr = std::unique_ptr<SlotImpl>;

// A Wrapper of SlotImpl which on destruction returns the SlotImpl to the deferred delete queue
// (detaches it).
struct Bookkeeper : public Slot {
Bookkeeper(InstanceImpl& parent, std::unique_ptr<SlotImpl>&& slot);
Bookkeeper(InstanceImpl& parent, SlotImplPtr&& slot);
~Bookkeeper() override { parent_.recycle(std::move(slot_)); }

// ThreadLocal::Slot
Expand All @@ -66,7 +69,7 @@ class InstanceImpl : Logger::Loggable<Logger::Id::main>, public NonCopyable, pub
void set(InitializeCb cb) override;

InstanceImpl& parent_;
std::unique_ptr<SlotImpl> slot_;
SlotImplPtr slot_;
std::shared_ptr<uint32_t> ref_count_;
};

Expand All @@ -75,7 +78,7 @@ class InstanceImpl : Logger::Loggable<Logger::Id::main>, public NonCopyable, pub
std::vector<ThreadLocalObjectSharedPtr> data_;
};

void recycle(std::unique_ptr<SlotImpl>&& slot);
void recycle(SlotImplPtr&& slot);
// Cleanup the deferred deletes queue.
void scheduleCleanup(SlotImpl* slot);

Expand All @@ -89,7 +92,7 @@ class InstanceImpl : Logger::Loggable<Logger::Id::main>, public NonCopyable, pub
// A indexed container for Slots that has to be deferred to delete due to out-going callbacks
// pointing to the Slot. To let the ref_count_ deleter find the SlotImpl by address, the container
// is defined as a map of SlotImpl address to the unique_ptr<SlotImpl>.
absl::flat_hash_map<SlotImpl*, std::unique_ptr<SlotImpl>> deferred_deletes_;
absl::flat_hash_map<SlotImpl*, SlotImplPtr> deferred_deletes_;

std::vector<SlotImpl*> slots_;
// A list of index of freed slots.
Expand All @@ -104,5 +107,7 @@ class InstanceImpl : Logger::Loggable<Logger::Id::main>, public NonCopyable, pub
friend class ThreadLocalInstanceImplTest;
};

using InstanceImplPtr = std::unique_ptr<InstanceImpl>;

} // namespace ThreadLocal
} // namespace Envoy
4 changes: 2 additions & 2 deletions source/exe/main_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ class MainCommonBase {
Stats::SymbolTablePtr symbol_table_;
Stats::AllocatorImpl stats_allocator_;

std::unique_ptr<ThreadLocal::InstanceImpl> tls_;
ThreadLocal::InstanceImplPtr tls_;
std::unique_ptr<Server::HotRestart> restarter_;
std::unique_ptr<Stats::ThreadLocalStoreImpl> stats_store_;
Stats::ThreadLocalStoreImplPtr stats_store_;
std::unique_ptr<Logger::Context> logging_context_;
std::unique_ptr<Init::Manager> init_manager_{std::make_unique<Init::ManagerImpl>("Server")};
std::unique_ptr<Server::InstanceImpl> server_;
Expand Down
2 changes: 2 additions & 0 deletions source/extensions/filters/common/lua/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ class ThreadLocalState : Logger::Loggable<Logger::Id::lua> {
uint64_t current_global_slot_{};
};

using ThreadLocalStatePtr = std::unique_ptr<ThreadLocalState>;

/**
* An exception specific to Lua errors.
*/
Expand Down
7 changes: 5 additions & 2 deletions test/common/stats/stat_test_utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,12 @@ Histogram& TestStore::histogramFromStatNameWithTags(const StatName& stat_name,
}

template <class StatType>
static absl::optional<std::reference_wrapper<const StatType>>
using StatTypeOptConstRef = absl::optional<std::reference_wrapper<const StatType>>;

template <class StatType>
static StatTypeOptConstRef<StatType>
findByString(const std::string& name, const absl::flat_hash_map<std::string, StatType*>& map) {
absl::optional<std::reference_wrapper<const StatType>> ret;
StatTypeOptConstRef<StatType> ret;
auto iter = map.find(name);
if (iter != map.end()) {
ret = *iter->second;
Expand Down
2 changes: 1 addition & 1 deletion test/common/stats/thread_local_store_speed_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ThreadLocalStorePerf {
Event::SimulatedTimeSystem time_system_;
Stats::AllocatorImpl heap_alloc_;
Event::DispatcherPtr dispatcher_;
std::unique_ptr<ThreadLocal::InstanceImpl> tls_;
ThreadLocal::InstanceImplPtr tls_;
Stats::ThreadLocalStoreImpl store_;
Api::ApiPtr api_;
envoy::config::metrics::v3::StatsConfig stats_config_;
Expand Down
10 changes: 5 additions & 5 deletions test/common/stats/thread_local_store_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class StatsThreadLocalStoreTest : public testing::Test {
NiceMock<ThreadLocal::MockInstance> tls_;
AllocatorImpl alloc_;
MockSink sink_;
std::unique_ptr<ThreadLocalStoreImpl> store_;
ThreadLocalStoreImplPtr store_;
};

class HistogramWrapper {
Expand Down Expand Up @@ -176,7 +176,7 @@ class HistogramTest : public testing::Test {
NiceMock<ThreadLocal::MockInstance> tls_;
AllocatorImpl alloc_;
MockSink sink_;
std::unique_ptr<ThreadLocalStoreImpl> store_;
ThreadLocalStoreImplPtr store_;
InSequence s;
std::vector<uint64_t> h1_cumulative_values_, h2_cumulative_values_, h1_interval_values_,
h2_interval_values_;
Expand Down Expand Up @@ -587,7 +587,7 @@ class ThreadLocalStoreNoMocksTestBase : public testing::Test {

SymbolTablePtr symbol_table_;
AllocatorImpl alloc_;
std::unique_ptr<ThreadLocalStoreImpl> store_;
ThreadLocalStoreImplPtr store_;
StatNamePool pool_;
};

Expand Down Expand Up @@ -1080,7 +1080,7 @@ class StatsThreadLocalStoreTestNoFixture : public testing::Test {
MockSink sink_;
SymbolTablePtr symbol_table_;
std::unique_ptr<AllocatorImpl> alloc_;
std::unique_ptr<ThreadLocalStoreImpl> store_;
ThreadLocalStoreImplPtr store_;
NiceMock<Event::MockDispatcher> main_thread_dispatcher_;
NiceMock<ThreadLocal::MockInstance> tls_;
TestUtil::SymbolTableCreatorTestPeer symbol_table_creator_test_peer_;
Expand Down Expand Up @@ -1514,7 +1514,7 @@ class ClusterShutdownCleanupStarvationTest : public ThreadLocalStoreNoMocksTestB
Event::DispatcherPtr main_dispatcher_;
std::vector<Event::DispatcherPtr> thread_dispatchers_;
Thread::ThreadFactory& thread_factory_;
std::unique_ptr<ThreadLocal::InstanceImpl> tls_;
ThreadLocal::InstanceImplPtr tls_;
Thread::ThreadPtr main_thread_;
std::vector<Thread::ThreadPtr> threads_;
StatNamePool pool_;
Expand Down
2 changes: 1 addition & 1 deletion test/extensions/filters/common/lua/lua_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class LuaTest : public testing::Test {
}

NiceMock<ThreadLocal::MockInstance> tls_;
std::unique_ptr<ThreadLocalState> state_;
ThreadLocalStatePtr state_;
std::function<void()> yield_callback_;
ReadyWatcher on_yield_;
};
Expand Down
2 changes: 1 addition & 1 deletion test/extensions/filters/common/lua/lua_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ template <class T> class LuaWrappersTestBase : public testing::Test {
MOCK_METHOD(void, testPrint, (const std::string&));

NiceMock<ThreadLocal::MockInstance> tls_;
std::unique_ptr<ThreadLocalState> state_;
ThreadLocalStatePtr state_;
std::function<void()> yield_callback_;
CoroutinePtr coroutine_;
};
Expand Down
2 changes: 1 addition & 1 deletion test/server/admin/stats_handler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AdminStatsTest : public testing::TestWithParam<Network::Address::IpVersion
NiceMock<ThreadLocal::MockInstance> tls_;
Stats::AllocatorImpl alloc_;
Stats::MockSink sink_;
std::unique_ptr<Stats::ThreadLocalStoreImpl> store_;
Stats::ThreadLocalStoreImplPtr store_;
};

INSTANTIATE_TEST_SUITE_P(IpVersions, AdminStatsTest,
Expand Down
2 changes: 1 addition & 1 deletion test/server/server_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class ServerInstanceImplTestBase {
testing::NiceMock<MockOptions> options_;
DefaultListenerHooks hooks_;
testing::NiceMock<MockHotRestart> restart_;
std::unique_ptr<ThreadLocal::InstanceImpl> thread_local_;
ThreadLocal::InstanceImplPtr thread_local_;
Stats::TestIsolatedStoreImpl stats_store_;
Thread::MutexBasicLockable fakelock_;
TestComponentFactory component_factory_;
Expand Down