1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace Level23\Druid\Tasks; |
||
5 | |||
6 | use InvalidArgumentException; |
||
7 | use Level23\Druid\DruidClient; |
||
8 | use Level23\Druid\Context\TaskContext; |
||
9 | use Level23\Druid\Concerns\HasInterval; |
||
10 | use Level23\Druid\Concerns\HasTuningConfig; |
||
11 | use Level23\Druid\Interval\IntervalInterface; |
||
12 | use Level23\Druid\Concerns\HasSegmentGranularity; |
||
13 | |||
14 | class CompactTaskBuilder extends TaskBuilder |
||
15 | { |
||
16 | use HasInterval, HasSegmentGranularity, HasTuningConfig; |
||
17 | |||
18 | protected string $dataSource; |
||
19 | |||
20 | protected ?int $targetCompactionSizeBytes = null; |
||
21 | |||
22 | /** |
||
23 | * CompactTaskBuilder constructor. |
||
24 | * |
||
25 | * A compaction task internally generates an index task spec for performing compaction work with some fixed |
||
26 | * parameters. For example, its input source is always DruidInputSource, and dimensionsSpec and metricsSpec |
||
27 | * include all dimensions and metrics of the input segments by default. |
||
28 | * |
||
29 | * Compaction tasks will exit with a failure status code, without doing anything, if the interval you specify has |
||
30 | * no data segments loaded in it (or if the interval you specify is empty). |
||
31 | * |
||
32 | * @param \Level23\Druid\DruidClient $druidClient |
||
33 | * @param string $dataSource |
||
34 | */ |
||
35 | 17 | public function __construct(DruidClient $druidClient, string $dataSource) |
|
36 | { |
||
37 | 17 | $this->client = $druidClient; |
|
38 | 17 | $this->dataSource = $dataSource; |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param \Level23\Druid\Context\TaskContext|array<string,string|int|bool> $context |
||
43 | * |
||
44 | * @return \Level23\Druid\Tasks\CompactTask |
||
45 | * @throws \Level23\Druid\Exceptions\QueryResponseException |
||
46 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
47 | */ |
||
48 | 6 | protected function buildTask(array|TaskContext $context): TaskInterface |
|
49 | { |
||
50 | 6 | if (is_array($context)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
51 | 5 | $context = new TaskContext($context); |
|
52 | } |
||
53 | |||
54 | 6 | $interval = $this->interval; |
|
55 | 6 | if (!$interval instanceof IntervalInterface) { |
|
56 | 1 | throw new InvalidArgumentException('You have to specify an interval!'); |
|
57 | } |
||
58 | |||
59 | // First, validate the given from and to. Make sure that these |
||
60 | // match the beginning and end of an interval. |
||
61 | 5 | $properties = $context->toArray(); |
|
62 | 5 | if (empty($properties['skipIntervalValidation'])) { |
|
63 | 5 | $this->validateInterval($this->dataSource, $interval); |
|
64 | } |
||
65 | |||
66 | // @todo: add support for building metricSpec and DimensionSpec. |
||
67 | |||
68 | 5 | return new CompactTask( |
|
69 | 5 | $this->dataSource, |
|
70 | 5 | $interval, |
|
71 | 5 | $this->segmentGranularity, |
|
72 | 5 | $this->tuningConfig, |
|
73 | 5 | $context, |
|
74 | 5 | $this->targetCompactionSizeBytes, |
|
75 | 5 | $this->taskId |
|
76 | 5 | ); |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param int $bytes |
||
81 | * |
||
82 | * @return $this |
||
83 | */ |
||
84 | 4 | public function targetCompactionSize(int $bytes): CompactTaskBuilder |
|
85 | { |
||
86 | 4 | $this->targetCompactionSizeBytes = $bytes; |
|
87 | |||
88 | 4 | return $this; |
|
89 | } |
||
90 | } |