Search in sources :

Example 1 with TimelineService

use of org.zalando.nakadi.service.timeline.TimelineService in project nakadi by zalando.

the class EventStreamTest method setupMocks.

@BeforeClass
public static void setupMocks() {
    final TimelineService timelineService = mock(TimelineService.class);
    final EventTypeCache eventTypeCache = mock(EventTypeCache.class);
    cursorConverter = new CursorConverterImpl(eventTypeCache, timelineService);
    writerProvider = mock(EventStreamWriterProvider.class);
}
Also used : TimelineService(org.zalando.nakadi.service.timeline.TimelineService) CursorConverterImpl(org.zalando.nakadi.service.converter.CursorConverterImpl) EventTypeCache(org.zalando.nakadi.repository.db.EventTypeCache) BeforeClass(org.junit.BeforeClass)

Example 2 with TimelineService

use of org.zalando.nakadi.service.timeline.TimelineService in project nakadi by zalando.

the class StreamingStateTest method prepareMocks.

@Before
public void prepareMocks() throws Exception {
    state = new StreamingState();
    final StreamingContext contextMock = mock(StreamingContext.class);
    when(contextMock.getCursorComparator()).thenReturn(Comparator.comparing(NakadiCursor::getOffset));
    when(contextMock.getSessionId()).thenReturn(SESSION_ID);
    when(contextMock.isInState(Mockito.same(state))).thenReturn(true);
    subscription = mock(Subscription.class);
    when(contextMock.getSubscription()).thenReturn(subscription);
    timelineService = mock(TimelineService.class);
    when(contextMock.getTimelineService()).thenReturn(timelineService);
    final MetricRegistry metricRegistry = mock(MetricRegistry.class);
    when(metricRegistry.register(any(), any())).thenReturn(null);
    when(contextMock.getMetricRegistry()).thenReturn(metricRegistry);
    zkMock = mock(ZkSubscriptionClient.class);
    when(contextMock.getZkClient()).thenReturn(zkMock);
    cursorConverter = mock(CursorConverter.class);
    when(contextMock.getCursorConverter()).thenReturn(cursorConverter);
    final Client client = mock(Client.class);
    when(client.getClientId()).thenReturn("consumingAppId");
    final StreamParameters spMock = createStreamParameters(1000, 100L, 100, 100L, 100, 100, 100, client);
    when(contextMock.getParameters()).thenReturn(spMock);
    state.setContext(contextMock, "test");
}
Also used : StreamingContext(org.zalando.nakadi.service.subscription.StreamingContext) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) MetricRegistry(com.codahale.metrics.MetricRegistry) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) Subscription(org.zalando.nakadi.domain.Subscription) ZkSubscription(org.zalando.nakadi.service.subscription.zk.ZkSubscription) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) Client(org.zalando.nakadi.security.Client) CursorConverter(org.zalando.nakadi.service.CursorConverter) StreamParametersTest.createStreamParameters(org.zalando.nakadi.service.subscription.StreamParametersTest.createStreamParameters) StreamParameters(org.zalando.nakadi.service.subscription.StreamParameters) Before(org.junit.Before)

Example 3 with TimelineService

use of org.zalando.nakadi.service.timeline.TimelineService in project nakadi by zalando.

the class SubscriptionValidationServiceTest method setUp.

