chennan 8 سال پیش
والد
کامیت
afc176e438

+ 2 - 2
src/main/java/org/es/lu/CommonConfig.java

@@ -4,13 +4,13 @@ import org.springframework.stereotype.Component;
 
 @Component
 public class CommonConfig {
-    public boolean getClusterReadable(String clusterKey) {
+    public boolean isConfiguredClusterActive(String clusterKey) {
         if ("BX".equalsIgnoreCase(clusterKey)) {
             return true;
         }
 
         if ("YP".equalsIgnoreCase(clusterKey)) {
-            return false;
+            return true;
         }
         return false;
     }

+ 2 - 2
src/main/java/org/es/lu/ElasticsearchCluster.java

@@ -53,8 +53,8 @@ public class ElasticsearchCluster implements InitializingBean, DisposableBean {
         return client;
     }
 
-    public boolean isClusterReadable() {
-        return commonConfig.getClusterReadable(this.clusterKey);
+    public boolean isConfiguredClusterActive() {
+        return commonConfig.isConfiguredClusterActive(this.clusterKey);
     }
 
     public void setCommonConfig(CommonConfig commonConfig) {

+ 30 - 10
src/main/java/org/es/lu/ElasticsearchClusterManager.java

@@ -21,13 +21,13 @@ public class ElasticsearchClusterManager {
     @Qualifier("cluster02")
     private ElasticsearchCluster cluster02;
 
-    public ElasticsearchCluster[] getReadableClusters() {
+    public ElasticsearchCluster[] getConfiguredActiveClusters() {
         List<ElasticsearchCluster> clusterList = Lists.newArrayList();
-        if (cluster01.isClusterReadable()) {
+        if (cluster01.isConfiguredClusterActive()) {
             clusterList.add(cluster01);
         }
 
-        if (cluster02.isClusterReadable()) {
+        if (cluster02.isConfiguredClusterActive()) {
             clusterList.add(cluster02);
         }
         return clusterList.toArray(new ElasticsearchCluster[clusterList.size()]);
@@ -44,13 +44,34 @@ public class ElasticsearchClusterManager {
         return null;
     }
 
-    public ElasticsearchCluster getReadableCluster() {
-        ElasticsearchCluster[] clusters = getReadableClusters();
-        if (clusters.length == 0) {
+    public ElasticsearchCluster getReadableCluster(String... indices) {
+        ElasticsearchCluster[] configuredActiveClusters = getConfiguredActiveClusters();
+        if (configuredActiveClusters.length == 0 || indices == null || indices.length == 0) {
             return null;
         }
-        if (clusters.length == 1) {
-            return clusters[0];
+
+        List<ElasticsearchCluster> readableClusterList = Lists.newArrayList();
+        for (ElasticsearchCluster configuredActiveCluster : configuredActiveClusters) {
+            boolean isIndexReadable = true;
+            for (String indexName : indices) {
+                IndexState indexState = configuredActiveCluster.indexState(indexName);
+                if (indexState.getIndexStatus() != IndexState.IndexStatus.GREEN
+                        && indexState.getIndexStatus() != IndexState.IndexStatus.YELLOW) {
+                    isIndexReadable = false;
+                    break;
+                }
+            }
+
+            if (isIndexReadable) {
+                readableClusterList.add(configuredActiveCluster);
+            }
+        }
+
+        if (readableClusterList.size() == 0) {
+            return null;
+        }
+        if (readableClusterList.size() == 1) {
+            return readableClusterList.get(0);
         }
 
         int index = requestCount.incrementAndGet();
@@ -58,7 +79,6 @@ public class ElasticsearchClusterManager {
             index = 0;
             requestCount.set(index);
         }
-        return clusters[index % clusters.length];
+        return readableClusterList.get(index % readableClusterList.size());
     }
-
 }

+ 8 - 4
src/test/java/org/es/test/lu/ElasticsearchClusterTest.java

@@ -23,7 +23,7 @@ public class ElasticsearchClusterTest {
 
     @Test
     public void test_initCluster() throws Exception {
-        for (int i = 0; i < 5; i++) {
+        for (int i = 0; i < 500; i++) {
             ElasticsearchCluster cluster = clusterManager.getCluster("BX");
             IndexState indexState = cluster.indexState("index");
             System.out.println(cluster.getClusterKey() + " ===========>" + indexState);
@@ -38,10 +38,14 @@ public class ElasticsearchClusterTest {
 
     @Test
     public void test_getReadableCluster() throws Exception {
-        for (int i = 0; i < 5; i++) {
-            ElasticsearchCluster cluster = clusterManager.getReadableCluster();
+        for (int i = 0; i < 500000; i++) {
+            ElasticsearchCluster cluster = clusterManager.getReadableCluster(".custom-dictionary");
+            if (cluster == null) {
+                System.out.println("cluster is null, continue.");
+                TimeUnit.SECONDS.sleep(1);
+                continue;
+            }
             System.out.println(cluster.getClusterKey());
-
             TimeUnit.SECONDS.sleep(1);
         }
     }