Search in sources :

Example 1 with NodeProperties

use of com.hedera.mirror.monitor.NodeProperties in project hedera-mirror-node by hashgraph.

the class TransactionPublisher method getClients.

private synchronized Flux<Client> getClients() {
    NodeValidationProperties validationProperties = monitorProperties.getNodeValidation();
    var configuredNodes = monitorProperties.getNodes();
    Map<String, AccountId> nodeMap = configuredNodes.stream().collect(Collectors.toMap(NodeProperties::getEndpoint, p -> AccountId.fromString(p.getAccountId())));
    this.nodes.addAll(configuredNodes);
    Client client = toClient(nodeMap);
    client.setMaxAttempts(validationProperties.getMaxAttempts());
    client.setMaxBackoff(validationProperties.getMaxBackoff());
    client.setMinBackoff(validationProperties.getMinBackoff());
    client.setRequestTimeout(validationProperties.getRequestTimeout());
    this.validationClient.set(client);
    if (validationProperties.isEnabled() && nodeValidator.get() == null) {
        int nodeCount = configuredNodes.size();
        int parallelism = Math.min(nodeCount, validationProperties.getMaxThreads());
        var scheduler = Schedulers.newParallel("validator", parallelism + 1);
        var disposable = Flux.interval(Duration.ZERO, validationProperties.getFrequency(), scheduler).filter(// In case it's later disabled
        i -> validationProperties.isEnabled()).flatMap(i -> Flux.fromIterable(configuredNodes)).parallel(parallelism).runOn(scheduler).map(this::validateNode).sequential().buffer(nodeCount).doOnNext(i -> log.info("{} of {} nodes are functional", nodes.size(), nodeCount)).doOnSubscribe(s -> log.info("Starting node validation")).onErrorContinue((e, i) -> log.error("Exception validating nodes: ", e)).subscribe();
        nodeValidator.set(disposable);
    }
    return Flux.range(0, publishProperties.getClients()).flatMap(i -> Mono.defer(() -> Mono.just(toClient(nodeMap))));
}
Also used : NodeProperties(com.hedera.mirror.monitor.NodeProperties) Disposable(reactor.core.Disposable) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) RequiredArgsConstructor(lombok.RequiredArgsConstructor) TimeoutException(java.util.concurrent.TimeoutException) Status(com.hedera.hashgraph.sdk.Status) AtomicReference(java.util.concurrent.atomic.AtomicReference) SecureRandom(java.security.SecureRandom) HbarUnit(com.hedera.hashgraph.sdk.HbarUnit) WithExecute(com.hedera.hashgraph.sdk.WithExecute) Duration(java.time.Duration) Map(java.util.Map) Transaction(com.hedera.hashgraph.sdk.Transaction) Schedulers(reactor.core.scheduler.Schedulers) AccountId(com.hedera.hashgraph.sdk.AccountId) Hbar(com.hedera.hashgraph.sdk.Hbar) Named(javax.inject.Named) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) TransactionId(com.hedera.hashgraph.sdk.TransactionId) TransactionRecordQuery(com.hedera.hashgraph.sdk.TransactionRecordQuery) NodeValidationProperties(com.hedera.mirror.monitor.NodeValidationProperties) Client(com.hedera.hashgraph.sdk.Client) Mono(reactor.core.publisher.Mono) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) TransactionReceiptQuery(com.hedera.hashgraph.sdk.TransactionReceiptQuery) Flux(reactor.core.publisher.Flux) SUCCESS(com.hedera.hashgraph.sdk.Status.SUCCESS) Log4j2(lombok.extern.log4j.Log4j2) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) MonitorProperties(com.hedera.mirror.monitor.MonitorProperties) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) AccountId(com.hedera.hashgraph.sdk.AccountId) NodeValidationProperties(com.hedera.mirror.monitor.NodeValidationProperties) Client(com.hedera.hashgraph.sdk.Client)

Example 2 with NodeProperties

use of com.hedera.mirror.monitor.NodeProperties in project hedera-mirror-node by hashgraph.

