-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUserCacheWarmerCommand.php
More file actions
388 lines (315 loc) · 12.3 KB
/
Copy pathUserCacheWarmerCommand.php
File metadata and controls
388 lines (315 loc) · 12.3 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies S.A.
*/
declare(strict_types=1);
namespace OAT\SimpleRoster\Command\Cache;
use Doctrine\ORM\ORMException;
use InvalidArgumentException;
use OAT\SimpleRoster\Command\BlackfireProfilerTrait;
use OAT\SimpleRoster\Command\CommandProgressBarFormatterTrait;
use OAT\SimpleRoster\Repository\Criteria\EuclideanDivisionCriterion;
use OAT\SimpleRoster\Repository\Criteria\FindUserCriteria;
use OAT\SimpleRoster\Repository\UserRepository;
use OAT\SimpleRoster\Service\Cache\UserCacheWarmerService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
class UserCacheWarmerCommand extends Command
{
use CommandProgressBarFormatterTrait;
use BlackfireProfilerTrait;
public const NAME = 'roster:cache-warmup:user';
private const OPTION_USERNAMES = 'usernames';
private const OPTION_LINE_ITEM_SLUGS = 'line-item-slugs';
private const OPTION_BATCH = 'batch';
private const OPTION_MODULO = 'modulo';
private const OPTION_REMAINDER = 'remainder';
private const DEFAULT_BATCH_SIZE = '1000';
private UserCacheWarmerService $userCacheWarmerService;
private UserRepository $userRepository;
private SymfonyStyle $symfonyStyle;
private int $batchSize;
/** @var string[] */
private array $usernames = [];
/** @var array */
private array $lineItemSlugs = [];
private ?int $modulo = null;
private ?int $remainder = null;
public function __construct(UserCacheWarmerService $userCacheWarmerService, UserRepository $userRepository)
{
$this->userCacheWarmerService = $userCacheWarmerService;
$this->userRepository = $userRepository;
parent::__construct(self::NAME);
}
protected function configure(): void
{
parent::configure();
$this->setDescription('User cache warmup');
// @codingStandardsIgnoreStart
$this->setHelp(<<<'EOF'
The <info>%command.name%</info> command warms up the cache for users.
<info>php %command.full_name%</info>
Use the --batch option to warm up the cache in batches:
<info>php %command.full_name% --batch=10000</info>
Use the --usernames option to warm up the cache for specific users:
<info>php %command.full_name% --usernames=username_1,username_2,username_3</info>
Use the --line-item-slugs option to warm up the cache for users having assignments for specific line items:
<info>php %command.full_name% --line-item-slugs=slug_1,slug_2,slug_3</info>
Use the --modulo and --remainder options for parallelized cache warmup:
<info>php %command.full_name% --modulo=4 --remainder=1</info>
<comment>(Documentation: https://github.com/oat-sa/simple-roster/blob/develop/docs/cli/user-cache-warmer-command.md#synchronous-cache-warmup-parallelization)
</comment>
EOF
);
// @codingStandardsIgnoreEnd
$this->addBlackfireProfilingOption();
$this->addOption(
self::OPTION_BATCH,
'b',
InputOption::VALUE_REQUIRED,
'Number of cache entries to process per batch',
self::DEFAULT_BATCH_SIZE
);
$this->addOption(
self::OPTION_USERNAMES,
'u',
InputOption::VALUE_REQUIRED,
'Comma separated list of usernames to scope the cache warmup'
);
$this->addOption(
self::OPTION_LINE_ITEM_SLUGS,
'l',
InputOption::VALUE_REQUIRED,
'Comma separated list of line item slugs to scope the cache warmup'
);
$this->addOption(
self::OPTION_MODULO,
'm',
InputOption::VALUE_REQUIRED,
"Modulo (M) of Euclidean division A = M*Q + R (0 ≤ R < |M|), where A = user id, Q = quotient, " .
"R = 'remainder' option"
);
$this->addOption(
self::OPTION_REMAINDER,
'r',
InputOption::VALUE_REQUIRED,
"Remainder (R) of Euclidean division A = M*Q + R (0 ≤ R < |M|), where A = user id, Q = quotient, " .
"M = 'modulo' option"
);
}
/**
* @throws InvalidArgumentException
*/
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->symfonyStyle = new SymfonyStyle($input, $output);
$this->symfonyStyle->title('Simple Roster - User Cache Warmer');
$this->initializeBatchSizeOption($input);
if ($input->getOption(self::OPTION_USERNAMES)) {
$this->initializeUsernamesOption($input);
}
if ($input->getOption(self::OPTION_LINE_ITEM_SLUGS)) {
$this->initializeLineItemSlugsOption($input);
}
if (!empty($this->usernames) && !empty($this->lineItemSlugs)) {
throw new InvalidArgumentException(
sprintf(
"Option '%s' and '%s' are exclusive options.",
self::OPTION_USERNAMES,
self::OPTION_LINE_ITEM_SLUGS
)
);
}
if ($input->getOption(self::OPTION_MODULO)) {
$this->initializeModuloOption($input);
}
if ($input->getOption(self::OPTION_REMAINDER) !== null) {
$this->initializeRemainderOption($input);
}
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @throws ORMException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->symfonyStyle->note('Calculating total number of entries to warm up...');
$criteria = $this->getFindUserCriteria();
$numberOfTotalUsers = $this->userRepository->countByCriteria($criteria);
if ($numberOfTotalUsers === 0) {
$this->symfonyStyle->warning('There are no users found in the database.');
return 0;
}
$this->symfonyStyle->note('Warming up doctrine result cache...');
$progressBar = $this->createFormattedProgressBar($output);
$progressBar->setMaxSteps($numberOfTotalUsers);
$progressBar->start();
$lastUserId = null;
$numberOfWarmedUpCacheEntries = 0;
try {
do {
$resultSet = $this->userRepository->findAllUsernamesPaged($this->batchSize, $lastUserId, $criteria);
if (!$resultSet->isEmpty()) {
$this->userCacheWarmerService->process($resultSet->getUsernameCollection());
}
$progressBar->advance($this->batchSize);
$numberOfWarmedUpCacheEntries += $resultSet->count();
$lastUserId = $resultSet->getLastUserId();
} while ($resultSet->hasMore());
$progressBar->finish();
$this->symfonyStyle->newLine(2);
$this->symfonyStyle->success(
sprintf(
'Cache warmup for %d users was successfully initiated.',
$numberOfWarmedUpCacheEntries,
)
);
} catch (Throwable $exception) {
$this->symfonyStyle->error(sprintf('An unexpected error occurred: %s', $exception->getMessage()));
return 1;
}
return 0;
}
/**
* @throws InvalidArgumentException
*/
private function initializeBatchSizeOption(InputInterface $input): void
{
$this->batchSize = (int)$input->getOption(self::OPTION_BATCH);
if ($this->batchSize < 1) {
throw new InvalidArgumentException(
sprintf("Invalid '%s' option received.", self::OPTION_BATCH)
);
}
}
/**
* @throws InvalidArgumentException
*/
private function initializeLineItemSlugsOption(InputInterface $input): void
{
$this->lineItemSlugs = array_filter(
explode(',', (string)$input->getOption(self::OPTION_LINE_ITEM_SLUGS)),
static function ($value): bool {
return !empty($value) && mb_strlen($value) > 1;
}
);
if (empty($this->lineItemSlugs)) {
throw new InvalidArgumentException(
sprintf("Invalid '%s' option received.", self::OPTION_LINE_ITEM_SLUGS)
);
}
}
/**
* @throws InvalidArgumentException
*/
private function initializeUsernamesOption(InputInterface $input): void
{
$this->usernames = array_filter(
explode(',', (string)$input->getOption(self::OPTION_USERNAMES)),
static function ($value): bool {
return !empty($value) && mb_strlen($value) > 1;
}
);
if (empty($this->usernames)) {
throw new InvalidArgumentException(
sprintf("Invalid '%s' option received.", self::OPTION_USERNAMES)
);
}
}
/**
* @throws InvalidArgumentException
*/
private function initializeModuloOption(InputInterface $input): void
{
if ($input->getOption(self::OPTION_REMAINDER) === null) {
throw new InvalidArgumentException(
sprintf("Option '%s' is expected to be specified.", self::OPTION_REMAINDER)
);
}
if (!is_numeric($input->getOption(self::OPTION_MODULO))) {
throw new InvalidArgumentException(
sprintf(
"Option '%s' is expected to be numeric.",
self::OPTION_MODULO
)
);
}
$modulo = (int)$input->getOption(self::OPTION_MODULO);
if ($modulo < 2 || $modulo > 100) {
throw new InvalidArgumentException(
sprintf(
"Invalid '%s' option received: %d, expected value: 2 <= m <= 100",
self::OPTION_MODULO,
$modulo
)
);
}
$this->modulo = $modulo;
}
/**
* @throws InvalidArgumentException
*/
private function initializeRemainderOption(InputInterface $input): void
{
if (!$input->getOption(self::OPTION_MODULO)) {
throw new InvalidArgumentException(
sprintf("Option '%s' is expected to be specified.", self::OPTION_MODULO)
);
}
if (!is_numeric($input->getOption(self::OPTION_REMAINDER))) {
throw new InvalidArgumentException(
sprintf(
"Option '%s' is expected to be numeric.",
self::OPTION_REMAINDER
)
);
}
$remainder = (int)$input->getOption(self::OPTION_REMAINDER);
if ($remainder < 0 || $remainder >= $this->modulo) {
throw new InvalidArgumentException(
sprintf(
"Invalid '%s' option received: %d, expected value: 0 <= r <= %d",
self::OPTION_REMAINDER,
$remainder,
$this->modulo - 1
)
);
}
$this->remainder = $remainder;
}
private function getFindUserCriteria(): FindUserCriteria
{
$criteria = new FindUserCriteria();
if (!empty($this->usernames)) {
$criteria->addUsernameCriterion(...$this->usernames);
}
if (!empty($this->lineItemSlugs)) {
$criteria->addLineItemSlugCriterion(...$this->lineItemSlugs);
}
if ($this->modulo && $this->remainder !== null) {
$criteria->addEuclideanDivisionCriterion(
new EuclideanDivisionCriterion($this->modulo, $this->remainder)
);
}
return $criteria;
}
}