Skip to content

Commit 0ac9945

Browse files
authored
WriteActror settings (#9251)
1 parent 005ffe6 commit 0ac9945

File tree

7 files changed

+114
-33
lines changed

7 files changed

+114
-33
lines changed

ydb/core/kqp/node_service/kqp_node_service.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <ydb/core/kqp/rm_service/kqp_rm_service.h>
1515
#include <ydb/core/kqp/runtime/kqp_read_actor.h>
1616
#include <ydb/core/kqp/runtime/kqp_read_iterator_common.h>
17+
#include <ydb/core/kqp/runtime/kqp_write_actor_settings.h>
1718
#include <ydb/core/kqp/runtime/kqp_compute_scheduler.h>
1819
#include <ydb/core/kqp/common/kqp_resolve.h>
1920

@@ -81,6 +82,9 @@ class TKqpNodeService : public TActorBootstrapped<TKqpNodeService> {
8182
if (config.HasIteratorReadQuotaSettings()) {
8283
SetIteratorReadsQuotaSettings(config.GetIteratorReadQuotaSettings());
8384
}
85+
if (config.HasWriteActorSettings()) {
86+
SetWriteActorSettings(config.GetWriteActorSettings());
87+
}
8488

8589
SchedulerOptions = {
8690
.AdvanceTimeInterval = TDuration::MicroSeconds(config.GetComputeSchedulerSettings().GetAdvanceTimeIntervalUsec()),
@@ -430,6 +434,24 @@ class TKqpNodeService : public TActorBootstrapped<TKqpNodeService> {
430434
SetReadIteratorBackoffSettings(ptr);
431435
}
432436

437+
void SetWriteActorSettings(const NKikimrConfig::TTableServiceConfig::TWriteActorSettings& settings) {
438+
auto ptr = MakeIntrusive<NKikimr::NKqp::TWriteActorSettings>();
439+
440+
ptr->InFlightMemoryLimitPerActorBytes = settings.GetInFlightMemoryLimitPerActorBytes();
441+
ptr->MemoryLimitPerMessageBytes = settings.GetMemoryLimitPerMessageBytes();
442+
ptr->MaxBatchesPerMessage = settings.GetMaxBatchesPerMessage();
443+
444+
ptr->StartRetryDelay = TDuration::MilliSeconds(settings.GetStartRetryDelayMs());
445+
ptr->MaxRetryDelay = TDuration::MilliSeconds(settings.GetMaxRetryDelayMs());
446+
ptr->UnsertaintyRatio = settings.GetUnsertaintyRatio();
447+
ptr->Multiplier = settings.GetMultiplier();
448+
449+
ptr->MaxWriteAttempts = settings.GetMaxWriteAttempts();
450+
ptr->MaxResolveAttempts = settings.GetMaxResolveAttempts();
451+
452+
NKikimr::NKqp::SetWriteActorSettings(ptr);
453+
}
454+
433455
void HandleWork(TEvents::TEvUndelivered::TPtr& ev) {
434456
switch (ev->Get()->SourceType) {
435457
case TEvKqpNode::TEvStartKqpTasksResponse::EventType: {

ydb/core/kqp/runtime/kqp_write_actor.cpp

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "kqp_write_actor.h"
22

33
#include "kqp_write_table.h"
4+
#include "kqp_write_actor_settings.h"
45

56
#include <util/generic/singleton.h>
67
#include <ydb/core/actorlib_impl/long_timer.h>
@@ -23,32 +24,14 @@
2324

2425

2526
namespace {
26-
constexpr i64 kInFlightMemoryLimitPerActor = 64_MB;
27-
constexpr i64 kMemoryLimitPerMessage = 64_MB;
28-
constexpr i64 kMaxBatchesPerMessage = 8;
29-
30-
struct TWriteActorBackoffSettings {
31-
TDuration StartRetryDelay = TDuration::MilliSeconds(250);
32-
TDuration MaxRetryDelay = TDuration::Seconds(5);
33-
double UnsertaintyRatio = 0.5;
34-
double Multiplier = 2.0;
35-
36-
ui64 MaxWriteAttempts = 32;
37-
ui64 MaxResolveAttempts = 5;
38-
};
39-
40-
const TWriteActorBackoffSettings* BackoffSettings() {
41-
return Singleton<TWriteActorBackoffSettings>();
42-
}
43-
44-
TDuration CalculateNextAttemptDelay(ui64 attempt) {
45-
auto delay = BackoffSettings()->StartRetryDelay;
27+
TDuration CalculateNextAttemptDelay(const NKikimr::NKqp::TWriteActorSettings& settings, ui64 attempt) {
28+
auto delay = settings.StartRetryDelay;
4629
for (ui64 index = 0; index < attempt; ++index) {
47-
delay *= BackoffSettings()->Multiplier;
30+
delay *= settings.Multiplier;
4831
}
4932

50-
delay *= 1 + BackoffSettings()->UnsertaintyRatio * (1 - 2 * RandomNumber<double>());
51-
delay = Min(delay, BackoffSettings()->MaxRetryDelay);
33+
delay *= 1 + settings.UnsertaintyRatio * (1 - 2 * RandomNumber<double>());
34+
delay = Min(delay, settings.MaxRetryDelay);
5235

5336
return delay;
5437
}
@@ -133,6 +116,7 @@ class TKqpDirectWriteActor : public TActorBootstrapped<TKqpDirectWriteActor>, pu
133116
TIntrusivePtr<TKqpCounters> counters)
134117
: LogPrefix(TStringBuilder() << "TxId: " << args.TxId << ", task: " << args.TaskId << ". ")
135118
, Settings(std::move(settings))
119+
, MessageSettings(GetWriteActorSettings())
136120
, OutputIndex(args.OutputIndex)
137121
, Callbacks(args.Callback)
138122
, Counters(counters)
@@ -149,6 +133,7 @@ class TKqpDirectWriteActor : public TActorBootstrapped<TKqpDirectWriteActor>, pu
149133
Settings.GetImmediateTx())
150134
, InconsistentTx(
151135
Settings.GetInconsistentTx())
136+
, MemoryLimit(MessageSettings.InFlightMemoryLimitPerActorBytes)
152137
{
153138
YQL_ENSURE(std::holds_alternative<ui64>(TxId));
154139
YQL_ENSURE(!ImmediateTx);
@@ -248,9 +233,9 @@ class TKqpDirectWriteActor : public TActorBootstrapped<TKqpDirectWriteActor>, pu
248233
}
249234

250235
void PlanResolveTable() {
251-
CA_LOG_D("Plan resolve with delay " << CalculateNextAttemptDelay(ResolveAttempts));
236+
CA_LOG_D("Plan resolve with delay " << CalculateNextAttemptDelay(MessageSettings, ResolveAttempts));
252237
TlsActivationContext->Schedule(
253-
CalculateNextAttemptDelay(ResolveAttempts),
238+
CalculateNextAttemptDelay(MessageSettings, ResolveAttempts),
254239
new IEventHandle(SelfId(), SelfId(), new TEvPrivate::TEvResolveRequestPlanned{}, 0, 0));
255240
}
256241

@@ -262,7 +247,7 @@ class TKqpDirectWriteActor : public TActorBootstrapped<TKqpDirectWriteActor>, pu
262247
SchemeEntry.reset();
263248
SchemeRequest.reset();
264249

265-
if (ResolveAttempts++ >= BackoffSettings()->MaxResolveAttempts) {
250+
if (ResolveAttempts++ >= MessageSettings.MaxResolveAttempts) {
266251
CA_LOG_E(TStringBuilder()
267252
<< "Too many table resolve attempts for table " << TableId << ".");
268253
RuntimeError(
@@ -596,7 +581,7 @@ class TKqpDirectWriteActor : public TActorBootstrapped<TKqpDirectWriteActor>, pu
596581
void SendDataToShard(const ui64 shardId) {
597582
const auto metadata = ShardedWriteController->GetMessageMetadata(shardId);
598583
YQL_ENSURE(metadata);
599-
if (metadata->SendAttempts >= BackoffSettings()->MaxWriteAttempts) {
584+
if (metadata->SendAttempts >= MessageSettings.MaxWriteAttempts) {
600585
CA_LOG_E("ShardId=" << shardId
601586
<< " for table '" << Settings.GetTable().GetPath()
602587
<< "': retry limit exceeded."
@@ -666,7 +651,7 @@ class TKqpDirectWriteActor : public TActorBootstrapped<TKqpDirectWriteActor>, pu
666651

667652
if (InconsistentTx) {
668653
TlsActivationContext->Schedule(
669-
CalculateNextAttemptDelay(metadata->SendAttempts),
654+
CalculateNextAttemptDelay(MessageSettings, metadata->SendAttempts),
670655
new IEventHandle(
671656
SelfId(),
672657
SelfId(),
@@ -760,11 +745,11 @@ class TKqpDirectWriteActor : public TActorBootstrapped<TKqpDirectWriteActor>, pu
760745
try {
761746
ShardedWriteController = CreateShardedWriteController(
762747
TShardedWriteControllerSettings {
763-
.MemoryLimitTotal = kInFlightMemoryLimitPerActor,
764-
.MemoryLimitPerMessage = kMemoryLimitPerMessage,
748+
.MemoryLimitTotal = MessageSettings.InFlightMemoryLimitPerActorBytes,
749+
.MemoryLimitPerMessage = MessageSettings.MemoryLimitPerMessageBytes,
765750
.MaxBatchesPerMessage = (SchemeEntry->Kind == NSchemeCache::TSchemeCacheNavigate::KindColumnTable
766751
? 1
767-
: kMaxBatchesPerMessage),
752+
: MessageSettings.MaxBatchesPerMessage),
768753
},
769754
std::move(columnsMetadata),
770755
TypeEnv,
@@ -800,6 +785,7 @@ class TKqpDirectWriteActor : public TActorBootstrapped<TKqpDirectWriteActor>, pu
800785

801786
TString LogPrefix;
802787
const NKikimrKqp::TKqpTableSinkSettings Settings;
788+
TWriteActorSettings MessageSettings;
803789
const ui64 OutputIndex;
804790
NYql::NDq::TDqAsyncStats EgressStats;
805791
NYql::NDq::IDqComputeActorAsyncOutput::ICallbacks * Callbacks = nullptr;
@@ -820,7 +806,7 @@ class TKqpDirectWriteActor : public TActorBootstrapped<TKqpDirectWriteActor>, pu
820806
THashMap<ui64, TLockInfo> LocksInfo;
821807
bool Finished = false;
822808

823-
const i64 MemoryLimit = kInFlightMemoryLimitPerActor;
809+
const i64 MemoryLimit;
824810

825811
IShardedWriteControllerPtr ShardedWriteController = nullptr;
826812
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "kqp_write_actor_settings.h"
2+
3+
#include <library/cpp/threading/hot_swap/hot_swap.h>
4+
#include <util/generic/singleton.h>
5+
6+
7+
namespace NKikimr {
8+
namespace NKqp {
9+
10+
struct TWriteActorDefaultSettings {
11+
THotSwap<TWriteActorSettings> SettingsPtr;
12+
13+
TWriteActorDefaultSettings() {
14+
SettingsPtr.AtomicStore(new TWriteActorSettings());
15+
}
16+
17+
};
18+
19+
TWriteActorSettings GetWriteActorSettings() {
20+
return *Singleton<TWriteActorDefaultSettings>()->SettingsPtr.AtomicLoad();
21+
}
22+
23+
void SetWriteActorSettings(TIntrusivePtr<TWriteActorSettings> ptr) {
24+
Singleton<TWriteActorDefaultSettings>()->SettingsPtr.AtomicStore(ptr);
25+
}
26+
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include <util/generic/ptr.h>
4+
#include <util/datetime/base.h>
5+
#include <util/generic/size_literals.h>
6+
7+
namespace NKikimr {
8+
namespace NKqp {
9+
10+
struct TWriteActorSettings : TAtomicRefCount<TWriteActorSettings> {
11+
i64 InFlightMemoryLimitPerActorBytes = 64_MB;
12+
i64 MemoryLimitPerMessageBytes = 64_MB;
13+
i64 MaxBatchesPerMessage = 1000;
14+
15+
TDuration StartRetryDelay = TDuration::Seconds(1);
16+
TDuration MaxRetryDelay = TDuration::Seconds(10);
17+
double UnsertaintyRatio = 0.5;
18+
double Multiplier = 2.0;
19+
20+
ui64 MaxWriteAttempts = 100;
21+
ui64 MaxResolveAttempts = 5;
22+
};
23+
24+
TWriteActorSettings GetWriteActorSettings();
25+
void SetWriteActorSettings(TIntrusivePtr<TWriteActorSettings> ptr);
26+
27+
}
28+
}

ydb/core/kqp/runtime/kqp_write_table.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace NKqp {
1818
namespace {
1919

2020
constexpr ui64 DataShardMaxOperationBytes = 8_MB;
21-
constexpr ui64 ColumnShardMaxOperationBytes = 8_MB;
21+
constexpr ui64 ColumnShardMaxOperationBytes = 64_MB;
2222
constexpr ui64 MaxUnshardedBatchBytes = 0_MB;
2323

2424
class IPayloadSerializer : public TThrRefBase {

ydb/core/kqp/runtime/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ SRCS(
2222
kqp_stream_lookup_worker.h
2323
kqp_tasks_runner.cpp
2424
kqp_transport.cpp
25+
kqp_write_actor_settings.cpp
2526
kqp_write_actor.cpp
2627
kqp_write_table.cpp
2728
)

ydb/core/protos/table_service_config.proto

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,20 @@ message TTableServiceConfig {
323323
optional bool EnableRowsDuplicationCheck = 69 [ default = false ];
324324

325325
optional bool EnableHtapTx = 71 [default = true];
326+
327+
message TWriteActorSettings {
328+
optional uint64 InFlightMemoryLimitPerActorBytes = 1 [ default = 67108864 ];
329+
optional uint64 MemoryLimitPerMessageBytes = 2 [ default = 67108864 ];
330+
optional uint64 MaxBatchesPerMessage = 3 [ default = 1000 ];
331+
332+
optional uint64 StartRetryDelayMs = 4 [ default = 1000 ];
333+
optional uint64 MaxRetryDelayMs = 5 [ default = 10000 ];
334+
optional double UnsertaintyRatio = 6 [ default = 0.5 ];
335+
optional double Multiplier = 7 [ default = 2.0 ];
336+
337+
optional uint64 MaxWriteAttempts = 8 [ default = 100 ];
338+
optional uint64 MaxResolveAttempts = 9 [ default = 5 ];
339+
}
340+
341+
optional TWriteActorSettings WriteActorSettings = 72;
326342
};

0 commit comments

Comments
 (0)