Search in sources :

Example 6 with FlowId

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);
    }
}
Also used : FlowId(co.cask.cdap.proto.id.FlowId) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) NamespaceId(co.cask.cdap.proto.id.NamespaceId) File(java.io.File) Test(org.junit.Test)

Example 7 with FlowId

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();
    }
}
Also used : FlowId(co.cask.cdap.proto.id.FlowId) QueueConsumer(co.cask.cdap.data2.queue.QueueConsumer) QueueProducer(co.cask.cdap.data2.queue.QueueProducer) TransactionContext(org.apache.tephra.TransactionContext) ConsumerConfig(co.cask.cdap.data2.queue.ConsumerConfig) QueueName(co.cask.cdap.common.queue.QueueName) ConsumerGroupConfig(co.cask.cdap.data2.queue.ConsumerGroupConfig) QueueEntry(co.cask.cdap.data2.queue.QueueEntry)

Example 8 with FlowId

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));
}
Also used : FlowId(co.cask.cdap.proto.id.FlowId) Test(org.junit.Test)

Aggregations

FlowId (co.cask.cdap.proto.id.FlowId)8 NamespaceId (co.cask.cdap.proto.id.NamespaceId)5 Test (org.junit.Test)4 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3 FakeApp (co.cask.cdap.client.app.FakeApp)2 NotFoundException (co.cask.cdap.common.NotFoundException)2 QueueName (co.cask.cdap.common.queue.QueueName)2 BatchProgramStatus (co.cask.cdap.proto.BatchProgramStatus)2 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)2 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)1 NamespaceQueryAdmin (co.cask.cdap.common.namespace.NamespaceQueryAdmin)1 ConsumerConfig (co.cask.cdap.data2.queue.ConsumerConfig)1 ConsumerGroupConfig (co.cask.cdap.data2.queue.ConsumerGroupConfig)1 QueueConsumer (co.cask.cdap.data2.queue.QueueConsumer)1 QueueEntry (co.cask.cdap.data2.queue.QueueEntry)1 QueueProducer (co.cask.cdap.data2.queue.QueueProducer)1 BatchProgram (co.cask.cdap.proto.BatchProgram)1 ProgramRecord (co.cask.cdap.proto.ProgramRecord)1 ProgramStatus (co.cask.cdap.proto.ProgramStatus)1 DatasetId (co.cask.cdap.proto.id.DatasetId)1