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 | 7 | public function switchIndexAlias(IndexConfig $indexConfig, Index $index, bool $force = false, bool $delete = true) |
|
82 | |||
83 | /** |
||
84 | * Builds an ElasticSearch request to rename or create an alias. |
||
85 | */ |
||
86 | 5 | 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 | private function closeIndex(Client $client, string $indexName): void |
||
140 | { |
||
141 | try { |
||
142 | $path = $indexName.'/_close'; |
||
143 | $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 | } |
||
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 | 7 | private function getAliasedIndex(Client $client, string $aliasName): ?string |
|
180 | } |
||
181 |