Commit e8d8529d authored by Matija Obreza's avatar Matija Obreza

Merge branch '443-es-not-containing-all-data' into 'master'

Resolve "ES not containing all data"

Closes #443

See merge request genesys-pgr/genesys-server!398
parents a8b6c76a 66ca5f7b
/*
* Copyright 2019 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.genesys2.server.component.aspect;
import java.util.concurrent.BlockingQueue;
import javax.annotation.Resource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.genesys2.server.component.elastic.ElasticReindex;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Modifications to AccessionId do not trigger ES reindexing. This aspects
* addresses that.
*
* @author Matija Obreza
*/
@Aspect
public class ExtraReindexingAspect {
private final static Logger LOG = LoggerFactory.getLogger(ExtraReindexingAspect.class);
@Resource
private BlockingQueue<ElasticReindex> elasticReindexQueue;
/**
* After AccessionIdRepository#save
*
* @param joinPoint the join point
* @param result the result
*/
@AfterReturning(value = "execution(* org.genesys2.server.persistence.AccessionIdRepository.save(..))", returning = "result")
public void afterPersist(final JoinPoint joinPoint, final Object result) {
LOG.trace("accessionIdRepository#save {} {}", joinPoint.toLongString(), joinPoint.getTarget());
try {
scheduleReindexing(result);
} catch (Throwable e) {
LOG.error(e.getMessage(), e);
}
}
/**
* Flatten collections (if Iterable) and check each object class if indexed
*
* @param toReindex
*/
private void scheduleReindexing(Object toReindex) {
if (toReindex == null) {
return;
}
if (toReindex instanceof Iterable<?>) {
// Iterate and test
Iterable<?> it = (Iterable<?>) toReindex;
for (Object obj : it) {
scheduleReindexing(obj);
}
} else {
AccessionId accessionId = (AccessionId) toReindex;
LOG.trace("Scheduling reindexing of {} {}", Accession.class.getName(), accessionId.getId());
elasticReindexQueue.add(new ElasticReindex(Accession.class.getName(), accessionId.getId()));
}
}
}
......@@ -18,10 +18,6 @@ package org.genesys2.server.component.aspect;
import java.util.regex.Matcher;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.model.ImageGallery;
......@@ -84,35 +80,12 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
}
/* (non-Javadoc)
* @see org.genesys.filerepository.service.aspect.AbstractImageGalleryAspects#afterRepositoryImageSaveIterable(org.aspectj.lang.JoinPoint, java.lang.Iterable)
*/
@Override
public Object afterRepositoryImageSaveIterable(JoinPoint joinPoint, Iterable<RepositoryImage> repositoryImages) throws Throwable {
if (repositoryImages != null) {
repositoryImages.forEach(ri -> {
handleAccessionImageSave(ri);
});
}
return super.afterRepositoryImageSaveIterable(joinPoint, repositoryImages);
}
/* (non-Javadoc)
* @see org.genesys.filerepository.service.aspect.AbstractImageGalleryAspects#afterRepositoryImageSave(org.aspectj.lang.JoinPoint, org.genesys.filerepository.model.RepositoryImage)
* @see org.genesys.filerepository.service.aspect.AbstractImageGalleryAspects#addImageToGallery(org.genesys.filerepository.model.RepositoryImage)
*/
@Override
@AfterReturning(value = "execution(* org.genesys.filerepository.persistence.RepositoryImagePersistence.save(*))", returning = "repositoryImage")
public Object afterRepositoryImageSave(JoinPoint joinPoint, RepositoryImage repositoryImage) throws Throwable {
handleAccessionImageSave(repositoryImage);
protected void addImageToGallery(RepositoryImage repositoryImage) throws InvalidRepositoryPathException {
imageGalleryAspect.addImageToGallery(repositoryImage);
return super.afterRepositoryImageSave(joinPoint, repositoryImage);
}
/**
* Ensures an image gallery and updates image count for accessions
* @param repositoryImage
*/
private void handleAccessionImageSave(RepositoryImage repositoryImage) {
RepositoryFolder folder = repositoryImage.getFolder();
if (isAccessionFolder(folder)) {
Accession accession = accessionService.findMatchingAccession(folder);
......@@ -127,11 +100,12 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
}
/* (non-Javadoc)
* @see org.genesys.filerepository.service.aspect.AbstractImageGalleryAspects#aroundRepositoryImageDelete(org.aspectj.lang.ProceedingJoinPoint, org.genesys.filerepository.model.RepositoryImage)
* @see org.genesys.filerepository.service.aspect.AbstractImageGalleryAspects#removeImageFromGallery(org.genesys.filerepository.model.RepositoryImage)
*/
@Override
@Around(value = "execution(* org.genesys.filerepository.persistence.RepositoryImagePersistence.delete(*)) && args(repositoryImage)")
public Object aroundRepositoryImageDelete(ProceedingJoinPoint joinPoint, RepositoryImage repositoryImage) throws Throwable {
protected void removeImageFromGallery(RepositoryImage repositoryImage) throws InvalidRepositoryPathException {
imageGalleryAspect.removeImageFromGallery(repositoryImage);
RepositoryFolder repositoryFolder = repositoryImage.getFolder();
if (isAccessionFolder(repositoryFolder)) {
Accession accession = accessionService.findMatchingAccession(repositoryFolder);
......@@ -139,10 +113,9 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
updateImageCountOfAcce(accession);
}
}
return super.aroundRepositoryImageDelete(joinPoint, repositoryImage);
}
/**
* Ensures that an {@link ImageGallery} exists for accession images.
*
......@@ -196,27 +169,12 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
ImageGallery imageGallery = instituteFilesService.loadImageGallery(accession.getInstitute(), accession);
if (imageGallery != null) {
accessionId.setImageCount(imageGallery.getImages().size());
accessionIdRepository.save(accessionId);
} else {
accessionId.setImageCount(0);
}
accessionIdRepository.save(accessionId);
} catch (InvalidRepositoryPathException e) {
LOG.warn("Error updating imageCount of accession, error is {}", e.getMessage());
}
}
/* (non-Javadoc)
* @see org.genesys.filerepository.service.aspect.AbstractImageGalleryAspects#addImageToGallery(org.genesys.filerepository.model.RepositoryImage)
*/
@Override
protected void addImageToGallery(RepositoryImage repositoryImage) throws InvalidRepositoryPathException {
imageGalleryAspect.addImageToGallery(repositoryImage);
}
/* (non-Javadoc)
* @see org.genesys.filerepository.service.aspect.AbstractImageGalleryAspects#removeImageFromGallery(org.genesys.filerepository.model.RepositoryImage)
*/
@Override
protected void removeImageFromGallery(RepositoryImage repositoryImage) throws InvalidRepositoryPathException {
imageGalleryAspect.removeImageFromGallery(repositoryImage);
}
}
......@@ -29,6 +29,7 @@ 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.EmbeddedClientFactoryBean;
import org.genesys2.server.component.aspect.ExtraReindexingAspect;
import org.genesys2.server.component.elastic.ElasticJPAListener;
import org.genesys2.server.component.elastic.ElasticJacksonAnnotationIntrospector;
import org.genesys2.server.component.elastic.ElasticReindex;
......@@ -78,6 +79,14 @@ public class ElasticsearchConfig {
@Value("${elasticsearch.embedded.data}")
private String embeddedDataHome;
/**
* Register aspect to handle extra ES reindexing not covered by other mechanisms.
*/
@Bean
public ExtraReindexingAspect extraReindexingAspect() {
return new ExtraReindexingAspect();
}
/**
* No-limits blocking queue for reindexing with ES
......
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