use of co.cask.cdap.proto.id.FlowId in project cdap by caskdata.
the class PreferencesClientTestRun method testPreferences.
@Test
public void testPreferences() throws Exception {
NamespaceId invalidNamespace = new NamespaceId("invalid");
namespaceClient.create(new NamespaceMeta.Builder().setName(invalidNamespace).build());
Map<String, String> propMap = client.getInstancePreferences();
Assert.assertEquals(ImmutableMap.<String, String>of(), propMap);
propMap.put("k1", "instance");
client.setInstancePreferences(propMap);
Assert.assertEquals(propMap, client.getInstancePreferences());
File jarFile = createAppJarFile(FakeApp.class);
appClient.deploy(NamespaceId.DEFAULT, jarFile);
try {
propMap.put("k1", "namespace");
client.setNamespacePreferences(NamespaceId.DEFAULT, propMap);
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, true));
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, false));
Assert.assertTrue(client.getNamespacePreferences(invalidNamespace, false).isEmpty());
Assert.assertEquals("instance", client.getNamespacePreferences(invalidNamespace, true).get("k1"));
client.deleteNamespacePreferences(NamespaceId.DEFAULT);
propMap.put("k1", "instance");
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, true));
Assert.assertEquals(ImmutableMap.<String, String>of(), client.getNamespacePreferences(NamespaceId.DEFAULT, false));
propMap.put("k1", "namespace");
client.setNamespacePreferences(NamespaceId.DEFAULT, propMap);
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, true));
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, false));
propMap.put("k1", "application");
client.setApplicationPreferences(FAKE_APP_ID, propMap);
Assert.assertEquals(propMap, client.getApplicationPreferences(FAKE_APP_ID, true));
Assert.assertEquals(propMap, client.getApplicationPreferences(FAKE_APP_ID, false));
propMap.put("k1", "program");
FlowId flow = FAKE_APP_ID.flow(FakeApp.FLOWS.get(0));
client.setProgramPreferences(flow, propMap);
Assert.assertEquals(propMap, client.getProgramPreferences(flow, true));
Assert.assertEquals(propMap, client.getProgramPreferences(flow, false));
client.deleteProgramPreferences(flow);
propMap.put("k1", "application");
Assert.assertTrue(client.getProgramPreferences(flow, false).isEmpty());
Assert.assertEquals(propMap, client.getProgramPreferences(flow, true));
client.deleteApplicationPreferences(FAKE_APP_ID);
propMap.put("k1", "namespace");
Assert.assertTrue(client.getApplicationPreferences(FAKE_APP_ID, false).isEmpty());
Assert.assertEquals(propMap, client.getApplicationPreferences(FAKE_APP_ID, true));
Assert.assertEquals(propMap, client.getProgramPreferences(flow, true));
client.deleteNamespacePreferences(NamespaceId.DEFAULT);
propMap.put("k1", "instance");
Assert.assertTrue(client.getNamespacePreferences(NamespaceId.DEFAULT, false).isEmpty());
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, true));
Assert.assertEquals(propMap, client.getApplicationPreferences(FAKE_APP_ID, true));
Assert.assertEquals(propMap, client.getProgramPreferences(flow, true));
client.deleteInstancePreferences();
propMap.clear();
Assert.assertEquals(propMap, client.getInstancePreferences());
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, true));
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, true));
Assert.assertEquals(propMap, client.getApplicationPreferences(FAKE_APP_ID, true));
Assert.assertEquals(propMap, client.getProgramPreferences(flow, true));
//Test Deleting Application
propMap.put("k1", "application");
client.setApplicationPreferences(FAKE_APP_ID, propMap);
Assert.assertEquals(propMap, client.getApplicationPreferences(FAKE_APP_ID, false));
propMap.put("k1", "program");
client.setProgramPreferences(flow, propMap);
Assert.assertEquals(propMap, client.getProgramPreferences(flow, false));
appClient.delete(FAKE_APP_ID);
// deleting the app should have deleted the preferences that were stored. so deploy the app and check
// if the preferences are empty. we need to deploy the app again since getting preferences of non-existent apps
// is not allowed by the API.
appClient.deploy(NamespaceId.DEFAULT, jarFile);
propMap.clear();
Assert.assertEquals(propMap, client.getApplicationPreferences(FAKE_APP_ID, false));
Assert.assertEquals(propMap, client.getProgramPreferences(flow, false));
} finally {
try {
appClient.delete(FAKE_APP_ID);
} catch (ApplicationNotFoundException e) {
// ok if this happens, means its already deleted.
}
namespaceClient.delete(invalidNamespace);
}
}
use of co.cask.cdap.proto.id.FlowId in project cdap by caskdata.
the class QueueTest method testClearOrDropAllForFlow.
private void testClearOrDropAllForFlow(boolean doDrop) throws Exception {
// this test is the same for clear and drop, except for two small places...
// using a different app name for each case as this test leaves some entries
String app = doDrop ? "tDAFF" : "tCAFF";
QueueName queueName1 = QueueName.fromFlowlet(NamespaceId.DEFAULT.getEntityName(), app, "flow1", "flowlet1", "out1");
QueueName queueName2 = QueueName.fromFlowlet(NamespaceId.DEFAULT.getEntityName(), app, "flow1", "flowlet2", "out2");
QueueName queueName3 = QueueName.fromFlowlet(NamespaceId.DEFAULT.getEntityName(), app, "flow2", "flowlet1", "out");
List<ConsumerGroupConfig> groupConfigs = ImmutableList.of(new ConsumerGroupConfig(0L, 1, DequeueStrategy.FIFO, null), new ConsumerGroupConfig(1L, 1, DequeueStrategy.FIFO, null));
configureGroups(queueName1, groupConfigs);
configureGroups(queueName2, groupConfigs);
configureGroups(queueName3, groupConfigs);
try (QueueProducer producer1 = queueClientFactory.createProducer(queueName1);
QueueProducer producer2 = queueClientFactory.createProducer(queueName2);
QueueProducer producer3 = queueClientFactory.createProducer(queueName3)) {
TransactionContext txContext = createTxContext(producer1, producer2, producer3);
txContext.start();
for (int i = 0; i < 10; i++) {
for (QueueProducer producer : Arrays.asList(producer1, producer2, producer3)) {
producer.enqueue(new QueueEntry(Bytes.toBytes(i)));
}
}
txContext.finish();
}
// consume 1 element from each queue
ConsumerConfig consumerConfig = new ConsumerConfig(0, 0, 1, DequeueStrategy.FIFO, null);
try (QueueConsumer consumer1 = queueClientFactory.createConsumer(queueName1, consumerConfig, 1);
QueueConsumer consumer2 = queueClientFactory.createConsumer(queueName2, consumerConfig, 1);
QueueConsumer consumer3 = queueClientFactory.createConsumer(queueName3, consumerConfig, 1)) {
TransactionContext txContext = createTxContext(consumer1, consumer2, consumer3);
txContext.start();
for (QueueConsumer consumer : Arrays.asList(consumer1, consumer2, consumer3)) {
DequeueResult<byte[]> result = consumer.dequeue(1);
Assert.assertFalse(result.isEmpty());
Assert.assertArrayEquals(Bytes.toBytes(0), result.iterator().next());
}
txContext.finish();
}
// verify the consumer config was deleted
verifyConsumerConfigExists(queueName1, queueName2);
// clear/drop all queues for flow1
FlowId flow1Id = NamespaceId.DEFAULT.app(app).flow("flow1");
if (doDrop) {
queueAdmin.dropAllForFlow(flow1Id);
} else {
queueAdmin.clearAllForFlow(flow1Id);
}
if (doDrop) {
// verify that only flow2's queues still exist
Assert.assertFalse(queueAdmin.exists(queueName1));
Assert.assertFalse(queueAdmin.exists(queueName2));
Assert.assertTrue(queueAdmin.exists(queueName3));
} else {
// verify all queues still exist
Assert.assertTrue(queueAdmin.exists(queueName1));
Assert.assertTrue(queueAdmin.exists(queueName2));
Assert.assertTrue(queueAdmin.exists(queueName3));
}
// verify the consumer config was deleted
verifyConsumerConfigIsDeleted(queueName1, queueName2);
// create new consumers because existing ones may have pre-fetched and cached some entries
configureGroups(queueName1, groupConfigs);
configureGroups(queueName2, groupConfigs);
try (QueueConsumer consumer1 = queueClientFactory.createConsumer(queueName1, consumerConfig, 1);
QueueConsumer consumer2 = queueClientFactory.createConsumer(queueName2, consumerConfig, 1);
QueueConsumer consumer3 = queueClientFactory.createConsumer(queueName3, consumerConfig, 1)) {
TransactionContext txContext = createTxContext(consumer1, consumer2, consumer3);
txContext.start();
// attempt to consume from flow1's queues, should be empty
for (QueueConsumer consumer : Arrays.asList(consumer1, consumer2)) {
DequeueResult<byte[]> result = consumer.dequeue(1);
Assert.assertTrue(result.isEmpty());
}
// but flow2 was not deleted -> consumer 3 should get another entry
DequeueResult<byte[]> result = consumer3.dequeue(1);
Assert.assertFalse(result.isEmpty());
Assert.assertArrayEquals(Bytes.toBytes(1), result.iterator().next());
txContext.finish();
}
}
use of co.cask.cdap.proto.id.FlowId in project cdap by caskdata.
the class QueueNameTest method testQueueNameForFlowlet.
@Test
public void testQueueNameForFlowlet() {
// create a queue name
FlowId flowId = new FlowId("namespace", "app", "flow");
QueueName queueName = QueueName.fromFlowlet(flowId, "flowlet", "out");
// verify all parts are correct
Assert.assertTrue(queueName.isQueue());
Assert.assertFalse(queueName.isStream());
Assert.assertEquals("namespace", queueName.getFirstComponent());
Assert.assertEquals("app", queueName.getSecondComponent());
Assert.assertEquals("flow", queueName.getThirdComponent());
Assert.assertEquals("flowlet", queueName.getFourthComponent());
Assert.assertEquals("out", queueName.getSimpleName());
// convert to URI and back, verify again
queueName = QueueName.from(queueName.toURI());
Assert.assertTrue(queueName.isQueue());
Assert.assertFalse(queueName.isStream());
Assert.assertEquals("namespace", queueName.getFirstComponent());
Assert.assertEquals("app", queueName.getSecondComponent());
Assert.assertEquals("flow", queueName.getThirdComponent());
Assert.assertEquals("flowlet", queueName.getFourthComponent());
Assert.assertEquals("out", queueName.getSimpleName());
// convert to bytes and back, verify again
queueName = QueueName.from(queueName.toBytes());
Assert.assertTrue(queueName.isQueue());
Assert.assertFalse(queueName.isStream());
Assert.assertEquals("namespace", queueName.getFirstComponent());
Assert.assertEquals("app", queueName.getSecondComponent());
Assert.assertEquals("flow", queueName.getThirdComponent());
Assert.assertEquals("flowlet", queueName.getFourthComponent());
Assert.assertEquals("out", queueName.getSimpleName());
// verify prefix methods
Assert.assertEquals("queue:///namespace/", QueueName.prefixForNamespacedQueue("namespace"));
Assert.assertEquals("queue:///namespace/app/flow/", QueueName.prefixForFlow(flowId));
}
Aggregations