the class TransactionPublisherTest method setup.

@BeforeEach
void setup() throws IOException {
    publishScenarioProperties = new PublishScenarioProperties();
    publishScenarioProperties.setName("test");
    publishScenarioProperties.setType(TransactionType.CRYPTO_TRANSFER);
    monitorProperties = new MonitorProperties();
    monitorProperties.setNodes(Set.of(new NodeProperties("0.0.3", "in-process:test")));
    monitorProperties.getNodeValidation().setEnabled(false);
    OperatorProperties operatorProperties = monitorProperties.getOperator();
    operatorProperties.setAccountId("0.0.100");
    operatorProperties.setPrivateKey(PrivateKey.generate().toString());
    publishProperties = new PublishProperties();
    transactionPublisher = new TransactionPublisher(monitorProperties, publishProperties);
    cryptoServiceStub = new CryptoServiceStub();
    server = InProcessServerBuilder.forName("test").addService(cryptoServiceStub).directExecutor().build().start();
}
Also used : NodeProperties(com.hedera.mirror.monitor.NodeProperties) OperatorProperties(com.hedera.mirror.monitor.OperatorProperties) MonitorProperties(com.hedera.mirror.monitor.MonitorProperties) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with NodeProperties

use of com.hedera.mirror.monitor.NodeProperties in project hedera-mirror-node by hashgraph.

the class TransactionPublisherTest method someValidNodes.

@Test
@Timeout(3)
void someValidNodes() {
    NodeProperties node1 = new NodeProperties("0.0.3", "in-process:test");
    NodeProperties node2 = new NodeProperties("0.0.4", "invalid:test");
    monitorProperties.setNodes(Set.of(node1, node2));
    // Initialize publisher internals with first transaction
    cryptoServiceStub.addTransactions(Mono.just(response(OK)));
    PublishRequest publishRequest = request().build();
    publishRequest.getTransaction().setNodeAccountIds(node1.getAccountIds());
    transactionPublisher.publish(publishRequest).as(StepVerifier::create).expectNextCount(1L).expectComplete().verify(Duration.ofSeconds(1L));
    // Validate one of the nodes as down manually
    assertThat(transactionPublisher.validateNode(node2)).isFalse();
    cryptoServiceStub.addTransactions(Mono.just(response(OK)));
    transactionPublisher.publish(request().build()).as(StepVerifier::create).expectNextCount(1L).expectComplete().verify(Duration.ofSeconds(1L));
}
Also used : NodeProperties(com.hedera.mirror.monitor.NodeProperties) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 4 with NodeProperties

use of com.hedera.mirror.monitor.NodeProperties in project hedera-mirror-node by hashgraph.

the class TransactionPublisherTest method validationRecovers.

