Search in sources :

Example 6 with TransactionContext

use of org.onosproject.store.service.TransactionContext in project onos by opennetworkinglab.

the class ConsistentResourceStore method register.

@Override
public boolean register(List<? extends Resource> resources) {
    checkNotNull(resources);
    if (log.isTraceEnabled()) {
        resources.forEach(r -> log.trace("registering {}", r));
    }
    // Retry the transaction until successful.
    while (true) {
        TransactionContext tx = service.transactionContextBuilder().build();
        tx.begin();
        // the order is preserved by LinkedHashMap
        Map<DiscreteResource, List<Resource>> resourceMap = resources.stream().filter(x -> x.parent().isPresent()).collect(groupingBy(x -> x.parent().get(), LinkedHashMap::new, Collectors.<Resource>toList()));
        TransactionalDiscreteResourceSubStore discreteTxStore = discreteStore.transactional(tx);
        TransactionalContinuousResourceSubStore continuousTxStore = continuousStore.transactional(tx);
        for (Map.Entry<DiscreteResource, List<Resource>> entry : resourceMap.entrySet()) {
            DiscreteResourceId parentId = entry.getKey().id();
            if (!discreteTxStore.lookup(parentId).isPresent()) {
                return abortTransaction(tx);
            }
            if (!register(discreteTxStore, continuousTxStore, parentId, entry.getValue())) {
                return abortTransaction(tx);
            }
        }
        try {
            CommitStatus status = commitTransaction(tx);
            if (status == CommitStatus.SUCCESS) {
                log.trace("Transaction commit succeeded on registration: resources={}", resources);
                List<ResourceEvent> events = resources.stream().filter(x -> x.parent().isPresent()).map(x -> new ResourceEvent(RESOURCE_ADDED, x)).collect(Collectors.toList());
                notifyDelegate(events);
                return true;
            }
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            log.warn("Transaction commit failed on registration", e);
            return false;
        }
    }
}
Also used : DiscreteResourceId(org.onosproject.net.resource.DiscreteResourceId) Tools(org.onlab.util.Tools) LoggerFactory(org.slf4j.LoggerFactory) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) ResourceStoreDelegate(org.onosproject.net.resource.ResourceStoreDelegate) TimeoutException(java.util.concurrent.TimeoutException) KryoNamespace(org.onlab.util.KryoNamespace) DistributedPrimitive(org.onosproject.store.service.DistributedPrimitive) Function(java.util.function.Function) LinkedHashMap(java.util.LinkedHashMap) Component(org.osgi.service.component.annotations.Component) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) StorageService(org.onosproject.store.service.StorageService) Map(java.util.Map) ResourceStore(org.onosproject.net.resource.ResourceStore) Activate(org.osgi.service.component.annotations.Activate) KryoNamespaces(org.onosproject.store.serializers.KryoNamespaces) LinkedHashSet(java.util.LinkedHashSet) Serializer(org.onosproject.store.service.Serializer) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) RESOURCE_REMOVED(org.onosproject.net.resource.ResourceEvent.Type.RESOURCE_REMOVED) ResourceConsumerId(org.onosproject.net.resource.ResourceConsumerId) ResourceEvent(org.onosproject.net.resource.ResourceEvent) Resources(org.onosproject.net.resource.Resources) CommitStatus(org.onosproject.store.service.CommitStatus) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Resource(org.onosproject.net.resource.Resource) TransactionContext(org.onosproject.store.service.TransactionContext) Collectors(java.util.stream.Collectors) ResourceAllocation(org.onosproject.net.resource.ResourceAllocation) Beta(com.google.common.annotations.Beta) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RESOURCE_ADDED(org.onosproject.net.resource.ResourceEvent.Type.RESOURCE_ADDED) List(java.util.List) Stream(java.util.stream.Stream) ResourceId(org.onosproject.net.resource.ResourceId) AbstractStore(org.onosproject.store.AbstractStore) Optional(java.util.Optional) ResourceConsumer(org.onosproject.net.resource.ResourceConsumer) ContinuousResourceId(org.onosproject.net.resource.ContinuousResourceId) Reference(org.osgi.service.component.annotations.Reference) ContinuousResource(org.onosproject.net.resource.ContinuousResource) DiscreteResource(org.onosproject.net.resource.DiscreteResource) Resource(org.onosproject.net.resource.Resource) ContinuousResource(org.onosproject.net.resource.ContinuousResource) DiscreteResource(org.onosproject.net.resource.DiscreteResource) DiscreteResource(org.onosproject.net.resource.DiscreteResource) TransactionContext(org.onosproject.store.service.TransactionContext) CommitStatus(org.onosproject.store.service.CommitStatus) ResourceEvent(org.onosproject.net.resource.ResourceEvent) List(java.util.List) DiscreteResourceId(org.onosproject.net.resource.DiscreteResourceId) ExecutionException(java.util.concurrent.ExecutionException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with TransactionContext

use of org.onosproject.store.service.TransactionContext in project onos by opennetworkinglab.

the class TransactionalMapTestGetCommand method doExecute.

@Override
protected void doExecute() {
    StorageService storageService = get(StorageService.class);
    TransactionContext context;
    context = storageService.transactionContextBuilder().build();
    context.begin();
    try {
        map = context.getTransactionalMap(mapName, serializer);
        String response = map.get(key);
        context.commit();
        if (response == null) {
            print("Key %s not found.", key);
        } else {
            print("Key-value pair (%s, %s) found.", key, response);
        }
    } catch (Exception e) {
        context.abort();
        throw e;
    }
}
Also used : TransactionContext(org.onosproject.store.service.TransactionContext) StorageService(org.onosproject.store.service.StorageService)

Example 8 with TransactionContext

use of org.onosproject.store.service.TransactionContext in project onos by opennetworkinglab.

the class TransactionalMapTestPutCommand method doExecute.

@Override
protected void doExecute() {
    StorageService storageService = get(StorageService.class);
    TransactionContext context;
    context = storageService.transactionContextBuilder().build();
    context.begin();
    try {
        map = context.getTransactionalMap(mapName, serializer);
        for (int i = 1; i <= numKeys; i++) {
            String key = prefix + i;
            String response = map.put(key, value);
            if (response == null) {
                print("Created Key %s with value %s.", key, value);
            } else {
                print("Put %s into key %s. The old value was %s.", value, key, response);
            }
        }
        context.commit();
    } catch (Exception e) {
        context.abort();
        throw e;
    }
}
Also used : TransactionContext(org.onosproject.store.service.TransactionContext) StorageService(org.onosproject.store.service.StorageService)

Aggregations

TransactionContext (org.onosproject.store.service.TransactionContext)8 ExecutionException (java.util.concurrent.ExecutionException)4 TimeoutException (java.util.concurrent.TimeoutException)4 ContinuousResource (org.onosproject.net.resource.ContinuousResource)4 DiscreteResource (org.onosproject.net.resource.DiscreteResource)4 Resource (org.onosproject.net.resource.Resource)4 ResourceAllocation (org.onosproject.net.resource.ResourceAllocation)3 ResourceConsumerId (org.onosproject.net.resource.ResourceConsumerId)3 CommitStatus (org.onosproject.store.service.CommitStatus)3 StorageService (org.onosproject.store.service.StorageService)3 Beta (com.google.common.annotations.Beta)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Collection (java.util.Collection)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2