|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package io.openmessaging.rocketmq.consumer; |
| 18 | + |
| 19 | +import io.openmessaging.KeyValue; |
| 20 | +import io.openmessaging.PropertyKeys; |
| 21 | +import io.openmessaging.rocketmq.domain.ConsumeRequest; |
| 22 | +import io.openmessaging.rocketmq.domain.NonStandardKeys; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.concurrent.BlockingQueue; |
| 26 | +import java.util.concurrent.ConcurrentHashMap; |
| 27 | +import java.util.concurrent.LinkedBlockingQueue; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | +import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer; |
| 30 | +import org.apache.rocketmq.client.exception.MQClientException; |
| 31 | +import org.apache.rocketmq.client.log.ClientLogger; |
| 32 | +import org.apache.rocketmq.common.message.MessageExt; |
| 33 | +import org.apache.rocketmq.common.message.MessageQueue; |
| 34 | +import org.slf4j.Logger; |
| 35 | + |
| 36 | +class LocalMessageCache { |
| 37 | + private final BlockingQueue<ConsumeRequest> consumeRequestCache; |
| 38 | + private final Map<String, ConsumeRequest> consumedRequest; |
| 39 | + private final ConcurrentHashMap<MessageQueue, Long> pullOffsetTable; |
| 40 | + private final DefaultMQPullConsumer rocketmqPullConsumer; |
| 41 | + private int pullBatchNums = 32; |
| 42 | + private int pollTimeout = -1; |
| 43 | + private final static Logger log = ClientLogger.getLog(); |
| 44 | + |
| 45 | + LocalMessageCache(final DefaultMQPullConsumer rocketmqPullConsumer, final KeyValue properties) { |
| 46 | + int cacheCapacity = 1000; |
| 47 | + if (properties.containsKey(NonStandardKeys.PULL_MESSAGE_CACHE_CAPACITY)) { |
| 48 | + cacheCapacity = properties.getInt(NonStandardKeys.PULL_MESSAGE_CACHE_CAPACITY); |
| 49 | + } |
| 50 | + consumeRequestCache = new LinkedBlockingQueue<>(cacheCapacity); |
| 51 | + |
| 52 | + if (properties.containsKey(NonStandardKeys.PULL_MESSAGE_BATCH_NUMS)) { |
| 53 | + pullBatchNums = properties.getInt(NonStandardKeys.PULL_MESSAGE_BATCH_NUMS); |
| 54 | + } |
| 55 | + |
| 56 | + if (properties.containsKey(PropertyKeys.OPERATION_TIMEOUT)) { |
| 57 | + pollTimeout = properties.getInt(PropertyKeys.OPERATION_TIMEOUT); |
| 58 | + } |
| 59 | + |
| 60 | + this.consumedRequest = new ConcurrentHashMap<>(); |
| 61 | + this.pullOffsetTable = new ConcurrentHashMap<>(); |
| 62 | + this.rocketmqPullConsumer = rocketmqPullConsumer; |
| 63 | + } |
| 64 | + |
| 65 | + int nextPullBatchNums() { |
| 66 | + return Math.min(pullBatchNums, consumeRequestCache.remainingCapacity()); |
| 67 | + } |
| 68 | + |
| 69 | + long nextPullOffset(MessageQueue remoteQueue) { |
| 70 | + if (!pullOffsetTable.containsKey(remoteQueue)) { |
| 71 | + try { |
| 72 | + pullOffsetTable.putIfAbsent(remoteQueue, |
| 73 | + rocketmqPullConsumer.fetchConsumeOffset(remoteQueue, false)); |
| 74 | + } catch (MQClientException e) { |
| 75 | + log.error("A error occurred in fetch consume offset process.", e); |
| 76 | + } |
| 77 | + } |
| 78 | + return pullOffsetTable.get(remoteQueue); |
| 79 | + } |
| 80 | + |
| 81 | + void updatePullOffset(MessageQueue remoteQueue, long nextPullOffset) { |
| 82 | + pullOffsetTable.put(remoteQueue, nextPullOffset); |
| 83 | + } |
| 84 | + |
| 85 | + void submitConsumeRequest(ConsumeRequest consumeRequest) { |
| 86 | + try { |
| 87 | + consumeRequestCache.put(consumeRequest); |
| 88 | + } catch (InterruptedException ignore) { |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + MessageExt poll() { |
| 93 | + try { |
| 94 | + ConsumeRequest consumeRequest = consumeRequestCache.take(); |
| 95 | + consumeRequest.setStartConsumeTimeMillis(System.currentTimeMillis()); |
| 96 | + consumedRequest.put(consumeRequest.getMessageExt().getMsgId(), consumeRequest); |
| 97 | + return consumeRequest.getMessageExt(); |
| 98 | + } catch (InterruptedException ignore) { |
| 99 | + } |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + MessageExt poll(final KeyValue properties) { |
| 104 | + int currentPollTimeout = pollTimeout; |
| 105 | + if (properties.containsKey(PropertyKeys.OPERATION_TIMEOUT)) { |
| 106 | + currentPollTimeout = properties.getInt(PropertyKeys.OPERATION_TIMEOUT); |
| 107 | + } |
| 108 | + |
| 109 | + if (currentPollTimeout == -1) { |
| 110 | + return poll(); |
| 111 | + } |
| 112 | + |
| 113 | + try { |
| 114 | + ConsumeRequest consumeRequest = consumeRequestCache.poll(currentPollTimeout, TimeUnit.MILLISECONDS); |
| 115 | + consumeRequest.setStartConsumeTimeMillis(System.currentTimeMillis()); |
| 116 | + consumedRequest.put(consumeRequest.getMessageExt().getMsgId(), consumeRequest); |
| 117 | + return consumeRequest.getMessageExt(); |
| 118 | + } catch (InterruptedException ignore) { |
| 119 | + } |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + void ack(final String messageId) { |
| 124 | + ConsumeRequest consumeRequest = consumedRequest.remove(messageId); |
| 125 | + if (consumeRequest != null) { |
| 126 | + long offset = consumeRequest.getProcessQueue().removeMessage(Collections.singletonList(consumeRequest.getMessageExt())); |
| 127 | + try { |
| 128 | + rocketmqPullConsumer.updateConsumeOffset(consumeRequest.getMessageQueue(), offset); |
| 129 | + } catch (MQClientException e) { |
| 130 | + log.error("A error occurred in update consume offset process.", e); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments