Search in sources :

Example 1 with ShiftedNakadiCursor

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

the class CursorOperationsServiceTest method shiftCursorRightToNextTimeline.

@Test
public void shiftCursorRightToNextTimeline() throws Exception {
    final Timeline initialTimeline = mockTimeline(1, 10L);
    final Timeline nextTimeline = mockTimeline(2, 3L);
    final ShiftedNakadiCursor shiftedCursor = new ShiftedNakadiCursor(initialTimeline, "0", "000000000000000003", 9L);
    mockTimelines(initialTimeline, nextTimeline);
    final NakadiCursor cursor = service.unshiftCursor(shiftedCursor);
    assertThat(cursor.getTimeline().getOrder(), equalTo(2));
    assertThat(cursor.getOffset(), equalTo("000000000000000001"));
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) Test(org.junit.Test)

Example 2 with ShiftedNakadiCursor

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

the class CursorOperationsServiceTest method shiftCursorToExpiredTimeline.

@Test
public void shiftCursorToExpiredTimeline() throws Exception {
    final Timeline initialTimeline = mockTimeline(1, 5L);
    final Timeline finalTimeline = mockTimeline(1);
    when(timelineService.getActiveTimelinesOrdered(any())).thenReturn(Lists.newArrayList(initialTimeline, finalTimeline));
    final ShiftedNakadiCursor shiftedCursor = new ShiftedNakadiCursor(finalTimeline, "0", "000000000000000003", -15L);
    try {
        service.unshiftCursor(shiftedCursor);
        fail();
    } catch (final InvalidCursorOperation e) {
        assertThat(e.getReason(), equalTo(TIMELINE_NOT_FOUND));
    } catch (final Exception e) {
        fail();
    }
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) InvalidCursorOperation(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) Test(org.junit.Test)

Example 3 with ShiftedNakadiCursor

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

the class CursorOperationsServiceTest method shiftCursorBackInTheSameTimelineClosed.

@Test
public void shiftCursorBackInTheSameTimelineClosed() {
    final Timeline initialTimeline = mockTimeline(0, 10L);
    final ShiftedNakadiCursor shiftedCursor = new ShiftedNakadiCursor(initialTimeline, "0", "000000000000000003", -3L);
    final NakadiCursor cursor = service.unshiftCursor(shiftedCursor);
    assertThat(cursor.getOffset(), equalTo("000000000000000000"));
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) Test(org.junit.Test)

Example 4 with ShiftedNakadiCursor

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

the class CursorOperationsServiceTest method testShiftWithEmptyTimelines.

@Test
public void testShiftWithEmptyTimelines() throws Exception {
    final Timeline first = mockTimeline(1, 9L);
    final Timeline last = mockOpenTimeline(5);
    mockTimelines(first, mockTimeline(2, -1L), mockTimeline(3, -1L), mockTimeline(4, -1L), last);
    final ShiftedNakadiCursor moveForward = new ShiftedNakadiCursor(first, "0", "000000000000000001", 19);
    assertEquals(service.unshiftCursor(moveForward), NakadiCursor.of(last, "0", "000000000000000010"));
    final ShiftedNakadiCursor moveBackward = new ShiftedNakadiCursor(last, "0", "000000000000000010", -19);
    assertEquals(service.unshiftCursor(moveBackward), NakadiCursor.of(first, "0", "000000000000000001"));
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) Test(org.junit.Test)

Example 5 with ShiftedNakadiCursor

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

the class CursorOperationsService method moveForward.

private NakadiCursor moveForward(final ShiftedNakadiCursor cursor) {
    NakadiCursor currentCursor = cursor.getNakadiCursor();
    long stillToAdd = cursor.getShift();
    while (currentCursor.getTimeline().getLatestPosition() != null) {
        final NakadiCursor timelineLastPosition = currentCursor.getTimeline().getLatestPosition().toNakadiCursor(currentCursor.getTimeline(), currentCursor.getPartition());
        final long distance = calculateDistance(currentCursor, timelineLastPosition);
        if (stillToAdd > distance) {
            stillToAdd -= distance;
            final Timeline nextTimeline = getTimeline(currentCursor.getEventType(), currentCursor.getTimeline().getOrder() + 1);
            currentCursor = NakadiCursor.of(nextTimeline, currentCursor.getPartition(), StaticStorageWorkerFactory.get(nextTimeline).getBeforeFirstOffset());
        } else {
            break;
        }
    }
    if (stillToAdd > 0) {
        return currentCursor.shiftWithinTimeline(stillToAdd);
    }
    return currentCursor;
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor)

Aggregations

ShiftedNakadiCursor (org.zalando.nakadi.domain.ShiftedNakadiCursor)12 Timeline (org.zalando.nakadi.domain.Timeline)11 Test (org.junit.Test)9 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 EventType (org.zalando.nakadi.domain.EventType)1 InvalidCursorOperation (org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation)1 Cursor (org.zalando.nakadi.view.Cursor)1 ShiftedCursor (org.zalando.nakadi.view.ShiftedCursor)1