Commit db7de125 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

ES6 with REST client

- TransportClient is deprecated
parent 27967fb9
......@@ -486,12 +486,12 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.2.5.RELEASE</version>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.8.6</version>
<version>6.8.7</version>
</dependency>
<dependency>
......
......@@ -42,13 +42,11 @@ import java.util.Collection;
/**
* Based on {@link NodeClientFactoryBean }
*/
public class EmbeddedNodeClientFactoryBean implements FactoryBean<Client>, InitializingBean, DisposableBean {
public class EmbeddedNodeFactoryBean implements FactoryBean<Node>, InitializingBean, DisposableBean {
private static final Logger logger = LoggerFactory.getLogger(EmbeddedNodeClientFactoryBean.class);
private boolean local;
private boolean enableHttp;
private static final Logger LOG = LoggerFactory.getLogger(EmbeddedNodeFactoryBean.class);
private String clusterName;
private NodeClient nodeClient;
private Node node;
private String pathData;
private String pathHome;
private String pathConfiguration;
......@@ -69,15 +67,11 @@ public class EmbeddedNodeClientFactoryBean implements FactoryBean<Client>, Initi
}
}
public EmbeddedNodeClientFactoryBean() {}
public EmbeddedNodeClientFactoryBean(boolean local) {
this.local = local;
}
public EmbeddedNodeFactoryBean() {}
@Override
public NodeClient getObject() throws Exception {
return nodeClient;
public Node getObject() throws Exception {
return node;
}
@Override
......@@ -102,7 +96,7 @@ public class EmbeddedNodeClientFactoryBean implements FactoryBean<Client>, Initi
.put("cluster.routing.allocation.disk.threshold_enabled", false)
.build();
nodeClient = (NodeClient) new EmbeddedNodeClientFactoryBean.TestNode(settings, asList(Netty4Plugin.class, ReindexPlugin.class)).start().client();
node = new TestNode(settings, asList(Netty4Plugin.class, ReindexPlugin.class)).start();
}
private Settings loadConfig() throws IOException {
......@@ -112,19 +106,11 @@ public class EmbeddedNodeClientFactoryBean implements FactoryBean<Client>, Initi
return Settings.builder().loadFromStream(pathConfiguration,
getClass().getClassLoader().getResourceAsStream(pathConfiguration), false).build();
}
logger.error(String.format("Unable to read node configuration from file [%s]", pathConfiguration));
LOG.error(String.format("Unable to read node configuration from file [%s]", pathConfiguration));
}
return Settings.builder().build();
}
public void setLocal(boolean local) {
this.local = local;
}
public void setEnableHttp(boolean enableHttp) {
this.enableHttp = enableHttp;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
......@@ -144,12 +130,12 @@ public class EmbeddedNodeClientFactoryBean implements FactoryBean<Client>, Initi
@Override
public void destroy() throws Exception {
try {
logger.info("Closing elasticSearch client");
if (nodeClient != null) {
nodeClient.close();
LOG.info("Closing elasticSearch node");
if (node != null) {
node.close();
}
} catch (final Exception e) {
logger.error("Error closing ElasticSearch client: ", e);
LOG.error("Error closing ElasticSearch node: ", e);
}
}
}
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2020 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,23 +12,21 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.server.mvc.admin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import javax.annotation.Resource;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.genesys2.server.component.elastic.ElasticReindex;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.service.ElasticsearchService;
......@@ -44,7 +42,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import com.fasterxml.jackson.databind.ObjectMapper;
@Controller
......@@ -63,7 +60,7 @@ public class ElasticSearchController {
private TaskExecutor taskExecutor;
@Autowired
private Client client;
private RestHighLevelClient client;
ObjectMapper mapper = new ObjectMapper();
......@@ -74,19 +71,9 @@ public class ElasticSearchController {
* @return
*/
@RequestMapping("/")
public String viewIndexesAndAliases(Model model) {
ImmutableOpenMap<String, IndexMetaData> indicesImmutableMap = client.admin().cluster().prepareState().execute().actionGet().getState().getMetaData().getIndices();
Map<String, List<AliasMetaData>> indexMap = new HashMap<>();
for (ObjectObjectCursor<String, IndexMetaData> cursor : indicesImmutableMap) {
List<AliasMetaData> innerAliasList = new ArrayList<>();
for (ObjectObjectCursor<String, AliasMetaData> innerCursor : cursor.value.getAliases()) {
innerAliasList.add(innerCursor.value);
}
indexMap.put(cursor.key, innerAliasList);
}
model.addAttribute("indexes", indexMap);
public String viewIndexesAndAliases(Model model) throws IOException {
GetIndexResponse getIndexResponse = client.indices().get(new GetIndexRequest("*"), RequestOptions.DEFAULT);
model.addAttribute("indexes", getIndexResponse.getAliases());
model.addAttribute("reindexTypes", createReindexTypesMap());
model.addAttribute("updateQueueSize", elasticReindexQueue.size());
......
......@@ -17,12 +17,18 @@
package org.genesys2.spring.config;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.elasticsearch.client.Client;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.node.Node;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.dataset.Dataset;
......@@ -30,7 +36,7 @@ import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys.catalog.model.vocab.ControlledVocabulary;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys.custom.elasticsearch.EmbeddedNodeClientFactoryBean;
import org.genesys.custom.elasticsearch.EmbeddedNodeFactoryBean;
import org.genesys2.server.component.aspect.ExtraReindexingAspect;
import org.genesys2.server.component.elastic.ElasticJPAListener;
import org.genesys2.server.component.elastic.ElasticJacksonAnnotationIntrospector;
......@@ -51,8 +57,14 @@ import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.TransportClientFactoryBean;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
import org.springframework.data.elasticsearch.core.ElasticsearchEntityMapper;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.EntityMapper;
import org.springframework.data.mapping.MappingException;
......@@ -70,7 +82,7 @@ import com.hazelcast.core.ILock;
* Elasticsearch config.
*/
@Configuration
public class ElasticsearchConfig {
public class ElasticsearchConfig extends ElasticsearchConfigurationSupport {
/** The Constant LOG. */
public static final Logger LOG = LoggerFactory.getLogger(ElasticsearchConfig.class);
......@@ -102,32 +114,59 @@ public class ElasticsearchConfig {
public BlockingQueue<ElasticReindex> elasticReindexQueue() {
return new LinkedBlockingQueue<>();
}
/**
* Elastic client.
*
* @return factory bean to create Elasticsearch client
*/
@Bean
public FactoryBean<? extends Client> elasticClient() {
@Bean("embeddedNode")
public FactoryBean<? extends Node> embeddedNode() {
if ("embedded".equals(clusterNodes)) {
LOG.info("Using embedded Elasticsearch cluster.name={} data.home={}", clusterName, embeddedPathData);
final EmbeddedNodeClientFactoryBean embedded = new EmbeddedNodeClientFactoryBean(true);
final EmbeddedNodeFactoryBean embedded = new EmbeddedNodeFactoryBean();
embedded.setPathHome(embeddedPathHome);
embedded.setPathData(embeddedPathData);
embedded.setClusterName(clusterName);
return embedded;
}
return null;
}
/**
* Elastic client.
*
* @return Elasticsearch restHighLevelClient
*/
@Bean
@DependsOn({ "embeddedNode" })
public RestHighLevelClient elasticClient() throws MalformedURLException {
if ("embedded".equals(clusterNodes)) {
return RestClients.create(ClientConfiguration.localhost()).rest();
} else {
LOG.info("Using external Elasticsearch cluster.name={} nodes={}", clusterName, clusterNodes);
final TransportClientFactoryBean tcfb = new TransportClientFactoryBean();
tcfb.setClusterName(clusterName);
tcfb.setClusterNodes(clusterNodes);
tcfb.setClientTransportSniff(false);
tcfb.setClientPingTimeout("5s");
return tcfb;
LOG.info("Using external Elasticsearch nodes={}", clusterNodes);
ArrayList<HttpHost> httpHosts = new ArrayList<>();
for (String host : clusterNodes.split(",")) {
URL hostUrl = new URL(host);
httpHosts.add(new HttpHost(hostUrl.getHost(), hostUrl.getPort(), hostUrl.getProtocol()));
}
return new RestHighLevelClient(RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()])));
}
}
@Bean
@Override
public EntityMapper entityMapper() {
ElasticsearchEntityMapper entityMapper = new GenesysEntityMapper();
return entityMapper;
}
/**
* Creates {@link ElasticsearchOperations}.
*
* @return never {@literal null}.
*/
@Bean(name = {"elasticsearchOperations", "elasticsearchTemplate"})
public ElasticsearchOperations elasticsearchOperations() throws Exception {
return new ElasticsearchRestTemplate(elasticClient(), entityMapper());
}
/**
* Elasticsearch admin lock.
*
......@@ -173,10 +212,10 @@ public class ElasticsearchConfig {
* @param client the client
* @return the elasticsearch template
*/
@Bean
public ElasticsearchTemplate elasticsearchTemplate(final Client client) {
return new ElasticsearchTemplate(client, new GenesysEntityMapper());
}
// @Bean
// public ElasticsearchTemplate elasticsearchTemplate(final Client client) {
// return new ElasticsearchTemplate(client, new GenesysEntityMapper());
// }
/**
* Aspects to listen to entity events.
......@@ -201,7 +240,7 @@ public class ElasticsearchConfig {
/**
* The Class GenesysEntityMapper.
*/
public static class GenesysEntityMapper implements EntityMapper {
public class GenesysEntityMapper extends ElasticsearchEntityMapper {
private final ObjectMapper mapper;
......@@ -209,6 +248,8 @@ public class ElasticsearchConfig {
* Instantiates a new genesys entity mapper.
*/
public GenesysEntityMapper() {
super(elasticsearchMappingContext(), new DefaultConversionService());
mapper = new ObjectMapper();
Hibernate5Module hibernateModule = new Hibernate5Module();
......
......@@ -18,14 +18,15 @@ package org.genesys.test.config;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.node.Node;
import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys.catalog.model.vocab.ControlledVocabulary;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys.custom.elasticsearch.EmbeddedNodeClientFactoryBean;
import org.genesys.custom.elasticsearch.EmbeddedNodeFactoryBean;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.ActivityPost;
import org.genesys2.server.model.impl.Article;
......@@ -41,13 +42,20 @@ import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
import org.springframework.data.elasticsearch.core.ElasticsearchEntityMapper;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.EntityMapper;
/**
* ElasticsearchConfig for JUnit
*/
@Configuration
public class TestElasticsearchConfig implements InitializingBean, DisposableBean {
public class TestElasticsearchConfig extends ElasticsearchConfigurationSupport implements InitializingBean, DisposableBean {
/** The data path. */
private File dataPath;
......@@ -76,19 +84,33 @@ public class TestElasticsearchConfig implements InitializingBean, DisposableBean
FileUtils.deleteQuietly(homePath);
}
/**
* Node client.
*
* @return the node client
*/
@Bean
public FactoryBean<? extends Client> elasticClient() {
final EmbeddedNodeClientFactoryBean embedded = new EmbeddedNodeClientFactoryBean();
@Bean("embeddedNode")
public FactoryBean<? extends Node> embeddedNode() {
final EmbeddedNodeFactoryBean embedded = new EmbeddedNodeFactoryBean();
embedded.setPathHome(this.homePath.getAbsolutePath());
embedded.setPathData(this.dataPath.getAbsolutePath());
embedded.setClusterName(clusterName);
return embedded;
}
@Bean
@DependsOn({ "embeddedNode" })
public RestHighLevelClient elasticClient() {
return RestClients.create(ClientConfiguration.localhost()).rest();
}
@Bean
@Override
public EntityMapper entityMapper() {
ElasticsearchEntityMapper entityMapper = new ElasticsearchConfig().new GenesysEntityMapper();
entityMapper.setConversions(elasticsearchCustomConversions());
return entityMapper;
}
@Bean(name = {"elasticsearchOperations", "elasticsearchTemplate"})
public ElasticsearchOperations elasticsearchOperations() {
return new ElasticsearchRestTemplate(elasticClient(), entityMapper());
}
/**
* Elasticsearch service.
......@@ -116,19 +138,5 @@ public class TestElasticsearchConfig implements InitializingBean, DisposableBean
return es;
}
/**
* Elasticsearch template.
*
* @param client the client
* @param entityMapper the entity mapper
* @return the elasticsearch template
*/
@Bean
public ElasticsearchTemplate elasticsearchTemplate(final Client client) {
final ElasticsearchTemplate t = new ElasticsearchTemplate(client, new ElasticsearchConfig.GenesysEntityMapper());
return t;
}
}
......@@ -8,7 +8,7 @@ import org.genesys2.spring.config.ElasticsearchConfig.GenesysEntityMapper;
import org.junit.Test;
public class ElasticSerializerTest {
GenesysEntityMapper mapper = new ElasticsearchConfig.GenesysEntityMapper();
GenesysEntityMapper mapper = new ElasticsearchConfig().new GenesysEntityMapper();
@Test
public void test1() throws IOException {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment