ElasticSearchController.java

/*
 * 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.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * 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.genesys.server.mvc.admin;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;

import javax.annotation.Resource;

import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.genesys.blocks.model.EmptyModel;
import org.genesys.blocks.model.filters.EmptyModelFilter;
import org.genesys.server.component.elastic.ElasticReindex;
import org.genesys.server.exception.InvalidApiUsageException;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.filter.AccessionFilter;
import org.genesys.spring.config.HazelcastConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.hazelcast.cp.lock.FencedLock;

@Controller
@RequestMapping("/admin/elastic")
@PreAuthorize("hasRole('ADMINISTRATOR')")
public class ElasticSearchController {
	public static final Logger LOG = LoggerFactory.getLogger(ElasticSearchController.class);

	@Autowired(required = false)
	private ElasticsearchService elasticsearchService;

	@Resource
	private BlockingQueue<ElasticReindex> elasticReindexQueue;

	@Resource
	private FencedLock elasticsearchReindexLock;

	@Autowired
	private TaskExecutor taskExecutor;

	@Autowired
	private RestHighLevelClient client;

	@Resource
	@Qualifier("clusterFlags")
	private Map<String, Boolean> clusterFlags;

	ObjectMapper mapper = new ObjectMapper();

	/**
	 * Renders view where indexes and their aliases are displayed.
	 *
	 * @param model
	 * @return
	 */
	@RequestMapping("/")
	public String viewIndexesAndAliases(Model model) throws IOException {
		if (client.indices().exists(new GetIndexRequest("*"), RequestOptions.DEFAULT)) {
			GetIndexResponse getIndexResponse = client.indices().get(new GetIndexRequest("*"), RequestOptions.DEFAULT);
			model.addAttribute("indexes", getIndexResponse.getAliases());
			model.addAttribute("reindexTypes", createReindexTypesMap());

			model.addAttribute("updateQueueSize", elasticReindexQueue.size());
			model.addAttribute("elasticsearchReindexLock", elasticsearchReindexLock.isLocked());
			model.addAttribute("clusterStopReindexAll", Boolean.TRUE.equals(clusterFlags.get(HazelcastConfig.ClusterFlags.STOP_REINDEX_ALL.value)));
			model.addAttribute("clusterStopReindex", Boolean.TRUE.equals(clusterFlags.get(HazelcastConfig.ClusterFlags.STOP_REINDEX.value)));
		}
		return "/admin/elastic/index";
	}

	/**
	 * This method refreshes data in the currently active index. It is very handy
	 * when having to refresh part of ES after direct database update.
	 *
	 * @param jsonFilter
	 * @throws IOException
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/action", params = { "reindex=accn", "filter" })
	public String reindexElasticFiltered(@RequestParam(value = "filter", required = true) String jsonfilt) throws IOException {

		if (elasticsearchReindexLock.isLocked() || elasticReindexQueue.size() > 0) {
			throw new RuntimeException("Reindex queue not empty or operation is locked! Unable to run new indexing.");
		}

		AccessionFilter accessionFilter = new ObjectMapper().registerModule(new JavaTimeModule()).readValue(jsonfilt, AccessionFilter.class);
		taskExecutor.execute(() -> {
			try {
				elasticsearchService.reindex(Accession.class, accessionFilter);
			} catch (Throwable e) {
				LOG.error("Error excecuting reindex Accession", e);
			}
		});
		return "redirect:/admin/elastic/";
	}

	/**
	 * This method deletes data in the currently active index. It is very handy
	 * when having to refresh part of ES after direct database update.
	 *
	 * @param jsonFilter
	 * @throws IOException
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/action", params = { "remove=filtered", "type", "filter" })
	public String removeElasticFiltered(@RequestParam(value = "type", required = true) String type, @RequestParam(value = "filter", required = true) String jsonfilt) throws Exception {

		if (elasticsearchReindexLock.isLocked() || elasticReindexQueue.size() > 0) {
			throw new RuntimeException("Reindex queue not empty or operation is locked! Unable to run new indexing.");
		}

		var filterType = Class.forName(type);

		if (!(EmptyModelFilter.class.isAssignableFrom(filterType))) {
			throw new InvalidApiUsageException("type is not an EmptyModelFilter");
		}

		var targetClass = ((ParameterizedType)filterType.getGenericSuperclass()).getActualTypeArguments()[1];

		@SuppressWarnings("unchecked")
		Class<EmptyModel> modelClass = (Class<EmptyModel>) targetClass;
		@SuppressWarnings("unchecked")
		Class<EmptyModelFilter<?, EmptyModel>> modelFilterClass = (Class<EmptyModelFilter<?, EmptyModel>>) filterType;

		EmptyModelFilter<?, EmptyModel> filter = new ObjectMapper().registerModule(new JavaTimeModule()).readValue(jsonfilt, modelFilterClass);
		LOG.warn("Removing from {} for filter: {}", modelClass, filter);
		taskExecutor.execute(() -> {
			try {
				elasticsearchService.remove(modelClass, filter);
			} catch (Throwable e) {
				LOG.error("Error executing reindex of " + type, e);
			}
		});
		return "redirect:/admin/elastic/";
	}

	/**
	 * This method removes documents in the currently active index. It is very handy
	 * when having to refresh part of ES after direct database update.
	 *
	 * @param jsonFilter
	 * @throws Exception 
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/action", params = { "remove=accn", "filter" })
	public String removeElasticFiltered(@RequestParam(value = "filter", required = true) String jsonfilt) throws Exception {
		return removeElasticFiltered(AccessionFilter.class.getName(), jsonfilt);
	}

	/**
	 * This method refreshes data in the currently active index. It is very handy
	 * when having to refresh part of ES after direct database update.
	 *
	 * @param type
	 * @throws IOException
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/action", params = { "reindex=content", "type" })
	public String reindexElasticContent(@RequestParam(value = "type", required = true) String type) throws IOException {

		if (elasticsearchReindexLock.isLocked() || elasticReindexQueue.size() > 0) {
			throw new RuntimeException("Reindex queue not empty or operation is locked! Unable to run new indexing.");
		}

		if (type.equals("All")) {
			taskExecutor.execute(() -> {
				try {
					elasticsearchService.reindexAll();
				} catch (Throwable e) {
					LOG.error("Error excecuting reindexAll", e);
				}
			});
		} else {
			taskExecutor.execute(() -> {
				try {
					elasticsearchService.reindex(Class.forName(type));
				} catch (Throwable e) {
					LOG.error("Error excecuting reindex of " + type, e);
				}
			});
		}

		return "redirect:/admin/elastic/";
	}

	/**
	 * Clear ES queue
	 *
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/action", params = { "clear-queues" })
	public String clearElasticQueues() {
//		elasticReindexer.clearQueues();
		return "redirect:/admin/elastic/";
	}

	@RequestMapping(method = RequestMethod.POST, value = "/action", params = { "action=realias" })
	public String moveAlias(@RequestParam(name = "aliasName") String aliasName, @RequestParam(name = "indexName") String indexName) {
		elasticsearchService.realias(aliasName, indexName);
		return "redirect:/admin/elastic/";
	}

	@RequestMapping(method = RequestMethod.POST, value = "/action", params = { "action=delete-alias" })
	public String deleteAlias(@RequestParam(name = "aliasName") String aliasName) {
		elasticsearchService.deleteAlias(aliasName);
		return "redirect:/admin/elastic/";
	}

	@RequestMapping(method = RequestMethod.POST, value = "/action", params = { "action=delete-index", "indexName" })
	public String deleteIndex(@RequestParam(name = "indexName") String indexName) {
		elasticsearchService.deleteIndex(indexName);
		return "redirect:/admin/elastic/";
	}

	private Map<String, String> createReindexTypesMap() {
		Map<String, String> reindexTypesMap = new HashMap<>();
		for (Class<?> clazz : elasticsearchService.getIndexedEntities()) {
			reindexTypesMap.put(clazz.getSimpleName(), clazz.getName());
		}
		return reindexTypesMap;
	}
}