forked from manticoresoftware/manticoresearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_dispatcher.cpp
335 lines (271 loc) · 8.44 KB
/
task_dispatcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//
// Copyright (c) 2022-2023, Manticore Software LTD (https://manticoresearch.com)
// All rights reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. You should have
// received a copy of the GPL license along with this program; if you
// did not, you can find it at http://www.gnu.org
//
#include "task_dispatcher.h"
#include "coroutine.h"
#include "client_task_info.h"
#include <atomic>
#include <cassert>
// artificial race right after source creation.
// That is, quite seldom race between 'make source' and 'clone context' will surely happen in debug build
#ifndef NDEBUG
#define MAKE_RACE 1
#else
#define MAKE_RACE 0
#endif
namespace {
inline int CalcConcurrency ( int iConcurrency )
{
if ( iConcurrency )
return iConcurrency;
iConcurrency = GetEffectiveDistThreads();
if ( iConcurrency )
return iConcurrency;
return Threads::NThreads();
}
}
/////////////////////////////////////////////////////////////////////////////
/// trivial concurrent task dispatcher
/////////////////////////////////////////////////////////////////////////////
class ConcurrentTaskDispatcher_c;
class SingleTaskSource_c final : public Dispatcher::TaskSource_i
{
ConcurrentTaskDispatcher_c& m_tParent;
const int m_iMaxJob;
public:
SingleTaskSource_c ( ConcurrentTaskDispatcher_c& tParent, int iJobs );
bool FetchTask ( int& iTask ) final;
};
class ConcurrentTaskDispatcher_c final: public Dispatcher::TaskDispatcher_i
{
const int m_iJobs;
const int m_iConcurrency;
std::atomic<int32_t> m_iCurrentJob;
public:
ConcurrentTaskDispatcher_c ( int iJobs, int iConcurrency );
int GetConcurrency() const final;
std::unique_ptr<Dispatcher::TaskSource_i> MakeSource() final;
int GetNextConcurrentTask();
};
SingleTaskSource_c::SingleTaskSource_c ( ConcurrentTaskDispatcher_c& tParent, int iJobs )
: m_tParent ( tParent )
, m_iMaxJob ( iJobs - 1 )
{}
bool SingleTaskSource_c::FetchTask ( int& iTask )
{
if ( iTask>=0 )
return true; // previous value wasn't yet consumed
auto iNextTask = m_tParent.GetNextConcurrentTask();
if ( iNextTask < 0 )
return false;
iTask = m_iMaxJob - iNextTask;
return true;
}
ConcurrentTaskDispatcher_c::ConcurrentTaskDispatcher_c ( int iJobs, int iConcurrency )
: m_iJobs ( iJobs )
, m_iConcurrency ( CalcConcurrency ( iConcurrency ) )
, m_iCurrentJob { iJobs - 1 }
{}
int ConcurrentTaskDispatcher_c::GetConcurrency() const
{
return m_iConcurrency;
}
std::unique_ptr<Dispatcher::TaskSource_i> ConcurrentTaskDispatcher_c::MakeSource()
{
auto pSource = std::make_unique<SingleTaskSource_c> ( *this, m_iJobs );
#if MAKE_RACE
Threads::Coro::Reschedule();
#endif
return pSource;
}
int ConcurrentTaskDispatcher_c::GetNextConcurrentTask()
{
return m_iCurrentJob.fetch_sub ( 1, std::memory_order_relaxed );
}
/////////////////////////////////////////////////////////////////////////////
/// round-robin task dispatcher
/////////////////////////////////////////////////////////////////////////////
class RRTaskSource_c final: public Dispatcher::TaskSource_i
{
const int m_iJobs;
const int m_iBatch;
const int m_iStride;
const int m_iOffset;
const bool m_bIdle;
int m_iMySerie = 0;
int m_iJobInSerie = 0;
public:
RRTaskSource_c ( int iJobs, int iFiber, int iBatch, int iFibers );
bool FetchTask ( int& iTask ) final;
};
RRTaskSource_c::RRTaskSource_c ( int iJobs, int iFiber, int iBatch, int iFibers )
: m_iJobs ( iJobs )
, m_iBatch ( iBatch )
, m_iStride ( iFibers * iBatch )
, m_iOffset ( iFiber * iBatch )
, m_bIdle ( iFiber >= iFibers )
{}
bool RRTaskSource_c::FetchTask ( int& iTask )
{
if ( iTask >= 0 )
return true; // previous value wasn't yet consumed
if ( m_bIdle )
return false;
auto iJob = m_iMySerie * m_iStride + m_iOffset + m_iJobInSerie;
if ( iJob >= m_iJobs )
return false;
iTask = iJob;
++m_iJobInSerie;
if ( m_iJobInSerie >= m_iBatch )
{
m_iJobInSerie = 0;
++m_iMySerie;
}
return true;
}
class NullTaskSource_c final: public Dispatcher::TaskSource_i
{
public:
bool FetchTask ( int& iTask ) final;
};
bool NullTaskSource_c::FetchTask ( int& iTask )
{
return iTask >= 0;
}
class RRTaskDispatcher_c final: public Dispatcher::TaskDispatcher_i
{
const int m_iJobs;
const int m_iConcurrency;
const int m_iBatch;
std::atomic<int32_t> m_iNextFiber {0};
public:
RRTaskDispatcher_c ( int iJobs, int iConcurrency, int iBatch );
int GetConcurrency() const final;
std::unique_ptr<Dispatcher::TaskSource_i> MakeSource() final;
};
RRTaskDispatcher_c::RRTaskDispatcher_c ( int iJobs, int iConcurrency, int iBatch )
: m_iJobs ( iJobs )
, m_iConcurrency ( CalcConcurrency ( iConcurrency ) )
, m_iBatch ( iBatch )
{}
int RRTaskDispatcher_c::GetConcurrency() const
{
return m_iConcurrency;
}
std::unique_ptr<Dispatcher::TaskSource_i> RRTaskDispatcher_c::MakeSource()
{
int iFiber = m_iNextFiber.fetch_add ( 1, std::memory_order_relaxed );
if ( iFiber >= m_iConcurrency )
return std::make_unique<NullTaskSource_c>();
auto pSource = std::make_unique<RRTaskSource_c> ( m_iJobs, iFiber, m_iBatch, m_iConcurrency );
#if MAKE_RACE
Threads::Coro::Reschedule();
#endif
return pSource;
}
namespace Dispatcher {
std::unique_ptr<TaskDispatcher_i> MakeTrivial ( int iJobs, int iConcurrency )
{
return std::make_unique<ConcurrentTaskDispatcher_c> ( iJobs, iConcurrency );
}
std::unique_ptr<TaskDispatcher_i> MakeRoundRobin ( int iJobs, int iConcurrency, int iBatch )
{
return std::make_unique<RRTaskDispatcher_c> ( iJobs, iConcurrency, iBatch );
}
std::unique_ptr<Dispatcher::TaskDispatcher_i> Make ( int iJobs, int iDefaultConcurrency, Dispatcher::Template_t tWhat, bool bSingle )
{
if ( bSingle )
iDefaultConcurrency = 1;
else if ( tWhat.concurrency )
iDefaultConcurrency = tWhat.concurrency;
if ( tWhat.batch )
return MakeRoundRobin ( iJobs, iDefaultConcurrency, tWhat.batch );
return MakeTrivial ( iJobs, iDefaultConcurrency );
}
Template_t ParseTemplate ( Str_t sTemplate )
{
StrtVec_t dStr;
sph::Split ( dStr, sTemplate.first, sTemplate.second, "/" );
dStr.for_each ( [] ( auto& tVal ) { tVal = sph::Trim ( tVal ); } );
while ( dStr.GetLength()<2 )
dStr.Add ( dEmptyStr );
auto fnParseValue = [] ( Str_t tVal ) -> int
{
if ( IsEmpty ( tVal ) )
return 0;
if ( tVal.second == 1 && ( *tVal.first == '0' || *tVal.first == '*' ) )
return 0;
return atoi ( CSphString ( tVal ).cstr() );
};
assert ( dStr.GetLength() >= 2 );
return { fnParseValue ( dStr[0] ), fnParseValue ( dStr[1] ) };
}
Template_t ParseTemplate ( const char* szTemplate )
{
return ParseTemplate ( FromSz ( szTemplate ) );
}
std::pair<Template_t, Template_t> ParseTemplates ( Str_t sTemplates )
{
sTemplates = sph::Trim ( sTemplates, '\'' ); // unquote, if necessary.
StrtVec_t dStr;
sph::Split ( dStr, sTemplates.first, sTemplates.second, "+" );
while ( dStr.GetLength() < 2 )
dStr.Add ( dEmptyStr );
assert ( dStr.GetLength() >= 2 );
return { ParseTemplate ( dStr[0] ), ParseTemplate ( dStr[1] ) };
}
std::pair<Template_t, Template_t> ParseTemplates ( const char* szTemplates )
{
return ParseTemplates ( FromSz ( szTemplates ) );
}
namespace {
std::pair<Template_t, Template_t> g_GlobalDispatcherTemplates = ParseTemplates ( getenv ( "MANTICORE_THREADS_EX" ) );
}
void SetGlobalDispatchers ( const char* szTemplates )
{
g_GlobalDispatcherTemplates = ParseTemplates ( szTemplates );
}
// return stored global dispatchers
Template_t GetGlobalBaseDispatcherTemplate()
{
return g_GlobalDispatcherTemplates.first;
}
Template_t GetGlobalPseudoShardingDispatcherTemplate()
{
return g_GlobalDispatcherTemplates.second;
}
void Unify ( Template_t& tBase, const Template_t tNew )
{
if ( tNew.concurrency )
tBase.concurrency = tNew.concurrency;
if ( tNew.batch )
tBase.batch = tNew.batch;
}
CSphString RenderTemplate ( Template_t tTemplate )
{
if ( tTemplate.concurrency && tTemplate.batch )
return SphSprintf ("%d/%d", tTemplate.concurrency, tTemplate.batch );
if ( tTemplate.concurrency )
return SphSprintf ( "%d", tTemplate.concurrency );
if ( tTemplate.batch )
return SphSprintf ( "/%d", tTemplate.batch );
return "";
}
void RenderTemplates ( StringBuilder_c& tOut, std::pair<Template_t, Template_t> dTemplates )
{
auto sFirst = RenderTemplate ( dTemplates.first );
auto sSecond = RenderTemplate ( dTemplates.second );
if ( !sFirst.IsEmpty() && !sSecond.IsEmpty() )
return (void)tOut.Sprint ( sFirst, '+', sSecond );
if ( !sFirst.IsEmpty() )
return (void)tOut.Sprint ( sFirst );
if ( !sSecond.IsEmpty() )
return (void)tOut.Sprint ( '+', sSecond );
}
} // namespace Dispatcher