@Before
public void setUp() throws InternalNakadiException {
    final NakadiSettings nakadiSettings = mock(NakadiSettings.class);
    when(nakadiSettings.getMaxSubscriptionPartitions()).thenReturn(MAX_SUBSCRIPTION_PARTITIONS);
    topicRepository = mock(TopicRepository.class);
    when(topicRepository.listPartitionNames(argThat(isOneOf(topicForET(ET1), topicForET(ET2), topicForET(ET3))))).thenReturn(ImmutableList.of(P0));
    etRepo = mock(EventTypeRepository.class);
    final Map<String, EventType> eventTypes = new HashMap<>();
    for (final String etName : new String[] { ET1, ET2, ET3 }) {
        final EventType eventType = new EventType();
        eventType.setName(etName);
        eventTypes.put(etName, eventType);
    }
    when(etRepo.findByNameO(any())).thenAnswer(invocation -> Optional.ofNullable(eventTypes.get(invocation.getArguments()[0])));
    final TimelineService timelineService = mock(TimelineService.class);
    for (final EventType et : eventTypes.values()) {
        final Timeline timeline = mock(Timeline.class);
        when(timeline.getTopic()).thenReturn(topicForET(et.getName()));
        when(timeline.getEventType()).thenReturn(et.getName());
        when(timelineService.getActiveTimeline(eq(et.getName()))).thenReturn(timeline);
    }
    when(timelineService.getTopicRepository((Timeline) any())).thenReturn(topicRepository);
    when(timelineService.getTopicRepository((EventType) any())).thenReturn(topicRepository);
    cursorConverter = mock(CursorConverter.class);
    subscriptionValidationService = new SubscriptionValidationService(timelineService, etRepo, nakadiSettings, cursorConverter);
    subscriptionBase = new SubscriptionBase();
    subscriptionBase.setEventTypes(ImmutableSet.of(ET1, ET2, ET3));
    subscriptionBase.setReadFrom(SubscriptionBase.InitialPosition.CURSORS);
}
Also used : SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) Timeline(org.zalando.nakadi.domain.Timeline) EventType(org.zalando.nakadi.domain.EventType) HashMap(java.util.HashMap) EventTypeRepository(org.zalando.nakadi.repository.EventTypeRepository) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) SubscriptionValidationService(org.zalando.nakadi.service.subscription.SubscriptionValidationService) TopicRepository(org.zalando.nakadi.repository.TopicRepository) NakadiSettings(org.zalando.nakadi.config.NakadiSettings) Before(org.junit.Before)

Example 4 with TimelineService

use of org.zalando.nakadi.service.timeline.TimelineService in project nakadi by zalando.

the class CursorConverterImplTest method testBeginConvertedVersionZero.

@Test
public void testBeginConvertedVersionZero() throws Exception {
    final String eventType = "test-et";
    final String partition = "2";
    final Storage storage = new Storage("", Storage.Type.KAFKA);
    final Timeline timeline = mock(Timeline.class);
    when(timeline.getStorage()).thenReturn(storage);
    final EventTypeCache eventTypeCache = mock(EventTypeCache.class);
    final TopicRepository topicRepository = mock(TopicRepository.class);
    final TimelineService timelineService = mock(TimelineService.class);
    final PartitionStatistics stats = mock(PartitionStatistics.class);
    when(timelineService.getActiveTimelinesOrdered(eq(eventType))).thenReturn(Collections.singletonList(timeline));
    when(timelineService.getTopicRepository(eq(timeline))).thenReturn(topicRepository);
    when(topicRepository.loadPartitionStatistics(eq(timeline), eq(partition))).thenReturn(Optional.of(stats));
    final NakadiCursor beforeFirstCursor = NakadiCursor.of(timeline, partition, "000001");
    when(stats.getBeforeFirst()).thenReturn(beforeFirstCursor);
    final CursorConverter converter = new CursorConverterImpl(eventTypeCache, timelineService);
    final NakadiCursor nakadiCursor = converter.convert(eventType, new Cursor(partition, "BEGIN"));
    Assert.assertEquals(timeline, nakadiCursor.getTimeline());
    Assert.assertEquals(partition, nakadiCursor.getPartition());
    Assert.assertEquals("000001", nakadiCursor.getOffset());
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) Storage(org.zalando.nakadi.domain.Storage) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) TopicRepository(org.zalando.nakadi.repository.TopicRepository) EventTypeCache(org.zalando.nakadi.repository.db.EventTypeCache) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) Cursor(org.zalando.nakadi.view.Cursor) CursorConverter(org.zalando.nakadi.service.CursorConverter) Test(org.junit.Test)

Example 5 with TimelineService

use of org.zalando.nakadi.service.timeline.TimelineService in project nakadi by zalando.

the class CursorsService method resetCursors.

