Search in sources :

Example 1 with KeyValueState

use of org.apache.storm.state.KeyValueState in project storm by apache.

the class CheckpointSpoutTest method testRecoveryRollback.

@Test
public void testRecoveryRollback() throws Exception {
    Map<String, Object> stormConf = new HashMap();
    KeyValueState<String, CheckPointState> state = (KeyValueState<String, CheckPointState>) StateFactory.getState("test-1", stormConf, mockTopologyContext);
    CheckPointState checkPointState = new CheckPointState(100, CheckPointState.State.PREPARING);
    state.put("__state", checkPointState);
    spout.open(mockTopologyContext, mockOutputCollector, 0, state);
    ArgumentCaptor<String> stream = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Values> values = ArgumentCaptor.forClass(Values.class);
    ArgumentCaptor<Object> msgId = ArgumentCaptor.forClass(Object.class);
    spout.nextTuple();
    Mockito.verify(mockOutputCollector, Mockito.times(1)).emit(stream.capture(), values.capture(), msgId.capture());
    Values expectedTuple = new Values(100L, Action.ROLLBACK);
    assertEquals(CheckpointSpout.CHECKPOINT_STREAM_ID, stream.getValue());
    assertEquals(expectedTuple, values.getValue());
    assertEquals(100L, msgId.getValue());
}
Also used : HashMap(java.util.HashMap) Values(org.apache.storm.tuple.Values) KeyValueState(org.apache.storm.state.KeyValueState) Test(org.junit.Test)

Example 2 with KeyValueState

use of org.apache.storm.state.KeyValueState in project storm by apache.

the class CheckpointSpoutTest method testRecoveryRollbackAck.

@Test
public void testRecoveryRollbackAck() throws Exception {
    Map<String, Object> stormConf = new HashMap();
    KeyValueState<String, CheckPointState> state = (KeyValueState<String, CheckPointState>) StateFactory.getState("test-1", stormConf, mockTopologyContext);
    CheckPointState checkPointState = new CheckPointState(100, CheckPointState.State.PREPARING);
    state.put("__state", checkPointState);
    spout.open(mockTopologyContext, mockOutputCollector, 0, state);
    ArgumentCaptor<String> stream = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Values> values = ArgumentCaptor.forClass(Values.class);
    ArgumentCaptor<Object> msgId = ArgumentCaptor.forClass(Object.class);
    spout.nextTuple();
    spout.ack(100L);
    spout.nextTuple();
    spout.ack(99L);
    spout.nextTuple();
    Mockito.verify(mockOutputCollector, Mockito.times(3)).emit(stream.capture(), values.capture(), msgId.capture());
    Values expectedTuple = new Values(100L, Action.PREPARE);
    assertEquals(CheckpointSpout.CHECKPOINT_STREAM_ID, stream.getValue());
    assertEquals(expectedTuple, values.getValue());
    assertEquals(100L, msgId.getValue());
}
Also used : HashMap(java.util.HashMap) Values(org.apache.storm.tuple.Values) KeyValueState(org.apache.storm.state.KeyValueState) Test(org.junit.Test)

Example 3 with KeyValueState

use of org.apache.storm.state.KeyValueState in project storm by apache.

the class CheckpointSpoutTest method testRecoveryCommit.

@Test
public void testRecoveryCommit() throws Exception {
    Map<String, Object> stormConf = new HashMap();
    KeyValueState<String, CheckPointState> state = (KeyValueState<String, CheckPointState>) StateFactory.getState("test-1", stormConf, mockTopologyContext);
    CheckPointState checkPointState = new CheckPointState(100, CheckPointState.State.COMMITTING);
    state.put("__state", checkPointState);
    spout.open(mockTopologyContext, mockOutputCollector, 0, state);
    ArgumentCaptor<String> stream = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Values> values = ArgumentCaptor.forClass(Values.class);
    ArgumentCaptor<Object> msgId = ArgumentCaptor.forClass(Object.class);
    spout.nextTuple();
    Mockito.verify(mockOutputCollector, Mockito.times(1)).emit(stream.capture(), values.capture(), msgId.capture());
    Values expectedTuple = new Values(100L, Action.COMMIT);
    assertEquals(CheckpointSpout.CHECKPOINT_STREAM_ID, stream.getValue());
    assertEquals(expectedTuple, values.getValue());
    assertEquals(100L, msgId.getValue());
}
Also used : HashMap(java.util.HashMap) Values(org.apache.storm.tuple.Values) KeyValueState(org.apache.storm.state.KeyValueState) Test(org.junit.Test)

Example 4 with KeyValueState

use of org.apache.storm.state.KeyValueState in project storm by apache.

the class CheckpointSpoutTest method testPrepareWithFail.

