7311: Convert PeerTaskExecutorResult to a record

Signed-off-by: Matilda Clerke <matilda.clerke@consensys.net>
pull/7628/head
Matilda Clerke 2 months ago
parent e31bb70037
commit 41923d3c40
  1. 19
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutor.java
  2. 22
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutorResult.java
  3. 36
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutorTest.java

@ -26,6 +26,7 @@ import org.hyperledger.besu.plugin.services.metrics.OperationTimer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
@ -80,10 +81,10 @@ public class PeerTaskExecutor {
executorResult = executeAgainstPeer(peerTask, peer);
} catch (NoAvailablePeerException e) {
executorResult =
new PeerTaskExecutorResult<>(null, PeerTaskExecutorResponseCode.NO_PEER_AVAILABLE);
new PeerTaskExecutorResult<>(Optional.empty(), PeerTaskExecutorResponseCode.NO_PEER_AVAILABLE);
}
} while (--triesRemaining > 0
&& executorResult.getResponseCode() != PeerTaskExecutorResponseCode.SUCCESS);
&& executorResult.responseCode() != PeerTaskExecutorResponseCode.SUCCESS);
return executorResult;
}
@ -112,28 +113,28 @@ public class PeerTaskExecutor {
result = peerTask.parseResponse(responseMessageData);
}
peer.recordUsefulResponse();
executorResult = new PeerTaskExecutorResult<>(result, PeerTaskExecutorResponseCode.SUCCESS);
executorResult = new PeerTaskExecutorResult<>(Optional.ofNullable(result), PeerTaskExecutorResponseCode.SUCCESS);
} catch (PeerConnection.PeerNotConnected e) {
executorResult =
new PeerTaskExecutorResult<>(null, PeerTaskExecutorResponseCode.PEER_DISCONNECTED);
new PeerTaskExecutorResult<>(Optional.empty(), PeerTaskExecutorResponseCode.PEER_DISCONNECTED);
} catch (InterruptedException | TimeoutException e) {
peer.recordRequestTimeout(requestMessageData.getCode());
executorResult = new PeerTaskExecutorResult<>(null, PeerTaskExecutorResponseCode.TIMEOUT);
executorResult = new PeerTaskExecutorResult<>(Optional.empty(), PeerTaskExecutorResponseCode.TIMEOUT);
} catch (InvalidPeerTaskResponseException e) {
peer.recordUselessResponse(e.getMessage());
executorResult =
new PeerTaskExecutorResult<>(null, PeerTaskExecutorResponseCode.INVALID_RESPONSE);
new PeerTaskExecutorResult<>(Optional.empty(), PeerTaskExecutorResponseCode.INVALID_RESPONSE);
} catch (ExecutionException e) {
executorResult =
new PeerTaskExecutorResult<>(null, PeerTaskExecutorResponseCode.INTERNAL_SERVER_ERROR);
new PeerTaskExecutorResult<>(Optional.empty(), PeerTaskExecutorResponseCode.INTERNAL_SERVER_ERROR);
}
} while (--triesRemaining > 0
&& executorResult.getResponseCode() != PeerTaskExecutorResponseCode.SUCCESS
&& executorResult.getResponseCode() != PeerTaskExecutorResponseCode.PEER_DISCONNECTED
&& executorResult.responseCode() != PeerTaskExecutorResponseCode.SUCCESS
&& executorResult.responseCode() != PeerTaskExecutorResponseCode.PEER_DISCONNECTED
&& sleepBetweenRetries());
return executorResult;

@ -16,20 +16,8 @@ package org.hyperledger.besu.ethereum.eth.manager.peertask;
import java.util.Optional;
public class PeerTaskExecutorResult<T> {
private final Optional<T> result;
private final PeerTaskExecutorResponseCode responseCode;
public PeerTaskExecutorResult(final T result, final PeerTaskExecutorResponseCode responseCode) {
this.result = Optional.ofNullable(result);
this.responseCode = responseCode;
}
public Optional<T> getResult() {
return result;
}
public PeerTaskExecutorResponseCode getResponseCode() {
return responseCode;
}
}
public record PeerTaskExecutorResult<T> (
Optional<T> result,
PeerTaskExecutorResponseCode responseCode
)
{}

