From 37c57d1d521928d778d85eb50e56e27b560f1c5d Mon Sep 17 00:00:00 2001 From: Tom Page Date: Tue, 28 Apr 2020 10:28:42 +0100 Subject: [PATCH] SEARCH-2202 Handle case where shard range is missed. --- .../solr/tracker/DocRouterFactory.java | 14 +++-- .../solr/tracker/DocRouterFactoryTest.java | 60 +++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 search-services/alfresco-search/src/test/java/org/alfresco/solr/tracker/DocRouterFactoryTest.java diff --git a/search-services/alfresco-search/src/main/java/org/alfresco/solr/tracker/DocRouterFactory.java b/search-services/alfresco-search/src/main/java/org/alfresco/solr/tracker/DocRouterFactory.java index 672313371..21d3b38ac 100644 --- a/search-services/alfresco-search/src/main/java/org/alfresco/solr/tracker/DocRouterFactory.java +++ b/search-services/alfresco-search/src/main/java/org/alfresco/solr/tracker/DocRouterFactory.java @@ -18,6 +18,7 @@ */ package org.alfresco.solr.tracker; +import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.repo.index.shard.ShardMethodEnum; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,14 +45,15 @@ public class DocRouterFactory LOGGER.info("Sharding via DB_ID"); return new DBIDRouter(); case DB_ID_RANGE: - if(properties.containsKey(SHARD_RANGE_KEY)) + if(!properties.containsKey(SHARD_RANGE_KEY)) { - LOGGER.info("Sharding via DB_ID_RANGE"); - String[] pair =properties.getProperty(SHARD_RANGE_KEY).split("-"); - long start = Long.parseLong(pair[0]); - long end = Long.parseLong(pair[1]); - return new DBIDRangeRouter(start, end); + throw new AlfrescoRuntimeException("DB_ID_RANGE sharding requires the " + SHARD_RANGE_KEY + " property to be set."); } + LOGGER.info("Sharding via DB_ID_RANGE"); + String[] pair = properties.getProperty(SHARD_RANGE_KEY).split("-"); + long start = Long.parseLong(pair[0]); + long end = Long.parseLong(pair[1]); + return new DBIDRangeRouter(start, end); case ACL_ID: LOGGER.info("Sharding via ACL_ID"); return new ACLIDMurmurRouter(); diff --git a/search-services/alfresco-search/src/test/java/org/alfresco/solr/tracker/DocRouterFactoryTest.java b/search-services/alfresco-search/src/test/java/org/alfresco/solr/tracker/DocRouterFactoryTest.java new file mode 100644 index 000000000..e4236c7b9 --- /dev/null +++ b/search-services/alfresco-search/src/test/java/org/alfresco/solr/tracker/DocRouterFactoryTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2005-2014 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.solr.tracker; + +import static org.alfresco.solr.tracker.DocRouterFactory.SHARD_RANGE_KEY; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Properties; + +import org.alfresco.error.AlfrescoRuntimeException; +import org.alfresco.repo.index.shard.ShardMethodEnum; +import org.junit.Test; + +/** Unit tests for the {@link DocRouterFactory}. */ +public class DocRouterFactoryTest +{ + /** Check that a DB_ID_RANGE router can be created. */ + @Test + public void testDBIDRANGEWithShardRangeKey() + { + Properties mockProperties = mock(Properties.class); + when(mockProperties.containsKey(SHARD_RANGE_KEY)).thenReturn(true); + when(mockProperties.getProperty(SHARD_RANGE_KEY)).thenReturn("100000000-150000000"); + + // Call the method under test. + DocRouter docRouter = DocRouterFactory.getRouter(mockProperties, ShardMethodEnum.DB_ID_RANGE); + + assertTrue("Expected to get a DBIDRangeRouter.", docRouter instanceof DBIDRangeRouter); + DBIDRangeRouter dbidRangeRouter = (DBIDRangeRouter) docRouter; + assertEquals("Unexpected start of range.", dbidRangeRouter.getStartRange(), 100000000L); + assertEquals("Unexpected end of range.", dbidRangeRouter.getEndRange(), 150000000L); + } + + /** Check that an exception is raised if the range information is missing. */ + @Test(expected = AlfrescoRuntimeException.class) + public void testDBIDRANGEWithoutShardRangeKey() + { + Properties mockProperties = mock(Properties.class); + DocRouterFactory.getRouter(mockProperties, ShardMethodEnum.DB_ID_RANGE); + } +}