Search in sources :

Example 1 with TopicAlreadyExistsException

use of io.cdap.cdap.api.messaging.TopicAlreadyExistsException 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(io.cdap.cdap.api.messaging.TopicNotFoundException) TopicId(io.cdap.cdap.proto.id.TopicId) TopicAlreadyExistsException(io.cdap.cdap.api.messaging.TopicAlreadyExistsException) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Example 2 with TopicAlreadyExistsException

use of io.cdap.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);
    }
}
Also used : DBException(org.iq80.leveldb.DBException) TopicId(io.cdap.cdap.proto.id.TopicId) IOException(java.io.IOException) TreeMap(java.util.TreeMap) TopicAlreadyExistsException(io.cdap.cdap.api.messaging.TopicAlreadyExistsException) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata)

Example 3 with TopicAlreadyExistsException

use of io.cdap.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());
}
Also used : TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) TopicId(io.cdap.cdap.proto.id.TopicId) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) TopicAlreadyExistsException(io.cdap.cdap.api.messaging.TopicAlreadyExistsException) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Example 4 with TopicAlreadyExistsException

use of io.cdap.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 = table.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 = table.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 = table.checkAndPut(rowKey, columnFamily, COL, value, putBuilder.build());
            }
        }
    } catch (IOException e) {
        throw exceptionHandler.handle(e);
    }
}
Also used : IOException(java.io.IOException) TreeMap(java.util.TreeMap) TopicAlreadyExistsException(io.cdap.cdap.api.messaging.TopicAlreadyExistsException) Result(org.apache.hadoop.hbase.client.Result) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata) PutBuilder(io.cdap.cdap.data2.util.hbase.PutBuilder) Get(org.apache.hadoop.hbase.client.Get) TopicId(io.cdap.cdap.proto.id.TopicId)

Example 5 with TopicAlreadyExistsException

use of io.cdap.cdap.api.messaging.TopicAlreadyExistsException in project cdap by caskdata.

the class TetheringServerHandler method createTethering.

/**
 * Creates a tethering with a client.
 */
@PUT
@Path("/tethering/connections/{peer}")
public void createTethering(FullHttpRequest request, HttpResponder responder, @PathParam("peer") String peer) throws NotImplementedException, IOException {
    checkTetheringServerEnabled();
    contextAccessEnforcer.enforce(InstanceId.SELF, InstancePermission.TETHER);
    String content = request.content().toString(StandardCharsets.UTF_8);
    TetheringConnectionRequest tetherRequest = GSON.fromJson(content, TetheringConnectionRequest.class);
    TopicId topicId = new TopicId(NamespaceId.SYSTEM.getNamespace(), topicPrefix + peer);
    try {
        messagingService.createTopic(new TopicMetadata(topicId, Collections.emptyMap()));
    } catch (TopicAlreadyExistsException e) {
        LOG.warn("Topic {} already exists", topicId);
    } catch (IOException e) {
        LOG.error("Failed to create topic {}", topicId, e);
        throw e;
    }
    // We don't need to keep track of the client metadata on the server side.
    PeerMetadata peerMetadata = new PeerMetadata(tetherRequest.getNamespaceAllocations(), Collections.emptyMap(), tetherRequest.getDescription());
    // We don't store the peer endpoint on the server side because the connection is initiated by the client.
    PeerInfo peerInfo = new PeerInfo(peer, null, TetheringStatus.PENDING, peerMetadata, tetherRequest.getRequestTime());
    try {
        store.addPeer(peerInfo);
    } catch (PeerAlreadyExistsException pae) {
        // Peer is already configured, treat this as a no-op.
        responder.sendStatus(HttpResponseStatus.OK);
        return;
    } catch (Exception e) {
        try {
            messagingService.deleteTopic(topicId);
        } catch (Exception ex) {
            e.addSuppressed(ex);
        }
        throw new IOException("Failed to create tethering with peer " + peer, e);
    }
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : TopicId(io.cdap.cdap.proto.id.TopicId) IOException(java.io.IOException) TopicAlreadyExistsException(io.cdap.cdap.api.messaging.TopicAlreadyExistsException) ForbiddenException(io.cdap.cdap.common.ForbiddenException) TopicAlreadyExistsException(io.cdap.cdap.api.messaging.TopicAlreadyExistsException) IOException(java.io.IOException) BadRequestException(io.cdap.cdap.common.BadRequestException) NotImplementedException(io.cdap.cdap.common.NotImplementedException) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Aggregations

TopicAlreadyExistsException (io.cdap.cdap.api.messaging.TopicAlreadyExistsException)7 TopicMetadata (io.cdap.cdap.messaging.TopicMetadata)6 TopicId (io.cdap.cdap.proto.id.TopicId)6 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)3 IOException (java.io.IOException)3 TreeMap (java.util.TreeMap)2 Test (org.junit.Test)2 BadRequestException (io.cdap.cdap.common.BadRequestException)1 ForbiddenException (io.cdap.cdap.common.ForbiddenException)1 NotImplementedException (io.cdap.cdap.common.NotImplementedException)1 PutBuilder (io.cdap.cdap.data2.util.hbase.PutBuilder)1 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)1 HttpRequest (io.cdap.common.http.HttpRequest)1 HttpResponse (io.cdap.common.http.HttpResponse)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 Get (org.apache.hadoop.hbase.client.Get)1 Result (org.apache.hadoop.hbase.client.Result)1 DBException (org.iq80.leveldb.DBException)1