Search in sources :

Example 11 with TopicMetadata

use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.

the class LevelDBMetadataTable method getMetadata.

@Override
public TopicMetadata getMetadata(TopicId topicId) throws IOException, TopicNotFoundException {
    try {
        byte[] value = levelDB.get(MessagingUtils.toMetadataRowKey(topicId));
        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 (DBException e) {
        // DBException is a RuntimeException. Turn it to IOException so that it forces caller to handle it.
        throw new IOException(e);
    }
}
Also used : DBException(org.iq80.leveldb.DBException) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) IOException(java.io.IOException) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

Example 12 with TopicMetadata

use of co.cask.cdap.messaging.TopicMetadata 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 13 with TopicMetadata

use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.

the class HBaseMetadataTable method createTopic.

@Override
public void createTopic(TopicMetadata topicMetadata) throws TopicAlreadyExistsException, IOException {
    TopicId topicId = topicMetadata.getTopicId();
    byte[] rowKey = MessagingUtils.toMetadataRowKey(topicId);
    PutBuilder putBuilder = tableUtil.buildPut(rowKey);
    Get get = tableUtil.buildGet(rowKey).addFamily(columnFamily).build();
    try {
        boolean completed = false;
        while (!completed) {
            Result result = hTable.get(get);
            byte[] value = result.getValue(columnFamily, COL);
            if (value == null) {
                TreeMap<String, String> properties = new TreeMap<>(topicMetadata.getProperties());
                properties.put(TopicMetadata.GENERATION_KEY, MessagingUtils.Constants.DEFAULT_GENERATION);
                putBuilder.add(columnFamily, COL, Bytes.toBytes(GSON.toJson(properties, MAP_TYPE)));
                completed = hTable.checkAndPut(rowKey, columnFamily, COL, null, putBuilder.build());
            } else {
                Map<String, String> properties = GSON.fromJson(Bytes.toString(value), MAP_TYPE);
                TopicMetadata metadata = new TopicMetadata(topicId, properties);
                if (metadata.exists()) {
                    throw new TopicAlreadyExistsException(topicId.getNamespace(), topicId.getTopic());
                }
                int newGenerationId = (metadata.getGeneration() * -1) + 1;
                TreeMap<String, String> newProperties = new TreeMap<>(properties);
                newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(newGenerationId));
                putBuilder.add(columnFamily, COL, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE)));
                completed = hTable.checkAndPut(rowKey, columnFamily, COL, value, putBuilder.build());
            }
        }
    } catch (IOException e) {
        throw exceptionHandler.handle(e);
    }
}
Also used : IOException(java.io.IOException) TreeMap(java.util.TreeMap) TopicAlreadyExistsException(co.cask.cdap.api.messaging.TopicAlreadyExistsException) Result(org.apache.hadoop.hbase.client.Result) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) PutBuilder(co.cask.cdap.data2.util.hbase.PutBuilder) Get(org.apache.hadoop.hbase.client.Get) TopicId(co.cask.cdap.proto.id.TopicId)

Example 14 with TopicMetadata

use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.

the class HBaseMetadataTable method deleteTopic.

@Override
public void deleteTopic(TopicId topicId) throws TopicNotFoundException, IOException {
    byte[] rowKey = MessagingUtils.toMetadataRowKey(topicId);
    boolean completed = false;
    try {
        // Keep trying to delete
        while (!completed) {
            TopicMetadata oldMetadata = getMetadata(topicId);
            TreeMap<String, String> newProperties = new TreeMap<>(oldMetadata.getProperties());
            newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(oldMetadata.getGeneration() * -1));
            Put put = tableUtil.buildPut(rowKey).add(columnFamily, COL, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE))).build();
            byte[] oldValue = Bytes.toBytes(GSON.toJson(new TreeMap<>(oldMetadata.getProperties()), MAP_TYPE));
            completed = hTable.checkAndPut(rowKey, columnFamily, COL, oldValue, put);
        }
    } catch (IOException e) {
        throw exceptionHandler.handle(e);
    }
}
Also used : IOException(java.io.IOException) TreeMap(java.util.TreeMap) Put(org.apache.hadoop.hbase.client.Put) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

Example 15 with TopicMetadata

use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.

the class HBaseMetadataTable method updateTopic.

@Override
public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException {
    byte[] rowKey = MessagingUtils.toMetadataRowKey(topicMetadata.getTopicId());
    boolean completed = false;
    try {
        // Keep trying to update
        while (!completed) {
            TopicMetadata oldMetadata = getMetadata(topicMetadata.getTopicId());
            TreeMap<String, String> newProperties = new TreeMap<>(topicMetadata.getProperties());
            newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(oldMetadata.getGeneration()));
            Put put = tableUtil.buildPut(rowKey).add(columnFamily, COL, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE))).build();
            byte[] oldValue = Bytes.toBytes(GSON.toJson(new TreeMap<>(oldMetadata.getProperties()), MAP_TYPE));
            completed = hTable.checkAndPut(rowKey, columnFamily, COL, oldValue, put);
        }
    } catch (IOException e) {
        throw exceptionHandler.handle(e);
    }
}
Also used : IOException(java.io.IOException) TreeMap(java.util.TreeMap) Put(org.apache.hadoop.hbase.client.Put) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

Aggregations

TopicMetadata (co.cask.cdap.messaging.TopicMetadata)42 TopicId (co.cask.cdap.proto.id.TopicId)29 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)17 IOException (java.io.IOException)14 NamespaceId (co.cask.cdap.proto.id.NamespaceId)13 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)12 MessageId (co.cask.cdap.messaging.data.MessageId)9 RawMessage (co.cask.cdap.messaging.data.RawMessage)9 TreeMap (java.util.TreeMap)7 TopicAlreadyExistsException (co.cask.cdap.api.messaging.TopicAlreadyExistsException)6 TimeProvider (co.cask.cdap.common.utils.TimeProvider)5 MetadataTable (co.cask.cdap.messaging.store.MetadataTable)4 Transaction (org.apache.tephra.Transaction)4 DBException (org.iq80.leveldb.DBException)4 RollbackDetail (co.cask.cdap.messaging.RollbackDetail)3 MessageTable (co.cask.cdap.messaging.store.MessageTable)3 Path (javax.ws.rs.Path)3 Result (org.apache.hadoop.hbase.client.Result)3 HashSet (java.util.HashSet)2