use of edu.snu.mist.common.types.Tuple2 in project mist by snuspl.
the class StateTransitionOperatorTest method testStateTransitionOperatorSetState.
/**
* Test setting state of StateTransitionOperator.
*/
@Test
public void testStateTransitionOperatorSetState() throws InterruptedException {
// Generate a new state and set it to StateTransitionOperator.
final String newFunctionState = "1";
final Map<String, Object> loadStateMap = new HashMap<>();
loadStateMap.put("stateTransitionOperatorState", newFunctionState);
// generate a set of final states
final Set<String> finalSet = new HashSet<>();
finalSet.add("2");
finalSet.add("3");
// generate a state table
final Map<String, Collection<Tuple2<MISTPredicate, String>>> stateTable = new HashMap<>();
final Collection<Tuple2<MISTPredicate, String>> list1 = new ArrayList<>();
list1.add(new Tuple2<>(new RuleBasedEQPredicate("number", 2), "2"));
final Collection<Tuple2<MISTPredicate, String>> list2 = new ArrayList<>();
list2.add(new Tuple2<>(new RuleBasedEQPredicate("number", 3), "3"));
stateTable.put("1", list1);
stateTable.put("2", list2);
final StateTransitionOperator stateTransitionOperator = new StateTransitionOperator("0", finalSet, stateTable);
stateTransitionOperator.setState(loadStateMap);
// Get the current StateTransitionOperator's state.
final Map<String, Object> operatorState = stateTransitionOperator.getStateSnapshot();
final String stateTransitionOperatorState = (String) operatorState.get("stateTransitionOperatorState");
// Compare the original and the set operator
Assert.assertEquals(newFunctionState, stateTransitionOperatorState);
// Test if the operator can properly process data.
final List<MistEvent> result = new LinkedList<>();
stateTransitionOperator.setOutputEmitter(new OutputBufferEmitter(result));
// expected result: 2--3
// generate input data event
final Map<String, Integer> value2 = new HashMap<>();
value2.put("number", 2);
final MistDataEvent data2 = new MistDataEvent(value2, 0L);
final Map<String, Integer> value3 = new HashMap<>();
value3.put("number", 3);
final MistDataEvent data3 = new MistDataEvent(value3, 1L);
stateTransitionOperator.processLeftData(data2);
Assert.assertEquals(1, result.size());
Assert.assertEquals(data2, result.get(0));
Assert.assertEquals("2", stateTransitionOperator.getStateSnapshot().get("stateTransitionOperatorState"));
stateTransitionOperator.processLeftData(data3);
Assert.assertEquals(2, result.size());
Assert.assertEquals(data3, result.get(1));
Assert.assertEquals("3", stateTransitionOperator.getStateSnapshot().get("stateTransitionOperatorState"));
}
use of edu.snu.mist.common.types.Tuple2 in project mist by snuspl.
the class QueryDeletion method submitQuery.
/**
* Submit a query containing reduce-by-key operator.
* The query reads strings from a source server, filters alphabetical words,
* counts words using reduce-by-key operator, and sends them to a sink server.
* @return result of the submission
* @throws IOException
* @throws InjectionException
*/
public static APIQueryControlResult submitQuery(final Configuration configuration) throws IOException, InjectionException, URISyntaxException {
final String sourceSocket = Tang.Factory.getTang().newInjector(configuration).getNamedInstance(NettySourceAddress.class);
final SourceConfiguration localTextSocketSourceConf = MISTExampleUtils.getLocalTextSocketSourceConf(sourceSocket);
// Simple reduce function.
final MISTBiFunction<Integer, Integer, Integer> reduceFunction = (v1, v2) -> {
return v1 + v2;
};
final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
queryBuilder.socketTextStream(localTextSocketSourceConf).filter(s -> isAlpha(s)).map(s -> new Tuple2(s, 1)).reduceByKey(0, String.class, reduceFunction).map(s -> s.toString()).textSocketOutput(MISTExampleUtils.SINK_HOSTNAME, MISTExampleUtils.SINK_PORT);
return MISTExampleUtils.submit(queryBuilder, configuration);
}
use of edu.snu.mist.common.types.Tuple2 in project mist by snuspl.
the class UnionMist method submitQuery.
/**
* Submit a query unifying two sources.
* The query reads strings from two source servers, unifies them, and send them to a sink server.
* @return result of the submission
* @throws IOException
* @throws InjectionException
*/
public static APIQueryControlResult submitQuery(final Configuration configuration) throws IOException, InjectionException, URISyntaxException {
final Injector injector = Tang.Factory.getTang().newInjector(configuration);
final String source1Socket = injector.getNamedInstance(UnionLeftSourceAddress.class);
final String source2Socket = injector.getNamedInstance(UnionRightSourceAddress.class);
final SourceConfiguration localTextSocketSource1Conf = MISTExampleUtils.getLocalTextSocketSourceConf(source1Socket);
final SourceConfiguration localTextSocketSource2Conf = MISTExampleUtils.getLocalTextSocketSourceConf(source2Socket);
// Simple reduce function.
final MISTBiFunction<Integer, Integer, Integer> reduceFunction = (v1, v2) -> {
return v1 + v2;
};
final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
final ContinuousStream sourceStream1 = queryBuilder.socketTextStream(localTextSocketSource1Conf).map(s -> new Tuple2(s, 1));
final ContinuousStream sourceStream2 = queryBuilder.socketTextStream(localTextSocketSource2Conf).map(s -> new Tuple2(s, 1));
sourceStream1.union(sourceStream2).reduceByKey(0, String.class, reduceFunction).map(s -> s.toString()).textSocketOutput(MISTExampleUtils.SINK_HOSTNAME, MISTExampleUtils.SINK_PORT);
return MISTExampleUtils.submit(queryBuilder, configuration);
}
use of edu.snu.mist.common.types.Tuple2 in project mist by snuspl.
the class WordCount method submitQuery.
/**
* Submit a query containing reduce-by-key operator.
* The query reads strings from a source server, filters alphabetical words,
* counts words using reduce-by-key operator, and sends them to a sink server.
* @return result of the submission
* @throws IOException
* @throws InjectionException
*/
public static APIQueryControlResult submitQuery(final Configuration configuration) throws IOException, InjectionException, URISyntaxException {
final String sourceSocket = Tang.Factory.getTang().newInjector(configuration).getNamedInstance(NettySourceAddress.class);
final SourceConfiguration localTextSocketSourceConf = MISTExampleUtils.getLocalTextSocketSourceConf(sourceSocket);
// Simple reduce function.
final MISTBiFunction<Integer, Integer, Integer> reduceFunction = (v1, v2) -> {
return v1 + v2;
};
final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
queryBuilder.socketTextStream(localTextSocketSourceConf).filter(s -> isAlpha(s)).map(s -> new Tuple2(s, 1)).reduceByKey(0, String.class, reduceFunction).map(s -> s.toString()).textSocketOutput(MISTExampleUtils.SINK_HOSTNAME, MISTExampleUtils.SINK_PORT);
return MISTExampleUtils.submit(queryBuilder, configuration);
}
use of edu.snu.mist.common.types.Tuple2 in project mist by snuspl.
the class QueryInfoStoreTest method diskStoreTest.
/**
* Tests whether the PlanStore correctly saves, deletes and loads the operator chain dag.
* @throws InjectionException
* @throws IOException
*/
@Test(timeout = 1000)
public void diskStoreTest() throws InjectionException, IOException {
// Generate a query
final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
queryBuilder.setApplicationId(TestParameters.SUPER_GROUP_ID);
queryBuilder.socketTextStream(TestParameters.LOCAL_TEXT_SOCKET_SOURCE_CONF).flatMap(s -> Arrays.asList(s.split(" "))).filter(s -> s.startsWith("A")).map(s -> new Tuple2<>(s, 1)).reduceByKey(0, String.class, (Integer x, Integer y) -> x + y).textSocketOutput(TestParameters.HOST, TestParameters.SINK_PORT);
final MISTQuery query = queryBuilder.build();
// Jar files
final List<ByteBuffer> jarFiles = new LinkedList<>();
final ByteBuffer byteBuffer1 = ByteBuffer.wrap(new byte[] { 0, 1, 0, 1, 1, 1 });
jarFiles.add(byteBuffer1);
final Injector injector = Tang.Factory.getTang().newInjector();
final QueryInfoStore store = injector.getInstance(QueryInfoStore.class);
final ApplicationCodeManager applicationCodeManager = injector.getInstance(ApplicationCodeManager.class);
final ApplicationMap applicationMap = injector.getInstance(ApplicationMap.class);
final String queryId1 = "testQuery1";
final String queryId2 = "testQuery2";
final String tmpFolderPath = injector.getNamedInstance(SharedStorePath.class);
final File folder = new File(tmpFolderPath);
// Store jar files
final List<String> paths = applicationCodeManager.registerNewAppCode(jarFiles).getJarPaths();
for (int i = 0; i < jarFiles.size(); i++) {
final ByteBuffer buf = ByteBuffer.allocateDirect(jarFiles.get(i).capacity());
final String path = paths.get(i);
final FileInputStream fis = new FileInputStream(path);
final FileChannel channel = fis.getChannel();
channel.read(buf);
Assert.assertEquals(jarFiles.get(i), buf);
}
final ApplicationInfo applicationInfo = mock(ApplicationInfo.class);
when(applicationInfo.getApplicationId()).thenReturn(TestParameters.SUPER_GROUP_ID);
when(applicationInfo.getJarFilePath()).thenReturn(paths);
applicationMap.putIfAbsent(TestParameters.SUPER_GROUP_ID, applicationInfo);
// Generate logical plan
final Tuple<List<AvroVertex>, List<Edge>> serializedDag = query.getAvroOperatorDag();
final AvroDag.Builder avroDagBuilder = AvroDag.newBuilder();
final AvroDag avroDag1 = avroDagBuilder.setAppId(TestParameters.SUPER_GROUP_ID).setQueryId(TestParameters.QUERY_ID).setJarPaths(paths).setAvroVertices(serializedDag.getKey()).setEdges(serializedDag.getValue()).build();
final AvroDag avroDag2 = avroDagBuilder.setAppId(TestParameters.SUPER_GROUP_ID).setQueryId(TestParameters.QUERY_ID).setJarPaths(paths).setAvroVertices(serializedDag.getKey()).setEdges(serializedDag.getValue()).build();
// Store the chained dag
store.saveAvroDag(new Tuple<>(queryId1, avroDag1));
store.saveAvroDag(new Tuple<>(queryId2, avroDag2));
while (!(store.isStored(queryId1) && store.isStored(queryId2))) {
// Wait until the plan is stored
}
Assert.assertTrue(new File(tmpFolderPath, queryId1 + ".plan").exists());
Assert.assertTrue(new File(tmpFolderPath, queryId2 + ".plan").exists());
// Test stored file
final AvroDag loadedDag1 = store.load(queryId1);
Assert.assertEquals(avroDag1.getEdges(), loadedDag1.getEdges());
Assert.assertEquals(avroDag1.getSchema(), loadedDag1.getSchema());
testVerticesEqual(avroDag1.getAvroVertices(), loadedDag1.getAvroVertices());
final AvroDag loadedDag2 = store.load(queryId2);
Assert.assertEquals(avroDag2.getEdges(), loadedDag2.getEdges());
Assert.assertEquals(avroDag2.getSchema(), loadedDag2.getSchema());
testVerticesEqual(avroDag2.getAvroVertices(), loadedDag2.getAvroVertices());
// Test deletion
store.delete(queryId1);
store.delete(queryId2);
Assert.assertFalse(store.isStored(queryId1));
Assert.assertFalse(new File(tmpFolderPath, queryId1 + ".plan").exists());
Assert.assertFalse(store.isStored(queryId2));
Assert.assertFalse(new File(tmpFolderPath, queryId2 + ".plan").exists());
for (final String path : paths) {
Assert.assertFalse(new File(path).exists());
}
folder.delete();
}
Aggregations