Search in sources :

Example 1 with TopicId

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

the class MetadataHandler method updateTopic.

@PUT
@Path("/topics/{topic}/properties")
public void updateTopic(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
    TopicId topicId = new NamespaceId(namespace).topic(topic);
    messagingService.updateTopic(new TopicMetadata(topicId, decodeTopicProperties(request.getContent())));
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 2 with TopicId

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

the class MetadataHandler method createTopic.

@PUT
@Path("/topics/{topic}")
public void createTopic(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
    TopicId topicId = new NamespaceId(namespace).topic(topic);
    messagingService.createTopic(new TopicMetadata(topicId, decodeTopicProperties(request.getContent())));
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 3 with TopicId

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

the class StoreHandler method store.

@POST
@Path("/store")
public void store(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
    TopicId topicId = new NamespaceId(namespace).topic(topic);
    StoreRequest storeRequest = createStoreRequest(topicId, request);
    // It must be transactional with payload for store request
    if (!storeRequest.isTransactional() || !storeRequest.hasNext()) {
        throw new BadRequestException("Store request must be transactional with payload. Topic: " + topicId);
    }
    messagingService.storePayload(storeRequest);
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : StoreRequest(co.cask.cdap.messaging.StoreRequest) BadRequestException(co.cask.cdap.common.BadRequestException) TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 4 with TopicId

use of co.cask.cdap.proto.id.TopicId 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);
    }
}
Also used : IOException(java.io.IOException) TreeMap(java.util.TreeMap) TopicAlreadyExistsException(co.cask.cdap.api.messaging.TopicAlreadyExistsException) Result(org.apache.hadoop.hbase.client.Result) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) PutBuilder(co.cask.cdap.data2.util.hbase.PutBuilder) Get(org.apache.hadoop.hbase.client.Get) TopicId(co.cask.cdap.proto.id.TopicId)

Example 5 with TopicId

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

the class MessagingUtilsTest method testSystemTopics.

@Test
public void testSystemTopics() {
    CConfiguration cConf = CConfiguration.create();
    cConf.set(Constants.MessagingSystem.SYSTEM_TOPICS, "  topic-1, topic_2 ,prefix:10,invalid.name");
    Set<TopicId> topics = MessagingServiceUtils.getSystemTopics(cConf, true);
    Set<TopicId> expected = new LinkedHashSet<>();
    expected.add(NamespaceId.SYSTEM.topic("topic-1"));
    expected.add(NamespaceId.SYSTEM.topic("topic_2"));
    for (int i = 0; i < 10; i++) {
        expected.add(NamespaceId.SYSTEM.topic("prefix" + i));
    }
    Assert.assertEquals(expected, topics);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TopicId(co.cask.cdap.proto.id.TopicId) CConfiguration(co.cask.cdap.common.conf.CConfiguration) Test(org.junit.Test)

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