Search in sources :

Example 46 with OperatorContext

use of com.datatorrent.api.Context.OperatorContext in project apex-malhar by apache.

the class SpillableSetMultimapImplTest method testLoad.

@Test
public void testLoad() {
    Random random = new Random();
    final int keySize = 1000000;
    final int valueSize = 100000000;
    final int numOfEntry = 100000;
    SpillableStateStore store = testMeta.store;
    SpillableSetMultimapImpl<String, String> multimap = new SpillableSetMultimapImpl<>(testMeta.store, ID1, 0L, createStringSerde(), createStringSerde());
    Attribute.AttributeMap.DefaultAttributeMap attributes = new Attribute.AttributeMap.DefaultAttributeMap();
    attributes.put(DAG.APPLICATION_PATH, testMeta.applicationPath);
    OperatorContext context = mockOperatorContext(testMeta.operatorContext.getId(), attributes);
    store.setup(context);
    multimap.setup(context);
    store.beginWindow(1);
    multimap.beginWindow(1);
    for (int i = 0; i < numOfEntry; ++i) {
        multimap.put(String.valueOf(random.nextInt(keySize)), String.valueOf(random.nextInt(valueSize)));
    }
    multimap.endWindow();
    store.endWindow();
}
Also used : Random(java.util.Random) Attribute(com.datatorrent.api.Attribute) OperatorContextTestHelper.mockOperatorContext(org.apache.apex.malhar.lib.helper.OperatorContextTestHelper.mockOperatorContext) OperatorContext(com.datatorrent.api.Context.OperatorContext) InMemSpillableStateStore(org.apache.apex.malhar.lib.state.spillable.inmem.InMemSpillableStateStore) Test(org.junit.Test)

Example 47 with OperatorContext

use of com.datatorrent.api.Context.OperatorContext in project apex-malhar by apache.

the class WindowUtilsTest method msToAppWindowCountRoundingTest.

@Test
public void msToAppWindowCountRoundingTest() {
    OperatorContext context = createOperatorContext(500, 10);
    long appWindowCount = WindowUtils.msToAppWindowCount(context, 10001L);
    Assert.assertEquals(3, appWindowCount);
}
Also used : OperatorContext(com.datatorrent.api.Context.OperatorContext) OperatorContextTestHelper.mockOperatorContext(org.apache.apex.malhar.lib.helper.OperatorContextTestHelper.mockOperatorContext) Test(org.junit.Test)

Example 48 with OperatorContext

use of com.datatorrent.api.Context.OperatorContext in project apex-malhar by apache.

the class DeduperTimeBasedPOJOImplTest method testDedupDifferentWindowSameKey.

@Test
public void testDedupDifferentWindowSameKey() {
    com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap attributes = new com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap();
    attributes.put(DAG.APPLICATION_ID, APP_ID);
    attributes.put(DAG.APPLICATION_PATH, applicationPath);
    attributes.put(DAG.InputPortMeta.TUPLE_CLASS, TestPojo.class);
    OperatorContext context = mockOperatorContext(OPERATOR_ID, attributes);
    deduper.setup(context);
    deduper.input.setup(new PortContext(attributes, context));
    deduper.activate(context);
    CollectorTestSink<TestPojo> uniqueSink = new CollectorTestSink<TestPojo>();
    TestUtils.setSink(deduper.unique, uniqueSink);
    CollectorTestSink<TestPojo> duplicateSink = new CollectorTestSink<TestPojo>();
    TestUtils.setSink(deduper.duplicate, duplicateSink);
    CollectorTestSink<TestPojo> expiredSink = new CollectorTestSink<TestPojo>();
    TestUtils.setSink(deduper.expired, expiredSink);
    deduper.beginWindow(0);
    long millis = System.currentTimeMillis();
    deduper.input.process(new TestPojo(10, new Date(millis)));
    deduper.input.process(new TestPojo(11, new Date(millis + 10000)));
    deduper.input.process(new TestPojo(12, new Date(millis + 20000)));
    deduper.input.process(new TestPojo(13, new Date(millis + 30000)));
    deduper.input.process(new TestPojo(14, new Date(millis + 40000)));
    deduper.input.process(new TestPojo(15, new Date(millis + 50000)));
    // Duplicate
    deduper.input.process(new TestPojo(10, new Date(millis)));
    deduper.input.process(new TestPojo(16, new Date(millis + 60000)));
    // New tuple with same key but outside expired window.
    deduper.input.process(new TestPojo(10, new Date(millis + 70000)));
    // Earlier tuple with earlier time -- Expired
    deduper.input.process(new TestPojo(10, new Date(millis)));
    // New tuple repeated again - Duplicate
    deduper.input.process(new TestPojo(10, new Date(millis + 70000)));
    deduper.handleIdleTime();
    deduper.endWindow();
    Assert.assertTrue(uniqueSink.collectedTuples.size() == 8);
    Assert.assertTrue(duplicateSink.collectedTuples.size() == 2);
    Assert.assertTrue(expiredSink.collectedTuples.size() == 1);
    deduper.teardown();
}
Also used : Date(java.util.Date) PortContext(com.datatorrent.stram.engine.PortContext) OperatorContextTestHelper.mockOperatorContext(org.apache.apex.malhar.lib.helper.OperatorContextTestHelper.mockOperatorContext) OperatorContext(com.datatorrent.api.Context.OperatorContext) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Example 49 with OperatorContext

