Search in sources :

Example 16 with TopicId

use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.

the class MessagingHttpServiceTest method testMetadataEndpoints.

@Test
public void testMetadataEndpoints() throws Exception {
    NamespaceId nsId = new NamespaceId("metadata");
    TopicId topic1 = nsId.topic("t1");
    TopicId topic2 = nsId.topic("t2");
    // Get a non exist topic should fail
    try {
        client.getTopic(topic1);
        Assert.fail("Expected TopicNotFoundException");
    } catch (TopicNotFoundException e) {
    // Expected
    }
    // Create the topic t1
    client.createTopic(new TopicMetadata(topic1));
    // Create an existing topic should fail
    try {
        client.createTopic(new TopicMetadata(topic1));
        Assert.fail("Expect TopicAlreadyExistsException");
    } catch (TopicAlreadyExistsException e) {
    // Expected
    }
    // Get the topic properties. Verify TTL is the same as the default one
    Assert.assertEquals(cConf.getInt(Constants.MessagingSystem.TOPIC_DEFAULT_TTL_SECONDS), client.getTopic(topic1).getTTL());
    // Update the topic t1 with new TTL
    client.updateTopic(new TopicMetadata(topic1, "ttl", "5"));
    // Get the topic t1 properties. Verify TTL is updated
    Assert.assertEquals(5, client.getTopic(topic1).getTTL());
    // Try to add another topic t2 with invalid ttl, it should fail
    try {
        client.createTopic(new TopicMetadata(topic2, "ttl", "xyz"));
        Assert.fail("Expect BadRequestException");
    } catch (IllegalArgumentException e) {
    // Expected
    }
    // Add topic t2 with valid ttl
    client.createTopic(new TopicMetadata(topic2, "ttl", "5"));
    // Get the topic t2 properties. It should have TTL set based on what provided
    Assert.assertEquals(5, client.getTopic(topic2).getTTL());
    // Listing topics under namespace ns1
    List<TopicId> topics = client.listTopics(nsId);
    Assert.assertEquals(Arrays.asList(topic1, topic2), topics);
    // Delete both topics
    client.deleteTopic(topic1);
    client.deleteTopic(topic2);
    // Delete a non exist topic should fail
    try {
        client.deleteTopic(topic1);
        Assert.fail("Expect TopicNotFoundException");
    } catch (TopicNotFoundException e) {
    // Expected
    }
    // Update a non exist topic should fail
    try {
        client.updateTopic(new TopicMetadata(topic1));
        Assert.fail("Expect TopicNotFoundException");
    } catch (TopicNotFoundException e) {
    // Expected
    }
    // Listing topics under namespace ns1 again, it should be empty
    Assert.assertTrue(client.listTopics(nsId).isEmpty());
}
Also used : TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) TopicAlreadyExistsException(co.cask.cdap.api.messaging.TopicAlreadyExistsException) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Example 17 with TopicId

use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.

the class MessagingHttpServiceTest method testGeMetadata.

@Test
public void testGeMetadata() throws Exception {
    TopicId topicId = new NamespaceId("ns2").topic("d");
    TopicMetadata metadata = new TopicMetadata(topicId, "ttl", "100");
    for (int i = 1; i <= 5; i++) {
        client.createTopic(metadata);
        TopicMetadata topicMetadata = client.getTopic(topicId);
        Assert.assertEquals(100, topicMetadata.getTTL());
        Assert.assertEquals(i, topicMetadata.getGeneration());
        client.deleteTopic(topicId);
    }
}
Also used : TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Example 18 with TopicId

use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.

the class MessagingHttpServiceTest method testTxMaxLifeTime.

@Test
public void testTxMaxLifeTime() throws Exception {
    NamespaceId nsId = new NamespaceId("txCheck");
    TopicId topic1 = nsId.topic("t1");
    // Create a topic
    client.createTopic(new TopicMetadata(topic1));
    final RollbackDetail rollbackDetail = client.publish(StoreRequestBuilder.of(topic1).setTransaction(1L).addPayloads("a", "b").build());
    try {
        client.publish(StoreRequestBuilder.of(topic1).setTransaction(-Long.MAX_VALUE).addPayloads("c", "d").build());
        Assert.fail("Expected IOException");
    } catch (IOException ex) {
    // expected
    }
    Set<String> msgs = new HashSet<>();
    CloseableIterator<RawMessage> messages = client.prepareFetch(topic1).fetch();
    while (messages.hasNext()) {
        RawMessage message = messages.next();
        msgs.add(Bytes.toString(message.getPayload()));
    }
    Assert.assertEquals(2, msgs.size());
    Assert.assertTrue(msgs.contains("a"));
    Assert.assertTrue(msgs.contains("b"));
    messages.close();
    client.rollback(topic1, rollbackDetail);
    client.deleteTopic(topic1);
}
Also used : RollbackDetail(co.cask.cdap.messaging.RollbackDetail) TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) IOException(java.io.IOException) RawMessage(co.cask.cdap.messaging.data.RawMessage) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with TopicId

use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.

the class DataCleanupTest method testOldGenCleanup.

@Test
public void testOldGenCleanup() throws Exception {
    try (MetadataTable metadataTable = getMetadataTable();
        MessageTable messageTable = getMessageTable();
        PayloadTable payloadTable = getPayloadTable()) {
        int txWritePtr = 100;
        TopicId topicId = NamespaceId.DEFAULT.topic("oldGenCleanup");
        TopicMetadata topic = new TopicMetadata(topicId, TopicMetadata.TTL_KEY, "100000", TopicMetadata.GENERATION_KEY, Integer.toString(GENERATION));
        metadataTable.createTopic(topic);
        List<MessageTable.Entry> entries = new ArrayList<>();
        List<PayloadTable.Entry> pentries = new ArrayList<>();
        byte[] messageId = new byte[MessageId.RAW_ID_SIZE];
        MessageId.putRawId(0L, (short) 0, 0L, (short) 0, messageId, 0);
        entries.add(new TestMessageEntry(topicId, GENERATION, "data", txWritePtr, (short) 0));
        pentries.add(new TestPayloadEntry(topicId, GENERATION, "data", txWritePtr, (short) 0));
        messageTable.store(entries.iterator());
        payloadTable.store(pentries.iterator());
        // Fetch the entries and make sure we are able to read it
        try (CloseableIterator<MessageTable.Entry> iterator = messageTable.fetch(topic, 0, Integer.MAX_VALUE, null)) {
            checkMessageEntry(iterator, txWritePtr);
        }
        try (CloseableIterator<PayloadTable.Entry> iterator = payloadTable.fetch(topic, txWritePtr, new MessageId(messageId), true, 100)) {
            checkPayloadEntry(iterator, txWritePtr);
        }
        // Now run full compaction
        forceFlushAndCompact(Table.MESSAGE);
        forceFlushAndCompact(Table.PAYLOAD);
        // Fetch the entries and make sure we are able to read it
        try (CloseableIterator<MessageTable.Entry> iterator = messageTable.fetch(topic, 0, Integer.MAX_VALUE, null)) {
            checkMessageEntry(iterator, txWritePtr);
        }
        try (CloseableIterator<PayloadTable.Entry> iterator = payloadTable.fetch(topic, txWritePtr, new MessageId(messageId), true, 100)) {
            checkPayloadEntry(iterator, txWritePtr);
        }
        metadataTable.deleteTopic(topicId);
        // Sleep so that the metadata cache in coprocessor expires
        TimeUnit.SECONDS.sleep(3 * METADATA_CACHE_EXPIRY);
        forceFlushAndCompact(Table.MESSAGE);
        forceFlushAndCompact(Table.PAYLOAD);
        try (CloseableIterator<MessageTable.Entry> iterator = messageTable.fetch(topic, 0, Integer.MAX_VALUE, null)) {
            Assert.assertFalse(iterator.hasNext());
        }
        try (CloseableIterator<PayloadTable.Entry> iterator = payloadTable.fetch(topic, txWritePtr, new MessageId(messageId), true, 100)) {
            Assert.assertFalse(iterator.hasNext());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) TopicId(co.cask.cdap.proto.id.TopicId) MessageId(co.cask.cdap.messaging.data.MessageId) Test(org.junit.Test)

Example 20 with TopicId

use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.

the class LevelDBMetadataTableTest method testScanTopics.

@Test
public void testScanTopics() throws Exception {
    try (MetadataTable metadataTable = createMetadataTable()) {
        LevelDBMetadataTable table = (LevelDBMetadataTable) metadataTable;
        TopicMetadata t1 = new TopicMetadata(NamespaceId.CDAP.topic("t1"), ImmutableMap.of(TopicMetadata.TTL_KEY, "10", TopicMetadata.GENERATION_KEY, "1"));
        TopicMetadata t2 = new TopicMetadata(NamespaceId.SYSTEM.topic("t2"), ImmutableMap.of(TopicMetadata.TTL_KEY, "20", TopicMetadata.GENERATION_KEY, "1"));
        metadataTable.createTopic(t1);
        metadataTable.createTopic(t2);
        List<TopicId> allTopics = table.listTopics();
        Assert.assertEquals(2, allTopics.size());
        List<TopicMetadata> metadatas = new ArrayList<>();
        Iterators.addAll(metadatas, table.scanTopics());
        Assert.assertEquals(2, metadatas.size());
        allTopics = table.listTopics(NamespaceId.CDAP);
        Assert.assertEquals(1, allTopics.size());
        allTopics = table.listTopics(NamespaceId.SYSTEM);
        Assert.assertEquals(1, allTopics.size());
        metadataTable.deleteTopic(t1.getTopicId());
        metadatas.clear();
        Iterators.addAll(metadatas, table.scanTopics());
        Assert.assertEquals(2, metadatas.size());
        Assert.assertEquals(1, metadataTable.listTopics().size());
        Assert.assertEquals(1, metadataTable.listTopics(NamespaceId.SYSTEM).size());
        Assert.assertTrue(metadataTable.listTopics(NamespaceId.CDAP).isEmpty());
        metadataTable.deleteTopic(t2.getTopicId());
        metadatas.clear();
        Iterators.addAll(metadatas, table.scanTopics());
        for (TopicMetadata metadata : metadatas) {
            Assert.assertEquals(-1, metadata.getGeneration());
        }
        Assert.assertTrue(metadataTable.listTopics().isEmpty());
    }
}
Also used : MetadataTable(co.cask.cdap.messaging.store.MetadataTable) ArrayList(java.util.ArrayList) TopicId(co.cask.cdap.proto.id.TopicId) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Test(org.junit.Test) MetadataTableTest(co.cask.cdap.messaging.store.MetadataTableTest)

Aggregations

TopicId (co.cask.cdap.proto.id.TopicId)60 TopicMetadata (co.cask.cdap.messaging.TopicMetadata)33 Test (org.junit.Test)28 NamespaceId (co.cask.cdap.proto.id.NamespaceId)25 ArrayList (java.util.ArrayList)20 Path (javax.ws.rs.Path)14 RawMessage (co.cask.cdap.messaging.data.RawMessage)13 IOException (java.io.IOException)12 MessageId (co.cask.cdap.messaging.data.MessageId)10 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)9 POST (javax.ws.rs.POST)7 TopicAlreadyExistsException (co.cask.cdap.api.messaging.TopicAlreadyExistsException)6 BadRequestException (co.cask.cdap.common.BadRequestException)6 RollbackDetail (co.cask.cdap.messaging.RollbackDetail)5 StoreRequest (co.cask.cdap.messaging.StoreRequest)5 GenericRecord (org.apache.avro.generic.GenericRecord)5 TimeProvider (co.cask.cdap.common.utils.TimeProvider)4 PUT (javax.ws.rs.PUT)4 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)4 Decoder (org.apache.avro.io.Decoder)4