import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
import org.opendaylight.controller.md.sal.common.impl.service.AbstractDataTransaction;
import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
+import org.opendaylight.controller.md.sal.dom.broker.impl.TransactionCommitFailedExceptionMapper;
import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
}
@Override
+ @SuppressWarnings("checkstyle:illegalcatch")
public CheckedFuture<Void, TransactionCommitFailedException> submit() {
final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
checkRunning(impl);
final Collection<T> txns = getSubtransactions();
final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
- // FIXME: deal with errors thrown by backed (ready and submit can fail in theory)
- for (final T txn : txns) {
- cohorts.add(txn.ready());
- }
+ CheckedFuture<Void, TransactionCommitFailedException> ret;
+ try {
+ for (final T txn : txns) {
+ cohorts.add(txn.ready());
+ }
- final CheckedFuture<Void, TransactionCommitFailedException> ret = impl.submit(this, cohorts);
+ ret = impl.submit(this, cohorts);
+ } catch (RuntimeException e) {
+ ret = Futures.immediateFailedCheckedFuture(
+ TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e));
+ }
FUTURE_UPDATER.lazySet(this, ret);
return ret;
}
--- /dev/null
+/*
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.databroker;
+
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doThrow;
+
+import com.google.common.util.concurrent.CheckedFuture;
+import java.util.Collection;
+import java.util.Collections;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
+
+public class AbstractDOMBrokerWriteTransactionTest {
+
+ @Mock
+ private AbstractDOMTransactionFactory abstractDOMTransactionFactory;
+
+ @Mock
+ private DOMStoreWriteTransaction domStoreWriteTransaction;
+
+ private class AbstractDOMBrokerWriteTransactionTestImpl
+ extends AbstractDOMBrokerWriteTransaction<DOMStoreWriteTransaction> {
+
+ AbstractDOMBrokerWriteTransactionTestImpl() {
+ super(new Object(), Collections.emptyMap(), abstractDOMTransactionFactory);
+ }
+
+ @Override
+ protected DOMStoreWriteTransaction createTransaction(LogicalDatastoreType key) {
+ return null;
+ }
+
+ @Override
+ protected Collection<DOMStoreWriteTransaction> getSubtransactions() {
+ return Collections.singletonList(domStoreWriteTransaction);
+ }
+ }
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @Test
+ public void readyRuntimeExceptionAndCancel() {
+ RuntimeException thrown = new RuntimeException();
+ doThrow(thrown).when(domStoreWriteTransaction).ready();
+ AbstractDOMBrokerWriteTransactionTestImpl abstractDOMBrokerWriteTransactionTestImpl =
+ new AbstractDOMBrokerWriteTransactionTestImpl();
+
+ CheckedFuture<Void, TransactionCommitFailedException> submitFuture =
+ abstractDOMBrokerWriteTransactionTestImpl.submit();
+ try {
+ submitFuture.checkedGet();
+ Assert.fail("TransactionCommitFailedException expected");
+ } catch (TransactionCommitFailedException e) {
+ assertTrue(e.getCause() == thrown);
+ abstractDOMBrokerWriteTransactionTestImpl.cancel();
+ }
+ }
+
+ @Test
+ public void submitRuntimeExceptionAndCancel() {
+ RuntimeException thrown = new RuntimeException();
+ doThrow(thrown).when(abstractDOMTransactionFactory).submit(any(), any());
+ AbstractDOMBrokerWriteTransactionTestImpl abstractDOMBrokerWriteTransactionTestImpl
+ = new AbstractDOMBrokerWriteTransactionTestImpl();
+
+ CheckedFuture<Void, TransactionCommitFailedException> submitFuture =
+ abstractDOMBrokerWriteTransactionTestImpl.submit();
+ try {
+ submitFuture.checkedGet();
+ Assert.fail("TransactionCommitFailedException expected");
+ } catch (TransactionCommitFailedException e) {
+ assertTrue(e.getCause() == thrown);
+ abstractDOMBrokerWriteTransactionTestImpl.cancel();
+ }
+ }
+}
\ No newline at end of file
}
@Override
+ @SuppressWarnings("checkstyle:illegalcatch")
public CheckedFuture<Void, TransactionCommitFailedException> submit() {
final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
checkRunning(impl);
final Collection<T> txns = getSubtransactions();
final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
- // FIXME: deal with errors thrown by backed (ready and submit can fail in theory)
- for (DOMStoreWriteTransaction txn : txns) {
- cohorts.add(txn.ready());
- }
+ CheckedFuture<Void, TransactionCommitFailedException> ret;
+ try {
+ for (DOMStoreWriteTransaction txn : txns) {
+ cohorts.add(txn.ready());
+ }
- final CheckedFuture<Void, TransactionCommitFailedException> ret = impl.submit(this, cohorts);
+ ret = impl.submit(this, cohorts);
+ } catch (RuntimeException e) {
+ ret = Futures.immediateFailedCheckedFuture(
+ TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e));
+ }
FUTURE_UPDATER.lazySet(this, ret);
return ret;
}
--- /dev/null
+/*
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.dom.broker.impl;
+
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+
+import com.google.common.util.concurrent.CheckedFuture;
+import java.util.Collections;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
+
+public class DOMForwardedWriteTransactionTest {
+
+ @Mock
+ private AbstractDOMForwardedTransactionFactory abstractDOMForwardedTransactionFactory;
+
+ @Mock
+ private DOMStoreWriteTransaction domStoreWriteTransaction;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @Test
+ public void readyRuntimeExceptionAndCancel() {
+ RuntimeException thrown = new RuntimeException();
+ doThrow(thrown).when(domStoreWriteTransaction).ready();
+ DOMForwardedWriteTransaction<DOMStoreWriteTransaction> domForwardedWriteTransaction =
+ new DOMForwardedWriteTransaction<>(
+ new Object(),
+ Collections.singletonMap(LogicalDatastoreType.OPERATIONAL, domStoreWriteTransaction),
+ abstractDOMForwardedTransactionFactory);
+ CheckedFuture<Void, TransactionCommitFailedException> submitFuture = domForwardedWriteTransaction.submit();
+ try {
+ submitFuture.checkedGet();
+ Assert.fail("TransactionCommitFailedException expected");
+ } catch (TransactionCommitFailedException e) {
+ assertTrue(e.getCause() == thrown);
+ domForwardedWriteTransaction.cancel();
+ }
+ }
+
+ @Test
+ public void submitRuntimeExceptionAndCancel() {
+ RuntimeException thrown = new RuntimeException();
+ doReturn(null).when(domStoreWriteTransaction).ready();
+ doThrow(thrown).when(abstractDOMForwardedTransactionFactory).submit(any(), any());
+ DOMForwardedWriteTransaction<DOMStoreWriteTransaction> domForwardedWriteTransaction =
+ new DOMForwardedWriteTransaction<>(
+ new Object(),
+ Collections.singletonMap(LogicalDatastoreType.OPERATIONAL, domStoreWriteTransaction),
+ abstractDOMForwardedTransactionFactory);
+ CheckedFuture<Void, TransactionCommitFailedException> submitFuture = domForwardedWriteTransaction.submit();
+ try {
+ submitFuture.checkedGet();
+ Assert.fail("TransactionCommitFailedException expected");
+ } catch (TransactionCommitFailedException e) {
+ assertTrue(e.getCause() == thrown);
+ domForwardedWriteTransaction.cancel();
+ }
+ }
+}
\ No newline at end of file