MQFaultStrategy主要是用来规划消息发送时的延迟策略

 这个类主要是用来规划消息发送时的延迟策略


package com.aliyun.openservices.shade.com.alibaba.rocketmq.client.latency;

import com.aliyun.openservices.shade.com.alibaba.rocketmq.client.impl.producer.TopicPublishInfo;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.client.log.ClientLogger;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.common.message.MessageQueue;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.logging.InternalLogger;

public class MQFaultStrategy {
    private static final InternalLogger log = ClientLogger.getLog();
    private final LatencyFaultTolerance<String> latencyFaultTolerance = new LatencyFaultToleranceImpl();
    private boolean sendLatencyFaultEnable = false;
    private long[] latencyMax = new long[]{50L, 100L, 550L, 1000L, 2000L, 3000L, 15000L};
    private long[] notAvailableDuration = new long[]{0L, 0L, 30000L, 60000L, 120000L, 180000L, 600000L};

    public MQFaultStrategy() {
    }

    public long[] getNotAvailableDuration() {
        return this.notAvailableDuration;
    }

    public void setNotAvailableDuration(long[] notAvailableDuration) {
        this.notAvailableDuration = notAvailableDuration;
    }

    public long[] getLatencyMax() {
        return this.latencyMax;
    }

    public void setLatencyMax(long[] latencyMax) {
        this.latencyMax = latencyMax;
    }

    public boolean isSendLatencyFaultEnable() {
        return this.sendLatencyFaultEnable;
    }

    public void setSendLatencyFaultEnable(boolean sendLatencyFaultEnable) {
        this.sendLatencyFaultEnable = sendLatencyFaultEnable;
    }

    public MessageQueue selectOneMessageQueue(TopicPublishInfo tpInfo, String lastBrokerName) {
        if (this.sendLatencyFaultEnable) {
            try {
                int index = tpInfo.getSendWhichQueue().getAndIncrement();
                int i = 0;

                while(true) {
                    int writeQueueNums;
                    MessageQueue mq;
                    if (i >= tpInfo.getMessageQueueList().size()) {
                        String notBestBroker = (String)this.latencyFaultTolerance.pickOneAtLeast();
                        writeQueueNums = tpInfo.getQueueIdByBroker(notBestBroker);
                        if (writeQueueNums > 0) {
                            mq = tpInfo.selectOneMessageQueue();
                            if (notBestBroker != null) {
                                mq.setBrokerName(notBestBroker);
                                mq.setQueueId(tpInfo.getSendWhichQueue().getAndIncrement() % writeQueueNums);
                            }

                            return mq;
                        }

                        this.latencyFaultTolerance.remove(notBestBroker);
                        break;
                    }

                    writeQueueNums = Math.abs(index++) % tpInfo.getMessageQueueList().size();
                    if (writeQueueNums < 0) {
                        writeQueueNums = 0;
                    }

                    mq = (MessageQueue)tpInfo.getMessageQueueList().get(writeQueueNums);
                    if (this.latencyFaultTolerance.isAvailable(mq.getBrokerName()) && (null == lastBrokerName || mq.getBrokerName().equals(lastBrokerName))) {
                        return mq;
                    }

                    ++i;
                }
            } catch (Exception var7) {
                log.error("Error occurred when selecting message queue", var7);
            }

            return tpInfo.selectOneMessageQueue();
        } else {
            return tpInfo.selectOneMessageQueue(lastBrokerName);
        }
    }

    public void updateFaultItem(String brokerName, long currentLatency, boolean isolation) {
        if (this.sendLatencyFaultEnable) {
            long duration = this.computeNotAvailableDuration(isolation ? 30000L : currentLatency);
            this.latencyFaultTolerance.updateFaultItem(brokerName, currentLatency, duration);
        }

    }

    private long computeNotAvailableDuration(long currentLatency) {
        for(int i = this.latencyMax.length - 1; i >= 0; --i) {
            if (currentLatency >= this.latencyMax[i]) {
                return this.notAvailableDuration[i];
            }
        }

        return 0L;
    }
}

 


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值