Search in sources :

Example 1 with QueueMetaData

use of io.openmessaging.connector.api.common.QueueMetaData in project rocketmq-externals by apache.

the class WorkerDirectTask method starkSinkTask.

private void starkSinkTask() {
    sinkTask.initialize(new SinkTaskContext() {

        @Override
        public KeyValue configs() {
            return taskConfig;
        }

        @Override
        public void resetOffset(QueueMetaData queueMetaData, Long offset) {
        }

        @Override
        public void resetOffset(Map<QueueMetaData, Long> offsets) {
        }

        @Override
        public void pause(List<QueueMetaData> queues) {
        }

        @Override
        public void resume(List<QueueMetaData> queues) {
        }
    });
    sinkTask.start(taskConfig);
    log.info("Sink task start, config:{}", JSON.toJSONString(taskConfig));
}
Also used : ConnectKeyValue(org.apache.rocketmq.connect.runtime.common.ConnectKeyValue) KeyValue(io.openmessaging.KeyValue) SinkTaskContext(io.openmessaging.connector.api.sink.SinkTaskContext) QueueMetaData(io.openmessaging.connector.api.common.QueueMetaData)

Example 2 with QueueMetaData

use of io.openmessaging.connector.api.common.QueueMetaData in project rocketmq-externals by apache.

the class WorkerSinkTask method preCommit.

private void preCommit() {
    long commitInterval = taskConfig.getLong(OFFSET_COMMIT_TIMEOUT_MS_CONFIG, 1000);
    if (nextCommitTime <= 0) {
        long now = System.currentTimeMillis();
        nextCommitTime = now + commitInterval;
    }
    if (nextCommitTime < System.currentTimeMillis()) {
        Map<QueueMetaData, Long> queueMetaDataLongMap = new HashMap<>(512);
        if (messageQueuesOffsetMap.size() > 0) {
            for (Map.Entry<MessageQueue, Long> messageQueueLongEntry : messageQueuesOffsetMap.entrySet()) {
                QueueMetaData queueMetaData = new QueueMetaData();
                queueMetaData.setQueueName(messageQueueLongEntry.getKey().getTopic());
                queueMetaData.setShardingKey(messageQueueLongEntry.getKey().getBrokerName() + COMMA + messageQueueLongEntry.getKey().getQueueId());
                queueMetaDataLongMap.put(queueMetaData, messageQueueLongEntry.getValue());
            }
        }
        sinkTask.preCommit(queueMetaDataLongMap);
        nextCommitTime = 0;
    }
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) QueueMetaData(io.openmessaging.connector.api.common.QueueMetaData) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with QueueMetaData

use of io.openmessaging.connector.api.common.QueueMetaData in project rocketmq-externals by apache.

the class WorkerSinkTask method run.

/**
 * Start a sink task, and receive data entry from MQ cyclically.
 */
