Search in sources :

Example 6 with TopicMessage

use of com.hedera.mirror.grpc.domain.TopicMessage in project hedera-mirror-node by hashgraph.

the class TopicMessageServiceImpl method incomingMessages.

private Flux<TopicMessage> incomingMessages(TopicContext topicContext) {
    if (topicContext.isComplete()) {
        return Flux.empty();
    }
    TopicMessageFilter filter = topicContext.getFilter();
    TopicMessage last = topicContext.getLast();
    long limit = filter.hasLimit() ? filter.getLimit() - topicContext.getCount().get() : 0;
    Instant startTime = last != null ? last.getConsensusTimestampInstant().plusNanos(1) : filter.getStartTime();
    TopicMessageFilter newFilter = filter.toBuilder().limit(limit).startTime(startTime).build();
    return topicListener.listen(newFilter).takeUntilOther(pastEndTime(topicContext)).concatMap(t -> missingMessages(topicContext, t));
}
Also used : TopicMessage(com.hedera.mirror.grpc.domain.TopicMessage) Instant(java.time.Instant) TopicMessageFilter(com.hedera.mirror.grpc.domain.TopicMessageFilter)

Example 7 with TopicMessage

use of com.hedera.mirror.grpc.domain.TopicMessage in project hedera-mirror-node by hashgraph.

the class TopicMessageRepositoryCustomImpl method findByFilter.

@Override
public Stream<TopicMessage> findByFilter(TopicMessageFilter filter) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<TopicMessage> query = cb.createQuery(TopicMessage.class);
    Root<TopicMessage> root = query.from(TopicMessage.class);
    Predicate predicate = cb.and(cb.equal(root.get("topicId"), filter.getTopicId().getId()), cb.greaterThanOrEqualTo(root.get("consensusTimestamp"), converter.convert(filter.getStartTime())));
    if (filter.getEndTime() != null) {
        predicate = cb.and(predicate, cb.lessThan(root.get("consensusTimestamp"), converter.convert(filter.getEndTime())));
    }
    query = query.select(root).where(predicate).orderBy(cb.asc(root.get("consensusTimestamp")));
    TypedQuery<TopicMessage> typedQuery = entityManager.createQuery(query);
    typedQuery.setHint(QueryHints.HINT_READONLY, true);
    if (filter.hasLimit()) {
        typedQuery.setMaxResults((int) filter.getLimit());
    }
    entityManager.createNativeQuery(TOPIC_MESSAGES_BY_ID_QUERY_HINT).executeUpdate();
    // getResultStream()'s cursor doesn't work with reactive streams
    return typedQuery.getResultList().stream();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) TopicMessage(com.hedera.mirror.grpc.domain.TopicMessage) Predicate(javax.persistence.criteria.Predicate)

Example 8 with TopicMessage

use of com.hedera.mirror.grpc.domain.TopicMessage in project hedera-mirror-node by hashgraph.

the class TopicMessageServiceTest method duplicateMessages.

