From e9bb60be43af477f17b30ee1f2ba205565b7fa15 Mon Sep 17 00:00:00 2001 From: Ashlee Young Date: Mon, 19 Oct 2015 10:14:31 -0700 Subject: Updated onos src tree to commit id 1e60f97ae50c05b94fcb6a10520738bfb5efdfd1 --- .../newresource/impl/ConsistentResourceStore.java | 2 +- .../store/packet/impl/DistributedPacketStore.java | 43 ++-- .../impl/ConsistentDeviceResourceStore.java | 225 --------------------- .../resource/impl/ConsistentIntentSetMultimap.java | 2 + .../resource/impl/ConsistentLinkResourceStore.java | 9 + 5 files changed, 41 insertions(+), 240 deletions(-) delete mode 100644 framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDeviceResourceStore.java (limited to 'framework/src/onos/core/store/dist') diff --git a/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java b/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java index 648119e5..10f79eb0 100644 --- a/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java +++ b/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java @@ -304,7 +304,7 @@ public class ConsistentResourceStore implements ResourceStore { } /** - * Removes teh values from the existing values associated with the specified key. + * Removes the values from the existing values associated with the specified key. * If the map doesn't contain the given values, removal will not happen. * * @param map map holding multiple values for a key diff --git a/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/packet/impl/DistributedPacketStore.java b/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/packet/impl/DistributedPacketStore.java index f0f3eb5e..3865a779 100644 --- a/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/packet/impl/DistributedPacketStore.java +++ b/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/packet/impl/DistributedPacketStore.java @@ -41,6 +41,7 @@ import org.onosproject.store.cluster.messaging.MessageSubject; import org.onosproject.store.serializers.KryoNamespaces; import org.onosproject.store.serializers.KryoSerializer; import org.onosproject.store.service.ConsistentMap; +import org.onosproject.store.service.ConsistentMapException; import org.onosproject.store.service.Serializer; import org.onosproject.store.service.StorageService; import org.slf4j.Logger; @@ -52,6 +53,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; import static org.onlab.util.Tools.groupedThreads; +import static org.onlab.util.Tools.retryable; import static org.slf4j.LoggerFactory.getLogger; /** @@ -66,6 +68,8 @@ public class DistributedPacketStore private final Logger log = getLogger(getClass()); + private static final int MAX_BACKOFF = 10; + // TODO: make this configurable. private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 4; @@ -159,11 +163,11 @@ public class DistributedPacketStore return tracker.requests(); } - private class PacketRequestTracker { + private final class PacketRequestTracker { private ConsistentMap> requests; - public PacketRequestTracker() { + private PacketRequestTracker() { requests = storageService.>consistentMapBuilder() .withName("onos-packet-requests") .withPartitionsDisabled() @@ -171,7 +175,17 @@ public class DistributedPacketStore .build(); } - public void add(PacketRequest request) { + private void add(PacketRequest request) { + AtomicBoolean firstRequest = + retryable(this::addInternal, ConsistentMapException.class, + 3, MAX_BACKOFF).apply(request); + if (firstRequest.get() && delegate != null) { + // The instance that makes the first request will push to all devices + delegate.requestPackets(request); + } + } + + private AtomicBoolean addInternal(PacketRequest request) { AtomicBoolean firstRequest = new AtomicBoolean(false); requests.compute(request.selector(), (s, existingRequests) -> { if (existingRequests == null) { @@ -186,14 +200,20 @@ public class DistributedPacketStore return existingRequests; } }); + return firstRequest; + } - if (firstRequest.get() && delegate != null) { - // The instance that makes the first request will push to all devices - delegate.requestPackets(request); + private void remove(PacketRequest request) { + AtomicBoolean removedLast = + retryable(this::removeInternal, ConsistentMapException.class, + 3, MAX_BACKOFF).apply(request); + if (removedLast.get() && delegate != null) { + // The instance that removes the last request will remove from all devices + delegate.cancelPackets(request); } } - public void remove(PacketRequest request) { + private AtomicBoolean removeInternal(PacketRequest request) { AtomicBoolean removedLast = new AtomicBoolean(false); requests.computeIfPresent(request.selector(), (s, existingRequests) -> { if (existingRequests.contains(request)) { @@ -209,15 +229,10 @@ public class DistributedPacketStore return existingRequests; } }); - - if (removedLast.get() && delegate != null) { - // The instance that removes the last request will remove from all devices - delegate.cancelPackets(request); - } - + return removedLast; } - public List requests() { + private List requests() { List list = Lists.newArrayList(); requests.values().forEach(v -> list.addAll(v.value())); list.sort((o1, o2) -> o1.priority().priorityValue() - o2.priority().priorityValue()); diff --git a/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDeviceResourceStore.java b/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDeviceResourceStore.java deleted file mode 100644 index 3266e96c..00000000 --- a/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDeviceResourceStore.java +++ /dev/null @@ -1,225 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.store.resource.impl; - -import org.apache.felix.scr.annotations.Activate; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Deactivate; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.ReferenceCardinality; -import org.apache.felix.scr.annotations.Service; -import org.onosproject.net.DeviceId; -import org.onosproject.net.Port; -import org.onosproject.net.device.DeviceService; -import org.onosproject.net.intent.IntentId; -import org.onosproject.net.resource.device.DeviceResourceStore; -import org.onosproject.store.serializers.KryoNamespaces; -import org.onosproject.store.service.ConsistentMap; -import org.onosproject.store.service.Serializer; -import org.onosproject.store.service.StorageService; -import org.onosproject.store.service.TransactionContext; -import org.onosproject.store.service.TransactionalMap; -import org.onosproject.store.service.Versioned; -import org.slf4j.Logger; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - -import static com.google.common.base.Preconditions.checkArgument; -import static org.slf4j.LoggerFactory.getLogger; -import static com.google.common.base.Preconditions.checkNotNull; - -/** - * Store that manages device resources using Copycat-backed TransactionalMaps. - */ -@Component(immediate = true, enabled = true) -@Service -public class ConsistentDeviceResourceStore implements DeviceResourceStore { - private final Logger log = getLogger(getClass()); - - private static final String PORT_ALLOCATIONS = "PortAllocations"; - private static final String INTENT_MAPPING = "IntentMapping"; - private static final String INTENT_ALLOCATIONS = "PortIntentAllocations"; - - private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API); - - private ConsistentMap portAllocMap; - private ConsistentMap> intentAllocMap; - private ConsistentMap> intentMapping; - - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) - protected StorageService storageService; - - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) - protected DeviceService deviceService; - - @Activate - public void activate() { - portAllocMap = storageService.consistentMapBuilder() - .withName(PORT_ALLOCATIONS) - .withSerializer(SERIALIZER) - .build(); - intentAllocMap = storageService.>consistentMapBuilder() - .withName(INTENT_ALLOCATIONS) - .withSerializer(SERIALIZER) - .build(); - intentMapping = storageService.>consistentMapBuilder() - .withName(INTENT_MAPPING) - .withSerializer(SERIALIZER) - .build(); - log.info("Started"); - } - - @Deactivate - public void deactivate() { - log.info("Stopped"); - } - - private TransactionalMap getPortAllocs(TransactionContext tx) { - return tx.getTransactionalMap(PORT_ALLOCATIONS, SERIALIZER); - } - - private TransactionalMap> getIntentAllocs(TransactionContext tx) { - return tx.getTransactionalMap(INTENT_ALLOCATIONS, SERIALIZER); - } - - private TransactionContext getTxContext() { - return storageService.transactionContextBuilder().build(); - } - - @Override - public Set getFreePorts(DeviceId deviceId) { - checkNotNull(deviceId); - - Set freePorts = new HashSet<>(); - for (Port port : deviceService.getPorts(deviceId)) { - if (!portAllocMap.containsKey(port)) { - freePorts.add(port); - } - } - - return freePorts; - } - - @Override - public boolean allocatePorts(Set ports, IntentId intentId) { - checkNotNull(ports); - checkArgument(ports.size() > 0); - checkNotNull(intentId); - - TransactionContext tx = getTxContext(); - tx.begin(); - try { - TransactionalMap portAllocs = getPortAllocs(tx); - for (Port port : ports) { - if (portAllocs.putIfAbsent(port, intentId) != null) { - throw new Exception("Port already allocated " + port.toString()); - } - } - - TransactionalMap> intentAllocs = getIntentAllocs(tx); - intentAllocs.put(intentId, ports); - tx.commit(); - } catch (Exception e) { - log.error("Exception thrown, rolling back", e); - tx.abort(); - return false; - } - - return true; - } - - @Override - public Set getAllocations(IntentId intentId) { - if (!intentAllocMap.containsKey(intentId)) { - Collections.emptySet(); - } - - return intentAllocMap.get(intentId).value(); - } - - @Override - public IntentId getAllocations(Port port) { - if (!portAllocMap.containsKey(port)) { - return null; - } - - return portAllocMap.get(port).value(); - } - - @Override - public Set getMapping(IntentId intentId) { - Versioned> result = intentMapping.get(intentId); - - if (result != null) { - return result.value(); - } - - return null; - } - - @Override - public boolean allocateMapping(IntentId keyIntentId, IntentId valIntentId) { - Versioned> versionedIntents = intentMapping.get(keyIntentId); - - if (versionedIntents == null) { - Set newSet = new HashSet<>(); - newSet.add(valIntentId); - intentMapping.put(keyIntentId, newSet); - } else { - versionedIntents.value().add(valIntentId); - } - - return true; - } - - @Override - public void releaseMapping(IntentId intentId) { - for (IntentId intent : intentMapping.keySet()) { - // TODO: optimize by checking for identical src & dst - Set mapping = intentMapping.get(intent).value(); - if (mapping.remove(intentId)) { - return; - } - } - } - - @Override - public boolean releasePorts(IntentId intentId) { - checkNotNull(intentId); - - TransactionContext tx = getTxContext(); - tx.begin(); - try { - TransactionalMap> intentAllocs = getIntentAllocs(tx); - Set ports = intentAllocs.get(intentId); - intentAllocs.remove(intentId); - - TransactionalMap portAllocs = getPortAllocs(tx); - for (Port port : ports) { - portAllocs.remove(port); - } - tx.commit(); - } catch (Exception e) { - log.error("Exception thrown, rolling back", e); - tx.abort(); - return false; - } - - return true; - } -} diff --git a/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentIntentSetMultimap.java b/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentIntentSetMultimap.java index 87e67215..8d5a1001 100644 --- a/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentIntentSetMultimap.java +++ b/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentIntentSetMultimap.java @@ -15,6 +15,7 @@ */ package org.onosproject.store.resource.impl; +import com.google.common.annotations.Beta; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Deactivate; @@ -42,6 +43,7 @@ import static org.slf4j.LoggerFactory.getLogger; */ @Component(immediate = true, enabled = true) @Service +@Beta public class ConsistentIntentSetMultimap implements IntentSetMultimap { private final Logger log = getLogger(getClass()); diff --git a/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentLinkResourceStore.java b/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentLinkResourceStore.java index 11137aa2..c332ada5 100644 --- a/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentLinkResourceStore.java +++ b/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentLinkResourceStore.java @@ -363,6 +363,15 @@ public class ConsistentLinkResourceStore extends after.add(allocations); linkAllocs.putIfAbsent(linkKey, after); } else { + boolean overlapped = before.stream() + .flatMap(x -> x.getResourceAllocation(link).stream()) + .anyMatch(x -> allocations.getResourceAllocation(link).contains(x)); + if (overlapped) { + throw new ResourceAllocationException( + String.format("Resource allocations are overlapped between %s and %s", + before, allocations) + ); + } List after = new ArrayList<>(before.size() + 1); after.addAll(before); after.add(allocations); -- cgit 1.2.3-korg