Search in sources :

Example 51 with TopicId

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

the class ClientMessagingService method listTopics.

@Override
public List<TopicId> listTopics(NamespaceId namespaceId) throws IOException {
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.GET, namespaceId.getNamespace() + "/topics").build();
    HttpResponse response = remoteClient.execute(request);
    handleError(response, "Failed to list topics in namespace " + namespaceId);
    List<String> topics = GSON.fromJson(response.getResponseBodyAsString(), TOPIC_LIST_TYPE);
    List<TopicId> result = new ArrayList<>(topics.size());
    for (String topic : topics) {
        result.add(namespaceId.topic(topic));
    }
    return Collections.unmodifiableList(result);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) ArrayList(java.util.ArrayList) HttpResponse(co.cask.common.http.HttpResponse) TopicId(co.cask.cdap.proto.id.TopicId)

Example 52 with TopicId

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

the class StoreHandler method rollback.

@POST
@Path("/rollback")
public void rollback(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
    TopicId topicId = new NamespaceId(namespace).topic(topic);
    Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ByteBufInputStream(request.content()), null);
    DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.PublishResponse.SCHEMA);
    messagingService.rollback(topicId, new GenericRecordRollbackDetail(datumReader.read(null, decoder)));
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) Decoder(org.apache.avro.io.Decoder) GenericRecord(org.apache.avro.generic.GenericRecord) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 53 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(FullHttpRequest 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 54 with TopicId

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

the class StoreHandler method publish.

@POST
@Path("/publish")
public void publish(FullHttpRequest 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);
    // Empty payload is only allowed for transactional publish
    if (!storeRequest.isTransactional() && !storeRequest.hasNext()) {
        throw new BadRequestException("Empty payload is only allowed for publishing transactional message. Topic: " + topicId);
    }
    // Publish the message and response with the rollback information
    RollbackDetail rollbackInfo = messagingService.publish(storeRequest);
    if (rollbackInfo == null) {
        // Non-tx publish doesn't have rollback info.
        responder.sendStatus(HttpResponseStatus.OK);
        return;
    }
    ByteBuf response = encodeRollbackDetail(rollbackInfo);
    responder.sendContent(HttpResponseStatus.OK, response, new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "avro/binary"));
}
Also used : RollbackDetail(co.cask.cdap.messaging.RollbackDetail) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) 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) ByteBuf(io.netty.buffer.ByteBuf) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 55 with TopicId

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

the class FetchHandler method poll.

@POST
@Path("poll")
public void poll(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
    TopicId topicId = new NamespaceId(namespace).topic(topic);
    // Currently only support avro
    if (!"avro/binary".equals(request.headers().get(HttpHeaderNames.CONTENT_TYPE))) {
        throw new BadRequestException("Only avro/binary content type is supported.");
    }
    // Decode the poll request
    Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ByteBufInputStream(request.content()), null);
    DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.ConsumeRequest.SCHEMA);
    // Fetch the messages
    CloseableIterator<RawMessage> iterator = fetchMessages(datumReader.read(null, decoder), topicId);
    try {
        responder.sendContent(HttpResponseStatus.OK, new MessagesBodyProducer(iterator, messageChunkSize), new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "avro/binary"));
    } catch (Throwable t) {
        iterator.close();
        throw t;
    }
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) Decoder(org.apache.avro.io.Decoder) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) BadRequestException(co.cask.cdap.common.BadRequestException) TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) GenericRecord(org.apache.avro.generic.GenericRecord) RawMessage(co.cask.cdap.messaging.data.RawMessage) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

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