From d04a1fa303f2a2e0a504b86f90fb8601a5b52ca3 Mon Sep 17 00:00:00 2001 From: Moiz Raja Date: Wed, 18 Jun 2014 14:00:08 -0700 Subject: [PATCH 1/1] Introduce ShardStrategy and related code Change-Id: I0df1f18f7b3488acae440e3375e155ae7ee06abf Signed-off-by: Moiz Raja --- .../shardstrategy/DefaultShardStrategy.java | 28 +++++++++++ .../shardstrategy/ShardStrategy.java | 24 ++++++++++ .../shardstrategy/ShardStrategyFactory.java | 48 +++++++++++++++++++ .../DefaultShardStrategyTest.java | 14 ++++++ .../ShardStrategyFactoryTest.java | 29 +++++++++++ 5 files changed, 143 insertions(+) create mode 100644 opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/DefaultShardStrategy.java create mode 100644 opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategy.java create mode 100644 opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactory.java create mode 100644 opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardstrategy/DefaultShardStrategyTest.java create mode 100644 opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactoryTest.java diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/DefaultShardStrategy.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/DefaultShardStrategy.java new file mode 100644 index 0000000000..a8ab5c4bd3 --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/DefaultShardStrategy.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.cluster.datastore.shardstrategy; + +import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; + +/** + * The DefaultShardStrategy basically puts all data into the default Shard + *

+ * The default shard stores data for all modules for which a specific set of shards has not been configured + *

+ */ +public class DefaultShardStrategy implements ShardStrategy{ + + public static final String NAME = "default"; + public static final String DEFAULT_SHARD = "default"; + + @Override + public String findShard(InstanceIdentifier path) { + return DEFAULT_SHARD; + } +} diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategy.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategy.java new file mode 100644 index 0000000000..f75eb2d863 --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategy.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.cluster.datastore.shardstrategy; + +import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; + +/** + * The role of ShardStrategy is to figure out which Shards a given piece of data belongs to + */ +public interface ShardStrategy { + /** + * Find the name of the shard in which the data pointed to by the specified path belongs in + * + * @param path The location of the data in the logical tree + * @return + */ + String findShard(InstanceIdentifier path); +} diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactory.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactory.java new file mode 100644 index 0000000000..210537925b --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactory.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.cluster.datastore.shardstrategy; + +import com.google.common.base.Preconditions; +import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class ShardStrategyFactory { + private static final Map moduleNameToStrategyMap = new ConcurrentHashMap(); + + private static final String UNKNOWN_MODULE_NAME = "unknown"; + + public static ShardStrategy getStrategy(InstanceIdentifier path){ + Preconditions.checkNotNull(path, "path should not be null"); + + String moduleName = getModuleName(path); + ShardStrategy shardStrategy = moduleNameToStrategyMap.get(moduleName); + if(shardStrategy == null){ + return new DefaultShardStrategy(); + } + + return shardStrategy; + } + + + private static String getModuleName(InstanceIdentifier path){ + return UNKNOWN_MODULE_NAME; + } + + /** + * This is to be used in the future to register a custom shard strategy + * + * @param moduleName + * @param shardStrategy + */ + public static void registerShardStrategy(String moduleName, ShardStrategy shardStrategy){ + throw new UnsupportedOperationException("registering a custom shard strategy not supported yet"); + } +} diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardstrategy/DefaultShardStrategyTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardstrategy/DefaultShardStrategyTest.java new file mode 100644 index 0000000000..d3ba9b1c15 --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardstrategy/DefaultShardStrategyTest.java @@ -0,0 +1,14 @@ +package org.opendaylight.controller.cluster.datastore.shardstrategy; + +import junit.framework.Assert; +import org.junit.Test; +import org.opendaylight.controller.md.cluster.datastore.model.TestModel; + +public class DefaultShardStrategyTest { + + @Test + public void testFindShard() throws Exception { + String shard = new DefaultShardStrategy().findShard(TestModel.TEST_PATH); + Assert.assertEquals(DefaultShardStrategy.DEFAULT_SHARD, shard); + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactoryTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactoryTest.java new file mode 100644 index 0000000000..2cff981b68 --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactoryTest.java @@ -0,0 +1,29 @@ +package org.opendaylight.controller.cluster.datastore.shardstrategy; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.opendaylight.controller.md.cluster.datastore.model.TestModel; + +import static junit.framework.Assert.assertNotNull; + +public class ShardStrategyFactoryTest { + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testGetStrategy(){ + ShardStrategy strategy = ShardStrategyFactory.getStrategy(TestModel.TEST_PATH); + assertNotNull(strategy); + } + + @Test + public void testGetStrategyNullPointerExceptionWhenPathIsNull(){ + expectedEx.expect(NullPointerException.class); + expectedEx.expectMessage("path should not be null"); + + ShardStrategyFactory.getStrategy(null); + } + +} \ No newline at end of file -- 2.36.6