import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicLong;
import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.TransactionsParams;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
private ScheduledFuture<?> writingFuture;
private ScheduledFuture<?> completingFuture;
- private long txCounter;
+ private final AtomicLong txCounter = new AtomicLong();
private volatile State state;
AbstractTransactionHandler(final TransactionsParams params) {
}
// Not completed yet: create a transaction and hook it up
- final long txId = txCounter++;
+ final long txId = txCounter.incrementAndGet();
final ListenableFuture<?> execFuture = execWrite(txId);
LOG.debug("New future #{} allocated", txId);
LOG.debug("Completed waiting for all futures");
state = State.SUCCESSFUL;
completingFuture.cancel(false);
- runSuccessful(txCounter);
+ runSuccessful(txCounter.get());
return true;
}
}
final void txFailure(final ListenableFuture<?> execFuture, final long txId, final Throwable cause) {
- LOG.debug("Future #{} failed", txId, cause);
+ LOG.error("Commit future failed for tx # {}", txId, cause);
futures.remove(execFuture);
final State local = state;
case WAITING:
state = State.FAILED;
writingFuture.cancel(false);
- runFailed(cause);
+ runFailed(cause, txId);
break;
default:
throw new IllegalStateException("Unhandled state " + local);
}
state = State.FAILED;
- runTimedOut(new TimeoutException("Collection did not finish in " + DEAD_TIMEOUT_SECONDS + " seconds"));
+ runTimedOut("Transactions did not finish in " + DEAD_TIMEOUT_SECONDS + " seconds");
}
abstract ListenableFuture<?> execWrite(long txId);
- abstract void runFailed(Throwable cause);
+ abstract void runFailed(Throwable cause, long txId);
abstract void runSuccessful(long allTx);
- abstract void runTimedOut(Exception cause);
+ abstract void runTimedOut(String cause);
}
}
@Override
- void runFailed(final Throwable cause) {
+ void runFailed(final Throwable cause, final long txId) {
closeProducer(itemProducer);
future.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
- .withError(RpcError.ErrorType.APPLICATION, "Submit failed", cause).build());
+ .withError(RpcError.ErrorType.APPLICATION, "Commit failed for tx # " + txId, cause).build());
}
@Override
}
@Override
- void runTimedOut(final Exception cause) {
+ void runTimedOut(final String cause) {
closeProducer(itemProducer);
future.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
- .withError(RpcError.ErrorType.APPLICATION,
- "Final submit was timed out by the test provider or was interrupted", cause).build());
+ .withError(RpcError.ErrorType.APPLICATION, cause).build());
}
}
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
-import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.SplittableRandom;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicLong;
import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
private static final Logger LOG = LoggerFactory.getLogger(WriteTransactionsHandler.class);
final SettableFuture<RpcResult<WriteTransactionsOutput>> completionFuture = SettableFuture.create();
- private final Set<Integer> usedValues = new HashSet<>();
+ private final Set<Integer> usedValues = ConcurrentHashMap.newKeySet();
private final YangInstanceIdentifier idListItem;
- private long insertTx = 0;
- private long deleteTx = 0;
+ private final AtomicLong insertTx = new AtomicLong();
+ private final AtomicLong deleteTx = new AtomicLong();
WriteTransactionsHandler(final YangInstanceIdentifier idListItem, final WriteTransactionsInput input) {
super(input);
if (usedValues.contains(i)) {
LOG.debug("Deleting item: {}", i);
- deleteTx++;
+ deleteTx.incrementAndGet();
tx.delete(LogicalDatastoreType.CONFIGURATION, entryId);
usedValues.remove(i);
} else {
LOG.debug("Inserting item: {}", i);
- insertTx++;
+ insertTx.incrementAndGet();
final MapEntryNode entry = ImmutableNodes.mapEntry(ITEM, NUMBER, i);
tx.put(LogicalDatastoreType.CONFIGURATION, entryId, entry);
usedValues.add(i);
}
@Override
- void runFailed(final Throwable cause) {
+ void runFailed(final Throwable cause, final long txId) {
completionFuture.set(RpcResultBuilder.<WriteTransactionsOutput>failed()
- .withError(RpcError.ErrorType.APPLICATION, "Submit failed", cause).build());
+ .withError(RpcError.ErrorType.APPLICATION, "Commit failed for tx # " + txId, cause).build());
}
@Override
void runSuccessful(final long allTx) {
final WriteTransactionsOutput output = new WriteTransactionsOutputBuilder()
.setAllTx(allTx)
- .setInsertTx(insertTx)
- .setDeleteTx(deleteTx)
+ .setInsertTx(insertTx.get())
+ .setDeleteTx(deleteTx.get())
.build();
completionFuture.set(RpcResultBuilder.<WriteTransactionsOutput>success()
}
@Override
- void runTimedOut(final Exception cause) {
+ void runTimedOut(final String cause) {
completionFuture.set(RpcResultBuilder.<WriteTransactionsOutput>failed()
- .withError(RpcError.ErrorType.APPLICATION,
- "Final submit was timed out by the test provider or was interrupted", cause).build());
+ .withError(RpcError.ErrorType.APPLICATION, cause).build());
}
abstract DOMDataWriteTransaction createTransaction();