aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexey Borzenkov <snaury@yandex-team.ru>2022-03-18 00:56:45 +0300
committerAlexey Borzenkov <snaury@yandex-team.ru>2022-03-18 00:56:45 +0300
commit2fbdf1c88f149f6e23c2363c649cda6f8ff21c92 (patch)
tree08a155f1ac2475bc52552a138044722e7a16b23b
parent47f097013d31b097f97ed0981092273bcc79ae58 (diff)
downloadydb-2fbdf1c88f149f6e23c2363c649cda6f8ff21c92.tar.gz
Add monotonic time provider, KIKIMR-13910
ref:72823653cdfd5fb04f1f1a001374fc120b3da7af
-rw-r--r--library/cpp/actors/core/CMakeLists.txt1
-rw-r--r--library/cpp/actors/core/monotonic_provider.cpp16
-rw-r--r--library/cpp/actors/core/monotonic_provider.h14
-rw-r--r--library/cpp/actors/testlib/test_runtime.cpp26
-rw-r--r--library/cpp/actors/testlib/test_runtime.h5
-rw-r--r--ydb/core/base/appdata.cpp1
-rw-r--r--ydb/core/base/appdata.h2
-rw-r--r--ydb/core/testlib/actors/test_runtime.cpp3
-rw-r--r--ydb/core/util/testactorsys.cpp8
-rw-r--r--ydb/core/util/testactorsys.h3
10 files changed, 79 insertions, 0 deletions
diff --git a/library/cpp/actors/core/CMakeLists.txt b/library/cpp/actors/core/CMakeLists.txt
index a20646c7cc..64c617307c 100644
--- a/library/cpp/actors/core/CMakeLists.txt
+++ b/library/cpp/actors/core/CMakeLists.txt
@@ -50,6 +50,7 @@ target_sources(cpp-actors-core PRIVATE
${CMAKE_SOURCE_DIR}/library/cpp/actors/core/memory_track.cpp
${CMAKE_SOURCE_DIR}/library/cpp/actors/core/memory_tracker.cpp
${CMAKE_SOURCE_DIR}/library/cpp/actors/core/monotonic.cpp
+ ${CMAKE_SOURCE_DIR}/library/cpp/actors/core/monotonic_provider.cpp
${CMAKE_SOURCE_DIR}/library/cpp/actors/core/worker_context.cpp
${CMAKE_SOURCE_DIR}/library/cpp/actors/core/probes.cpp
${CMAKE_SOURCE_DIR}/library/cpp/actors/core/process_stats.cpp
diff --git a/library/cpp/actors/core/monotonic_provider.cpp b/library/cpp/actors/core/monotonic_provider.cpp
new file mode 100644
index 0000000000..fb3b656da6
--- /dev/null
+++ b/library/cpp/actors/core/monotonic_provider.cpp
@@ -0,0 +1,16 @@
+#include "monotonic_provider.h"
+
+namespace NActors {
+
+class TDefaultMonotonicTimeProvider : public IMonotonicTimeProvider {
+public:
+ TMonotonic Now() override {
+ return TMonotonic::Now();
+ }
+};
+
+TIntrusivePtr<IMonotonicTimeProvider> CreateDefaultMonotonicTimeProvider() {
+ return TIntrusivePtr<IMonotonicTimeProvider>(new TDefaultMonotonicTimeProvider);
+}
+
+} // namespace NActors
diff --git a/library/cpp/actors/core/monotonic_provider.h b/library/cpp/actors/core/monotonic_provider.h
new file mode 100644
index 0000000000..98e1203400
--- /dev/null
+++ b/library/cpp/actors/core/monotonic_provider.h
@@ -0,0 +1,14 @@
+#pragma once
+
+#include "monotonic.h"
+
+namespace NActors {
+
+class IMonotonicTimeProvider : public TThrRefBase {
+public:
+ virtual TMonotonic Now() = 0;
+};
+
+TIntrusivePtr<IMonotonicTimeProvider> CreateDefaultMonotonicTimeProvider();
+
+} // namespace NActors
diff --git a/library/cpp/actors/testlib/test_runtime.cpp b/library/cpp/actors/testlib/test_runtime.cpp
index 6fa25b9965..51d93ba6e9 100644
--- a/library/cpp/actors/testlib/test_runtime.cpp
+++ b/library/cpp/actors/testlib/test_runtime.cpp
@@ -233,6 +233,20 @@ namespace NActors {
TTestActorRuntimeBase& Runtime;
};
+ class TTestActorRuntimeBase::TMonotonicTimeProvider : public IMonotonicTimeProvider {
+ public:
+ TMonotonicTimeProvider(TTestActorRuntimeBase& runtime)
+ : Runtime(runtime)
+ { }
+
+ TMonotonic Now() override {
+ return Runtime.GetCurrentMonotonicTime();
+ }
+
+ private:
+ TTestActorRuntimeBase& Runtime;
+ };
+
class TTestActorRuntimeBase::TSchedulerThreadStub : public ISchedulerThread {
public:
TSchedulerThreadStub(TTestActorRuntimeBase* runtime, TTestActorRuntimeBase::TNodeDataBase* node)
@@ -470,6 +484,7 @@ namespace NActors {
, NeedMonitoring(false)
, RandomProvider(CreateDeterministicRandomProvider(DefaultRandomSeed))
, TimeProvider(new TTimeProvider(*this))
+ , MonotonicTimeProvider(new TMonotonicTimeProvider(*this))
, ShouldContinue()
, CurrentTimestamp(0)
, DispatchTimeout(DEFAULT_DISPATCH_TIMEOUT)
@@ -797,6 +812,12 @@ namespace NActors {
return TInstant::MicroSeconds(CurrentTimestamp);
}
+ TMonotonic TTestActorRuntimeBase::GetCurrentMonotonicTime() const {
+ TGuard<TMutex> guard(Mutex);
+ Y_VERIFY(!UseRealThreads);
+ return TMonotonic::MicroSeconds(CurrentTimestamp);
+ }
+
void TTestActorRuntimeBase::UpdateCurrentTime(TInstant newTime) {
static int counter = 0;
++counter;
@@ -823,6 +844,11 @@ namespace NActors {
return TimeProvider;
}
+ TIntrusivePtr<IMonotonicTimeProvider> TTestActorRuntimeBase::GetMonotonicTimeProvider() {
+ Y_VERIFY(!UseRealThreads);
+ return MonotonicTimeProvider;
+ }
+
ui32 TTestActorRuntimeBase::GetNodeId(ui32 index) const {
Y_VERIFY(index < NodeCount);
return FirstNodeId + index;
diff --git a/library/cpp/actors/testlib/test_runtime.h b/library/cpp/actors/testlib/test_runtime.h
index 26e3b45c98..c14cde1b66 100644
--- a/library/cpp/actors/testlib/test_runtime.h
+++ b/library/cpp/actors/testlib/test_runtime.h
@@ -6,6 +6,7 @@
#include <library/cpp/actors/core/events.h>
#include <library/cpp/actors/core/executor_thread.h>
#include <library/cpp/actors/core/mailbox.h>
+#include <library/cpp/actors/core/monotonic_provider.h>
#include <library/cpp/actors/util/should_continue.h>
#include <library/cpp/actors/interconnect/poller_tcp.h>
#include <library/cpp/actors/interconnect/mock/ic_mock.h>
@@ -188,6 +189,7 @@ namespace NActors {
class TSchedulerThreadStub;
class TExecutorPoolStub;
class TTimeProvider;
+ class TMonotonicTimeProvider;
enum class EEventAction {
PROCESS,
@@ -229,7 +231,9 @@ namespace NActors {
void SetLogBackend(const TAutoPtr<TLogBackend> logBackend);
void SetLogPriority(NActors::NLog::EComponent component, NActors::NLog::EPriority priority);
TIntrusivePtr<ITimeProvider> GetTimeProvider();
+ TIntrusivePtr<IMonotonicTimeProvider> GetMonotonicTimeProvider();
TInstant GetCurrentTime() const;
+ TMonotonic GetCurrentMonotonicTime() const;
void UpdateCurrentTime(TInstant newTime);
void AdvanceCurrentTime(TDuration duration);
void AddLocalService(const TActorId& actorId, const TActorSetupCmd& cmd, ui32 nodeIndex = 0);
@@ -534,6 +538,7 @@ namespace NActors {
TIntrusivePtr<IRandomProvider> RandomProvider;
TIntrusivePtr<ITimeProvider> TimeProvider;
+ TIntrusivePtr<IMonotonicTimeProvider> MonotonicTimeProvider;
protected:
struct TNodeDataBase: public TThrRefBase {
diff --git a/ydb/core/base/appdata.cpp b/ydb/core/base/appdata.cpp
index f9e517fc42..b2b77e9e88 100644
--- a/ydb/core/base/appdata.cpp
+++ b/ydb/core/base/appdata.cpp
@@ -19,6 +19,7 @@ TAppData::TAppData(
, TypeRegistry(typeRegistry)
, FunctionRegistry(functionRegistry)
, FormatFactory(formatFactory)
+ , MonotonicTimeProvider(CreateDefaultMonotonicTimeProvider())
, ProxySchemeCacheNodes(Max<ui64>() / 4)
, ProxySchemeCacheDistrNodes(Max<ui64>() / 4)
, CompilerSchemeCachePaths(Max<ui64>() / 4)
diff --git a/ydb/core/base/appdata.h b/ydb/core/base/appdata.h
index 2dae5a954b..c48d712dae 100644
--- a/ydb/core/base/appdata.h
+++ b/ydb/core/base/appdata.h
@@ -21,6 +21,7 @@
#include <library/cpp/actors/interconnect/poller_tcp.h>
#include <library/cpp/actors/core/executor_thread.h>
+#include <library/cpp/actors/core/monotonic_provider.h>
#include <library/cpp/actors/util/should_continue.h>
#include <library/cpp/random_provider/random_provider.h>
#include <library/cpp/time_provider/time_provider.h>
@@ -111,6 +112,7 @@ struct TAppData {
static TIntrusivePtr<IRandomProvider> RandomProvider;
static TIntrusivePtr<ITimeProvider> TimeProvider;
+ TIntrusivePtr<IMonotonicTimeProvider> MonotonicTimeProvider;
TIntrusivePtr<TDomainsInfo> DomainsInfo;
TIntrusivePtr<TChannelProfiles> ChannelProfiles;
TIntrusivePtr<TDynamicNameserviceConfig> DynamicNameserviceConfig;
diff --git a/ydb/core/testlib/actors/test_runtime.cpp b/ydb/core/testlib/actors/test_runtime.cpp
index 12e37f4d87..ff09f2df27 100644
--- a/ydb/core/testlib/actors/test_runtime.cpp
+++ b/ydb/core/testlib/actors/test_runtime.cpp
@@ -120,6 +120,9 @@ namespace NActors {
node->LogSettings->MessagePrefix = " node " + ToString(nodeId);
auto* nodeAppData = node->GetAppData<NKikimr::TAppData>();
+ if (!UseRealThreads) {
+ nodeAppData->MonotonicTimeProvider = MonotonicTimeProvider;
+ }
nodeAppData->DataShardExportFactory = app0->DataShardExportFactory;
nodeAppData->DomainsInfo = app0->DomainsInfo;
nodeAppData->ChannelProfiles = app0->ChannelProfiles;
diff --git a/ydb/core/util/testactorsys.cpp b/ydb/core/util/testactorsys.cpp
index 73d27bc9d9..4a270e4f8c 100644
--- a/ydb/core/util/testactorsys.cpp
+++ b/ydb/core/util/testactorsys.cpp
@@ -208,4 +208,12 @@ TIntrusivePtr<ITimeProvider> TTestActorSystem::CreateTimeProvider() {
return MakeIntrusive<TTestActorTimeProvider>();
}
+TIntrusivePtr<IMonotonicTimeProvider> TTestActorSystem::CreateMonotonicTimeProvider() {
+ class TTestActorMonotonicTimeProvider : public IMonotonicTimeProvider {
+ public:
+ TMonotonic Now() override { return TMonotonic::MicroSeconds(CurrentTestActorSystem->Clock.MicroSeconds()); }
+ };
+ return MakeIntrusive<TTestActorMonotonicTimeProvider>();
+}
+
}
diff --git a/ydb/core/util/testactorsys.h b/ydb/core/util/testactorsys.h
index ce6ad7be10..722d5a8163 100644
--- a/ydb/core/util/testactorsys.h
+++ b/ydb/core/util/testactorsys.h
@@ -187,6 +187,8 @@ public:
Y_VERIFY(!CurrentTestActorSystem);
CurrentTestActorSystem = this;
+
+ AppData.MonotonicTimeProvider = CreateMonotonicTimeProvider();
}
~TTestActorSystem() {
@@ -195,6 +197,7 @@ public:
}
static TIntrusivePtr<ITimeProvider> CreateTimeProvider();
+ static TIntrusivePtr<IMonotonicTimeProvider> CreateMonotonicTimeProvider();
TAppData *GetAppData() {
return &AppData;