use of co.cask.cdap.api.messaging.TopicAlreadyExistsException 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());
}
use of co.cask.cdap.api.messaging.TopicAlreadyExistsException in project cdap by caskdata.
the class LevelDBMetadataTable method createTopic.
@Override
public void createTopic(TopicMetadata topicMetadata) throws TopicAlreadyExistsException, IOException {
try {
TopicId topicId = topicMetadata.getTopicId();
byte[] key = MessagingUtils.toMetadataRowKey(topicId);
TreeMap<String, String> properties = new TreeMap<>(topicMetadata.getProperties());
properties.put(TopicMetadata.GENERATION_KEY, MessagingUtils.Constants.DEFAULT_GENERATION);
synchronized (this) {
byte[] tableValue = levelDB.get(key);
if (tableValue != null) {
Map<String, String> oldProperties = GSON.fromJson(Bytes.toString(tableValue), MAP_TYPE);
TopicMetadata metadata = new TopicMetadata(topicId, oldProperties);
if (metadata.exists()) {
throw new TopicAlreadyExistsException(topicId.getNamespace(), topicId.getTopic());
}
int newGenerationId = (metadata.getGeneration() * -1) + 1;
properties.put(TopicMetadata.GENERATION_KEY, Integer.toString(newGenerationId));
}
byte[] value = Bytes.toBytes(GSON.toJson(properties, MAP_TYPE));
levelDB.put(key, value, WRITE_OPTIONS);
}
} catch (DBException e) {
throw new IOException(e);
}
}
use of co.cask.cdap.api.messaging.TopicAlreadyExistsException in project cdap by caskdata.
the class HBaseMetadataTable method createTopic.
@Override
public void createTopic(TopicMetadata topicMetadata) throws TopicAlreadyExistsException, IOException {
TopicId topicId = topicMetadata.getTopicId();
byte[] rowKey = MessagingUtils.toMetadataRowKey(topicId);
PutBuilder putBuilder = tableUtil.buildPut(rowKey);
Get get = tableUtil.buildGet(rowKey).addFamily(columnFamily).build();
try {
boolean completed = false;
while (!completed) {
Result result = hTable.get(get);
byte[] value = result.getValue(columnFamily, COL);
if (value == null) {
TreeMap<String, String> properties = new TreeMap<>(topicMetadata.getProperties());
properties.put(TopicMetadata.GENERATION_KEY, MessagingUtils.Constants.DEFAULT_GENERATION);
putBuilder.add(columnFamily, COL, Bytes.toBytes(GSON.toJson(properties, MAP_TYPE)));
completed = hTable.checkAndPut(rowKey, columnFamily, COL, null, putBuilder.build());
} else {
Map<String, String> properties = GSON.fromJson(Bytes.toString(value), MAP_TYPE);
TopicMetadata metadata = new TopicMetadata(topicId, properties);
if (metadata.exists()) {
throw new TopicAlreadyExistsException(topicId.getNamespace(), topicId.getTopic());
}
int newGenerationId = (metadata.getGeneration() * -1) + 1;
TreeMap<String, String> newProperties = new TreeMap<>(properties);
newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(newGenerationId));
putBuilder.add(columnFamily, COL, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE)));
completed = hTable.checkAndPut(rowKey, columnFamily, COL, value, putBuilder.build());
}
}
} catch (IOException e) {
throw exceptionHandler.handle(e);
}
}
use of co.cask.cdap.api.messaging.TopicAlreadyExistsException in project cdap by caskdata.
the class CoreMessagingService method createTopicIfNotExists.
/**
* Creates the given topic if it is not yet created.
*/
private void createTopicIfNotExists(TopicId topicId) throws IOException {
try {
createTopic(new TopicMetadata(topicId));
LOG.info("System topic created: {}", topicId);
} catch (TopicAlreadyExistsException e) {
// OK for the topic already created. Just log a debug as it happens on every restart.
LOG.debug("System topic already exists: {}", topicId);
}
}
use of co.cask.cdap.api.messaging.TopicAlreadyExistsException in project cdap by caskdata.
the class ClientMessagingService method createTopic.
@Override
public void createTopic(TopicMetadata topicMetadata) throws TopicAlreadyExistsException, IOException {
TopicId topicId = topicMetadata.getTopicId();
HttpRequest request = remoteClient.requestBuilder(HttpMethod.PUT, createTopicPath(topicId)).withBody(GSON.toJson(topicMetadata.getProperties())).build();
HttpResponse response = remoteClient.execute(request);
if (response.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
throw new TopicAlreadyExistsException(topicId.getNamespace(), topicId.getTopic());
}
handleError(response, "Failed to create topic " + topicId);
}
Aggregations