Search in sources :

Example 31 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(HttpRequest 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.getHeader(HttpHeaders.Names.CONTENT_TYPE))) {
        throw new BadRequestException("Only avro/binary content type is supported.");
    }
    // Decode the poll request
    Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ChannelBufferInputStream(request.getContent()), 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), ImmutableMultimap.of(HttpHeaders.Names.CONTENT_TYPE, "avro/binary"));
    } catch (Throwable t) {
        iterator.close();
        throw t;
    }
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) BadRequestException(co.cask.cdap.common.BadRequestException) TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) Decoder(org.apache.avro.io.Decoder) GenericRecord(org.apache.avro.generic.GenericRecord) RawMessage(co.cask.cdap.messaging.data.RawMessage) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 32 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(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);
    // 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;
    }
    ChannelBuffer response = encodeRollbackDetail(rollbackInfo);
    responder.sendContent(HttpResponseStatus.OK, response, "avro/binary", null);
}
Also used : RollbackDetail(co.cask.cdap.messaging.RollbackDetail) 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) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 33 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(HttpRequest 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 ChannelBufferInputStream(request.getContent()), 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) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) Decoder(org.apache.avro.io.Decoder) GenericRecord(org.apache.avro.generic.GenericRecord) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 34 with TopicId

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

the class DefaultMessageTableCacheProvider method getMessageCache.

@Nullable
@Override
public MessageCache<MessageTable.Entry> getMessageCache(TopicId topicId) {
    if (!initialized) {
        synchronized (this) {
            if (!initialized) {
                Map<TopicId, MessageCache<MessageTable.Entry>> caches = new HashMap<>();
                long cacheSize = cConf.getInt(Constants.MessagingSystem.CACHE_SIZE_MB) * 1024 * 1024;
                Set<TopicId> systemTopics = MessagingServiceUtils.getSystemTopics(cConf, true);
                if (cacheSize > 0 && !systemTopics.isEmpty()) {
                    MessageTableEntryWeigher weigher = new MessageTableEntryWeigher();
                    MessageTableEntryComparator comparator = new MessageTableEntryComparator();
                    // Just evenly distributed the cache among all system topics.
                    // More sophisticated logic can be employed at runtime to monitor the metrics from MessageCache
                    // for each topic and adjust the soft/hard limit accordingly to maximize efficiency in
                    // memory usage and performance
                    long hardLimit = cacheSize / systemTopics.size();
                    if (hardLimit > 0) {
                        // Have reduce trigger as 70% of the hard limit and min retain as 50% of the hard limit
                        // In future, it can be adjusted dynamically based on metrics
                        MessageCache.Limits limits = new MessageCache.Limits(hardLimit / 2, hardLimit * 7 / 10, hardLimit);
                        for (TopicId topic : systemTopics) {
                            caches.put(topic, new MessageCache<>(comparator, weigher, limits, createMetricsContext(cConf, topic, metricsCollectionService)));
                        }
                    }
                }
                topicMessageCaches = caches;
                initialized = true;
            }
        }
    }
    return topicMessageCaches.get(topicId);
}
Also used : MessageCache(co.cask.cdap.messaging.cache.MessageCache) HashMap(java.util.HashMap) MessageTable(co.cask.cdap.messaging.store.MessageTable) TopicId(co.cask.cdap.proto.id.TopicId) Nullable(javax.annotation.Nullable)

Example 35 with TopicId

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

the class HBaseMetadataTable method scanTopics.

/**
 * Scans the HBase table to get a list of {@link TopicId}.
 */
private List<TopicId> scanTopics(ScanBuilder scanBuilder) throws IOException {
    Scan scan = scanBuilder.setFilter(new FirstKeyOnlyFilter()).setCaching(scanCacheRows).build();
    try {
        List<TopicId> topicIds = new ArrayList<>();
        try (ResultScanner resultScanner = hTable.getScanner(scan)) {
            for (Result result : resultScanner) {
                TopicId topicId = MessagingUtils.toTopicId(result.getRow());
                byte[] value = result.getValue(columnFamily, COL);
                Map<String, String> properties = GSON.fromJson(Bytes.toString(value), MAP_TYPE);
                TopicMetadata metadata = new TopicMetadata(topicId, properties);
                if (metadata.exists()) {
                    topicIds.add(topicId);
                }
            }
        }
        return topicIds;
    } catch (IOException e) {
        throw exceptionHandler.handle(e);
    }
}
Also used : ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) FirstKeyOnlyFilter(org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter) ArrayList(java.util.ArrayList) Scan(org.apache.hadoop.hbase.client.Scan) TopicId(co.cask.cdap.proto.id.TopicId) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

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