summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Elastic/Indexer/Rollover.php
blob: dcd10da822237dd313a9285e2528d08e470b0e8c (plain)
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
<?php

namespace SMW\Elastic\Indexer;

use SMW\Elastic\Connection\Client as ElasticClient;
use SMW\Elastic\Exception\NoConnectionException;

/**
 * The index uses V1/V2 to switch between versions during a rebuild allowing the
 * index to be available while a reindex is on going and after the process has
 * been indices will be switched (aka rollover) without down time. The index uses
 * aliases to hide the "real" identity of the current active index.
 *
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-rollover-index.html
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class Rollover {

	/**
	 * @var ElasticClient
	 */
	private $connection;

	/**
	 * @since 3.0
	 *
	 * @param ElasticClient $connection
	 */
	public function __construct( ElasticClient $connection ) {
		$this->connection = $connection;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $type
	 * @param string $version
	 *
	 * @return string
	 */
	public function rollover( $type, $version ) {

		$index = $this->connection->getIndexNameByType( $type );
		$indices = $this->connection->indices();

		$params = [];
		$actions = [];

		$old = $version === 'v2' ? 'v1' : 'v2';
		$check = false;

		if ( $indices->exists( [ 'index' => "$index-$old" ] ) ) {
			$actions = [
				[ 'remove' => [ 'index' => "$index-$old", 'alias' => $index ] ],
				[ 'add' => [ 'index' => "$index-$version", 'alias' => $index ] ]
			];

			$check = true;
		} else {
			// No old index
			$old = $version;

			$actions = [
				[ 'add' => [ 'index' => "$index-$version", 'alias' => $index ] ]
			];
		}

		$params['body'] = [ 'actions' => $actions ];

		$indices->updateAliases( $params );

		if ( $check && $indices->exists( [ 'index' => "$index-$old" ] ) ) {
			$indices->delete( [ "index" => "$index-$old" ] );
		}

		$this->connection->releaseLock( $type );

		return $old;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $type
	 *
	 * @throws NoConnectionException
	 */
	public function update( $type ) {

		// Fail hard since we expect to create an index but are unable to do so!
		if ( !$this->connection->ping() ) {
			throw new NoConnectionException();
		}

		$indices = $this->connection->indices();

		$index = $this->connection->getIndexName(
			$type
		);

		// Shouldn't happen but just in case where the root index is
		// used as index but not an alias
		if ( $indices->exists( [ 'index' => "$index" ] ) && !$indices->existsAlias( [ 'name' => "$index" ] ) ) {
			$indices->delete(  [ 'index' => "$index" ] );
		}

		// Check v1/v2 and if both exists (which shouldn't happen but most likely
		// caused by an unfinshed rebuilder run) then use v1 as master
		if ( $indices->exists( [ 'index' => "$index-v1" ] ) ) {

			// Just in case
			if ( $indices->exists( [ 'index' => "$index-v2" ] ) ) {
				$indices->delete(  [ 'index' => "$index-v2" ] );
			}

			$actions[] = [ 'add' => [ 'index' => "$index-v1", 'alias' => $index ] ];
		} elseif ( $indices->exists( [ 'index' => "$index-v2" ] ) ) {
			$actions[] = [ 'add' => [ 'index' => "$index-v2", 'alias' => $index ] ];
		} else {
			$version = $this->connection->createIndex( $type );

			$actions = [
				[ 'add' => [ 'index' => "$index-$version", 'alias' => $index ] ]
			];
		}

		$params['body'] = [ 'actions' => $actions ];
		$indices->updateAliases( $params );
	}

	/**
	 * @since 3.0
	 *
	 * @param string $type
	 *
	 * @throws NoConnectionException
	 */
	public function delete( $type ) {

		// Fail hard since we expect to delete an index but are unable to do so!
		if ( !$this->connection->ping() ) {
			throw new NoConnectionException();
		}

		$indices = $this->connection->indices();

		$index = $this->connection->getIndexName(
			$type
		);

		if ( $indices->exists( [ 'index' => "$index-v1" ] ) ) {
			$indices->delete( [ 'index' => "$index-v1" ] );
		}

		if ( $indices->exists( [ 'index' => "$index-v2" ] ) ) {
			$indices->delete( [ 'index' => "$index-v2" ] );
		}

		if ( $indices->exists( [ 'index' => "$index" ] ) && !$indices->existsAlias( [ 'name' => "$index" ] ) ) {
			$indices->delete( [ 'index' => "$index" ] );
		}

		$this->connection->releaseLock( $type );
	}

}