@Test
void duplicateMessages() {
    TopicListener topicListener = Mockito.mock(TopicListener.class);
    EntityRepository entityRepository = Mockito.mock(EntityRepository.class);
    TopicMessageRetriever topicMessageRetriever = Mockito.mock(TopicMessageRetriever.class);
    topicMessageService = new TopicMessageServiceImpl(new GrpcProperties(), topicListener, entityRepository, topicMessageRetriever, new SimpleMeterRegistry());
    TopicMessageFilter retrieverFilter = TopicMessageFilter.builder().startTime(Instant.EPOCH).topicId(topicId).build();
    Mockito.when(entityRepository.findById(retrieverFilter.getTopicId().getId())).thenReturn(Optional.of(Entity.builder().type(EntityType.TOPIC).build()));
    Mockito.when(topicMessageRetriever.retrieve(ArgumentMatchers.isA(TopicMessageFilter.class), ArgumentMatchers.eq(true))).thenReturn(Flux.just(topicMessage(1, Instant.EPOCH), topicMessage(1, Instant.EPOCH.plus(1, ChronoUnit.NANOS)), topicMessage(2, Instant.EPOCH.plus(2, ChronoUnit.NANOS)), topicMessage(1, Instant.EPOCH.plus(3, ChronoUnit.NANOS))));
    Mockito.when(topicListener.listen(ArgumentMatchers.any())).thenReturn(Flux.empty());
    topicMessageService.subscribeTopic(retrieverFilter).map(TopicMessage::getSequenceNumber).as(StepVerifier::create).expectNext(1L, 2L).expectComplete().verify(Duration.ofMillis(700));
}
Also used : TopicMessageRetriever(com.hedera.mirror.grpc.retriever.TopicMessageRetriever) TopicListener(com.hedera.mirror.grpc.listener.TopicListener) GrpcProperties(com.hedera.mirror.grpc.GrpcProperties) TopicMessage(com.hedera.mirror.grpc.domain.TopicMessage) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) EntityRepository(com.hedera.mirror.grpc.repository.EntityRepository) TopicMessageFilter(com.hedera.mirror.grpc.domain.TopicMessageFilter) GrpcIntegrationTest(com.hedera.mirror.grpc.GrpcIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 9 with TopicMessage

use of com.hedera.mirror.grpc.domain.TopicMessage in project hedera-mirror-node by hashgraph.

the class TopicMessageServiceTest method noMessagesWithFutureEndTime.

@Test
void noMessagesWithFutureEndTime() {
    Instant endTime = now.plusMillis(250);
    TopicMessageFilter filter = TopicMessageFilter.builder().startTime(now).endTime(endTime).topicId(topicId).build();
    topicMessageService.subscribeTopic(filter).map(TopicMessage::getSequenceNumber).as(StepVerifier::create).expectNextCount(0L).expectComplete().verify(Duration.ofMillis(1000));
}
Also used : TopicMessage(com.hedera.mirror.grpc.domain.TopicMessage) Instant(java.time.Instant) TopicMessageFilter(com.hedera.mirror.grpc.domain.TopicMessageFilter) GrpcIntegrationTest(com.hedera.mirror.grpc.GrpcIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 10 with TopicMessage

use of com.hedera.mirror.grpc.domain.TopicMessage in project hedera-mirror-node by hashgraph.

the class TopicMessageServiceTest method missingMessagesFromListenerSomeRetrieved.

@Test
void missingMessagesFromListenerSomeRetrieved() {
    TopicMessageFilter filter = TopicMessageFilter.builder().startTime(Instant.EPOCH).topicId(topicId).build();
    missingMessagesFromListenerTest(filter, Flux.just(topicMessage(5), topicMessage(6)));
    topicMessageService.subscribeTopic(filter).map(TopicMessage::getSequenceNumber).as(StepVerifier::create).expectNext(1L, 2L, 3L, 4L, 5L, 6L).expectError(IllegalStateException.class).verify(Duration.ofMillis(700));
}
Also used : TopicMessage(com.hedera.mirror.grpc.domain.TopicMessage) TopicMessageFilter(com.hedera.mirror.grpc.domain.TopicMessageFilter) GrpcIntegrationTest(com.hedera.mirror.grpc.GrpcIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

TopicMessage (com.hedera.mirror.grpc.domain.TopicMessage)58 TopicMessageFilter (com.hedera.mirror.grpc.domain.TopicMessageFilter)50 Test (org.junit.jupiter.api.Test)45 GrpcIntegrationTest (com.hedera.mirror.grpc.GrpcIntegrationTest)43 Instant (java.time.Instant)17 StepVerifier (reactor.test.StepVerifier)15 Duration (java.time.Duration)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 DomainBuilder (com.hedera.mirror.grpc.domain.DomainBuilder)10 Resource (javax.annotation.Resource)10 ValueSource (org.junit.jupiter.params.provider.ValueSource)10 Autowired (org.springframework.beans.factory.annotation.Autowired)10 Flux (reactor.core.publisher.Flux)10 EntityId (com.hedera.mirror.common.domain.entity.EntityId)9 EntityType (com.hedera.mirror.common.domain.entity.EntityType)9 AfterEach (org.junit.jupiter.api.AfterEach)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 ConsensusTopicQuery (com.hedera.mirror.api.proto.ConsensusTopicQuery)6 GrpcProperties (com.hedera.mirror.grpc.GrpcProperties)5 ListenerProperties (com.hedera.mirror.grpc.listener.ListenerProperties)5