Search in sources :

Example 16 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class ClientMessagingService method deleteTopic.

@Override
public void deleteTopic(TopicId topicId) throws TopicNotFoundException, IOException {
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.DELETE, createTopicPath(topicId)).build();
    HttpResponse response = remoteClient.execute(request);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
    }
    handleError(response, "Failed to update topic " + topicId);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) HttpResponse(co.cask.common.http.HttpResponse)

Example 17 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class LevelDBMetadataTable method deleteTopic.

@Override
public void deleteTopic(TopicId topicId) throws TopicNotFoundException, IOException {
    byte[] rowKey = MessagingUtils.toMetadataRowKey(topicId);
    try {
        synchronized (this) {
            byte[] tableValue = levelDB.get(rowKey);
            if (tableValue == null) {
                throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
            }
            Map<String, String> oldProperties = GSON.fromJson(Bytes.toString(tableValue), MAP_TYPE);
            TopicMetadata metadata = new TopicMetadata(topicId, oldProperties);
            if (!metadata.exists()) {
                throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
            }
            // Mark the topic as deleted
            TreeMap<String, String> newProperties = new TreeMap<>(metadata.getProperties());
            newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(-1 * metadata.getGeneration()));
            levelDB.put(rowKey, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE)), WRITE_OPTIONS);
        }
    } catch (DBException e) {
        throw new IOException(e);
    }
}
Also used : DBException(org.iq80.leveldb.DBException) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

Example 18 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class MetadataTableTest method testGenerations.

@Test
public void testGenerations() throws Exception {
    try (MetadataTable table = createMetadataTable()) {
        TopicId topicId = NamespaceId.DEFAULT.topic("gtopic");
        for (int i = 1; i <= 50; i++) {
            table.createTopic(new TopicMetadata(topicId, "ttl", 1));
            TopicMetadata metadata = table.getMetadata(topicId);
            Assert.assertEquals(i, metadata.getGeneration());
            Assert.assertEquals(1, metadata.getTTL());
            table.deleteTopic(topicId);
            try {
                table.getMetadata(topicId);
                Assert.fail("Expected TopicNotFoundException");
            } catch (TopicNotFoundException ex) {
            // Expected
            }
        }
    }
}
Also used : TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) TopicId(co.cask.cdap.proto.id.TopicId) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Example 19 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class MetadataTableTest method testCRUD.

@Test
public void testCRUD() throws Exception {
    try (MetadataTable table = createMetadataTable()) {
        TopicId topicId = NamespaceId.DEFAULT.topic("topic");
        // Update a non-existing topic should fail.
        try {
            table.updateTopic(new TopicMetadata(topicId, "ttl", 10));
            Assert.fail("Expected TopicNotFoundException");
        } catch (TopicNotFoundException e) {
        // Expected
        }
        // Create a topic and validate
        table.createTopic(new TopicMetadata(topicId, "ttl", 10));
        Assert.assertEquals(10, table.getMetadata(topicId).getTTL());
        // Update the property and validate
        table.updateTopic(new TopicMetadata(topicId, "ttl", 30));
        Assert.assertEquals(30, table.getMetadata(topicId).getTTL());
        // Create the same topic again should fail
        try {
            table.createTopic(new TopicMetadata(topicId, "ttl", 10));
            Assert.fail("Expected TopicAlreadyExistsException");
        } catch (TopicAlreadyExistsException e) {
        // Expected
        }
        // It shouldn't affect the topic at all if creation failed
        Assert.assertEquals(30, table.getMetadata(topicId).getTTL());
        // Delete the topic
        table.deleteTopic(topicId);
        try {
            table.getMetadata(topicId);
            Assert.fail("Expected TopicNotFoundException");
        } catch (TopicNotFoundException e) {
        // Expected
        }
        // Delete again should raise a TopicNotFoundException
        try {
            table.deleteTopic(topicId);
            Assert.fail("Expected TopicNotFoundException");
        } catch (TopicNotFoundException e) {
        // Expected
        }
    }
}
Also used : TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) TopicId(co.cask.cdap.proto.id.TopicId) TopicAlreadyExistsException(co.cask.cdap.api.messaging.TopicAlreadyExistsException) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Example 20 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class MetadataTableTest method testBasic.

