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;
}
}
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);
}
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);
}
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);
}
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);
}
}
Aggregations