use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class DataCleanupTest method testOldGenCleanup.
@Test
public void testOldGenCleanup() throws Exception {
try (MetadataTable metadataTable = getMetadataTable();
MessageTable messageTable = getMessageTable();
PayloadTable payloadTable = getPayloadTable()) {
int txWritePtr = 100;
TopicId topicId = NamespaceId.DEFAULT.topic("oldGenCleanup");
TopicMetadata topic = new TopicMetadata(topicId, TopicMetadata.TTL_KEY, "100000", TopicMetadata.GENERATION_KEY, Integer.toString(GENERATION));
metadataTable.createTopic(topic);
List<MessageTable.Entry> entries = new ArrayList<>();
List<PayloadTable.Entry> pentries = new ArrayList<>();
byte[] messageId = new byte[MessageId.RAW_ID_SIZE];
MessageId.putRawId(0L, (short) 0, 0L, (short) 0, messageId, 0);
entries.add(new TestMessageEntry(topicId, GENERATION, "data", txWritePtr, (short) 0));
pentries.add(new TestPayloadEntry(topicId, GENERATION, "data", txWritePtr, (short) 0));
messageTable.store(entries.iterator());
payloadTable.store(pentries.iterator());
// Fetch the entries and make sure we are able to read it
try (CloseableIterator<MessageTable.Entry> iterator = messageTable.fetch(topic, 0, Integer.MAX_VALUE, null)) {
checkMessageEntry(iterator, txWritePtr);
}
try (CloseableIterator<PayloadTable.Entry> iterator = payloadTable.fetch(topic, txWritePtr, new MessageId(messageId), true, 100)) {
checkPayloadEntry(iterator, txWritePtr);
}
// Now run full compaction
forceFlushAndCompact(Table.MESSAGE);
forceFlushAndCompact(Table.PAYLOAD);
// Fetch the entries and make sure we are able to read it
try (CloseableIterator<MessageTable.Entry> iterator = messageTable.fetch(topic, 0, Integer.MAX_VALUE, null)) {
checkMessageEntry(iterator, txWritePtr);
}
try (CloseableIterator<PayloadTable.Entry> iterator = payloadTable.fetch(topic, txWritePtr, new MessageId(messageId), true, 100)) {
checkPayloadEntry(iterator, txWritePtr);
}
metadataTable.deleteTopic(topicId);
// Sleep so that the metadata cache in coprocessor expires
TimeUnit.SECONDS.sleep(3 * METADATA_CACHE_EXPIRY);
forceFlushAndCompact(Table.MESSAGE);
forceFlushAndCompact(Table.PAYLOAD);
try (CloseableIterator<MessageTable.Entry> iterator = messageTable.fetch(topic, 0, Integer.MAX_VALUE, null)) {
Assert.assertFalse(iterator.hasNext());
}
try (CloseableIterator<PayloadTable.Entry> iterator = payloadTable.fetch(topic, txWritePtr, new MessageId(messageId), true, 100)) {
Assert.assertFalse(iterator.hasNext());
}
}
}
use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class MessageTableTest method testSingleMessage.
@Test
public void testSingleMessage() throws Exception {
TopicId topicId = NamespaceId.DEFAULT.topic("singleMessage");
TopicMetadata metadata = new TopicMetadata(topicId, DEFAULT_PROPERTY);
String payload = "data";
long txWritePtr = 123L;
try (MessageTable table = getMessageTable();
MetadataTable metadataTable = getMetadataTable()) {
metadataTable.createTopic(metadata);
List<MessageTable.Entry> entryList = new ArrayList<>();
entryList.add(new TestMessageEntry(topicId, GENERATION, 0L, 0, txWritePtr, Bytes.toBytes(payload)));
table.store(entryList.iterator());
byte[] messageId = new byte[MessageId.RAW_ID_SIZE];
MessageId.putRawId(0L, (short) 0, 0L, (short) 0, messageId, 0);
try (CloseableIterator<MessageTable.Entry> iterator = table.fetch(metadata, new MessageId(messageId), false, 50, null)) {
// Fetch not including the first message, expect empty
Assert.assertFalse(iterator.hasNext());
}
try (CloseableIterator<MessageTable.Entry> iterator = table.fetch(metadata, new MessageId(messageId), true, 50, null)) {
// Fetch including the first message, should get the message
Assert.assertTrue(iterator.hasNext());
MessageTable.Entry entry = iterator.next();
Assert.assertArrayEquals(Bytes.toBytes(payload), entry.getPayload());
Assert.assertFalse(iterator.hasNext());
}
try (CloseableIterator<MessageTable.Entry> iterator = table.fetch(metadata, 0, 50, null)) {
// Fetch by time, should get the entry
MessageTable.Entry entry = iterator.next();
Assert.assertArrayEquals(Bytes.toBytes(payload), entry.getPayload());
Assert.assertFalse(iterator.hasNext());
}
RollbackDetail rollbackDetail = new TestRollbackDetail(123L, 0, (short) 0, 0L, (short) 0);
table.rollback(metadata, rollbackDetail);
try (CloseableIterator<MessageTable.Entry> iterator = table.fetch(metadata, new MessageId(messageId), true, 50, null)) {
// Fetching the message non-tx should provide a result even after deletion
MessageTable.Entry entry = iterator.next();
Assert.assertArrayEquals(Bytes.toBytes(payload), entry.getPayload());
Assert.assertFalse(iterator.hasNext());
}
Transaction tx = new Transaction(200, 200, new long[0], new long[0], -1);
try (CloseableIterator<MessageTable.Entry> iterator = table.fetch(metadata, new MessageId(messageId), true, 50, tx)) {
// Fetching messages transactionally should not return any entry
Assert.assertFalse(iterator.hasNext());
}
}
}
use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class ClientMessagingService method getTopic.
@Override
public TopicMetadata getTopic(TopicId topicId) throws TopicNotFoundException, IOException {
HttpRequest request = remoteClient.requestBuilder(HttpMethod.GET, createTopicPath(topicId)).build();
HttpResponse response = remoteClient.execute(request);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
}
handleError(response, "Failed to update topic " + topicId);
Map<String, String> properties = GSON.fromJson(response.getResponseBodyAsString(), TOPIC_PROPERTY_TYPE);
return new TopicMetadata(topicId, properties);
}
use of co.cask.cdap.messaging.TopicMetadata 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);
}
use of co.cask.cdap.messaging.TopicMetadata 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);
}
Aggregations