Skip to content

Commit caa7f3f

Browse files
committed
move validation, fix test
1 parent 4c3af0e commit caa7f3f

3 files changed

Lines changed: 69 additions & 73 deletions

File tree

graylog2-server/src/main/java/org/graylog2/indexer/indices/OutdatedIndexService.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import jakarta.inject.Inject;
2323
import jakarta.inject.Singleton;
2424
import jakarta.validation.constraints.NotNull;
25-
import jakarta.ws.rs.NotFoundException;
2625
import org.graylog2.indexer.ElasticsearchException;
2726
import org.graylog2.indexer.cluster.Cluster;
2827
import org.graylog2.indexer.indexset.registry.IndexSetRegistry;
@@ -68,11 +67,7 @@ public List<OutdatedIndex> getOutdatedIndices() {
6867
}
6968

7069
public void reindex(String index, boolean withReplicas) {
71-
OutdatedIndex outdatedIndex = getOutdatedIndices().stream()
72-
.filter(OutdatedIndex::isSystemIndex)
73-
.filter(i -> i.indexName().equals(index))
74-
.findAny().orElseThrow(() -> new NotFoundException("Index " + index + " not found or is no system index"));
75-
HealthStatus sourceStatus = indicesAdapter.waitForRecovery(outdatedIndex.indexName(), 2);
70+
HealthStatus sourceStatus = indicesAdapter.waitForRecovery(index, 2);
7671
if (sourceStatus != HealthStatus.Green) {
7772
throw new IllegalStateException("Index " + index + " state is not healthy: " + sourceStatus);
7873
}
@@ -156,10 +151,6 @@ private Map<String, Object> cleanIndexSettings(Map<String, Object> settings, boo
156151
}
157152

158153
public void delete(@NotNull String index) {
159-
OutdatedIndex outdatedIndex = getOutdatedIndices().stream()
160-
.filter(i -> !i.managedIndex())
161-
.filter(i -> i.indexName().equals(index))
162-
.findAny().orElseThrow(() -> new NotFoundException("Index " + index + " not found or is an index managed by Graylog"));
163-
indicesAdapter.delete(outdatedIndex.indexName());
154+
indicesAdapter.delete(index);
164155
}
165156
}

