Madan Jampani | 551d0d2 | 2016-02-01 12:51:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
| 22 | import java.util.TreeMap; |
| 23 | |
| 24 | import org.apache.commons.lang.StringUtils; |
| 25 | import org.onlab.util.Tools; |
| 26 | import org.onosproject.cluster.PartitionId; |
| 27 | import org.onosproject.store.primitives.DistributedPrimitiveCreator; |
| 28 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 29 | import org.onosproject.store.service.AsyncAtomicValue; |
| 30 | import org.onosproject.store.service.AsyncConsistentMap; |
| 31 | import org.onosproject.store.service.AsyncDistributedSet; |
| 32 | import org.onosproject.store.service.AsyncLeaderElector; |
| 33 | import org.onosproject.store.service.DistributedQueue; |
| 34 | import org.onosproject.store.service.Serializer; |
| 35 | |
| 36 | import com.google.common.collect.Lists; |
| 37 | import com.google.common.collect.Maps; |
| 38 | import com.google.common.hash.HashCode; |
| 39 | import com.google.common.hash.Hashing; |
| 40 | import com.google.common.primitives.Bytes; |
| 41 | |
| 42 | /** |
| 43 | * {@code DistributedPrimitiveCreator} that federates responsibility for creating |
| 44 | * distributed primitives to a collection of other {@link DistributedPrimitiveCreator creators}. |
| 45 | */ |
| 46 | public class FederatedDistributedPrimitiveCreator implements DistributedPrimitiveCreator { |
| 47 | |
| 48 | private final TreeMap<PartitionId, DistributedPrimitiveCreator> members; |
| 49 | private final List<PartitionId> sortedMemberPartitionIds; |
| 50 | |
| 51 | public FederatedDistributedPrimitiveCreator(Map<PartitionId, DistributedPrimitiveCreator> members) { |
| 52 | this.members = Maps.newTreeMap(); |
| 53 | this.members.putAll(checkNotNull(members)); |
| 54 | this.sortedMemberPartitionIds = Lists.newArrayList(members.keySet()); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public <K, V> AsyncConsistentMap<K, V> newAsyncConsistentMap(String name, Serializer serializer) { |
| 59 | checkNotNull(name); |
| 60 | checkNotNull(serializer); |
| 61 | Map<PartitionId, AsyncConsistentMap<K, V>> maps = |
| 62 | Maps.transformValues(members, |
| 63 | partition -> partition.newAsyncConsistentMap(name, serializer)); |
| 64 | Hasher<K> hasher = key -> { |
| 65 | long hashCode = HashCode.fromBytes(Bytes.ensureCapacity(serializer.encode(key), 8, 0)).asLong(); |
| 66 | return sortedMemberPartitionIds.get(Hashing.consistentHash(hashCode, members.size())); |
| 67 | }; |
| 68 | return new PartitionedAsyncConsistentMap<>(name, maps, hasher); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public <E> AsyncDistributedSet<E> newAsyncDistributedSet(String name, Serializer serializer) { |
| 73 | return DistributedPrimitives.newSetFromMap(newAsyncConsistentMap(name, serializer)); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public AsyncAtomicCounter newAsyncCounter(String name) { |
| 78 | return getCreator(name).newAsyncCounter(name); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public <V> AsyncAtomicValue<V> newAsyncAtomicValue(String name, Serializer serializer) { |
| 83 | return getCreator(name).newAsyncAtomicValue(name, serializer); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public <E> DistributedQueue<E> newDistributedQueue(String name, Serializer serializer) { |
| 88 | return getCreator(name).newDistributedQueue(name, serializer); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public AsyncLeaderElector newAsyncLeaderElector(String name) { |
| 93 | return getCreator(name).newAsyncLeaderElector(name); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns the {@code DistributedPrimitiveCreator} to use for hosting a primitive. |
| 98 | * @param name primitive name |
| 99 | * @return primitive creator |
| 100 | */ |
| 101 | private DistributedPrimitiveCreator getCreator(String name) { |
| 102 | long hashCode = HashCode.fromBytes(Tools.getBytesUtf8(StringUtils.leftPad(name, 8))).asLong(); |
| 103 | int index = Hashing.consistentHash(hashCode, members.size()); |
| 104 | return members.get(sortedMemberPartitionIds.get(index)); |
| 105 | } |
| 106 | } |