use of com.datatorrent.api.Context.OperatorContext in project apex-malhar by apache.

the class OperatorContextTestHelper method mockOperatorContext.

public static OperatorContext mockOperatorContext(int id, final AttributeMap map) {
    OperatorContext context = Mockito.mock(OperatorContext.class);
    Mockito.when(context.getId()).thenReturn(id);
    Mockito.when(context.getAttributes()).thenReturn(map);
    Mockito.doThrow(new UnsupportedOperationException("not supported")).when(context).sendMetrics(Mockito.<Collection<String>>any());
    Mockito.doThrow(new UnsupportedOperationException("not supported")).when(context).setCounters(Mockito.any());
    Mockito.when(context.getValue(Mockito.<Attribute>any())).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            final Attribute key = (Attribute) invocation.getArguments()[0];
            Object value = map.get(key);
            if (value != null) {
                return value;
            }
            return key.defaultValue;
        }
    });
    Mockito.doNothing().when(context).setCounters(Mockito.any());
    Mockito.when(context.getWindowsFromCheckpoint()).thenReturn(0);
    return context;
}
Also used : Attribute(com.datatorrent.api.Attribute) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OperatorContext(com.datatorrent.api.Context.OperatorContext)

Example 50 with OperatorContext

use of com.datatorrent.api.Context.OperatorContext in project apex-malhar by apache.

the class ManagedTimeStateImplTest method testRecovery.

@Test
public void testRecovery() throws ExecutionException, InterruptedException {
    Slice one = ManagedStateTestUtils.getSliceFor("1");
    testMeta.managedState.setup(testMeta.operatorContext);
    long time = System.currentTimeMillis();
    testMeta.managedState.beginWindow(0);
    testMeta.managedState.put(0, time, one, one);
    testMeta.managedState.endWindow();
    testMeta.managedState.beforeCheckpoint(0);
    testMeta.managedState.teardown();
    // there is a failure and the operator is re-deployed.
    testMeta.managedState.setStateTracker(new StateTracker());
    Attribute.AttributeMap.DefaultAttributeMap attributes = new Attribute.AttributeMap.DefaultAttributeMap();
    attributes.put(DAG.APPLICATION_PATH, testMeta.applicationPath);
    attributes.put(Context.OperatorContext.ACTIVATION_WINDOW_ID, 0L);
    OperatorContext operatorContext = mockOperatorContext(1, attributes);
    testMeta.managedState.setup(operatorContext);
    Bucket.DefaultBucket defaultBucket = (Bucket.DefaultBucket) testMeta.managedState.getBucket(0);
    Assert.assertEquals("value of one", one, defaultBucket.get(one, time, Bucket.ReadSource.MEMORY));
}
Also used : DefaultBucket(org.apache.apex.malhar.lib.state.managed.Bucket.DefaultBucket) Attribute(com.datatorrent.api.Attribute) DefaultBucket(org.apache.apex.malhar.lib.state.managed.Bucket.DefaultBucket) DefaultBucket(org.apache.apex.malhar.lib.state.managed.Bucket.DefaultBucket) Slice(com.datatorrent.netlet.util.Slice) OperatorContextTestHelper.mockOperatorContext(org.apache.apex.malhar.lib.helper.OperatorContextTestHelper.mockOperatorContext) OperatorContext(com.datatorrent.api.Context.OperatorContext) Test(org.junit.Test)

Aggregations

OperatorContext (com.datatorrent.api.Context.OperatorContext)60 OperatorContextTestHelper.mockOperatorContext (org.apache.apex.malhar.lib.helper.OperatorContextTestHelper.mockOperatorContext)57 Test (org.junit.Test)51 Attribute (com.datatorrent.api.Attribute)27 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)19 TestPortContext (org.apache.apex.malhar.lib.helper.TestPortContext)12 AttributeMap (com.datatorrent.api.Attribute.AttributeMap)11 ArrayList (java.util.ArrayList)9 Random (java.util.Random)8 FieldInfo (org.apache.apex.malhar.lib.util.FieldInfo)6 InMemSpillableStateStore (org.apache.apex.malhar.lib.state.spillable.inmem.InMemSpillableStateStore)5 Partitioner (com.datatorrent.api.Partitioner)4 IOException (java.io.IOException)4 Statement (java.sql.Statement)4 FilePartitionMapping (org.apache.apex.malhar.hive.AbstractFSRollingOutputOperator.FilePartitionMapping)4 TestEvent (org.apache.apex.malhar.lib.db.jdbc.JdbcNonTransactionalOutputOperatorTest.TestEvent)4 StringSerde (org.apache.apex.malhar.lib.utils.serde.StringSerde)4 PortContext (com.datatorrent.stram.engine.PortContext)3 Connection (java.sql.Connection)3 Date (java.sql.Date)3