graylog2-server/src/main/java/org/graylog2/rest/resources/system/indexer/IndicesResource.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,11 @@ public List<OutdatedIndex> getOutdatedIndices() {
332332
@AuditEvent(type = AuditEventTypes.ES_INDEX_REINDEX)
333333
public void reindex(@Parameter(name = "index") @PathParam("index") @NotNull String index,
334334
@Parameter(name = "withReplication") @QueryParam("withReplication") @DefaultValue("true") boolean withReplication) {
335-
outdatedIndexService.reindex(index, withReplication);
335+
OutdatedIndex outdatedIndex = getOutdatedIndices().stream()
336+
.filter(OutdatedIndex::isSystemIndex)
337+
.filter(i -> i.indexName().equals(index))
338+
.findAny().orElseThrow(() -> new NotFoundException("Index " + index + " not found or is no system index"));
339+
outdatedIndexService.reindex(outdatedIndex.indexName(), withReplication);
336340
}
337341

338342
@DELETE
@@ -342,7 +346,11 @@ public void reindex(@Parameter(name = "index") @PathParam("index") @NotNull Stri
342346
@Produces(MediaType.APPLICATION_JSON)
343347
@AuditEvent(type = AuditEventTypes.ES_INDEX_DELETE)
344348
public void deleteOutdated(@Parameter(name = "index") @PathParam("index") @NotNull String index) {
345-
outdatedIndexService.delete(index);
349+
OutdatedIndex outdatedIndex = getOutdatedIndices().stream()
350+
.filter(i -> !i.managedIndex())
351+
.filter(i -> i.indexName().equals(index))
352+
.findAny().orElseThrow(() -> new NotFoundException("Index " + index + " not found or is an index managed by Graylog"));
353+
outdatedIndexService.delete(outdatedIndex.indexName());
346354
}
347355

348356
private OpenIndicesInfo getOpenIndicesInfo(Set<IndexStatistics> indicesStatistics) {

graylog2-server/src/test/java/org/graylog2/indexer/indices/OutdatedIndexServiceTest.java

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@
4747
@ExtendWith(MockitoExtension.class)
4848
class OutdatedIndexServiceTest {
4949

50-
@Mock
51-
Indices indices;
52-
5350
@Mock
5451
IndicesAdapter indicesAdapter;
5552

@@ -85,7 +82,7 @@ void getOutdatedIndicesSucceeds() {
8582
);
8683
when(indexSetRegistry.isManagedIndex("outdated1")).thenReturn(true);
8784
when(indexSetRegistry.isManagedIndex("outdated2")).thenReturn(false);
88-
when(indices.getOutdatedIndices(2)).thenReturn(outdatedIndices);
85+
when(indicesAdapter.getOutdatedIndices(2)).thenReturn(outdatedIndices);
8986
assertThat(outdatedIndexService.getOutdatedIndices()).isEqualTo(List.of(
9087
new OutdatedIndex("outdated1", "1.3.0", false, true),
9188
new OutdatedIndex("outdated2", "1.3.0", true, false)
@@ -95,7 +92,7 @@ void getOutdatedIndicesSucceeds() {
9592

9693
@Test
9794
void reindexFailsIfSourceIndexNotHealthy() {
98-
when(indices.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Yellow);
95+
when(indicesAdapter.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Yellow);
9996

10097
Assertions.assertThatThrownBy(() -> outdatedIndexService.reindex("my_index", true))
10198
.isInstanceOf(IllegalStateException.class)
@@ -104,8 +101,8 @@ void reindexFailsIfSourceIndexNotHealthy() {
104101

105102
@Test
106103
void reindexFailsIfSourceSettingsAreNull() {
107-
when(indices.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
108-
when(indices.indexSettings("my_index")).thenReturn(null);
104+
when(indicesAdapter.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
105+
when(indicesAdapter.getStructuredIndexSettings("my_index")).thenReturn(null);
109106

110107
Assertions.assertThatThrownBy(() -> outdatedIndexService.reindex("my_index", true))
111108
.isInstanceOf(IllegalStateException.class)
@@ -114,46 +111,46 @@ void reindexFailsIfSourceSettingsAreNull() {
114111

115112
@Test
116113
void reindexFailsIfTempIndexIsNotHealthyAfterCreation() throws IOException {
117-
when(indices.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
118-
when(indices.indexSettings("my_index")).thenReturn(sourceSettings());
119-
when(indices.indexMapping("my_index")).thenReturn(sourceMapping());
114+
when(indicesAdapter.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
115+
when(indicesAdapter.getStructuredIndexSettings("my_index")).thenReturn(sourceSettings());
116+
when(indicesAdapter.getIndexMapping("my_index")).thenReturn(sourceMapping());
120117
when(indicesAdapter.exists(".gltmp_my_index")).thenReturn(false);
121-
when(indices.waitForRecovery(".gltmp_my_index")).thenReturn(HealthStatus.Red);
118+
when(indicesAdapter.waitForRecovery(".gltmp_my_index")).thenReturn(HealthStatus.Red);
122119

123120
Assertions.assertThatThrownBy(() -> outdatedIndexService.reindex("my_index", true))
124121
.isInstanceOf(IllegalStateException.class)
125122
.hasMessageContaining("Temporary index .gltmp_my_index could not be created successfully: Red");
126123

127-
verify(indices, never()).reindex(any(), any());
128-
verify(indices, never()).delete(any());
124+
verify(indicesAdapter, never()).reindex(any(), any(), any());
125+
verify(indicesAdapter, never()).delete(any());
129126
}
130127

131128
@Test
132129
void reindexFailsIfRecreatedTargetIndexIsNotHealthy() throws IOException {
133-
when(indices.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
134-
when(indices.indexSettings("my_index")).thenReturn(sourceSettings());
135-
when(indices.indexMapping("my_index")).thenReturn(sourceMapping());
130+
when(indicesAdapter.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
131+
when(indicesAdapter.getStructuredIndexSettings("my_index")).thenReturn(sourceSettings());
132+
when(indicesAdapter.getIndexMapping("my_index")).thenReturn(sourceMapping());
136133
when(indicesAdapter.exists(".gltmp_my_index")).thenReturn(false);
137-
when(indices.waitForRecovery(".gltmp_my_index")).thenReturn(HealthStatus.Green);
138-
when(indices.waitForRecovery("my_index")).thenReturn(HealthStatus.Yellow);
134+
when(indicesAdapter.waitForRecovery(".gltmp_my_index")).thenReturn(HealthStatus.Green);
135+
when(indicesAdapter.waitForRecovery("my_index")).thenReturn(HealthStatus.Yellow);
139136

140137
Assertions.assertThatThrownBy(() -> outdatedIndexService.reindex("my_index", true))
141138
.isInstanceOf(IllegalStateException.class)
142139
.hasMessageContaining("Index my_index could not be recreated successfully: Yellow");
143140

144141
// The reindex into temp and the source delete have already happened, but the
145142
// final reindex back into the source must not run if the recreated index is unhealthy.
146-
verify(indices).reindex("my_index", ".gltmp_my_index");
147-
verify(indices).delete("my_index");
148-
verify(indices, never()).reindex(".gltmp_my_index", "my_index");
149-
verify(indices, never()).delete(".gltmp_my_index");
143+
verify(indicesAdapter).reindex(eq("my_index"), eq(".gltmp_my_index"), any());
144+
verify(indicesAdapter).delete("my_index");
145+
verify(indicesAdapter, never()).reindex(eq(".gltmp_my_index"), eq("my_index"), any());
146+
verify(indicesAdapter, never()).delete(".gltmp_my_index");
150147
}
151148

152149
@Test
153150
void reindexWrapsIOExceptionInRuntimeException() throws IOException {
154-
when(indices.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
155-
when(indices.indexSettings("my_index")).thenReturn(sourceSettings());
156-
when(indices.indexMapping("my_index")).thenReturn(sourceMapping());
151+
when(indicesAdapter.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
152+
when(indicesAdapter.getStructuredIndexSettings("my_index")).thenReturn(sourceSettings());
153+
when(indicesAdapter.getIndexMapping("my_index")).thenReturn(sourceMapping());
157154
when(indicesAdapter.exists(".gltmp_my_index")).thenThrow(new IOException("boom"));
158155

159156
Assertions.assertThatThrownBy(() -> outdatedIndexService.reindex("my_index", true))
@@ -164,38 +161,38 @@ void reindexWrapsIOExceptionInRuntimeException() throws IOException {
164161
@Test
165162
void reindexSucceedsAndPerformsAllStepsInOrder() throws IOException {
166163
Map<String, Object> sourceMapping = sourceMapping();
167-
when(indices.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
168-
when(indices.indexSettings("my_index")).thenReturn(sourceSettings());
169-
when(indices.indexMapping("my_index")).thenReturn(sourceMapping);
164+
when(indicesAdapter.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
165+
when(indicesAdapter.getStructuredIndexSettings("my_index")).thenReturn(sourceSettings());
166+
when(indicesAdapter.getIndexMapping("my_index")).thenReturn(sourceMapping);
170167
when(indicesAdapter.exists(".gltmp_my_index")).thenReturn(false);
171-
when(indices.waitForRecovery(".gltmp_my_index")).thenReturn(HealthStatus.Green);
172-
when(indices.waitForRecovery("my_index")).thenReturn(HealthStatus.Green);
168+
when(indicesAdapter.waitForRecovery(".gltmp_my_index")).thenReturn(HealthStatus.Green);
169+
when(indicesAdapter.waitForRecovery("my_index")).thenReturn(HealthStatus.Green);
173170

174171
outdatedIndexService.reindex("my_index", true);
175172

176-
InOrder inOrder = inOrder(indices, indicesAdapter);
177-
inOrder.verify(indices).waitForRecovery("my_index", 2);
178-
inOrder.verify(indices).indexSettings("my_index");
179-
inOrder.verify(indices).indexMapping("my_index");
173+
InOrder inOrder = inOrder(indicesAdapter, indicesAdapter);
174+
inOrder.verify(indicesAdapter).waitForRecovery("my_index", 2);
175+
inOrder.verify(indicesAdapter).getStructuredIndexSettings("my_index");
176+
inOrder.verify(indicesAdapter).getIndexMapping("my_index");
180177
inOrder.verify(indicesAdapter).exists(".gltmp_my_index");
181178
inOrder.verify(indicesAdapter).create(eq(".gltmp_my_index"), any(IndexSettings.class), eq(sourceMapping));
182-
inOrder.verify(indices).waitForRecovery(".gltmp_my_index");
183-
inOrder.verify(indices).reindex("my_index", ".gltmp_my_index");
184-
inOrder.verify(indices).delete("my_index");
179+
inOrder.verify(indicesAdapter).waitForRecovery(".gltmp_my_index");
180+
inOrder.verify(indicesAdapter).reindex(eq("my_index"), eq(".gltmp_my_index"), any());
181+
inOrder.verify(indicesAdapter).delete("my_index");
185182
inOrder.verify(indicesAdapter).create(eq("my_index"), any(IndexSettings.class), eq(sourceMapping));
186-
inOrder.verify(indices).waitForRecovery("my_index");
187-
inOrder.verify(indices).reindex(".gltmp_my_index", "my_index");
188-
inOrder.verify(indices).delete(".gltmp_my_index");
183+
inOrder.verify(indicesAdapter).waitForRecovery("my_index");
184+
inOrder.verify(indicesAdapter).reindex(eq(".gltmp_my_index"), eq("my_index"), any());
185+
inOrder.verify(indicesAdapter).delete(".gltmp_my_index");
189186
}
190187

191188
@Test
192189
void reindexCleansSourceSettingsBeforeCreatingTempIndex() throws IOException {
193-
when(indices.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
194-
when(indices.indexSettings("my_index")).thenReturn(sourceSettings());
195-
when(indices.indexMapping("my_index")).thenReturn(sourceMapping());
190+
when(indicesAdapter.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
191+
when(indicesAdapter.getStructuredIndexSettings("my_index")).thenReturn(sourceSettings());
192+
when(indicesAdapter.getIndexMapping("my_index")).thenReturn(sourceMapping());
196193
when(indicesAdapter.exists(".gltmp_my_index")).thenReturn(false);
197-
when(indices.waitForRecovery(".gltmp_my_index")).thenReturn(HealthStatus.Green);
198-
when(indices.waitForRecovery("my_index")).thenReturn(HealthStatus.Green);
194+
when(indicesAdapter.waitForRecovery(".gltmp_my_index")).thenReturn(HealthStatus.Green);
195+
when(indicesAdapter.waitForRecovery("my_index")).thenReturn(HealthStatus.Green);
199196

200197
outdatedIndexService.reindex("my_index", true);
201198

@@ -214,12 +211,12 @@ void reindexCleansSourceSettingsBeforeCreatingTempIndex() throws IOException {
214211

215212
@Test
216213
void reindexWithoutReplicasOverridesNumberOfReplicasOnTempIndex() throws IOException {
217-
when(indices.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
218-
when(indices.indexSettings("my_index")).thenReturn(sourceSettings());
219-
when(indices.indexMapping("my_index")).thenReturn(sourceMapping());
214+
when(indicesAdapter.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
215+
when(indicesAdapter.getStructuredIndexSettings("my_index")).thenReturn(sourceSettings());
216+
when(indicesAdapter.getIndexMapping("my_index")).thenReturn(sourceMapping());
220217
when(indicesAdapter.exists(".gltmp_my_index")).thenReturn(false);
221-
when(indices.waitForRecovery(".gltmp_my_index")).thenReturn(HealthStatus.Green);
222-
when(indices.waitForRecovery("my_index")).thenReturn(HealthStatus.Green);
218+
when(indicesAdapter.waitForRecovery(".gltmp_my_index")).thenReturn(HealthStatus.Green);
219+
when(indicesAdapter.waitForRecovery("my_index")).thenReturn(HealthStatus.Green);
223220

224221
outdatedIndexService.reindex("my_index", false);
225222

@@ -232,28 +229,28 @@ void reindexWithoutReplicasOverridesNumberOfReplicasOnTempIndex() throws IOExcep
232229

233230
@Test
234231
void reindexStripsDotsFromIndexNameForTempIndex() throws IOException {
235-
when(indices.waitForRecovery("graylog_2.0", 2)).thenReturn(HealthStatus.Green);
236-
when(indices.indexSettings("graylog_2.0")).thenReturn(sourceSettings());
237-
when(indices.indexMapping("graylog_2.0")).thenReturn(sourceMapping());
232+
when(indicesAdapter.waitForRecovery("graylog_2.0", 2)).thenReturn(HealthStatus.Green);
233+
when(indicesAdapter.getStructuredIndexSettings("graylog_2.0")).thenReturn(sourceSettings());
234+
when(indicesAdapter.getIndexMapping("graylog_2.0")).thenReturn(sourceMapping());
238235
when(indicesAdapter.exists(".gltmp_graylog_20")).thenReturn(false);
239-
when(indices.waitForRecovery(".gltmp_graylog_20")).thenReturn(HealthStatus.Green);
240-
when(indices.waitForRecovery("graylog_2.0")).thenReturn(HealthStatus.Green);
236+
when(indicesAdapter.waitForRecovery(".gltmp_graylog_20")).thenReturn(HealthStatus.Green);
237+
when(indicesAdapter.waitForRecovery("graylog_2.0")).thenReturn(HealthStatus.Green);
241238

242239
outdatedIndexService.reindex("graylog_2.0", true);
243240

244241
verify(indicesAdapter).exists(".gltmp_graylog_20");
245242
verify(indicesAdapter).create(eq(".gltmp_graylog_20"), any(IndexSettings.class), any());
246-
verify(indices).reindex("graylog_2.0", ".gltmp_graylog_20");
247-
verify(indices).reindex(".gltmp_graylog_20", "graylog_2.0");
243+
verify(indicesAdapter).reindex(eq("graylog_2.0"), eq(".gltmp_graylog_20"), any());
244+
verify(indicesAdapter).reindex(eq(".gltmp_graylog_20"), eq("graylog_2.0"), any());
248245
}
249246

250247
@Test
251248
void reindexFailsIfSourceSettingsContainsNonMapValue() {
252249
Map<String, Object> badSettings = new HashMap<>();
253250
badSettings.put("index", "not_a_map");
254-
when(indices.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
255-
when(indices.indexSettings("my_index")).thenReturn(badSettings);
256-
when(indices.indexMapping("my_index")).thenReturn(sourceMapping());
251+
when(indicesAdapter.waitForRecovery("my_index", 2)).thenReturn(HealthStatus.Green);
252+
when(indicesAdapter.getStructuredIndexSettings("my_index")).thenReturn(badSettings);
253+
when(indicesAdapter.getIndexMapping("my_index")).thenReturn(sourceMapping());
257254

258255
Assertions.assertThatThrownBy(() -> outdatedIndexService.reindex("my_index", true))
259256
.isInstanceOf(IllegalStateException.class)

0 commit comments

Comments
 (0)