ElasticSearchController.java

/*
 * Copyright 2022 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.api.admin.v1;

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

import javax.annotation.Resource;

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.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.genesys.blocks.model.EmptyModel;
import org.genesys.blocks.model.filters.EmptyModelFilter;
import org.genesys.server.api.ApiBaseController;
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.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

@RestController("elasticSearchApi1")
@RequestMapping(ElasticSearchController.CONTROLLER_URL)
@PreAuthorize("hasRole('ADMINISTRATOR')")
public class ElasticSearchController {

	/** The Constant CONTROLLER_URL. */
	public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/admin/elastic";

	public static final Logger LOG = LoggerFactory.getLogger(ElasticSearchController.class);

	@Autowired(required = false)
	private ElasticsearchService elasticsearchService;

	@Autowired
	private TaskExecutor taskExecutor;

	@Autowired(required = false)
	private RestHighLevelClient client;

	@Resource
	private BlockingQueue<ElasticReindex> elasticReindexQueue;

	@Resource
	private Lock elasticsearchReindexLock;

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

	@GetMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
	public IndexResponse getIndexResponse() throws IOException {
		IndexResponse indexResponse = new IndexResponse();
		indexResponse.clusterStopReindexAll = Boolean.TRUE.equals(clusterFlags.get(HazelcastConfig.ClusterFlags.STOP_REINDEX_ALL.value));
		indexResponse.clusterStopReindex = Boolean.TRUE.equals(clusterFlags.get(HazelcastConfig.ClusterFlags.STOP_REINDEX.value));
		if (client.indices().exists(new GetIndexRequest("*"), RequestOptions.DEFAULT)) {
			GetIndexResponse getIndexResponse = client.indices().get(new GetIndexRequest("*"), RequestOptions.DEFAULT);
			indexResponse.indexes = getIndexResponse.getAliases();
			indexResponse.reindexTypes = createReindexTypesMap();
			indexResponse.updateQueueSize = elasticReindexQueue.size();
		}
		return indexResponse;
	}

	@PostMapping(value = "/action/reindex/accn")
	public void reindexElasticFiltered(@RequestBody AccessionFilter accessionFilter) throws IOException {

		if (!assertReindex()) {
			throw new RuntimeException("Reindex queue not empty or operation is locked! Unable to run new indexing.");
		}

		taskExecutor.execute(() -> {
			try {
				elasticsearchService.reindex(Accession.class, accessionFilter);
			} catch (Throwable e) {
				LOG.error("Error excecuting reindex Accession", e);
			}
		});
	}

	@PostMapping(value = "/action/remove/filtered")
	public void removeElasticFiltered(@RequestParam(value = "type") String type, @RequestBody String jsonFilter) throws Exception {

		if (!assertReindex()) {
			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;

		var filter = new ObjectMapper().registerModule(new JavaTimeModule()).readValue(jsonFilter, modelFilterClass);

		removeElasticFiltered(modelClass, filter);
	}

	@PostMapping(value = "/action/remove/accn")
	public void removeElasticFiltered(@RequestBody AccessionFilter filter) {
		removeElasticFiltered(Accession.class, filter);
	}

	private <E extends EmptyModel> void removeElasticFiltered(Class<E> modelClass, EmptyModelFilter<?, E> filter) {
		LOG.warn("Removing from {} for filter: {}", modelClass, filter);
		taskExecutor.execute(() -> {
			try {
				elasticsearchService.remove(modelClass, filter);
			} catch (Throwable e) {
				LOG.error("Error executing reindex of " + modelClass, e);
			}
		});
	}

	@PostMapping(value = "/action/reindex")
	public void reindexElasticContent(@RequestParam(value = "type", required = true) String type) throws IOException {

		if (!assertReindex()) {
			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);
				}
			});
		}

	}

	@PostMapping(value = "/action/stop-reindex")
	public void stopReindex() {
		elasticsearchService.stopReindex();
	}

	@PostMapping(value = "/action/stop-reindex-all")
	public void stopReindexAll() {
		elasticsearchService.stopReindexAll();
	}

	@PostMapping(value = "/action/allow-reindex-all")
	public void allowReindexAll() {
		elasticsearchService.allowReindexAll();
	}

	@PostMapping(value = "/action/realias")
	public void moveAlias(@RequestParam(name = "aliasName") String aliasName, @RequestParam(name = "indexName") String indexName) {
		elasticsearchService.realias(aliasName, indexName);
	}

	@PostMapping(value = "/action/add-alias")
	public void addAlias(@RequestParam(name = "aliasName") String aliasName, @RequestParam(name = "indexName") String indexName) {
		elasticsearchService.addAlias(aliasName, indexName);
	}

	@PostMapping(value = "/action/delete-alias")
	public void deleteAlias(@RequestParam(name = "aliasName") String aliasName) {
		elasticsearchService.deleteAlias(aliasName);
	}

	@PostMapping(value = "/action/delete-index/{indexName}")
	public void deleteIndex(@PathVariable(name = "indexName") String indexName) {
		elasticsearchService.deleteIndex(indexName);
	}

	@GetMapping(value = "/cluster/status", produces = { MediaType.APPLICATION_JSON_VALUE })
	public ClusterHealthResponse getClusterHealthStatus() throws IOException {
		var clusterHealthResponse = client.cluster().health(new ClusterHealthRequest(), RequestOptions.DEFAULT);
		return clusterHealthResponse;
	}

	public static class IndexResponse {
		public Map<String, List<AliasMetaData>> indexes;
		public Map<String, String> reindexTypes;
		public int updateQueueSize;
		public Boolean clusterStopReindexAll;
		public Boolean clusterStopReindex;
	}

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

	private boolean assertReindex() {
		try {
			boolean isLockAcquired = elasticsearchReindexLock.tryLock();
			if (!isLockAcquired || elasticReindexQueue.size() > 0) {
				return false;
			}
		} finally {
			elasticsearchReindexLock.unlock();
		}
		return true;
	}

}