Browse code

BM-14961 Fix: handle shared indexes on reset/init

Thomas Fricker authored on 20/06/2019 14:07:20
Showing 1 changed files
... ...
@@ -33,6 +33,7 @@ import java.util.concurrent.ConcurrentHashMap;
33 33
 import java.util.concurrent.TimeUnit;
34 34
 import java.util.concurrent.locks.Lock;
35 35
 import java.util.concurrent.locks.ReentrantLock;
36
+import java.util.regex.Pattern;
36 37
 
37 38
 import org.eclipse.core.runtime.IConfigurationElement;
38 39
 import org.eclipse.core.runtime.IExtension;
... ...
@@ -381,20 +382,34 @@ public final class ESearchActivator implements BundleActivator {
381 382
 		logger.info("reset index {}", index);
382 383
 		try {
383 384
 			client.admin().indices().prepareDelete(index).execute().actionGet();
385
+			if (isPrimary(index)) {
386
+				int count = indexes.values().stream().filter(item -> item.supportsIndex(index)).findFirst().get().count;
387
+				if (count > 1) {
388
+					for (int i = 1; i <= count; i++) {
389
+						String indexName = index + "_" + i;
390
+						logger.info("reset index {}", indexName);
391
+						client.admin().indices().prepareDelete(indexName).execute().actionGet();
392
+					}
393
+				}
394
+			}
384 395
 			logger.info("index {} reset.", index);
385 396
 		} catch (Exception e) {
386 397
 
387 398
 		}
388 399
 
389
-		initIndex(client, index);
400
+		initIndex(client, index, isPrimary(index));
390 401
 	}
391 402
 
392 403
 	public static void initIndex(String index) {
393 404
 		Client client = ESearchActivator.getClient();
394
-		initIndex(client, index);
405
+		initIndex(client, index, isPrimary(index));
406
+	}
407
+
408
+	private static boolean isPrimary(String index) {
409
+		return !Pattern.compile(".*_\\d+").matcher(index).matches();
395 410
 	}
396 411
 
397
-	public static void initIndex(Client client, String index) {
412
+	private static void initIndex(Client client, String index, boolean primary) {
398 413
 		Optional<IndexDefinition> indexDefinition = indexes.values().stream().filter(item -> item.supportsIndex(index))
399 414
 				.findFirst();
400 415
 
... ...
@@ -402,12 +417,13 @@ public final class ESearchActivator implements BundleActivator {
402 417
 			logger.warn("no SCHEMA for {}", index);
403 418
 			try {
404 419
 				client.admin().indices().prepareCreate(index).execute().actionGet();
420
+				return;
405 421
 			} catch (Exception e) {
406 422
 				logger.warn("failed to create indice {} : {}", index, e.getMessage());
407 423
 			}
408 424
 		} else {
409 425
 			IndexDefinition definition = indexDefinition.get();
410
-			int count = definition.count;
426
+			int count = primary ? definition.count : 1;
411 427
 			byte[] schema = definition.schema;
412 428
 			try {
413 429
 				if (count > 1) {
... ...
@@ -429,7 +445,6 @@ public final class ESearchActivator implements BundleActivator {
429 445
 					ClusterHealthResponse resp = client.admin().cluster().prepareHealth(index).setWaitForGreenStatus()
430 446
 							.execute().actionGet();
431 447
 					logger.debug("index health {}", resp);
432
-
433 448
 				}
434 449
 			} catch (Exception e) {
435 450
 				logger.warn("failed to create indice {} : {}", index, e.getMessage(), e);