@Test
public void testPrepareWithFail() throws Exception {
    Map<String, Object> stormConf = new HashMap<>();
    KeyValueState<String, CheckPointState> state = (KeyValueState<String, CheckPointState>) StateFactory.getState("__state", stormConf, mockTopologyContext);
    CheckPointState txState = new CheckPointState(-1, COMMITTED);
    state.put("__state", txState);
    spout.open(mockTopologyContext, mockOutputCollector, 0, state);
    ArgumentCaptor<String> stream = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Values> values = ArgumentCaptor.forClass(Values.class);
    ArgumentCaptor<Object> msgId = ArgumentCaptor.forClass(Object.class);
    spout.nextTuple();
    spout.ack(-1L);
    Utils.sleep(10);
    spout.nextTuple();
    spout.ack(0L);
    Utils.sleep(10);
    spout.nextTuple();
    spout.ack(0L);
    Utils.sleep(10);
    spout.nextTuple();
    spout.fail(1L);
    Utils.sleep(10);
    spout.nextTuple();
    spout.fail(1L);
    Utils.sleep(10);
    spout.nextTuple();
    spout.ack(1L);
    Utils.sleep(10);
    spout.nextTuple();
    spout.ack(0L);
    Utils.sleep(10);
    spout.nextTuple();
    Mockito.verify(mockOutputCollector, Mockito.times(8)).emit(stream.capture(), values.capture(), msgId.capture());
    Values expectedTuple = new Values(1L, Action.PREPARE);
    assertEquals(CheckpointSpout.CHECKPOINT_STREAM_ID, stream.getValue());
    assertEquals(expectedTuple, values.getValue());
    assertEquals(1L, msgId.getValue());
}
Also used : HashMap(java.util.HashMap) Values(org.apache.storm.tuple.Values) KeyValueState(org.apache.storm.state.KeyValueState) Test(org.junit.Test)

Example 5 with KeyValueState

use of org.apache.storm.state.KeyValueState in project storm by apache.

the class StatefulWindowedBoltExecutorTest method testRecovery.

@Test
public void testRecovery() throws Exception {
    mockStormConf.put(Config.TOPOLOGY_BOLTS_MESSAGE_ID_FIELD_NAME, "msgid");
    mockStormConf.put(Config.TOPOLOGY_BOLTS_WINDOW_LENGTH_COUNT, 5);
    mockStormConf.put(Config.TOPOLOGY_BOLTS_SLIDING_INTERVAL_COUNT, 5);
    KeyValueState<TaskStream, WindowState> mockState;
    mockState = Mockito.mock(KeyValueState.class);
    Map<GlobalStreamId, Grouping> mockMap = Mockito.mock(Map.class);
    Mockito.when(mockTopologyContext.getThisSources()).thenReturn(mockMap);
    Mockito.when(mockTopologyContext.getComponentTasks(Mockito.anyString())).thenReturn(Collections.singletonList(1));
    Mockito.when(mockMap.keySet()).thenReturn(Collections.singleton(new GlobalStreamId("a", "s")));
    WindowState mockWindowState = new WindowState(4, 4);
    Mockito.when(mockState.get(Mockito.any(TaskStream.class))).thenReturn(mockWindowState);
    executor.prepare(mockStormConf, mockTopologyContext, mockOutputCollector, mockState);
    executor.initState(null);
    List<Tuple> tuples = getMockTuples(10);
    for (Tuple tuple : tuples) {
        executor.execute(tuple);
    }
    WindowState expectedState = new WindowState(4, 9);
    Mockito.verify(mockState, Mockito.times(1)).put(Mockito.any(TaskStream.class), Mockito.eq(expectedState));
}
Also used : WindowState(org.apache.storm.topology.StatefulWindowedBoltExecutor.WindowState) TaskStream(org.apache.storm.topology.StatefulWindowedBoltExecutor.TaskStream) GlobalStreamId(org.apache.storm.generated.GlobalStreamId) KeyValueState(org.apache.storm.state.KeyValueState) Grouping(org.apache.storm.generated.Grouping) Tuple(org.apache.storm.tuple.Tuple) Test(org.junit.Test)

Aggregations

KeyValueState (org.apache.storm.state.KeyValueState)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)4 Values (org.apache.storm.tuple.Values)4 TaskStream (org.apache.storm.topology.StatefulWindowedBoltExecutor.TaskStream)2 WindowState (org.apache.storm.topology.StatefulWindowedBoltExecutor.WindowState)2 Tuple (org.apache.storm.tuple.Tuple)2 GlobalStreamId (org.apache.storm.generated.GlobalStreamId)1 Grouping (org.apache.storm.generated.Grouping)1