use of org.onosproject.store.service.TransactionContext in project onos by opennetworkinglab.
the class TransactionPerfApp method initializeMap.
/**
* Initializes the map.
*/
private void initializeMap() {
TransactionContext context = storageService.transactionContextBuilder().build();
context.begin();
try {
TransactionalMap<String, String> map = context.getTransactionalMap(mapName, serializer);
for (int i = 0; i < totalOperationsPerTransaction; i++) {
map.put(KEY_PREFIX + i, KEY_PREFIX + i);
}
context.commit().join();
} catch (Exception e) {
context.abort();
log.warn("An exception occurred during initialization: {}", e);
}
}
use of org.onosproject.store.service.TransactionContext in project onos by opennetworkinglab.
the class TransactionPerfApp method runTest.
/**
* Runs the test.
*/
private void runTest(int iteration) {
testRunner.execute(() -> {
// Attempt the transaction until successful if retries are enabled.
CommitStatus status = null;
do {
TransactionContext context = storageService.transactionContextBuilder().build();
context.begin();
try {
TransactionalMap<String, String> map = context.getTransactionalMap(mapName, serializer);
int reads = (int) (totalOperationsPerTransaction * readPercentage);
for (int i = 0; i < reads; i++) {
map.get(KEY_PREFIX + i);
}
int writes = (int) (totalOperationsPerTransaction * (1 - readPercentage));
for (int i = 0; i < writes; i++) {
map.put(KEY_PREFIX + i, KEY_PREFIX + iteration + i);
}
status = context.commit().join();
attempted.incrementAndGet();
} catch (Exception e) {
context.abort();
log.warn("An exception occurred during a transaction: {}", e);
}
} while (withRetries && status != CommitStatus.SUCCESS);
// If the transaction was successful, increment succeeded transactions.
if (status == CommitStatus.SUCCESS) {
succeeded.incrementAndGet();
}
runTest(this.iteration.getAndIncrement());
});
}
use of org.onosproject.store.service.TransactionContext in project onos by opennetworkinglab.
the class ConsistentResourceStore method unregister.
@Override
public boolean unregister(List<? extends ResourceId> ids) {
checkNotNull(ids);
// Retry the transaction until successful.
while (true) {
TransactionContext tx = service.transactionContextBuilder().build();
tx.begin();
TransactionalDiscreteResourceSubStore discreteTxStore = discreteStore.transactional(tx);
TransactionalContinuousResourceSubStore continuousTxStore = continuousStore.transactional(tx);
// Look up resources by resource IDs
List<Resource> resources = ids.stream().filter(x -> x.parent().isPresent()).map(x -> {
// avoid access to consistent map in the case of discrete resource
if (x instanceof DiscreteResourceId) {
return Optional.of(Resources.discrete((DiscreteResourceId) x).resource());
} else {
return continuousTxStore.lookup((ContinuousResourceId) x);
}
}).flatMap(Tools::stream).collect(Collectors.toList());
// the order is preserved by LinkedHashMap
Map<DiscreteResourceId, List<Resource>> resourceMap = resources.stream().collect(Collectors.groupingBy(x -> x.parent().get().id(), LinkedHashMap::new, Collectors.toList()));
for (Map.Entry<DiscreteResourceId, List<Resource>> entry : resourceMap.entrySet()) {
if (!unregister(discreteTxStore, continuousTxStore, entry.getKey(), entry.getValue())) {
log.warn("Failed to unregister {}: Failed to remove {} values.", entry.getKey(), entry.getValue().size());
return abortTransaction(tx);
}
}
try {
CommitStatus status = commitTransaction(tx);
if (status == CommitStatus.SUCCESS) {
List<ResourceEvent> events = resources.stream().filter(x -> x.parent().isPresent()).map(x -> new ResourceEvent(RESOURCE_REMOVED, x)).collect(Collectors.toList());
notifyDelegate(events);
return true;
}
} catch (InterruptedException | ExecutionException | TimeoutException e) {
String message = resources.stream().map(Resource::simpleTypeName).collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().stream().map(entry -> String.format("%d %s type resources", entry.getValue(), entry.getKey())).collect(Collectors.joining(", "));
log.warn("Failed to unregister {}: {}", message, e);
return false;
}
}
}
use of org.onosproject.store.service.TransactionContext in project onos by opennetworkinglab.
the class ConsistentResourceStore method allocate.
@Override
public boolean allocate(List<? extends Resource> resources, ResourceConsumer consumer) {
checkNotNull(resources);
checkNotNull(consumer);
while (true) {
TransactionContext tx = service.transactionContextBuilder().build();
tx.begin();
TransactionalDiscreteResourceSubStore discreteTxStore = discreteStore.transactional(tx);
TransactionalContinuousResourceSubStore continuousTxStore = continuousStore.transactional(tx);
for (Resource resource : resources) {
if (resource instanceof DiscreteResource) {
if (!discreteTxStore.allocate(consumer.consumerId(), (DiscreteResource) resource)) {
return abortTransaction(tx);
}
} else if (resource instanceof ContinuousResource) {
if (!continuousTxStore.allocate(consumer.consumerId(), (ContinuousResource) resource)) {
return abortTransaction(tx);
}
}
}
try {
if (commitTransaction(tx) == CommitStatus.SUCCESS) {
return true;
}
} catch (InterruptedException | ExecutionException | TimeoutException e) {
log.warn("Failed to allocate {}: {}", resources, e);
return false;
}
}
}
use of org.onosproject.store.service.TransactionContext in project onos by opennetworkinglab.
the class ConsistentResourceStore method release.
@Override
public boolean release(List<ResourceAllocation> allocations) {
checkNotNull(allocations);
while (true) {
TransactionContext tx = service.transactionContextBuilder().build();
tx.begin();
TransactionalDiscreteResourceSubStore discreteTxStore = discreteStore.transactional(tx);
TransactionalContinuousResourceSubStore continuousTxStore = continuousStore.transactional(tx);
for (ResourceAllocation allocation : allocations) {
Resource resource = allocation.resource();
ResourceConsumerId consumerId = allocation.consumerId();
if (resource instanceof DiscreteResource) {
if (!discreteTxStore.release(consumerId, (DiscreteResource) resource)) {
return abortTransaction(tx);
}
} else if (resource instanceof ContinuousResource) {
if (!continuousTxStore.release(consumerId, (ContinuousResource) resource)) {
return abortTransaction(tx);
}
}
}
try {
if (commitTransaction(tx) == CommitStatus.SUCCESS) {
return true;
}
} catch (InterruptedException | ExecutionException | TimeoutException e) {
log.warn("Failed to release {}: {}", allocations, e);
return false;
}
}
}
Aggregations