Search in sources :

Example 6 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class LevelDBMetadataTable method updateTopic.

@Override
public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException {
    try {
        TopicId topicId = topicMetadata.getTopicId();
        byte[] key = MessagingUtils.toMetadataRowKey(topicId);
        synchronized (this) {
            byte[] tableValue = levelDB.get(key);
            if (tableValue == null) {
                throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
            }
            Map<String, String> oldProperties = GSON.fromJson(Bytes.toString(tableValue), MAP_TYPE);
            TopicMetadata oldMetadata = new TopicMetadata(topicId, oldProperties);
            if (!oldMetadata.exists()) {
                throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
            }
            TreeMap<String, String> newProperties = new TreeMap<>(topicMetadata.getProperties());
            newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(oldMetadata.getGeneration()));
            levelDB.put(key, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE)), WRITE_OPTIONS);
        }
    } catch (DBException e) {
        throw new IOException(e);
    }
}
Also used : DBException(org.iq80.leveldb.DBException) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) TopicId(co.cask.cdap.proto.id.TopicId) IOException(java.io.IOException) TreeMap(java.util.TreeMap) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

Example 7 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException 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);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) HttpResponse(co.cask.common.http.HttpResponse) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

Example 8 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class ClientMessagingService method rollback.

@Override
public void rollback(TopicId topicId, RollbackDetail rollbackDetail) throws TopicNotFoundException, IOException {
    ByteBuffer requestBody = (rollbackDetail instanceof ClientRollbackDetail) ? ByteBuffer.wrap(((ClientRollbackDetail) rollbackDetail).getEncoded()) : encodeRollbackDetail(rollbackDetail);
    HttpRequest httpRequest = remoteClient.requestBuilder(HttpMethod.POST, createTopicPath(topicId) + "/rollback").addHeader(HttpHeaders.CONTENT_TYPE, "avro/binary").withBody(requestBody).build();
    HttpResponse response = remoteClient.execute(httpRequest);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
    }
    handleError(response, "Failed to rollback message in topic " + topicId + " with rollback detail " + rollbackDetail);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) HttpResponse(co.cask.common.http.HttpResponse) ByteBuffer(java.nio.ByteBuffer)

Example 9 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class ClientMessagingService method updateTopic.

@Override
public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException {
    TopicId topicId = topicMetadata.getTopicId();
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.PUT, createTopicPath(topicId) + "/properties").withBody(GSON.toJson(topicMetadata.getProperties())).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);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) HttpResponse(co.cask.common.http.HttpResponse) TopicId(co.cask.cdap.proto.id.TopicId)

Example 10 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class CoreMessagingService method rollback.

@Override
public void rollback(TopicId topicId, RollbackDetail rollbackDetail) throws TopicNotFoundException, IOException {
    TopicMetadata metadata = getTopic(topicId);
    Exception failure = null;
    try (MessageTable messageTable = createMessageTable(metadata)) {
        messageTable.rollback(metadata, rollbackDetail);
    } catch (Exception e) {
        failure = e;
    }
    // Throw if there is any failure in rollback.
    if (failure != null) {
        Throwables.propagateIfPossible(failure, TopicNotFoundException.class, IOException.class);
        throw Throwables.propagate(failure);
    }
}
Also used : MessageTable(co.cask.cdap.messaging.store.MessageTable) TopicAlreadyExistsException(co.cask.cdap.api.messaging.TopicAlreadyExistsException) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

Aggregations

TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)20 TopicMetadata (co.cask.cdap.messaging.TopicMetadata)12 TopicId (co.cask.cdap.proto.id.TopicId)8 IOException (java.io.IOException)7 Test (org.junit.Test)7 HttpRequest (co.cask.common.http.HttpRequest)5 HttpResponse (co.cask.common.http.HttpResponse)5 TopicAlreadyExistsException (co.cask.cdap.api.messaging.TopicAlreadyExistsException)3 DBException (org.iq80.leveldb.DBException)3 CloseableIterator (co.cask.cdap.api.dataset.lib.CloseableIterator)2 Message (co.cask.cdap.api.messaging.Message)2 MessageFetcher (co.cask.cdap.api.messaging.MessageFetcher)2 MessagePublisher (co.cask.cdap.api.messaging.MessagePublisher)2 MessagingAdmin (co.cask.cdap.api.messaging.MessagingAdmin)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2 ApplicationManager (co.cask.cdap.test.ApplicationManager)2 TreeMap (java.util.TreeMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 PartitionKey (co.cask.cdap.api.dataset.lib.PartitionKey)1