@Override
public void run() {
    try {
        consumer.start();
        log.info("Sink task consumer start.");
        state.compareAndSet(WorkerTaskState.NEW, WorkerTaskState.PENDING);
        sinkTask.initialize(new SinkTaskContext() {

            @Override
            public void resetOffset(QueueMetaData queueMetaData, Long offset) {
                String shardingKey = queueMetaData.getShardingKey();
                String queueName = queueMetaData.getQueueName();
                if (StringUtils.isNotEmpty(shardingKey) && StringUtils.isNotEmpty(queueName)) {
                    String[] s = shardingKey.split(COMMA);
                    if (s.length == 2 && StringUtils.isNotEmpty(s[0]) && StringUtils.isNotEmpty(s[1])) {
                        String brokerName = s[0];
                        Integer queueId = Integer.valueOf(s[1]);
                        MessageQueue messageQueue = new MessageQueue(queueName, brokerName, queueId);
                        messageQueuesOffsetMap.put(messageQueue, offset);
                        offsetManagementService.putPosition(convertToByteBufferKey(messageQueue), convertToByteBufferValue(offset));
                        return;
                    }
                }
                log.warn("Missing parameters, queueMetaData {}", queueMetaData);
            }

            @Override
            public void resetOffset(Map<QueueMetaData, Long> offsets) {
                for (Map.Entry<QueueMetaData, Long> entry : offsets.entrySet()) {
                    String shardingKey = entry.getKey().getShardingKey();
                    String queueName = entry.getKey().getQueueName();
                    if (StringUtils.isNotEmpty(shardingKey) && StringUtils.isNotEmpty(queueName)) {
                        String[] s = shardingKey.split(COMMA);
                        if (s.length == 2 && StringUtils.isNotEmpty(s[0]) && StringUtils.isNotEmpty(s[1])) {
                            String brokerName = s[0];
                            Integer queueId = Integer.valueOf(s[1]);
                            MessageQueue messageQueue = new MessageQueue(queueName, brokerName, queueId);
                            messageQueuesOffsetMap.put(messageQueue, entry.getValue());
                            offsetManagementService.putPosition(convertToByteBufferKey(messageQueue), convertToByteBufferValue(entry.getValue()));
                            continue;
                        }
                    }
                    log.warn("Missing parameters, queueMetaData {}", entry.getKey());
                }
            }

            @Override
            public void pause(List<QueueMetaData> queueMetaDatas) {
                if (null != queueMetaDatas && queueMetaDatas.size() > 0) {
                    for (QueueMetaData queueMetaData : queueMetaDatas) {
                        String shardingKey = queueMetaData.getShardingKey();
                        String queueName = queueMetaData.getQueueName();
                        if (StringUtils.isNotEmpty(shardingKey) && StringUtils.isNotEmpty(queueName)) {
                            String[] s = shardingKey.split(COMMA);
                            if (s.length == 2 && StringUtils.isNotEmpty(s[0]) && StringUtils.isNotEmpty(s[1])) {
                                String brokerName = s[0];
                                Integer queueId = Integer.valueOf(s[1]);
                                MessageQueue messageQueue = new MessageQueue(queueName, brokerName, queueId);
                                messageQueuesStateMap.put(messageQueue, QueueState.PAUSE);
                                continue;
                            }
                        }
                        log.warn("Missing parameters, queueMetaData {}", queueMetaData);
                    }
                }
            }

            @Override
            public void resume(List<QueueMetaData> queueMetaDatas) {
                if (null != queueMetaDatas && queueMetaDatas.size() > 0) {
                    for (QueueMetaData queueMetaData : queueMetaDatas) {
                        String shardingKey = queueMetaData.getShardingKey();
                        String queueName = queueMetaData.getQueueName();
                        if (StringUtils.isNotEmpty(shardingKey) && StringUtils.isNotEmpty(queueName)) {
                            String[] s = shardingKey.split(COMMA);
                            if (s.length == 2 && StringUtils.isNotEmpty(s[0]) && StringUtils.isNotEmpty(s[1])) {
                                String brokerName = s[0];
                                Integer queueId = Integer.valueOf(s[1]);
                                MessageQueue messageQueue = new MessageQueue(queueName, brokerName, queueId);
                                messageQueuesStateMap.remove(messageQueue);
                                continue;
                            }
                        }
                        log.warn("Missing parameters, queueMetaData {}", queueMetaData);
                    }
                }
            }

            @Override
            public KeyValue configs() {
                return taskConfig;
            }
        });
        String topicNamesStr = taskConfig.getString(QUEUENAMES_CONFIG);
        String topicQueuesStr = taskConfig.getString(TOPIC_QUEUES_CONFIG);
        if (!StringUtils.isEmpty(topicNamesStr)) {
            String[] topicNames = topicNamesStr.split(COMMA);
            for (String topicName : topicNames) {
                final Set<MessageQueue> messageQueues = consumer.fetchSubscribeMessageQueues(topicName);
                for (MessageQueue messageQueue : messageQueues) {
                    final long offset = consumer.searchOffset(messageQueue, TIMEOUT);
                    messageQueuesOffsetMap.put(messageQueue, offset);
                }
                messageQueues.addAll(messageQueues);
            }
            log.debug("{} Initializing and starting task for topicNames {}", this, topicNames);
        } else if (!StringUtils.isEmpty(topicQueuesStr)) {
            String[] topicQueues = topicQueuesStr.split(SEMICOLON);
            for (String messageQueueStr : topicQueues) {
                String[] items = messageQueueStr.split(COMMA);
                if (items.length != 3) {
                    log.error("Topic queue format error, topicQueueStr : " + topicNamesStr);
                    return;
                }
                MessageQueue messageQueue = new MessageQueue(items[0], items[1], Integer.valueOf(items[2]));
                final long offset = consumer.searchOffset(messageQueue, TIMEOUT);
                messageQueuesOffsetMap.put(messageQueue, offset);
            }
        } else {
            log.error("Lack of sink comsume topicNames config");
            state.set(WorkerTaskState.ERROR);
            return;
        }
        for (Map.Entry<MessageQueue, Long> entry : messageQueuesOffsetMap.entrySet()) {
            MessageQueue messageQueue = entry.getKey();
            ByteBuffer byteBuffer = offsetStorageReader.getPosition(convertToByteBufferKey(messageQueue));
            if (null != byteBuffer) {
                messageQueuesOffsetMap.put(messageQueue, convertToOffset(byteBuffer));
            }
        }
        sinkTask.start(taskConfig);
        // we assume executed here means we are safe
        log.info("Sink task start, config:{}", JSON.toJSONString(taskConfig));
        state.compareAndSet(WorkerTaskState.PENDING, WorkerTaskState.RUNNING);
        while (WorkerState.STARTED == workerState.get() && WorkerTaskState.RUNNING == state.get()) {
            // this method can block up to 3 minutes long
            pullMessageFromQueues();
        }
        sinkTask.stop();
        state.compareAndSet(WorkerTaskState.STOPPING, WorkerTaskState.STOPPED);
        log.info("Sink task stop, config:{}", JSON.toJSONString(taskConfig));
    } catch (Exception e) {
        log.error("Run task failed.", e);
        state.set(WorkerTaskState.ERROR);
    } finally {
        if (consumer != null) {
            consumer.shutdown();
            log.info("Sink task consumer shutdown.");
        }
    }
}
Also used : ConnectKeyValue(org.apache.rocketmq.connect.runtime.common.ConnectKeyValue) KeyValue(io.openmessaging.KeyValue) SinkTaskContext(io.openmessaging.connector.api.sink.SinkTaskContext) QueueMetaData(io.openmessaging.connector.api.common.QueueMetaData) ByteBuffer(java.nio.ByteBuffer) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) SinkDataEntry(io.openmessaging.connector.api.data.SinkDataEntry) SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

QueueMetaData (io.openmessaging.connector.api.common.QueueMetaData)3 KeyValue (io.openmessaging.KeyValue)2 SinkTaskContext (io.openmessaging.connector.api.sink.SinkTaskContext)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)2 ConnectKeyValue (org.apache.rocketmq.connect.runtime.common.ConnectKeyValue)2 SinkDataEntry (io.openmessaging.connector.api.data.SinkDataEntry)1 SourceDataEntry (io.openmessaging.connector.api.data.SourceDataEntry)1 ByteBuffer (java.nio.ByteBuffer)1 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)1 MQClientException (org.apache.rocketmq.client.exception.MQClientException)1 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)1