@Test
public void testBasic() throws Exception {
    try (MetadataTable table = createMetadataTable()) {
        Assert.assertTrue(table.listTopics().isEmpty());
        Assert.assertTrue(table.listTopics(NamespaceId.DEFAULT).isEmpty());
        try {
            table.getMetadata(NamespaceId.DEFAULT.topic("t1"));
            Assert.fail("Expected exception on topic that doesn't exist");
        } catch (TopicNotFoundException e) {
        // expected
        }
        // Create topic default:t1
        table.createTopic(new TopicMetadata(NamespaceId.DEFAULT.topic("t1"), "ttl", 10));
        ;
        Assert.assertEquals(1, table.listTopics(NamespaceId.DEFAULT).size());
        // Create topic default:t2
        TopicMetadata topicMetadata = new TopicMetadata(NamespaceId.DEFAULT.topic("t2"), "ttl", 20);
        table.createTopic(topicMetadata);
        Assert.assertEquals(topicMetadata.getTopicId(), table.getMetadata(NamespaceId.DEFAULT.topic("t2")).getTopicId());
        Assert.assertEquals(topicMetadata.getTTL(), table.getMetadata(NamespaceId.DEFAULT.topic("t2")).getTTL());
        Assert.assertEquals(1, table.getMetadata(NamespaceId.DEFAULT.topic("t2")).getGeneration());
        // Create topic system:t3
        table.createTopic(new TopicMetadata(NamespaceId.SYSTEM.topic("t3"), "ttl", 30));
        // List default namespace, should get 2
        Assert.assertEquals(2, table.listTopics(NamespaceId.DEFAULT).size());
        // List all topics, should get 3
        Assert.assertEquals(3, table.listTopics().size());
        // Delete t1
        table.deleteTopic(NamespaceId.DEFAULT.topic("t1"));
        Assert.assertEquals(1, table.listTopics(NamespaceId.DEFAULT).size());
        Assert.assertEquals(2, table.listTopics().size());
        // Delete t2
        table.deleteTopic(NamespaceId.DEFAULT.topic("t2"));
        Assert.assertTrue(table.listTopics(NamespaceId.DEFAULT).isEmpty());
        Assert.assertEquals(1, table.listTopics(NamespaceId.SYSTEM).size());
        // Delete t3
        table.deleteTopic(NamespaceId.SYSTEM.topic("t3"));
        Assert.assertTrue(table.listTopics(NamespaceId.DEFAULT).isEmpty());
        Assert.assertTrue(table.listTopics(NamespaceId.SYSTEM).isEmpty());
        Assert.assertTrue(table.listTopics().isEmpty());
    }
}
Also used : TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Aggregations

TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)20 TopicMetadata (co.cask.cdap.messaging.TopicMetadata)12 TopicId (co.cask.cdap.proto.id.TopicId)8 IOException (java.io.IOException)7 Test (org.junit.Test)7 HttpRequest (co.cask.common.http.HttpRequest)5 HttpResponse (co.cask.common.http.HttpResponse)5 TopicAlreadyExistsException (co.cask.cdap.api.messaging.TopicAlreadyExistsException)3 DBException (org.iq80.leveldb.DBException)3 CloseableIterator (co.cask.cdap.api.dataset.lib.CloseableIterator)2 Message (co.cask.cdap.api.messaging.Message)2 MessageFetcher (co.cask.cdap.api.messaging.MessageFetcher)2 MessagePublisher (co.cask.cdap.api.messaging.MessagePublisher)2 MessagingAdmin (co.cask.cdap.api.messaging.MessagingAdmin)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2 ApplicationManager (co.cask.cdap.test.ApplicationManager)2 TreeMap (java.util.TreeMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 PartitionKey (co.cask.cdap.api.dataset.lib.PartitionKey)1