Search in sources :

Example 1 with Storage

use of org.zalando.nakadi.domain.Storage in project nakadi by zalando.

the class StreamingStateTest method ensureOffsetsSubscriptionsAreRefreshedAndClosed.

@Test
public void ensureOffsetsSubscriptionsAreRefreshedAndClosed() throws InternalNakadiException, NoSuchEventTypeException, ServiceUnavailableException, InvalidCursorException {
    final ZkSubscription<SubscriptionCursorWithoutToken> offsetSubscription = mock(ZkSubscription.class);
    final EventTypePartition pk = new EventTypePartition("t", "0");
    Mockito.when(zkMock.subscribeForOffsetChanges(Mockito.eq(pk), Mockito.any())).thenReturn(offsetSubscription);
    final EventConsumer.ReassignableEventConsumer consumer = mock(EventConsumer.ReassignableEventConsumer.class);
    when(consumer.getAssignment()).thenReturn(Collections.emptySet());
    when(timelineService.createEventConsumer(any())).thenReturn(consumer);
    when(subscription.getEventTypes()).thenReturn(Collections.singleton("t"));
    final Storage storage = mock(Storage.class);
    when(storage.getType()).thenReturn(Storage.Type.KAFKA);
    final Timeline timeline = new Timeline("t", 0, storage, "t", new Date());
    when(timelineService.getActiveTimelinesOrdered(eq("t"))).thenReturn(Collections.singletonList(timeline));
    final TopicRepository topicRepository = mock(TopicRepository.class);
    when(timelineService.getTopicRepository(eq(timeline))).thenReturn(topicRepository);
    final PartitionStatistics stats = mock(PartitionStatistics.class);
    final NakadiCursor beforeFirstCursor = NakadiCursor.of(timeline, "0", "0");
    when(stats.getBeforeFirst()).thenReturn(beforeFirstCursor);
    when(topicRepository.loadTopicStatistics(any())).thenReturn(Lists.newArrayList(stats));
    state.onEnter();
    final NakadiCursor anyCursor = NakadiCursor.of(timeline, "0", "0");
    when(cursorConverter.convert(any(SubscriptionCursorWithoutToken.class))).thenReturn(anyCursor);
    state.refreshTopologyUnlocked(new Partition[] { new Partition(pk.getEventType(), pk.getPartition(), SESSION_ID, null, Partition.State.ASSIGNED) });
    Mockito.verify(zkMock, Mockito.times(1)).subscribeForOffsetChanges(Mockito.eq(pk), Mockito.any());
    Mockito.verify(offsetSubscription, Mockito.times(0)).close();
    Mockito.verify(offsetSubscription, Mockito.times(0)).getData();
    state.offsetChanged(pk);
    Mockito.verify(zkMock, Mockito.times(1)).subscribeForOffsetChanges(Mockito.eq(pk), Mockito.any());
    Mockito.verify(offsetSubscription, Mockito.times(0)).close();
    Mockito.verify(offsetSubscription, Mockito.times(1)).getData();
    // Verify that offset change listener is removed
    state.refreshTopologyUnlocked(new Partition[0]);
    Mockito.verify(zkMock, Mockito.times(1)).subscribeForOffsetChanges(Mockito.eq(pk), Mockito.any());
    Mockito.verify(offsetSubscription, Mockito.times(1)).close();
    Mockito.verify(offsetSubscription, Mockito.times(1)).getData();
}
Also used : SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) EventConsumer(org.zalando.nakadi.repository.EventConsumer) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Partition(org.zalando.nakadi.service.subscription.model.Partition) Timeline(org.zalando.nakadi.domain.Timeline) Storage(org.zalando.nakadi.domain.Storage) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) TopicRepository(org.zalando.nakadi.repository.TopicRepository) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Date(java.util.Date) Test(org.junit.Test)

Example 2 with Storage

use of org.zalando.nakadi.domain.Storage in project nakadi by zalando.

the class StartingStateTest method testGetSubscriptionOffsetsBegin.

@Test
public void testGetSubscriptionOffsetsBegin() throws Exception {
    when(subscription.getReadFrom()).thenReturn(SubscriptionBase.InitialPosition.BEGIN);
    final NakadiCursor beforeBegin0 = mock(NakadiCursor.class);
    final SubscriptionCursorWithoutToken beforeBegin0Converted = mock(SubscriptionCursorWithoutToken.class);
    when(cursorConverter.convertToNoToken(eq(beforeBegin0))).thenReturn(beforeBegin0Converted);
    final NakadiCursor beforeBegin1 = mock(NakadiCursor.class);
    final SubscriptionCursorWithoutToken beforeBegin1Converted = mock(SubscriptionCursorWithoutToken.class);
    when(cursorConverter.convertToNoToken(eq(beforeBegin1))).thenReturn(beforeBegin1Converted);
    final TopicRepository topicRepository = mock(TopicRepository.class);
    final PartitionStatistics resultForTopic0 = mock(PartitionStatistics.class);
    when(resultForTopic0.getBeforeFirst()).thenReturn(beforeBegin0);
    final PartitionStatistics resultForTopic1 = mock(PartitionStatistics.class);
    when(resultForTopic1.getBeforeFirst()).thenReturn(beforeBegin1);
    when(topicRepository.loadTopicStatistics(any())).thenReturn(Lists.newArrayList(resultForTopic0, resultForTopic1));
    when(timelineService.getTopicRepository(eq(timelineEt00))).thenReturn(topicRepository);
    final Storage storage = mock(Storage.class);
    when(timelineEt00.getStorage()).thenReturn(storage);
    when(timelineEt10.getStorage()).thenReturn(storage);
    final List<SubscriptionCursorWithoutToken> cursors = StartingState.calculateStartPosition(subscription, timelineService, cursorConverter);
    Assert.assertEquals(cursors.size(), 2);
    Assert.assertEquals(beforeBegin0Converted, cursors.get(0));
    Assert.assertEquals(beforeBegin1Converted, cursors.get(1));
}
Also used : SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) Storage(org.zalando.nakadi.domain.Storage) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) TopicRepository(org.zalando.nakadi.repository.TopicRepository) Test(org.junit.Test)

