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

@ -16,20 +16,8 @@ package org.hyperledger.besu.ethereum.eth.manager.peertask;
import java.util.Optional; import java.util.Optional;
public class PeerTaskExecutorResult<T> { public record PeerTaskExecutorResult<T> (
private final Optional<T> result; Optional<T> result,
private final PeerTaskExecutorResponseCode responseCode; 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;
}
}

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

Loading…
Cancel
Save