public void resetCursors(final String subscriptionId, final List<NakadiCursor> cursors) throws ServiceUnavailableException, NoSuchSubscriptionException, UnableProcessException, OperationTimeoutException, ZookeeperException, InternalNakadiException, NoSuchEventTypeException, InvalidCursorException {
    final Subscription subscription = subscriptionRepository.getSubscription(subscriptionId);
    validateCursorsBelongToSubscription(subscription, cursors);
    for (final NakadiCursor cursor : cursors) {
        cursor.checkStorageAvailability();
    }
    final Map<TopicRepository, List<NakadiCursor>> topicRepositories = cursors.stream().collect(Collectors.groupingBy(c -> timelineService.getTopicRepository(c.getTimeline())));
    for (final Map.Entry<TopicRepository, List<NakadiCursor>> entry : topicRepositories.entrySet()) {
        entry.getKey().validateReadCursors(entry.getValue());
    }
    final ZkSubscriptionClient zkClient = zkSubscriptionFactory.createClient(subscription, "subscription." + subscriptionId + ".reset_cursors");
    // In case if subscription was never initialized - initialize it
    zkClient.runLocked(() -> StartingState.initializeSubscriptionLocked(zkClient, subscription, timelineService, cursorConverter));
    // add 1 second to commit timeout in order to give time to finish reset if there is uncommitted events
    if (!cursors.isEmpty()) {
        final long timeout = TimeUnit.SECONDS.toMillis(nakadiSettings.getDefaultCommitTimeoutSeconds()) + TimeUnit.SECONDS.toMillis(1);
        zkClient.resetCursors(cursors.stream().map(cursorConverter::convertToNoToken).collect(Collectors.toList()), timeout);
    }
}
Also used : StartingState(org.zalando.nakadi.service.subscription.state.StartingState) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) NakadiSettings(org.zalando.nakadi.config.NakadiSettings) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) Subscription(org.zalando.nakadi.domain.Subscription) NakadiException(org.zalando.nakadi.exceptions.NakadiException) SubscriptionNotInitializedException(org.zalando.nakadi.service.subscription.zk.SubscriptionNotInitializedException) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) Partition(org.zalando.nakadi.service.subscription.model.Partition) UUIDGenerator(org.zalando.nakadi.util.UUIDGenerator) ImmutableList(com.google.common.collect.ImmutableList) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Map(java.util.Map) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) TopicRepository(org.zalando.nakadi.repository.TopicRepository) SubscriptionClientFactory(org.zalando.nakadi.service.subscription.zk.SubscriptionClientFactory) EventTypeCache(org.zalando.nakadi.repository.db.EventTypeCache) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) OperationTimeoutException(org.zalando.nakadi.exceptions.runtime.OperationTimeoutException) ZookeeperException(org.zalando.nakadi.exceptions.runtime.ZookeeperException) Collectors(java.util.stream.Collectors) TimeLogger(org.zalando.nakadi.util.TimeLogger) TimeUnit(java.util.concurrent.TimeUnit) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) Component(org.springframework.stereotype.Component) List(java.util.List) Stream(java.util.stream.Stream) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException) SubscriptionDbRepository(org.zalando.nakadi.repository.db.SubscriptionDbRepository) InvalidStreamIdException(org.zalando.nakadi.exceptions.InvalidStreamIdException) NoSuchSubscriptionException(org.zalando.nakadi.exceptions.NoSuchSubscriptionException) CursorError(org.zalando.nakadi.domain.CursorError) Comparator(java.util.Comparator) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) TopicRepository(org.zalando.nakadi.repository.TopicRepository) Subscription(org.zalando.nakadi.domain.Subscription) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

TimelineService (org.zalando.nakadi.service.timeline.TimelineService)9 TopicRepository (org.zalando.nakadi.repository.TopicRepository)7 Before (org.junit.Before)6 EventTypeCache (org.zalando.nakadi.repository.db.EventTypeCache)5 CursorConverter (org.zalando.nakadi.service.CursorConverter)4 Subscription (org.zalando.nakadi.domain.Subscription)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 HashMap (java.util.HashMap)2 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)2 NakadiSettings (org.zalando.nakadi.config.NakadiSettings)2 SecuritySettings (org.zalando.nakadi.config.SecuritySettings)2 EventType (org.zalando.nakadi.domain.EventType)2 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)2 Timeline (org.zalando.nakadi.domain.Timeline)2 EventTypeRepository (org.zalando.nakadi.repository.EventTypeRepository)2 SubscriptionDbRepository (org.zalando.nakadi.repository.db.SubscriptionDbRepository)2 ClientResolver (org.zalando.nakadi.security.ClientResolver)2 FeatureToggleService (org.zalando.nakadi.service.FeatureToggleService)2 CursorConverterImpl (org.zalando.nakadi.service.converter.CursorConverterImpl)2 SubscriptionClientFactory (org.zalando.nakadi.service.subscription.zk.SubscriptionClientFactory)2