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);
}
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);
}
}
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
}
}
}
}
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
}
}
}
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());
}
}
Aggregations