@Test
@Timeout(3)
void validationRecovers() {
    // Initialize publisher internals with first transaction
    var request = request().build();
    cryptoServiceStub.addTransactions(Mono.just(response(OK)));
    transactionPublisher.publish(request).as(StepVerifier::create).expectNextCount(1L).expectComplete().verify(Duration.ofSeconds(1L));
    var scenario = request.getScenario();
    assertThat(scenario.getCount()).isEqualTo(1);
    assertThat(scenario.getErrors()).isEmpty();
    // Validate node as down manually
    NodeProperties nodeProperties = monitorProperties.getNodes().iterator().next();
    cryptoServiceStub.addQueries(Mono.just(receipt(ACCOUNT_DELETED)));
    cryptoServiceStub.addTransactions(Mono.just(response(OK)));
    assertThat(transactionPublisher.validateNode(nodeProperties)).isFalse();
    request = request().build();
    transactionPublisher.publish(request).as(StepVerifier::create).expectErrorSatisfies(t -> assertThat(t).isInstanceOf(PublishException.class).hasMessageContaining("No valid nodes available").hasCauseInstanceOf(IllegalArgumentException.class)).verify(Duration.ofSeconds(1L));
    scenario = request.getScenario();
    assertThat(scenario.getCount()).isZero();
    assertThat(scenario.getErrors()).containsOnly(Map.entry(IllegalArgumentException.class.getSimpleName(), 1));
    // Node recovers
    cryptoServiceStub.addQueries(Mono.just(receipt(SUCCESS)));
    cryptoServiceStub.addTransactions(Mono.just(response(OK)));
    assertThat(transactionPublisher.validateNode(nodeProperties)).isTrue();
    request = request().build();
    cryptoServiceStub.addTransactions(Mono.just(response(OK)));
    transactionPublisher.publish(request).as(StepVerifier::create).expectNextCount(1L).expectComplete().verify(Duration.ofSeconds(1L));
    scenario = request.getScenario();
    assertThat(scenario.getCount()).isEqualTo(1);
    assertThat(scenario.getErrors()).isEmpty();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CryptoServiceGrpc(com.hedera.hashgraph.sdk.proto.CryptoServiceGrpc) NodeProperties(com.hedera.mirror.monitor.NodeProperties) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) OperatorProperties(com.hedera.mirror.monitor.OperatorProperties) OK(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.OK) TransactionType(com.hedera.mirror.monitor.publish.transaction.TransactionType) TimeoutException(java.util.concurrent.TimeoutException) InProcessServerBuilder(io.grpc.inprocess.InProcessServerBuilder) ResponseCodeEnum(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum) Query(com.hedera.hashgraph.sdk.proto.Query) Transaction(com.hedera.hashgraph.sdk.proto.Transaction) StreamObserver(io.grpc.stub.StreamObserver) ResponseHeader(com.hedera.hashgraph.sdk.proto.ResponseHeader) Response(com.hedera.hashgraph.sdk.proto.Response) Duration(java.time.Duration) Map(java.util.Map) TransactionReceipt(com.hedera.hashgraph.sdk.proto.TransactionReceipt) TransactionRecord(com.hedera.hashgraph.sdk.proto.TransactionRecord) Server(io.grpc.Server) ACCOUNT_DELETED(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.ACCOUNT_DELETED) SUCCESS(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.SUCCESS) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Set(java.util.Set) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) Instant(java.time.Instant) TransactionGetRecordResponse(com.hedera.hashgraph.sdk.proto.TransactionGetRecordResponse) Test(org.junit.jupiter.api.Test) TransactionGetReceiptResponse(com.hedera.hashgraph.sdk.proto.TransactionGetReceiptResponse) AfterEach(org.junit.jupiter.api.AfterEach) TransactionResponse(com.hedera.hashgraph.sdk.proto.TransactionResponse) Data(lombok.Data) Log4j2(lombok.extern.log4j.Log4j2) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) Queue(java.util.Queue) Timeout(org.junit.jupiter.api.Timeout) MonitorProperties(com.hedera.mirror.monitor.MonitorProperties) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) NodeProperties(com.hedera.mirror.monitor.NodeProperties) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

NodeProperties (com.hedera.mirror.monitor.NodeProperties)4 MonitorProperties (com.hedera.mirror.monitor.MonitorProperties)3 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)2 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)2 OperatorProperties (com.hedera.mirror.monitor.OperatorProperties)2 Duration (java.time.Duration)2 Instant (java.time.Instant)2 Map (java.util.Map)2 TimeoutException (java.util.concurrent.TimeoutException)2 Log4j2 (lombok.extern.log4j.Log4j2)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 Timeout (org.junit.jupiter.api.Timeout)2 Uninterruptibles (com.google.common.util.concurrent.Uninterruptibles)1 AccountId (com.hedera.hashgraph.sdk.AccountId)1 Client (com.hedera.hashgraph.sdk.Client)1 Hbar (com.hedera.hashgraph.sdk.Hbar)1 HbarUnit (com.hedera.hashgraph.sdk.HbarUnit)1 Status (com.hedera.hashgraph.sdk.Status)1 SUCCESS (com.hedera.hashgraph.sdk.Status.SUCCESS)1