-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMigrate.php
More file actions
205 lines (175 loc) · 6.03 KB
/
Copy pathMigrate.php
File metadata and controls
205 lines (175 loc) · 6.03 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
<?php declare(strict_types = 1);
namespace Spameri\Elastic\Model\TypeToNewIndex;
class Migrate
{
private \Symfony\Component\Console\Output\OutputInterface $output;
public function __construct(
private readonly DocumentMigrateStatus $documentMigrateStatus,
private readonly \Spameri\Elastic\ClientProvider $clientProvider,
private readonly \Spameri\Elastic\Provider\DateTimeProvider $dateTimeProvider,
private readonly \Spameri\Elastic\Model\Delete $delete,
private readonly \Spameri\Elastic\Model\Get $get,
private readonly \Spameri\Elastic\Model\Indices\Close $close,
private readonly \Spameri\Elastic\Model\Indices\GetMapping $getMapping,
private readonly \Spameri\Elastic\Model\Indices\PutMapping $putMapping,
private readonly \Spameri\Elastic\Model\Search $search,
private readonly \Spameri\Elastic\Model\Indices\Create $create,
private readonly \Spameri\Elastic\Model\Indices\Get $indicesGet,
private readonly \Spameri\Elastic\Model\Indices\AddAlias $addAlias,
private readonly \Spameri\Elastic\Model\VersionProvider $versionProvider,
)
{
}
public function setOutput(
\Symfony\Component\Console\Output\OutputInterface $output,
): void
{
$this->output = $output;
}
/**
* @throws \Elastic\Elasticsearch\Exception\ElasticsearchException
*/
public function execute(
string $indexFrom,
string $typeFrom,
string $indexTo,
string $aliasTo,
string|null $typeTo,
bool $allowClose,
): void
{
// 1. Close index
if ($allowClose) {
$this->output->writeln('Closing index ' . $indexFrom);
$this->close->execute($indexFrom);
}
// 2. Create new index
$indexTo .= '_' . $this->dateTimeProvider->provide()->format(\Spameri\Elastic\Entity\Property\DateTime::INDEX_FORMAT);
$this->output->writeln('Creating index ' . $indexTo);
// 2a. Put settings to new index
$oldIndexSettings = $this->indicesGet->execute($indexFrom);
$settings = \reset($oldIndexSettings);
$indexToParameters = [];
if (isset($settings['settings']['index']['analysis'])) {
$indexToParameters = [
'settings' => [
'index' => $settings['settings']['index']['analysis'] ?? [],
],
];
}
$this->create->execute($indexTo, $indexToParameters);
// 2b. Set mapping in new index
$this->output->writeln('Transferring mapping from index: ' . $indexFrom . ' and type: ' . $typeFrom . ' to index: ' . $indexTo);
$oldMapping = $this->getMapping->execute($indexFrom, $typeFrom);
$firstMapping = \reset($oldMapping);
$this->putMapping->execute($indexTo, $firstMapping['mappings']);
// 3. Foreach index data
$this->output->writeln('Starting migration.');
$progressBar = new \Symfony\Component\Console\Helper\ProgressBar($this->output);
$progressBar->setFormat('debug');
$continue = TRUE;
$from = 0;
$elasticQuery = new \Spameri\ElasticQuery\ElasticQuery();
$elasticQuery->options()->changeSize(5000);
while ($continue) {
$result = $this->search->execute($elasticQuery, $indexFrom, $typeFrom);
// 4. Input data to new index
// 4a. if closed delete data
// 4b. if open store migrated version
/** @var \Spameri\ElasticQuery\Response\Result\Hit $response */
foreach ($result->hits() as $response) {
$this->processHit($indexTo, $typeTo, $indexFrom, $response, $allowClose);
}
if (\count($result->hits()->getIterator()) === 0) {
$continue = FALSE;
} else {
$progressBar->advance(5000);
$from += 5000;
$elasticQuery->options()->changeFrom($from);
}
}
$progressBar->finish();
// 5. loop end
// 6. If open transfer again changed docs
// 7. Apply previous step until empty queue or 10 loops
if ($allowClose === FALSE) {
$this->output->writeln('Starting update changed documents');
$updateBar = new \Symfony\Component\Console\Helper\ProgressBar($this->output);
$updateBar->setFormat('debug');
$canContinue = TRUE;
$loops = 0;
while ($canContinue) {
$changed = 0;
// phpcs:ignore
foreach ($this->documentMigrateStatus->storage() as $documentId => $documentVersion) {
$response = $this->get->execute(
new \Spameri\Elastic\Entity\Property\ElasticId((string) $documentId),
$indexFrom,
$typeFrom,
);
if ($this->documentMigrateStatus->isChanged((string) $documentId, $response->hit()->version())) {
// Reindex this document
$this->processHit($indexTo, $typeTo, $indexFrom, $response->hit(), $allowClose);
$changed++;
$updateBar->advance();
}
}
$loops++;
if ($loops >= 10) {
$canContinue = FALSE;
$updateBar->finish();
$this->output->writeln('Loops limit reached, data is too frequently updated, please keep in mind there can be inconsistencies after this command.');
}
if ($changed === 0) {
$canContinue = FALSE;
$updateBar->finish();
$this->output->writeln('Documents changed during migrate reindexed.');
}
}
}
// 8. Switch to new index
$this->output->writeln('Adding alias: ' . $aliasTo . ' to index: ' . $indexTo);
$this->addAlias->execute($aliasTo, $indexTo);
// 9. Write info
$this->output->writeln(
'Migration done. All old data remains in old index: ' . $indexFrom . ' with type: ' . $typeFrom
. ' it is recommended to manually delete data after this command',
);
// 10. Done
}
/**
* @throws \Elastic\Elasticsearch\Exception\ElasticsearchException
*/
public function processHit(
string $indexTo,
string|null $typeTo,
string $indexFrom,
\Spameri\ElasticQuery\Response\Result\Hit $hit,
bool $allowClose,
): void
{
if ($this->versionProvider->provide() >= \Spameri\ElasticQuery\Response\Result\Version::ELASTIC_VERSION_ID_7) {
$typeTo = NULL;
}
$this->clientProvider->client()->index(
(
new \Spameri\ElasticQuery\Document(
$indexTo,
new \Spameri\ElasticQuery\Document\Body\Plain($hit->source()),
$typeTo,
$hit->id(),
)
)->toArray(),
)
;
if ($allowClose === FALSE) {
$this->documentMigrateStatus->add($hit->id(), $hit->version());
}
if ($allowClose === TRUE) {
$this->delete->execute(
new \Spameri\Elastic\Entity\Property\ElasticId($hit->id()),
$indexFrom,
);
}
}
}