use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.
the class DefaultAuditPublisher method publish.
@Override
public void publish(EntityId entityId, AuditType auditType, AuditPayload auditPayload) {
String userId = Objects.firstNonNull(SecurityRequestContext.getUserId(), "");
AuditMessage auditMessage = new AuditMessage(System.currentTimeMillis(), entityId, userId, auditType, auditPayload);
LOG.trace("Publishing audit message {}", auditMessage);
try {
MessagingServices.publishWithRetry(messagingService, auditTopic, retryStrategy, GSON.toJson(auditMessage).getBytes(StandardCharsets.UTF_8));
} catch (TopicNotFoundException e) {
LOG.error("Missing topic for audit publish: {}", auditTopic);
} catch (Exception e) {
LOG.error("Got exception publishing audit message {}. Exception:", auditMessage, e);
}
}
use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.
the class MessagingAppTestRun method testWithWorker.
@Test
public void testWithWorker() throws Exception {
ApplicationManager appManager = deployWithArtifact(NAMESPACE, MessagingApp.class, artifactJar);
final WorkerManager workerManager = appManager.getWorkerManager(MessagingApp.MessagingWorker.class.getSimpleName()).start();
MessagingContext messagingContext = getMessagingContext();
final MessagingAdmin messagingAdmin = getMessagingAdmin(NAMESPACE);
// Wait for the worker to create the topic
Tasks.waitFor(true, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
try {
messagingAdmin.getTopicProperties(MessagingApp.TOPIC);
return true;
} catch (TopicNotFoundException e) {
return false;
}
}
}, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
// Publish a message
String message = "message";
MessagePublisher messagePublisher = messagingContext.getMessagePublisher();
messagePublisher.publish(NAMESPACE.getNamespace(), MessagingApp.TOPIC, message);
// The worker will publish back a message with payload as concat(message, message)
final MessageFetcher messageFetcher = messagingContext.getMessageFetcher();
Tasks.waitFor(message + message, new Callable<String>() {
@Override
public String call() throws Exception {
try (CloseableIterator<Message> iterator = messageFetcher.fetch(NAMESPACE.getNamespace(), MessagingApp.TOPIC, Integer.MAX_VALUE, 0L)) {
Message message = Iterators.getLast(iterator, null);
return message == null ? null : message.getPayloadAsString();
}
}
}, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
// Publish concat(message + message) to the app
messagePublisher.publish(NAMESPACE.getNamespace(), MessagingApp.TOPIC, message + message);
// timeout.
try {
Tasks.waitFor(message + message + message + message, new Callable<String>() {
@Override
public String call() throws Exception {
try (CloseableIterator<Message> iterator = messageFetcher.fetch(NAMESPACE.getNamespace(), MessagingApp.TOPIC, Integer.MAX_VALUE, 0L)) {
Message message = Iterators.getLast(iterator, null);
return message == null ? null : message.getPayloadAsString();
}
}
}, 2, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
Assert.fail("Expected timeout exception");
} catch (TimeoutException e) {
// expected
}
// Now publish a message to the control topic, to unblock the transaction block.
messagePublisher.publish(NAMESPACE.getNamespace(), MessagingApp.CONTROL_TOPIC, message);
// Should expect a new message as concat(message, message, message, message)
Tasks.waitFor(message + message + message + message, new Callable<String>() {
@Override
public String call() throws Exception {
try (CloseableIterator<Message> iterator = messageFetcher.fetch(NAMESPACE.getNamespace(), MessagingApp.TOPIC, Integer.MAX_VALUE, 0L)) {
Message message = Iterators.getLast(iterator, null);
return message == null ? null : message.getPayloadAsString();
}
}
}, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
// Wait for the worker to finish and verify that it completes successfully.
workerManager.waitForRun(ProgramRunStatus.COMPLETED, 5, TimeUnit.SECONDS);
}
use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.
the class CoreMessagingService method storePayload.
@Override
public void storePayload(StoreRequest request) throws TopicNotFoundException, IOException {
try {
TopicMetadata metadata = topicCache.get(request.getTopicId());
payloadTableWriterCache.get(request.getTopicId()).persist(request, metadata);
} catch (ExecutionException e) {
Throwable cause = Objects.firstNonNull(e.getCause(), e);
Throwables.propagateIfPossible(cause, TopicNotFoundException.class, IOException.class);
throw Throwables.propagate(e);
}
}
use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.
the class HBaseMetadataTable method getMetadata.
@Override
public TopicMetadata getMetadata(TopicId topicId) throws IOException, TopicNotFoundException {
Get get = tableUtil.buildGet(MessagingUtils.toMetadataRowKey(topicId)).addFamily(columnFamily).build();
try {
Result result = hTable.get(get);
byte[] value = result.getValue(columnFamily, COL);
if (value == null) {
throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
}
Map<String, String> properties = GSON.fromJson(Bytes.toString(value), MAP_TYPE);
TopicMetadata topicMetadata = new TopicMetadata(topicId, properties);
if (!topicMetadata.exists()) {
throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
}
return topicMetadata;
} catch (IOException e) {
throw exceptionHandler.handle(e);
}
}
use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.
the class ClientMessagingService method performWriteRequest.
/**
* Makes a request to the server for writing to the messaging system
*
* @param request contains information about what to write
* @param publish {@code true} to make publish call, {@code false} to make store call.
* @return the response from the server
* @throws IOException if failed to perform the write operation
* @throws TopicNotFoundException if the topic to write to does not exist
*/
private HttpResponse performWriteRequest(StoreRequest request, boolean publish) throws IOException, TopicNotFoundException {
GenericRecord record = new GenericData.Record(Schemas.V1.PublishRequest.SCHEMA);
if (request.isTransactional()) {
record.put("transactionWritePointer", request.getTransactionWritePointer());
}
record.put("messages", convertPayloads(request));
// Encode the request as avro
ExposedByteArrayOutputStream os = new ExposedByteArrayOutputStream();
Encoder encoder = EncoderFactory.get().directBinaryEncoder(os, null);
DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(Schemas.V1.PublishRequest.SCHEMA);
datumWriter.write(record, encoder);
// Make the publish request
String writeType = publish ? "publish" : "store";
TopicId topicId = request.getTopicId();
HttpRequest httpRequest = remoteClient.requestBuilder(HttpMethod.POST, createTopicPath(topicId) + "/" + writeType).addHeader(HttpHeaders.CONTENT_TYPE, "avro/binary").withBody(os.toByteBuffer()).build();
HttpResponse response = remoteClient.execute(httpRequest);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
}
handleError(response, "Failed to " + writeType + " message to topic " + topicId);
return response;
}
Aggregations