1 | <?php |
||
21 | class AliasProcessor |
||
22 | { |
||
23 | /** |
||
24 | * Sets the randomised root name for an index. |
||
25 | */ |
||
26 | 2 | public function setRootName(IndexConfig $indexConfig, Index $index) |
|
35 | |||
36 | /** |
||
37 | * Switches an index to become the new target for an alias. Only applies for |
||
38 | * indexes that are set to use aliases. |
||
39 | * |
||
40 | * $force will delete an index encountered where an alias is expected. |
||
41 | * |
||
42 | * @throws AliasIsIndexException |
||
43 | */ |
||
44 | 8 | public function switchIndexAlias(IndexConfig $indexConfig, Index $index, bool $force = false, bool $delete = true) |
|
45 | { |
||
46 | 8 | $client = $index->getClient(); |
|
47 | |||
48 | 8 | $aliasName = $indexConfig->getElasticSearchName(); |
|
49 | 8 | $oldIndexName = null; |
|
50 | 8 | $newIndexName = $index->getName(); |
|
51 | |||
52 | try { |
||
53 | 8 | $oldIndexName = $this->getAliasedIndex($client, $aliasName); |
|
54 | 3 | } catch (AliasIsIndexException $e) { |
|
55 | 2 | if (!$force) { |
|
56 | 1 | throw $e; |
|
57 | } |
||
58 | |||
59 | 1 | if ($delete) { |
|
60 | 1 | $this->deleteIndex($client, $aliasName); |
|
61 | } else { |
||
62 | $this->closeIndex($client, $aliasName); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | try { |
||
67 | 6 | $aliasUpdateRequest = $this->buildAliasUpdateRequest($oldIndexName, $aliasName, $newIndexName); |
|
68 | 6 | $client->request('_aliases', 'POST', $aliasUpdateRequest); |
|
69 | 1 | } catch (ExceptionInterface $e) { |
|
70 | 1 | $this->cleanupRenameFailure($client, $newIndexName, $e); |
|
71 | } |
||
72 | |||
73 | // Delete the old index after the alias has been switched |
||
74 | 5 | if (null !== $oldIndexName) { |
|
75 | 3 | if ($delete) { |
|
76 | 2 | $this->deleteIndex($client, $oldIndexName); |
|
77 | } else { |
||
78 | 1 | $this->closeIndex($client, $oldIndexName); |
|
79 | } |
||
80 | } |
||
81 | 5 | } |
|
82 | |||
83 | /** |
||
84 | * Builds an ElasticSearch request to rename or create an alias. |
||
85 | */ |
||
86 | 6 | private function buildAliasUpdateRequest(?string $aliasedIndex, string $aliasName, string $newIndexName): array |
|
103 | |||
104 | /** |
||
105 | * Cleans up an index when we encounter a failure to rename the alias. |
||
106 | */ |
||
107 | 1 | private function cleanupRenameFailure(Client $client, string $indexName, \Exception $renameAliasException): void |
|
108 | { |
||
109 | 1 | $additionalError = ''; |
|
110 | try { |
||
111 | 1 | $this->deleteIndex($client, $indexName); |
|
112 | } catch (ExceptionInterface $deleteNewIndexException) { |
||
113 | $additionalError = \sprintf( |
||
114 | 'Tried to delete newly built index %s, but also failed: %s', |
||
115 | $indexName, |
||
116 | $deleteNewIndexException->getMessage() |
||
117 | ); |
||
118 | } |
||
119 | |||
120 | 1 | throw new \RuntimeException(\sprintf('Failed to updated index alias: %s. %s', $renameAliasException->getMessage(), $additionalError ?: \sprintf('Newly built index %s was deleted', $indexName)), 0, $renameAliasException); |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * Delete an index. |
||
125 | */ |
||
126 | 4 | private function deleteIndex(Client $client, string $indexName): void |
|
127 | { |
||
128 | try { |
||
129 | 4 | $path = $indexName; |
|
130 | 4 | $client->request($path, Request::DELETE); |
|
131 | } catch (ExceptionInterface $deleteOldIndexException) { |
||
132 | throw new \RuntimeException(\sprintf('Failed to delete index "%s" with message: "%s"', $indexName, $deleteOldIndexException->getMessage()), 0, $deleteOldIndexException); |
||
133 | } |
||
134 | 4 | } |
|
135 | |||
136 | /** |
||
137 | * Close an index. |
||
138 | */ |
||
139 | 1 | private function closeIndex(Client $client, string $indexName): void |
|
140 | { |
||
141 | try { |
||
142 | 1 | $path = $indexName.'/_close'; |
|
143 | 1 | $client->request($path, Request::POST); |
|
144 | } catch (ExceptionInterface $e) { |
||
145 | throw new \RuntimeException(\sprintf('Failed to close index "%s" with message: "%s"', $indexName, $e->getMessage()), 0, $e); |
||
146 | } |
||
147 | 1 | } |
|
148 | |||
149 | /** |
||
150 | * Returns the name of a single index that an alias points to or throws |
||
151 | * an exception if there is more than one. |
||
152 | * |
||
153 | * @throws AliasIsIndexException |
||
154 | */ |
||
155 | 8 | private function getAliasedIndex(Client $client, string $aliasName): ?string |
|
180 | } |
||
181 |