Example 3 with Storage

use of org.zalando.nakadi.domain.Storage in project nakadi by zalando.

the class StartingStateTest method testGetSubscriptionOffsetsEnd.

@Test
public void testGetSubscriptionOffsetsEnd() throws Exception {
    when(subscription.getReadFrom()).thenReturn(SubscriptionBase.InitialPosition.END);
    final NakadiCursor end0 = mock(NakadiCursor.class);
    final SubscriptionCursorWithoutToken end0Converted = mock(SubscriptionCursorWithoutToken.class);
    when(cursorConverter.convertToNoToken(eq(end0))).thenReturn(end0Converted);
    final NakadiCursor end1 = mock(NakadiCursor.class);
    final SubscriptionCursorWithoutToken end1Converted = mock(SubscriptionCursorWithoutToken.class);
    when(cursorConverter.convertToNoToken(eq(end1))).thenReturn(end1Converted);
    final TopicRepository topicRepository = mock(TopicRepository.class);
    final PartitionStatistics statsForEt0 = mock(PartitionStatistics.class);
    when(statsForEt0.getLast()).thenReturn(end0);
    final PartitionStatistics statsForTopic1 = mock(PartitionStatistics.class);
    when(statsForTopic1.getLast()).thenReturn(end1);
    when(topicRepository.loadTopicEndStatistics(any())).thenReturn(Lists.newArrayList(statsForEt0, statsForTopic1));
    when(timelineService.getTopicRepository(eq(timelineEt01))).thenReturn(topicRepository);
    final Storage storage = mock(Storage.class);
    when(timelineEt01.getStorage()).thenReturn(storage);
    when(timelineEt11.getStorage()).thenReturn(storage);
    final List<SubscriptionCursorWithoutToken> cursors = StartingState.calculateStartPosition(subscription, timelineService, cursorConverter);
    Assert.assertEquals(cursors.size(), 2);
    Assert.assertEquals(end0Converted, cursors.get(0));
    Assert.assertEquals(end1Converted, cursors.get(1));
}
Also used : SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) Storage(org.zalando.nakadi.domain.Storage) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) TopicRepository(org.zalando.nakadi.repository.TopicRepository) Test(org.junit.Test)

Example 4 with Storage

use of org.zalando.nakadi.domain.Storage in project nakadi by zalando.

the class VersionOneConverterTest method testCorrectParse.

@Test
public void testCorrectParse() throws Exception {
    final Cursor cursor = new Cursor("1", "001-0010-012345");
    final String eventTypeName = "my_et";
    final Timeline firstTimeline = mock(Timeline.class);
    when(firstTimeline.getStorage()).thenReturn(new Storage("default", Storage.Type.KAFKA));
    when(firstTimeline.getOrder()).thenReturn(16);
    when(eventTypeCache.getTimelinesOrdered(eq(eventTypeName))).thenReturn(Collections.singletonList(firstTimeline));
    final NakadiCursor nakadiCursor = converter.convert(eventTypeName, cursor);
    Assert.assertEquals(firstTimeline, nakadiCursor.getTimeline());
    Assert.assertEquals("1", nakadiCursor.getPartition());
    Assert.assertEquals("012345", nakadiCursor.getOffset());
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) Storage(org.zalando.nakadi.domain.Storage) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) Cursor(org.zalando.nakadi.view.Cursor) Test(org.junit.Test)

Example 5 with Storage

use of org.zalando.nakadi.domain.Storage in project nakadi by zalando.

the class VersionOneConverterTest method testFormatOffset.

@Test
public void testFormatOffset() {
    final Timeline timeline = mock(Timeline.class);
    when(timeline.getOrder()).thenReturn(15);
    when(timeline.getStorage()).thenReturn(new Storage("", Storage.Type.KAFKA));
    final NakadiCursor cursor = NakadiCursor.of(timeline, "x", "012345");
    Assert.assertEquals("001-000f-012345", new VersionOneConverter(null).formatOffset(cursor));
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) Storage(org.zalando.nakadi.domain.Storage) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) Test(org.junit.Test)

Aggregations

Storage (org.zalando.nakadi.domain.Storage)31 Test (org.junit.Test)16 Timeline (org.zalando.nakadi.domain.Timeline)14 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)10 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)7 TopicRepository (org.zalando.nakadi.repository.TopicRepository)7 Date (java.util.Date)6 DefaultStorage (org.zalando.nakadi.domain.DefaultStorage)6 List (java.util.List)4 EventType (org.zalando.nakadi.domain.EventType)4 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)4 IOException (java.io.IOException)3 Collectors (java.util.stream.Collectors)3 DuplicatedStorageException (org.zalando.nakadi.exceptions.runtime.DuplicatedStorageException)3 TimelineService (org.zalando.nakadi.service.timeline.TimelineService)3 Cursor (org.zalando.nakadi.view.Cursor)3 SubscriptionCursorWithoutToken (org.zalando.nakadi.view.SubscriptionCursorWithoutToken)3 Collections (java.util.Collections)2 Optional (java.util.Optional)2 Logger (org.slf4j.Logger)2