Rework elasticsearch polling
MODIFY
common/es/src/main/java/io/apiman/common/es/util/DefaultEsClientFactory.java
common/es/src/main/java/io/apiman/common/es/util/DefaultEsClientFactory.java
@@ -34,10 +34,12 @@ import org.apache.http.nio.conn.SchemeIOSessionStrategy;
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
import org.apache.http.ssl.SSLContextBuilder;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
+import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
+import org.elasticsearch.common.unit.TimeValue;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
@@ -97,8 +99,7 @@ public class DefaultEsClientFactory extends AbstractClientFactory implements IEs
String password = config.get("client.password"); //$NON-NLS-1$
Integer timeout = NumberUtils.toInt(config.get("client.timeout"), 10000); //$NON-NLS-1$
- long pollingTime = NumberUtils.toLong(config.get("client.polling.time"), 60); //$NON-NLS-1$
- long pollingPeriod = NumberUtils.toLong(config.get("client.polling.period"), 3); //$NON-NLS-1$
+ long pollingTime = NumberUtils.toLong(config.get("client.polling.time"), 600); //$NON-NLS-1$
if (StringUtils.isBlank(protocol)) {
protocol = "http"; //$NON-NLS-1$
@@ -112,7 +113,7 @@ public class DefaultEsClientFactory extends AbstractClientFactory implements IEs
throw new RuntimeException("Missing client.host configuration for EsRegistry."); //$NON-NLS-1$
}
- logger.info(String.format("Create elasticsearch client for %s:%d over %s", host, port, protocol));
+ logger.info(String.format("Demand elasticsearch client for %s:%d over %s for index prefix %s", host, port, protocol, indexNamePrefix));
synchronized (clients) {
String clientKey = StringUtils.isNotBlank(indexNamePrefix) ? "es:" + host + ':' + port + '/' + indexNamePrefix : null;
@@ -121,6 +122,7 @@ public class DefaultEsClientFactory extends AbstractClientFactory implements IEs
if (clientKey != null && clients.containsKey(clientKey)) {
client = clients.get(clientKey);
+ logger.info("Use cached elasticsearch client with client key " + clientKey);
} else {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
if(username != null && password != null) {
@@ -146,10 +148,11 @@ public class DefaultEsClientFactory extends AbstractClientFactory implements IEs
client = new RestHighLevelClient(clientBuilder);
try {
- this.waitForElasticsearch(client, pollingTime, pollingPeriod);
+ this.waitForElasticsearch(client, pollingTime);
// put client to list if polling is successful
if(clientKey != null) {
clients.put(clientKey, client);
+ logger.info(String.format("Created elasticsearch client for %s:%d over %s for index prefix %s", host, port, protocol, indexNamePrefix));
}
} catch (Exception e) {
logger.error(e);
@@ -164,7 +167,7 @@ public class DefaultEsClientFactory extends AbstractClientFactory implements IEs
}
}
- private void waitForElasticsearch(RestHighLevelClient client, long pollingTime, long pollingPeriod) throws Exception {
+ private void waitForElasticsearch(RestHighLevelClient client, long pollingTime) throws Exception {
final Date startTime = new Date();
AtomicBoolean pollingSuccess = new AtomicBoolean(false);
@@ -177,8 +180,11 @@ public class DefaultEsClientFactory extends AbstractClientFactory implements IEs
try {
//Do Health request
ClusterHealthRequest healthRequest = new ClusterHealthRequest();
- client.cluster().health(healthRequest, RequestOptions.DEFAULT);
+ healthRequest.timeout(new TimeValue(5, TimeUnit.SECONDS));
+ final ClusterHealthResponse healthResponse = client.cluster().health(healthRequest, RequestOptions.DEFAULT);
+
+ if (!healthResponse.isTimedOut()) {
// set polling status as successful
pollingSuccess.set(true);
// measure time if health request is successful
@@ -188,13 +194,15 @@ public class DefaultEsClientFactory extends AbstractClientFactory implements IEs
// wake up the waiting thread
cdl.countDown();
+ }
+
} catch (IOException e) {
logger.info("Unable to reach Elasticsearch. Will continue polling.");
exception.setValue(e);
}
},
0, // Start immediately
- pollingPeriod, // Poll every pollingPeriod seconds
+ 10, // Poll every pollingPeriod seconds
TimeUnit.SECONDS);
cdl.await(pollingTime, TimeUnit.SECONDS); // Max wait for polling time

MODIFY
distro/vertx/src/main/resources/overlay/configs/conf-3scale-immutable-es.json
distro/vertx/src/main/resources/overlay/configs/conf-3scale-immutable-es.json
"password": null,
"timeout": 10000,
"polling": {
- "time": 60,
- "period": 3
+ "time": 60
}
}
}
"password": "${apiman.es.password}",
"timeout": "${apiman.es.timeout}",
"polling": {
- "time": "${apiman.es.polling.time}",
- "period": "${apiman.es.polling.period}"
+ "time": "${apiman.es.polling.time}"
}
}
}
"password": "${apiman.es.password}",
"timeout": "${apiman.es.timeout}",
"polling": {
- "time": "${apiman.es.polling.time}",
- "period": "${apiman.es.polling.period}"
+ "time": "${apiman.es.polling.time}"
}
}
}
"password": "${apiman.es.password}",
"timeout": "${apiman.es.timeout}",
"polling": {
- "time": "${apiman.es.polling.time}",
- "period": "${apiman.es.polling.period}"
+ "time": "${apiman.es.polling.time}"
}
}
}
"password": "${apiman.es.password}",
"timeout": "${apiman.es.timeout}",
"polling": {
- "time": "${apiman.es.polling.time}",
- "period": "${apiman.es.polling.period}"
+ "time": "${apiman.es.polling.time}"
}
}
}

MODIFY
distro/vertx/src/main/resources/overlay/configs/conf-es.json
distro/vertx/src/main/resources/overlay/configs/conf-es.json
"password": null,
"timeout": 10000,
"polling": {
- "time": 60,
- "period": 3
+ "time": 60
}
}
}
"port": "${apiman.es.port}",
"initialize": true,
"polling": {
- "time": "${apiman.es.polling.time}",
- "period": "${apiman.es.polling.period}"
+ "time": "${apiman.es.polling.time}"
},
"username": "${apiman.es.username}",
"password": "${apiman.es.password}",

MODIFY
distro/vertx/src/main/resources/overlay/configs/conf-headless-es.json
distro/vertx/src/main/resources/overlay/configs/conf-headless-es.json
"password": null,
"timeout": 10000,
"polling": {
- "time": 60,
- "period": 3
+ "time": 60
}
}
}
"port": "${apiman.es.port}",
"initialize": true,
"polling": {
- "time": "${apiman.es.polling.time}",
- "period": "${apiman.es.polling.period}"
+ "time": "${apiman.es.polling.time}"
},
"username": "${apiman.es.username}",
"password": "${apiman.es.password}",
"port": "${apiman.es.port}",
"initialize": true,
"polling": {
- "time": "${apiman.es.polling.time}",
- "period": "${apiman.es.polling.period}"
+ "time": "${apiman.es.polling.time}"
},
"username": "${apiman.es.username}",
"password": "${apiman.es.password}",
"port": "${apiman.es.port}",
"initialize": true,
"polling": {
- "time": "${apiman.es.polling.time}",
- "period": "${apiman.es.polling.period}"
+ "time": "${apiman.es.polling.time}"
},
"username": "${apiman.es.username}",
"password": "${apiman.es.password}",
"port": "${apiman.es.port}",
"initialize": true,
"polling": {
- "time": "${apiman.es.polling.time}",
- "period": "${apiman.es.polling.period}"
+ "time": "${apiman.es.polling.time}"
},
"username": "${apiman.es.username}",
"password": "${apiman.es.password}",

MODIFY
distro/wildfly/src/main/resources/overlay/standalone/configuration/apiman.properties
distro/wildfly/src/main/resources/overlay/standalone/configuration/apiman.properties
@@ -27,7 +27,6 @@ apiman-gateway.jdbc.jndi-location=java:jboss/datasources/apiman-gateway
#apiman.es.password=
#apiman.es.timeout=10000
#apiman.es.polling.time=
-#apiman.es.polling.period=
#apiman.es.initialize=true
#apiman.es.keystore=
#apiman.es.keystore.password=
@@ -71,7 +70,6 @@ apiman-manager.storage.jpa.initialize=true
#apiman-manager.storage.es.client.timeout=${apiman.es.timeout}
#apiman-manager.storage.es.client.initialize=${apiman.es.initialize}
#apiman-manager.storage.es.client.polling.time=${apiman.es.polling.time}
-#apiman-manager.storage.es.client.polling.period=${apiman.es.polling.period}
#apiman-manager.storage.es.client.keystore=${apiman.es.keystore}
#apiman-manager.storage.es.client.keystore.password=${apiman.es.keystore.password}
#apiman-manager.storage.es.client.truststore=${apiman.es.truststore}
@@ -92,7 +90,6 @@ apiman-manager.metrics.datasource.jndi-location=${apiman-gateway.jdbc.jndi-locat
#apiman-manager.metrics.es.client.password=${apiman.es.password}
#apiman-manager.metrics.es.client.timeout=${apiman.es.timeout}
#apiman-manager.metrics.es.client.polling.time=${apiman.es.polling.time}
-#apiman-manager.metrics.es.client.polling.period=${apiman.es.polling.period}
#apiman-manager.metrics.es.client.keystore=${apiman.es.keystore}
#apiman-manager.metrics.es.client.keystore.password=${apiman.es.keystore.password}
#apiman-manager.metrics.es.client.truststore=${apiman.es.truststore}
@@ -145,7 +142,6 @@ apiman-gateway.metrics.datasource.jndi-location=${apiman-gateway.jdbc.jndi-locat
#apiman-gateway.metrics.client.timeout=${apiman.es.timeout}
#apiman-gateway.metrics.client.initialize=${apiman.es.initialize}
#apiman-gateway.metrics.client.polling.time=${apiman.es.polling.time}
-#apiman-gateway.metrics.client.polling.period=${apiman.es.polling.period}
#apiman-gateway.metrics.client.keystore=${apiman.es.keystore}
#apiman-gateway.metrics.client.keystore.password=${apiman.es.keystore.password}
#apiman-gateway.metrics.client.truststore=${apiman.es.truststore}
@@ -207,7 +203,6 @@ apiman-gateway.initializers.jdbc.datasource.type=h2
#apiman-gateway.registry.client.timeout=${apiman.es.timeout}
#apiman-gateway.registry.client.initialize=${apiman.es.initialize}
#apiman-gateway.registry.client.polling.time=${apiman.es.polling.time}
-#apiman-gateway.registry.client.polling.period=${apiman.es.polling.period}
#apiman-gateway.registry.client.keystore=${apiman.es.keystore}
#apiman-gateway.registry.client.keystore.password=${apiman.es.keystore.password}
#apiman-gateway.registry.client.truststore=${apiman.es.truststore}
@@ -229,7 +224,6 @@ apiman-gateway.components.ISharedStateComponent=io.apiman.gateway.engine.impl.In
#apiman-gateway.components.ISharedStateComponent.client.timeout=${apiman.es.timeout}
#apiman-gateway.components.ISharedStateComponent.client.initialize=${apiman.es.initialize}
#apiman-gateway.components.ISharedStateComponent.client.polling.time=${apiman.es.polling.time}
-#apiman-gateway.components.ISharedStateComponent.client.polling.period=${apiman.es.polling.period}
#apiman-gateway.components.ISharedStateComponent.client.keystore=${apiman.es.keystore}
#apiman-gateway.components.ISharedStateComponent.client.keystore.password=${apiman.es.keystore.password}
#apiman-gateway.components.ISharedStateComponent.client.truststore=${apiman.es.truststore}
@@ -252,7 +246,6 @@ apiman-gateway.components.IRateLimiterComponent=io.apiman.gateway.engine.impl.In
#apiman-gateway.components.IRateLimiterComponent.client.timeout=${apiman.es.timeout}
#apiman-gateway.components.IRateLimiterComponent.client.initialize=${apiman.es.initialize}
#apiman-gateway.components.IRateLimiterComponent.client.polling.time=${apiman.es.polling.time}
-#apiman-gateway.components.IRateLimiterComponent.client.polling.period=${apiman.es.polling.period}
#apiman-gateway.components.IRateLimiterComponent.client.keystore=${apiman.es.keystore}
#apiman-gateway.components.IRateLimiterComponent.client.keystore.password=${apiman.es.keystore.password}
#apiman-gateway.components.IRateLimiterComponent.client.truststore=${apiman.es.truststore}
@@ -274,7 +267,6 @@ apiman-gateway.components.ICacheStoreComponent=io.apiman.gateway.engine.impl.InM
#apiman-gateway.components.ICacheStoreComponent.client.timeout=${apiman.es.timeout}
#apiman-gateway.components.ICacheStoreComponent.client.initialize=${apiman.es.initialize}
#apiman-gateway.components.ICacheStoreComponent.client.polling.time=${apiman.es.polling.time}
-#apiman-gateway.components.ICacheStoreComponent.client.polling.period=${apiman.es.polling.period}
#apiman-gateway.components.ICacheStoreComponent.client.keystore=${apiman.es.keystore}
#apiman-gateway.components.ICacheStoreComponent.client.keystore.password=${apiman.es.keystore.password}
#apiman-gateway.components.ICacheStoreComponent.client.truststore=${apiman.es.truststore}

MODIFY
gateway/platforms/vertx3/vertx3/README.adoc
gateway/platforms/vertx3/vertx3/README.adoc
@@ -43,8 +43,7 @@ For example:
"port": "9200",
"initialize": true,
"polling": {
- "time": 60,
- "period": 3
+ "time": 60
}
"apiman": ["great", "fantastic", "excellent"]
}
@@ -60,7 +59,6 @@ client.host=127.0.0.1
client.port=9200
client.initialize=true
client.polling.time=60
-client.es.polling.period=3
client.apiman=great,fantastic,excellent
```

Rework elasticsearch polling (#901)