Do not process missing index types (#1985)

Fixes an error that arises, when a plugin with search
indices for repositories is removed and later on a repository
is deleted. This led to a null pointer exception in the
SearchableTypeResolver, but only because an "illegal" index
detail without proper type was given as a parameter. This
type cannot be resolved any longer due to the removed plugin.
We therefore can simply filter these index details.
This commit is contained in:
René Pfeuffer
2022-03-30 15:30:54 +02:00
committed by GitHub
parent 5505b3ce11
commit de9984ae2c
5 changed files with 31 additions and 53 deletions

View File

@@ -0,0 +1,2 @@
- type: fixed
description: Do not process index types which no longer exist ([#1985](https://github.com/scm-manager/scm-manager/pull/1985))

View File

@@ -27,6 +27,8 @@ package sonia.scm.search;
import com.google.common.base.Joiner;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.work.CentralWorkQueue;
import sonia.scm.work.CentralWorkQueue.Enqueue;
import sonia.scm.work.Task;
@@ -41,6 +43,8 @@ import java.util.stream.Collectors;
public class LuceneSearchEngine implements SearchEngine {
private static final Logger LOG = LoggerFactory.getLogger(LuceneSearchEngine.class);
private final IndexManager indexManager;
private final SearchableTypeResolver resolver;
private final LuceneQueryBuilderFactory queryBuilderFactory;
@@ -128,10 +132,19 @@ public class LuceneSearchEngine implements SearchEngine {
indexManager.all()
.stream()
.filter(predicate)
.filter(this::isTypeAvailable)
.map(details -> new IndexParams(details.getName(), resolver.resolve(details.getType())))
.forEach(consumer);
}
private boolean isTypeAvailable(IndexDetails details) {
if (details.getType() == null) {
LOG.info("no type found for index with name '{}'; index will not be updated", details.getName());
return false;
}
return true;
}
private void batch(IndexParams params, Task task) {
LuceneSearchEngine.this.enqueue(params.getSearchableType(), params.getIndex(), resources, task);
}

View File

@@ -86,9 +86,6 @@ class SearchableTypeResolver {
}
public LuceneSearchableType resolve(Class<?> type) {
if (type == null) {
throw notFound(entity("type", "null"));
}
LuceneSearchableType searchableType = classToSearchableType.get(type);
if (searchableType == null) {
throw notFound(entity("type", type.getName()));

View File

@@ -54,6 +54,7 @@ import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -341,6 +342,15 @@ class LuceneSearchEngineTest {
verify(enqueue.runAsAdmin()).enqueue(any(Task.class));
}
@Test
void shouldIgnoreDetailsWithMissingType() {
mockDetails(new LuceneIndexDetails(null, "default"));
searchEngine.forIndices().batch(index -> {});
verify(enqueue.runAsAdmin(), never()).enqueue(any(Task.class));
}
private <T extends IndexDetails> void mockDetails(LuceneIndexDetails... details) {
for (LuceneIndexDetails detail : details) {
mockType(detail.getType());
@@ -360,20 +370,19 @@ class LuceneSearchEngineTest {
}
private void mockType(Class<?> type){
LuceneSearchableType searchableType = mock(LuceneSearchableType.class);
lenient().when(searchableType.getType()).thenAnswer(ic -> type);
lenient().when(searchableType.getName()).thenReturn(type.getSimpleName().toLowerCase(Locale.ENGLISH));
lenient().when(resolver.resolve(type)).thenReturn(searchableType);
if (type != null) {
LuceneSearchableType searchableType = mock(LuceneSearchableType.class);
lenient().when(searchableType.getType()).thenAnswer(ic -> type);
lenient().when(searchableType.getName()).thenReturn(type.getSimpleName().toLowerCase(Locale.ENGLISH));
lenient().when(resolver.resolve(type)).thenReturn(searchableType);
}
}
}
public static class DummyIndexTask implements IndexTask<Repository> {
@Override
public void update(Index<Repository> index) {
}
}

View File

@@ -1,43 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.search;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.NotFoundException;
import static org.junit.jupiter.api.Assertions.assertThrows;
@ExtendWith(MockitoExtension.class)
class SearchableTypeResolverTest {
private final SearchableTypeResolver resolver = new SearchableTypeResolver();
@Test
void shouldThrowNotFoundForNullValue() {
assertThrows(NotFoundException.class, () -> resolver.resolve(null));
}
}