5
5
use Psr \Log \LoggerInterface ;
6
6
use Psr \Log \NullLogger ;
7
7
use Elasticsearch \Client ;
8
+ use Zumba \ElasticsearchRotator \Common \PrimaryIndexStrategy ;
8
9
9
10
class IndexRotator
10
11
{
11
- const INDEX_NAME_CONFIG = '.%s_configuration ' ;
12
- const TYPE_CONFIGURATION = 'configuration ' ;
13
12
const SECONDARY_NAME_ONLY = 0 ;
14
13
const SECONDARY_INCLUDE_ID = 1 ;
15
- const PRIMARY_ID = 'primary ' ;
16
14
const RETRY_TIME_COPY = 500000 ;
17
15
const MAX_RETRY_COUNT = 5 ;
16
+ const STRATEGY_CONFIGURATION = 'Zumba\ElasticsearchRotator\Strategy\ConfigurationStrategy ' ;
17
+ const STRATEGY_ALIAS = 'Zumba\ElasticsearchRotator\Strategy\AliasStrategy ' ;
18
+ const DEFAULT_PRIMARY_INDEX_STRATEGY = self ::STRATEGY_CONFIGURATION ;
18
19
19
20
/**
20
21
* Elasticsearch client instance.
@@ -24,18 +25,18 @@ class IndexRotator
24
25
private $ engine ;
25
26
26
27
/**
27
- * Prefix identifier for this index.
28
+ * Configuration index name for this index.
28
29
*
29
- * @var string
30
+ * @var \Zumba\ElasticsearchRotator\ConfigurationIndex
30
31
*/
31
- private $ prefix ;
32
+ private $ configurationIndex ;
32
33
33
34
/**
34
- * Configuration index name for this index.
35
+ * Strategy to employ when working on primary index.
35
36
*
36
- * @var string
37
+ * @var \Zumba\ElasticsearchRotator\Common\PrimaryIndexStrategy
37
38
*/
38
- private $ configurationIndexName ;
39
+ private $ primaryIndexStrategy ;
39
40
40
41
/**
41
42
* Mapping for configuration index.
@@ -62,13 +63,31 @@ class IndexRotator
62
63
public function __construct (\Elasticsearch \Client $ engine , $ prefix , LoggerInterface $ logger = null )
63
64
{
64
65
$ this ->engine = $ engine ;
65
- $ this ->prefix = $ prefix ;
66
- if ($ logger !== null ) {
67
- $ this ->logger = $ logger ;
68
- } else {
69
- $ this ->logger = new NullLogger ();
70
- }
71
- $ this ->configurationIndexName = sprintf (static ::INDEX_NAME_CONFIG , $ this ->prefix );
66
+ $ this ->logger = $ logger ?: new NullLogger ();
67
+ $ this ->configurationIndex = new ConfigurationIndex ($ this ->engine , $ this ->logger , $ prefix );
68
+ $ this ->setPrimaryIndexStrategy ($ this ->strategyFactory (static ::DEFAULT_PRIMARY_INDEX_STRATEGY , [
69
+ 'configuration_index ' => $ this ->configurationIndex
70
+ ]));
71
+ }
72
+
73
+ /**
74
+ * Instantiate a specific strategy.
75
+ *
76
+ * @param string $strategyClass Fully qualified class name for a strategy.
77
+ * @param array $options Options specific to the strategy
78
+ * @return \Zumba\ElasticsearchRotator\Common\PrimaryIndexStrategy
79
+ */
80
+ public function strategyFactory ($ strategyClass , array $ options = []) {
81
+ return new $ strategyClass ($ this ->engine , $ this ->logger , $ options );
82
+ }
83
+
84
+ /**
85
+ * Set the primary index strategy.
86
+ *
87
+ * @param \Zumba\ElasticsearchRotator\Common\PrimaryIndexStrategy $strategy
88
+ */
89
+ public function setPrimaryIndexStrategy (PrimaryIndexStrategy $ strategy ) {
90
+ $ this ->primaryIndexStrategy = $ strategy ;
72
91
}
73
92
74
93
/**
@@ -79,23 +98,8 @@ public function __construct(\Elasticsearch\Client $engine, $prefix, LoggerInterf
79
98
*/
80
99
public function getPrimaryIndex ()
81
100
{
82
- if (!$ this ->engine ->indices ()->exists (['index ' => $ this ->configurationIndexName ])) {
83
- $ this ->logger ->error ('Primary index configuration index not available. ' );
84
- throw new Exception \MissingPrimaryIndex ('Primary index configuration index not available. ' );
85
- }
86
- $ primaryPayload = [
87
- 'index ' => $ this ->configurationIndexName ,
88
- 'type ' => static ::TYPE_CONFIGURATION ,
89
- 'id ' => static ::PRIMARY_ID ,
90
- 'preference ' => '_primary '
91
- ];
92
- try {
93
- $ primary = $ this ->engine ->get ($ primaryPayload );
94
- } catch (\Elasticsearch \Common \Exceptions \Missing404Exception $ e ) {
95
- $ this ->logger ->error ('Primary index does not exist. ' );
96
- throw new Exception \MissingPrimaryIndex ('Primary index not available. ' );
97
- }
98
- return $ primary ['_source ' ]['name ' ];
101
+ return $ this ->primaryIndexStrategy ->getPrimaryIndex ();
102
+
99
103
}
100
104
101
105
/**
@@ -106,19 +110,7 @@ public function getPrimaryIndex()
106
110
*/
107
111
public function setPrimaryIndex ($ name )
108
112
{
109
- if (!$ this ->engine ->indices ()->exists (['index ' => $ this ->configurationIndexName ])) {
110
- $ this ->createCurrentIndexConfiguration ();
111
- }
112
- $ this ->engine ->index ([
113
- 'index ' => $ this ->configurationIndexName ,
114
- 'type ' => static ::TYPE_CONFIGURATION ,
115
- 'id ' => static ::PRIMARY_ID ,
116
- 'body ' => [
117
- 'name ' => $ name ,
118
- 'timestamp ' => time ()
119
- ]
120
- ]);
121
- $ this ->logger ->debug ('Primary index set. ' , compact ('name ' ));
113
+ $ this ->primaryIndexStrategy ->setPrimaryIndex ($ name );
122
114
}
123
115
124
116
/**
@@ -130,9 +122,7 @@ public function setPrimaryIndex($name)
130
122
*/
131
123
public function copyPrimaryIndexToSecondary ($ retryCount = 0 )
132
124
{
133
- if (!$ this ->engine ->indices ()->exists (['index ' => $ this ->configurationIndexName ])) {
134
- $ this ->createCurrentIndexConfiguration ();
135
- }
125
+ $ this ->configurationIndex ->createCurrentIndexConfiguration ();
136
126
try {
137
127
$ primaryName = $ this ->getPrimaryIndex ();
138
128
} catch (\Elasticsearch \Common \Exceptions \ServerErrorResponseException $ e ) {
@@ -144,8 +134,8 @@ public function copyPrimaryIndexToSecondary($retryCount = 0)
144
134
return $ this ->copyPrimaryIndexToSecondary ($ retryCount ++);
145
135
}
146
136
$ id = $ this ->engine ->index ([
147
- 'index ' => $ this ->configurationIndexName ,
148
- 'type ' => static ::TYPE_CONFIGURATION ,
137
+ 'index ' => ( string ) $ this ->configurationIndex ,
138
+ 'type ' => ConfigurationIndex ::TYPE_CONFIGURATION ,
149
139
'body ' => [
150
140
'name ' => $ primaryName ,
151
141
'timestamp ' => time ()
@@ -170,14 +160,14 @@ public function getSecondaryIndices(\DateTime $olderThan = null, $disposition =
170
160
$ olderThan = new \DateTime ();
171
161
}
172
162
$ params = [
173
- 'index ' => $ this ->configurationIndexName ,
174
- 'type ' => static ::TYPE_CONFIGURATION ,
163
+ 'index ' => ( string ) $ this ->configurationIndex ,
164
+ 'type ' => ConfigurationIndex ::TYPE_CONFIGURATION ,
175
165
'body ' => [
176
166
'query ' => [
177
167
'bool ' => [
178
168
'must_not ' => [
179
169
'term ' => [
180
- '_id ' => static ::PRIMARY_ID
170
+ '_id ' => ConfigurationIndex ::PRIMARY_ID
181
171
]
182
172
],
183
173
'filter ' => [
@@ -231,51 +221,20 @@ public function deleteSecondaryIndices(\DateTime $olderThan = null)
231
221
if ($ this ->engine ->indices ()->exists (['index ' => $ indexToDelete ['index ' ]])) {
232
222
$ results [$ indexToDelete ['index ' ]] = [
233
223
'index ' => $ this ->engine ->indices ()->delete (['index ' => $ indexToDelete ['index ' ]]),
234
- 'config ' => $ this ->deleteConfigurationEntry ($ indexToDelete ['configuration_id ' ])
224
+ 'config ' => $ this ->configurationIndex -> deleteConfigurationEntry ($ indexToDelete ['configuration_id ' ])
235
225
];
236
226
$ this ->logger ->debug ('Deleted secondary index. ' , compact ('indexToDelete ' ));
237
227
} else {
238
228
$ results [$ indexToDelete ] = [
239
229
'index ' => null ,
240
- 'config ' => $ this ->deleteConfigurationEntry ($ indexToDelete ['configuration_id ' ])
230
+ 'config ' => $ this ->configurationIndex -> deleteConfigurationEntry ($ indexToDelete ['configuration_id ' ])
241
231
];
242
232
$ this ->logger ->debug ('Index not found to delete. ' , compact ('indexToDelete ' ));
243
233
}
244
234
}
245
235
return $ results ;
246
236
}
247
237
248
- /**
249
- * Delete an entry from the configuration index.
250
- *
251
- * @param string $id
252
- * @return array
253
- */
254
- private function deleteConfigurationEntry ($ id )
255
- {
256
- return $ this ->engine ->delete ([
257
- 'index ' => $ this ->configurationIndexName ,
258
- 'type ' => static ::TYPE_CONFIGURATION ,
259
- 'id ' => $ id
260
- ]);
261
- }
262
-
263
- /**
264
- * Create the index needed to store the primary index name.
265
- *
266
- * @return void
267
- */
268
- private function createCurrentIndexConfiguration ()
269
- {
270
- $ this ->engine ->indices ()->create ([
271
- 'index ' => $ this ->configurationIndexName ,
272
- 'body ' => static ::$ elasticSearchConfigurationMapping
273
- ]);
274
- $ this ->logger ->debug ('Configuration index created. ' , [
275
- 'index ' => $ this ->configurationIndexName
276
- ]);
277
- }
278
-
279
238
/**
280
239
* Determines if the combined filter in query DSL is supported.
281
240
*
0 commit comments