@ -84,9 +84,9 @@ public class PeerTaskExecutorTest {
Mockito.verify(ethPeer).recordUsefulResponse();
Assertions.assertNotNull(result);
Assertions.assertTrue(result.getResult().isPresent());
Assertions.assertSame(responseObject, result.getResult().get());
Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.getResponseCode());
Assertions.assertTrue(result.result().isPresent());
Assertions.assertSame(responseObject, result.result().get());
Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.responseCode());
}
@Test
@ -117,9 +117,9 @@ public class PeerTaskExecutorTest {
Mockito.verify(ethPeer).recordUsefulResponse();
Assertions.assertNotNull(result);
Assertions.assertTrue(result.getResult().isPresent());
Assertions.assertSame(responseObject, result.getResult().get());
Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.getResponseCode());
Assertions.assertTrue(result.result().isPresent());
Assertions.assertSame(responseObject, result.result().get());
Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.responseCode());
}
@Test
@ -139,9 +139,9 @@ public class PeerTaskExecutorTest {
PeerTaskExecutorResult<Object> result = peerTaskExecutor.executeAgainstPeer(peerTask, ethPeer);
Assertions.assertNotNull(result);
Assertions.assertTrue(result.getResult().isEmpty());
Assertions.assertTrue(result.result().isEmpty());
Assertions.assertEquals(
PeerTaskExecutorResponseCode.PEER_DISCONNECTED, result.getResponseCode());
PeerTaskExecutorResponseCode.PEER_DISCONNECTED, result.responseCode());
}
@Test
@ -165,8 +165,8 @@ public class PeerTaskExecutorTest {
Mockito.verify(ethPeer).recordRequestTimeout(requestMessageDataCode);
Assertions.assertNotNull(result);
Assertions.assertTrue(result.getResult().isEmpty());
Assertions.assertEquals(PeerTaskExecutorResponseCode.TIMEOUT, result.getResponseCode());
Assertions.assertTrue(result.result().isEmpty());
Assertions.assertEquals(PeerTaskExecutorResponseCode.TIMEOUT, result.responseCode());
}
@Test
@ -191,9 +191,9 @@ public class PeerTaskExecutorTest {
Mockito.verify(ethPeer).recordUselessResponse(null);
Assertions.assertNotNull(result);
Assertions.assertTrue(result.getResult().isEmpty());
Assertions.assertTrue(result.result().isEmpty());
Assertions.assertEquals(
PeerTaskExecutorResponseCode.INVALID_RESPONSE, result.getResponseCode());
PeerTaskExecutorResponseCode.INVALID_RESPONSE, result.responseCode());
}
@Test
@ -222,9 +222,9 @@ public class PeerTaskExecutorTest {
Mockito.verify(ethPeer).recordUsefulResponse();
Assertions.assertNotNull(result);
Assertions.assertTrue(result.getResult().isPresent());
Assertions.assertSame(responseObject, result.getResult().get());
Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.getResponseCode());
Assertions.assertTrue(result.result().isPresent());
Assertions.assertSame(responseObject, result.result().get());
Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.responseCode());
}
@Test
@ -262,8 +262,8 @@ public class PeerTaskExecutorTest {
Mockito.verify(peer2).recordUsefulResponse();
Assertions.assertNotNull(result);
Assertions.assertTrue(result.getResult().isPresent());
Assertions.assertSame(responseObject, result.getResult().get());
Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.getResponseCode());
Assertions.assertTrue(result.result().isPresent());
Assertions.assertSame(responseObject, result.result().get());
Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.responseCode());
}
}

Loading…
Cancel
Save