6
6
7
7
namespace NKikimr ::NKqp::NComputeActor {
8
8
9
+
9
10
struct TMemoryQuotaManager : public NYql ::NDq::TGuaranteeQuotaManager {
10
11
11
12
TMemoryQuotaManager (std::shared_ptr<NRm::IKqpResourceManager> resourceManager
@@ -26,7 +27,10 @@ struct TMemoryQuotaManager : public NYql::NDq::TGuaranteeQuotaManager {
26
27
}
27
28
28
29
~TMemoryQuotaManager () override {
29
- State->OnTaskTerminate (TxId, TaskId, Success);
30
+ if (State) {
31
+ State->OnTaskTerminate (TxId, TaskId, Success);
32
+ }
33
+
30
34
ResourceManager->FreeResources (TxId, TaskId);
31
35
}
32
36
@@ -59,6 +63,10 @@ struct TMemoryQuotaManager : public NYql::NDq::TGuaranteeQuotaManager {
59
63
return TotalQueryAllocationsSize >= ReasonableSpillingTreshold;
60
64
}
61
65
66
+ TString MemoryConsumptionDetails () const override {
67
+ return ResourceManager->GetTxResourcesUsageDebugInfo (TxId);
68
+ }
69
+
62
70
void TerminateHandler (bool success, const NYql::TIssues& issues) {
63
71
AFL_DEBUG (NKikimrServices::KQP_COMPUTE)
64
72
(" problem" , " finish_compute_actor" )
@@ -77,66 +85,97 @@ struct TMemoryQuotaManager : public NYql::NDq::TGuaranteeQuotaManager {
77
85
};
78
86
79
87
class TKqpCaFactory : public IKqpNodeComputeActorFactory {
80
- NKikimrConfig::TTableServiceConfig::TResourceManager Config;
81
88
std::shared_ptr<NRm::IKqpResourceManager> ResourceManager_;
82
89
NYql::NDq::IDqAsyncIoFactory::TPtr AsyncIoFactory;
83
90
const std::optional<TKqpFederatedQuerySetup> FederatedQuerySetup;
84
91
92
+ std::atomic<ui64> MkqlLightProgramMemoryLimit = 0 ;
93
+ std::atomic<ui64> MkqlHeavyProgramMemoryLimit = 0 ;
94
+ std::atomic<ui64> MinChannelBufferSize = 0 ;
95
+ std::atomic<ui64> ReasonableSpillingTreshold = 0 ;
96
+
85
97
public:
86
98
TKqpCaFactory (const NKikimrConfig::TTableServiceConfig::TResourceManager& config,
87
99
std::shared_ptr<NRm::IKqpResourceManager> resourceManager,
88
100
NYql::NDq::IDqAsyncIoFactory::TPtr asyncIoFactory,
89
101
const std::optional<TKqpFederatedQuerySetup> federatedQuerySetup)
90
- : Config(config)
91
- , ResourceManager_(resourceManager)
102
+ : ResourceManager_(resourceManager)
92
103
, AsyncIoFactory(asyncIoFactory)
93
104
, FederatedQuerySetup(federatedQuerySetup)
94
- {}
105
+ {
106
+ ApplyConfig (config);
107
+ }
95
108
96
- TActorId CreateKqpComputeActor (const TActorId& executerId, ui64 txId, NYql::NDqProto::TDqTask* dqTask,
97
- const NYql::NDq::TComputeRuntimeSettings& settings,
98
- NWilson::TTraceId traceId, TIntrusivePtr<NActors::TProtoArenaHolder> arena, const TString& serializedGUCSettings,
99
- TComputeStagesWithScan& computesByStage, ui64 outputChunkMaxSize, std::shared_ptr<IKqpNodeState> state,
100
- NRm::EKqpMemoryPool memoryPool, ui32 numberOfTasks)
109
+ void ApplyConfig (const NKikimrConfig::TTableServiceConfig::TResourceManager& config)
101
110
{
111
+ MkqlLightProgramMemoryLimit.store (config.GetMkqlLightProgramMemoryLimit ());
112
+ MkqlHeavyProgramMemoryLimit.store (config.GetMkqlHeavyProgramMemoryLimit ());
113
+ MinChannelBufferSize.store (config.GetMinChannelBufferSize ());
114
+ ReasonableSpillingTreshold.store (config.GetReasonableSpillingTreshold ());
115
+ }
116
+
117
+ TActorStartResult CreateKqpComputeActor (TCreateArgs&& args) {
102
118
NYql::NDq::TComputeMemoryLimits memoryLimits;
103
119
memoryLimits.ChannelBufferSize = 0 ;
104
- memoryLimits.MkqlLightProgramMemoryLimit = Config.GetMkqlLightProgramMemoryLimit ();
105
- memoryLimits.MkqlHeavyProgramMemoryLimit = Config.GetMkqlHeavyProgramMemoryLimit ();
120
+ memoryLimits.MkqlLightProgramMemoryLimit = MkqlLightProgramMemoryLimit.load ();
121
+ memoryLimits.MkqlHeavyProgramMemoryLimit = MkqlHeavyProgramMemoryLimit.load ();
122
+
123
+ auto estimation = ResourceManager_->EstimateTaskResources (*args.Task , args.NumberOfTasks );
124
+ NRm::TKqpResourcesRequest resourcesRequest;
125
+ resourcesRequest.MemoryPool = args.MemoryPool ;
126
+ resourcesRequest.ExecutionUnits = 1 ;
127
+ resourcesRequest.Memory = memoryLimits.MkqlLightProgramMemoryLimit ;
106
128
107
- auto estimation = EstimateTaskResources (*dqTask, Config, numberOfTasks);
129
+ auto rmResult = ResourceManager_->AllocateResources (
130
+ args.TxId , args.Task ->GetId (), resourcesRequest);
131
+
132
+ if (!rmResult) {
133
+ return NRm::TKqpRMAllocateResult{rmResult};
134
+ }
108
135
109
136
{
110
137
ui32 inputChannelsCount = 0 ;
111
- for (auto && i : dqTask ->GetInputs ()) {
138
+ for (auto && i : args. Task ->GetInputs ()) {
112
139
inputChannelsCount += i.ChannelsSize ();
113
140
}
114
141
115
- memoryLimits.ChannelBufferSize = std::max<ui32>(estimation.ChannelBufferMemoryLimit / std::max<ui32>(1 , inputChannelsCount), Config. GetMinChannelBufferSize ());
116
- memoryLimits.OutputChunkMaxSize = outputChunkMaxSize ;
142
+ memoryLimits.ChannelBufferSize = std::max<ui32>(estimation.ChannelBufferMemoryLimit / std::max<ui32>(1 , inputChannelsCount), MinChannelBufferSize. load ());
143
+ memoryLimits.OutputChunkMaxSize = args. OutputChunkMaxSize ;
117
144
AFL_DEBUG (NKikimrServices::KQP_COMPUTE)(" event" , " channel_info" )
118
145
(" ch_size" , estimation.ChannelBufferMemoryLimit )
119
146
(" ch_count" , estimation.ChannelBuffersCount )
120
147
(" ch_limit" , memoryLimits.ChannelBufferSize )
121
- (" inputs" , dqTask ->InputsSize ())
148
+ (" inputs" , args. Task ->InputsSize ())
122
149
(" input_channels_count" , inputChannelsCount);
123
150
}
124
151
125
- auto & taskOpts = dqTask ->GetProgram ().GetSettings ();
152
+ auto & taskOpts = args. Task ->GetProgram ().GetSettings ();
126
153
auto limit = taskOpts.GetHasMapJoin () || taskOpts.GetHasStateAggregation ()
127
154
? memoryLimits.MkqlHeavyProgramMemoryLimit
128
155
: memoryLimits.MkqlLightProgramMemoryLimit ;
129
156
130
157
memoryLimits.MemoryQuotaManager = std::make_shared<TMemoryQuotaManager>(
131
158
ResourceManager_,
132
- memoryPool ,
133
- std::move (state ),
134
- txId ,
135
- dqTask ->GetId (),
159
+ args. MemoryPool ,
160
+ std::move (args. State ),
161
+ args. TxId ,
162
+ args. Task ->GetId (),
136
163
limit,
137
- Config.GetReasonableSpillingTreshold ());
164
+ ReasonableSpillingTreshold.load ());
165
+
166
+ auto runtimeSettings = args.RuntimeSettings ;
167
+ runtimeSettings.ExtraMemoryAllocationPool = args.MemoryPool ;
168
+ runtimeSettings.UseSpilling = args.WithSpilling ;
169
+ runtimeSettings.StatsMode = args.StatsMode ;
170
+
171
+ if (args.Deadline ) {
172
+ runtimeSettings.Timeout = args.Deadline - TAppData::TimeProvider->Now ();
173
+ }
174
+
175
+ if (args.RlPath ) {
176
+ runtimeSettings.RlPath = args.RlPath ;
177
+ }
138
178
139
- auto runtimeSettings = settings;
140
179
NYql::NDq::IMemoryQuotaManager::TWeakPtr memoryQuotaManager = memoryLimits.MemoryQuotaManager ;
141
180
runtimeSettings.TerminateHandler = [memoryQuotaManager]
142
181
(bool success, const NYql::TIssues& issues) {
@@ -157,29 +196,32 @@ class TKqpCaFactory : public IKqpNodeComputeActorFactory {
157
196
};
158
197
159
198
ETableKind tableKind = ETableKind::Unknown;
160
- if (dqTask->HasMetaId ()) {
161
- YQL_ENSURE (computesByStage.GetMetaById (*dqTask, meta) || dqTask->GetMeta ().UnpackTo (&meta), " cannot take meta on MetaId exists in tasks" );
199
+ if (args.Task ->HasMetaId ()) {
200
+ YQL_ENSURE (args.ComputesByStages );
201
+ YQL_ENSURE (args.ComputesByStages ->GetMetaById (*args.Task , meta) || args.Task ->GetMeta ().UnpackTo (&meta), " cannot take meta on MetaId exists in tasks" );
162
202
tableKind = tableKindExtract (meta);
163
- } else if (dqTask ->GetMeta ().UnpackTo (&meta)) {
203
+ } else if (args. Task ->GetMeta ().UnpackTo (&meta)) {
164
204
tableKind = tableKindExtract (meta);
165
205
}
166
206
167
207
if (tableKind == ETableKind::Datashard || tableKind == ETableKind::Olap) {
168
- auto & info = computesByStage.UpsertTaskWithScan (*dqTask, meta, !AppData ()->FeatureFlags .GetEnableSeparationComputeActorsFromRead ());
169
- IActor* computeActor = CreateKqpScanComputeActor (executerId, txId, dqTask,
208
+ YQL_ENSURE (args.ComputesByStages );
209
+ auto & info = args.ComputesByStages ->UpsertTaskWithScan (*args.Task , meta, !AppData ()->FeatureFlags .GetEnableSeparationComputeActorsFromRead ());
210
+ IActor* computeActor = CreateKqpScanComputeActor (args.ExecuterId , args.TxId , args.Task ,
170
211
AsyncIoFactory, runtimeSettings, memoryLimits,
171
- std::move (traceId ), std::move (arena ));
212
+ std::move (args. TraceId ), std::move (args. Arena ));
172
213
TActorId result = TlsActivationContext->Register (computeActor);
173
214
info.MutableActorIds ().emplace_back (result);
174
215
return result;
175
216
} else {
176
217
std::shared_ptr<TGUCSettings> GUCSettings;
177
- if (!serializedGUCSettings .empty ()) {
178
- GUCSettings = std::make_shared<TGUCSettings>(serializedGUCSettings );
218
+ if (!args. SerializedGUCSettings .empty ()) {
219
+ GUCSettings = std::make_shared<TGUCSettings>(args. SerializedGUCSettings );
179
220
}
180
- IActor* computeActor = ::NKikimr::NKqp::CreateKqpComputeActor (executerId, txId, dqTask, AsyncIoFactory,
181
- runtimeSettings, memoryLimits, std::move (traceId), std::move (arena), FederatedQuerySetup, GUCSettings);
182
- return TlsActivationContext->Register (computeActor);
221
+ IActor* computeActor = ::NKikimr::NKqp::CreateKqpComputeActor (args.ExecuterId , args.TxId , args.Task , AsyncIoFactory,
222
+ runtimeSettings, memoryLimits, std::move (args.TraceId ), std::move (args.Arena ), FederatedQuerySetup, GUCSettings);
223
+ return args.ShareMailbox ? TlsActivationContext->AsActorContext ().RegisterWithSameMailbox (computeActor) :
224
+ TlsActivationContext->AsActorContext ().Register (computeActor);
183
225
}
184
226
}
185
227
};
0 commit comments