Skip to content

Commit b2a8697

Browse files
committed
Ability throw checked exceptions from transactionManager
1 parent 22838f1 commit b2a8697

File tree

6 files changed

+49
-33
lines changed

6 files changed

+49
-33
lines changed

services/src/kilda-core/kilda-persistence-api/src/main/java/org/openkilda/persistence/TransactionCallback.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
* @param <T> The return type of the callback.
2222
*/
2323
@FunctionalInterface
24-
public interface TransactionCallback<T> {
25-
T doInTransaction();
24+
public interface TransactionCallback<T, E extends Exception> {
25+
T doInTransaction() throws E;
2626
}

services/src/kilda-core/kilda-persistence-api/src/main/java/org/openkilda/persistence/TransactionCallbackWithoutResult.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
* Callback to be executed inside an managed transaction.
2020
*/
2121
@FunctionalInterface
22-
public interface TransactionCallbackWithoutResult {
23-
void doInTransaction();
22+
public interface TransactionCallbackWithoutResult<E extends Exception> {
23+
void doInTransaction() throws E;
2424
}

services/src/kilda-core/kilda-persistence-api/src/main/java/org/openkilda/persistence/TransactionManager.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public interface TransactionManager {
2828
* @param action the transactional action
2929
* @return a result returned by the callback
3030
*/
31-
<T> T doInTransaction(TransactionCallback<T> action);
31+
<T, E extends Exception> T doInTransaction(TransactionCallback<T, E> action);
3232

3333
/**
3434
* Execute the action specified by the given callback within a transaction.
@@ -38,5 +38,5 @@ public interface TransactionManager {
3838
*
3939
* @param action the transactional action
4040
*/
41-
void doInTransaction(TransactionCallbackWithoutResult action);
41+
<E extends Exception> void doInTransaction(TransactionCallbackWithoutResult<E> action);
4242
}

services/src/kilda-core/kilda-persistence-neo4j/src/main/java/org/openkilda/persistence/Neo4jTransactionManager.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.openkilda.persistence.repositories.impl.Neo4jSessionFactory;
1919

2020
import com.google.common.annotations.VisibleForTesting;
21+
import lombok.SneakyThrows;
2122
import lombok.extern.slf4j.Slf4j;
2223
import org.neo4j.ogm.session.Session;
2324
import org.neo4j.ogm.session.SessionFactory;
@@ -49,8 +50,9 @@ public Session getSession() {
4950
return Optional.ofNullable(SESSION_HOLDER.get()).orElseGet(sessionFactory::openSession);
5051
}
5152

53+
@SneakyThrows
5254
@Override
53-
public <T> T doInTransaction(TransactionCallback<T> action) {
55+
public <T, E extends Exception> T doInTransaction(TransactionCallback<T, E> action) {
5456
begin();
5557

5658
try {
@@ -63,8 +65,9 @@ public <T> T doInTransaction(TransactionCallback<T> action) {
6365
}
6466
}
6567

68+
@SneakyThrows
6669
@Override
67-
public void doInTransaction(TransactionCallbackWithoutResult action) {
70+
public <E extends Exception> void doInTransaction(TransactionCallbackWithoutResult<E> action) {
6871
begin();
6972

7073
try {

services/src/kilda-core/kilda-persistence-neo4j/src/test/java/org/openkilda/persistence/Neo4jTransactionManagerTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,27 @@ public void shouldRollbackExtendedAndRootTx() {
148148
// then
149149
assertEquals(0, repository.findAll().size());
150150
}
151+
152+
@Test(expected = TestCheckedException.class)
153+
public void shouldCheckedExceptionThrown() {
154+
// when
155+
txManager.doInTransaction(() -> {
156+
throw new TestCheckedException();
157+
});
158+
159+
fail();
160+
}
161+
162+
@Test(expected = TestCheckedException.class)
163+
public void shouldCheckedExceptionThrownWithoutResult() {
164+
// when
165+
txManager.doInTransaction((TransactionCallbackWithoutResult<TestCheckedException>) () -> {
166+
throw new TestCheckedException();
167+
});
168+
169+
fail();
170+
}
171+
172+
private class TestCheckedException extends Exception {
173+
}
151174
}

services/wfm/src/main/java/org/openkilda/wfm/topology/flow/service/FlowService.java

+15-25
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import java.util.ArrayList;
5252
import java.util.HashSet;
5353
import java.util.List;
54-
import java.util.Objects;
5554
import java.util.Optional;
5655
import java.util.Set;
5756

@@ -228,34 +227,25 @@ public FlowPair updateFlow(Flow updatingFlow, String diverseFlowId, FlowCommandS
228227
SwitchValidationException {
229228
flowValidator.validate(updatingFlow);
230229

231-
if (diverseFlowId != null && !doesFlowExist(diverseFlowId)) {
232-
throw new FlowNotFoundException(diverseFlowId);
233-
}
234-
235-
// TODO: the strategy is defined either per flow or system-wide.
236-
PathComputer pathComputer = pathComputerFactory.getPathComputer();
237-
PathPair pathPair = pathComputer.getPath(updatingFlow, true);
238-
239230
updatingFlow.setStatus(FlowStatus.IN_PROGRESS);
240231

241232
UpdatedFlowPairWithSegments result = transactionManager.doInTransaction(() -> {
242-
Optional<FlowPair> foundFlowPair = getFlowPair(updatingFlow.getFlowId());
243-
if (!foundFlowPair.isPresent()) {
244-
return Optional.<UpdatedFlowPairWithSegments>empty();
245-
}
233+
FlowPair currentFlow = getFlowPair(updatingFlow.getFlowId())
234+
.orElseThrow(() -> new FlowNotFoundException(updatingFlow.getFlowId()));
246235

247-
FlowPair currentFlow = foundFlowPair.get();
236+
if (diverseFlowId != null && !doesFlowExist(diverseFlowId)) {
237+
throw new FlowNotFoundException(diverseFlowId);
238+
}
248239

249-
// group id update
250-
updatingFlow.setGroupId(currentFlow.getForward().getGroupId());
251-
if (diverseFlowId != null) {
252-
if (Objects.equals(updatingFlow.getFlowId(), diverseFlowId)) {
253-
updatingFlow.setGroupId(null);
254-
} else {
255-
updatingFlow.setGroupId(getOrCreateFlowGroupId(diverseFlowId));
256-
}
240+
if (diverseFlowId == null) {
241+
updatingFlow.setGroupId(null);
242+
} else {
243+
updatingFlow.setGroupId(getOrCreateFlowGroupId(diverseFlowId));
257244
}
258245

246+
PathComputer pathComputer = pathComputerFactory.getPathComputer();
247+
PathPair pathPair = pathComputer.getPath(updatingFlow, true);
248+
259249
List<FlowSegment> forwardSegments = getFlowSegments(currentFlow.getForward());
260250
List<FlowSegment> reverseSegments = getFlowSegments(currentFlow.getReverse());
261251
List<FlowSegment> flowSegments = union(forwardSegments, reverseSegments);
@@ -278,11 +268,11 @@ public FlowPair updateFlow(Flow updatingFlow, String diverseFlowId, FlowCommandS
278268

279269
flowResourcesManager.deallocateFlow(currentFlow);
280270

281-
return Optional.of(UpdatedFlowPairWithSegments.builder()
271+
return UpdatedFlowPairWithSegments.builder()
282272
.oldFlowPair(currentFlow).oldForwardSegments(forwardSegments).oldReverseSegments(reverseSegments)
283273
.flowPair(newFlowWithResources).forwardSegments(newForwardSegments)
284-
.reverseSegments(newReverseSegments).build());
285-
}).orElseThrow(() -> new FlowNotFoundException(updatingFlow.getFlowId()));
274+
.reverseSegments(newReverseSegments).build();
275+
});
286276

287277
// To avoid race condition in DB updates, we should send commands only after DB transaction commit.
288278
sender.sendUpdateRulesCommand(result);

0 commit comments

Comments
 (0)