From d967d84cfda4b53be35d51df1c1a1f48a4261f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Fri, 5 Jul 2024 17:06:13 +0200 Subject: [PATCH] add flag to only shift an index if there are documents --- .../src/main/java/org/xbib/elx/api/BasicClient.java | 2 ++ .../main/java/org/xbib/elx/api/IndexDefinition.java | 4 ++++ .../org/xbib/elx/common/AbstractAdminClient.java | 6 ++++++ .../org/xbib/elx/common/AbstractBasicClient.java | 5 +++++ .../org/xbib/elx/common/DefaultIndexDefinition.java | 13 +++++++++++++ gradle.properties | 2 +- 6 files changed, 31 insertions(+), 1 deletion(-) diff --git a/elx-api/src/main/java/org/xbib/elx/api/BasicClient.java b/elx-api/src/main/java/org/xbib/elx/api/BasicClient.java index 320432f..0f3f560 100644 --- a/elx-api/src/main/java/org/xbib/elx/api/BasicClient.java +++ b/elx-api/src/main/java/org/xbib/elx/api/BasicClient.java @@ -47,6 +47,8 @@ public interface BasicClient extends Closeable { boolean isIndexExists(IndexDefinition indexDefinition); + boolean isIndexEmpty(IndexDefinition indexDefinition); + String getIndexState(IndexDefinition indexDefinition); boolean isIndexOpen(IndexDefinition indexDefinition); diff --git a/elx-api/src/main/java/org/xbib/elx/api/IndexDefinition.java b/elx-api/src/main/java/org/xbib/elx/api/IndexDefinition.java index c7f3a7e..6924f52 100644 --- a/elx-api/src/main/java/org/xbib/elx/api/IndexDefinition.java +++ b/elx-api/src/main/java/org/xbib/elx/api/IndexDefinition.java @@ -59,6 +59,10 @@ public interface IndexDefinition { boolean isShiftEnabled(); + void setShiftNotEmpty(boolean shiftNotEmpty); + + boolean isShiftNotEmpty(); + void setPrune(boolean prune); boolean isPruneEnabled(); diff --git a/elx-common/src/main/java/org/xbib/elx/common/AbstractAdminClient.java b/elx-common/src/main/java/org/xbib/elx/common/AbstractAdminClient.java index 79f8380..d00f6ff 100644 --- a/elx-common/src/main/java/org/xbib/elx/common/AbstractAdminClient.java +++ b/elx-common/src/main/java/org/xbib/elx/common/AbstractAdminClient.java @@ -351,6 +351,12 @@ public abstract class AbstractAdminClient extends AbstractBasicClient implements return new EmptyIndexShiftResult(); } if (indexDefinition.isShiftEnabled()) { + if (indexDefinition.isShiftNotEmpty() && isIndexEmpty(indexDefinition)) { + logger.log(Level.WARNING, "index is empty, deleting index and rejecting to continue shifting: " + + indexDefinition); + deleteIndex(indexDefinition); + return new EmptyIndexShiftResult(); + } if (indexDefinition.isCloseShifted()) { getAlias(indexDefinition.getIndex()).stream() .filter(s -> !s.equals(indexDefinition.getFullIndexName())) diff --git a/elx-common/src/main/java/org/xbib/elx/common/AbstractBasicClient.java b/elx-common/src/main/java/org/xbib/elx/common/AbstractBasicClient.java index e273ab6..27680cc 100644 --- a/elx-common/src/main/java/org/xbib/elx/common/AbstractBasicClient.java +++ b/elx-common/src/main/java/org/xbib/elx/common/AbstractBasicClient.java @@ -212,6 +212,11 @@ public abstract class AbstractBasicClient implements BasicClient { return indicesExistsResponse.isExists(); } + @Override + public boolean isIndexEmpty(IndexDefinition indexDefinition) { + return getSearchableDocs(indexDefinition) == 0L; + } + @Override public String getIndexState(IndexDefinition indexDefinition) { ClusterStateRequest clusterStateRequest = new ClusterStateRequest(); diff --git a/elx-common/src/main/java/org/xbib/elx/common/DefaultIndexDefinition.java b/elx-common/src/main/java/org/xbib/elx/common/DefaultIndexDefinition.java index 2b56aa2..4bcc657 100644 --- a/elx-common/src/main/java/org/xbib/elx/common/DefaultIndexDefinition.java +++ b/elx-common/src/main/java/org/xbib/elx/common/DefaultIndexDefinition.java @@ -44,6 +44,8 @@ public class DefaultIndexDefinition implements IndexDefinition { private boolean shift; + private boolean shiftNotEmpty; + private boolean prune; private boolean forcemerge; @@ -74,6 +76,7 @@ public class DefaultIndexDefinition implements IndexDefinition { setFullIndexName(index + getDateTimeFormatter().format(LocalDateTime.now())); setShardCount(1); setShift(false); + setShiftNotEmpty(false); setPrune(false); setForceMerge(false); setCloseShifted(false); @@ -265,6 +268,16 @@ public class DefaultIndexDefinition implements IndexDefinition { return shift; } + @Override + public void setShiftNotEmpty(boolean shiftNotEmpty) { + this.shiftNotEmpty = shiftNotEmpty; + } + + @Override + public boolean isShiftNotEmpty() { + return shiftNotEmpty; + } + @Override public void setPrune(boolean prune) { this.prune = prune; diff --git a/gradle.properties b/gradle.properties index 8f527ec..32fa9c9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ group = org.xbib name = elx -version = 7.10.2.45 +version = 7.10.2.46