SEARCH-2202 Handle case where shard range is missed.

This commit is contained in:
Tom Page
2020-04-28 10:28:42 +01:00
parent cfb670302c
commit 37c57d1d52
2 changed files with 68 additions and 6 deletions
@@ -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();
@@ -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 <http://www.gnu.org/licenses/>.
*/
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);
}
}