org.apache.rocketmq.common.protocol.heartbeat.HeartbeatData Java Examples

The following examples show how to use org.apache.rocketmq.common.protocol.heartbeat.HeartbeatData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public int sendHearbeat(
    final String addr,
    final HeartbeatData heartbeatData,
    final long timeoutMillis
) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);

    long startEncode = System.currentTimeMillis();
    request.setBody(heartbeatData.encode());
    long startSend = System.currentTimeMillis();
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    log.info("sendHeartbeat, clientId={},addr={},heartBeat.pCnt={},Cnt={};encode.size={}B,time={}ms;send={}ms",
        heartbeatData.getClientID(), addr, heartbeatData.getProducerDataSet().size(), heartbeatData.getConsumerDataSet().size(),
        ArrayUtils.getLength(request.getBody()), startSend - startEncode, System.currentTimeMillis() - startSend);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            return response.getVersion();
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #2
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public int sendHearbeat(
    final String addr,
    final HeartbeatData heartbeatData,
    final long timeoutMillis
) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);
    request.setLanguage(clientConfig.getLanguage());
    request.setBody(heartbeatData.encode());
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            return response.getVersion();
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #3
Source File: MQClientAPIImpl.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
public void sendHearbeat(//
    final String addr, //
    final HeartbeatData heartbeatData, //
    final long timeoutMillis//
) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);

    request.setBody(heartbeatData.encode());
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            return;
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #4
Source File: MQClientAPIImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
public int sendHearbeat(//
    final String addr, //
    final HeartbeatData heartbeatData, //
    final long timeoutMillis//
) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);

    request.setBody(heartbeatData.encode());
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            return response.getVersion();
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #5
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public int sendHearbeat(
    final String addr,
    final HeartbeatData heartbeatData,
    final long timeoutMillis
) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);

    long startEncode = System.currentTimeMillis();
    request.setBody(heartbeatData.encode());
    long startSend = System.currentTimeMillis();
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    log.info("sendHeartbeat, clientId={},addr={},heartBeat.pCnt={},Cnt={};encode.size={}B,time={}ms;send={}ms",
        heartbeatData.getClientID(), addr, heartbeatData.getProducerDataSet().size(), heartbeatData.getConsumerDataSet().size(),
        ArrayUtils.getLength(request.getBody()), startSend - startEncode, System.currentTimeMillis() - startSend);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            return response.getVersion();
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #6
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public void sendHearbeat(//
                         final String addr, //
                         final HeartbeatData heartbeatData, //
                         final long timeoutMillis//
) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);

    request.setBody(heartbeatData.encode());
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            return;
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #7
Source File: MQClientAPIImpl.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * 发送心跳
 * @param addr addr 地址
 * @param heartbeatData headerBeatData
 * @param timeoutMillis 超时时间
 * @return ;
 * @throws RemotingException ;
 * @throws MQBrokerException ;
 * @throws InterruptedException ;
 */
public int sendHearbeat(
    final String addr,
    final HeartbeatData heartbeatData,
    final long timeoutMillis
) throws RemotingException, MQBrokerException, InterruptedException {

    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);
    request.setLanguage(clientConfig.getLanguage());
    request.setBody(heartbeatData.encode());

    String acturallyAddr = getActurallyBrokerAddr(addr);
    RemotingCommand response = this.remotingClient.invokeSync(acturallyAddr, request, timeoutMillis);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            return response.getVersion();
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #8
Source File: MQClientAPIImpl.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
public int sendHearbeat(
        final String addr,
        final HeartbeatData heartbeatData,
        final long timeoutMillis
    ) throws RemotingException, MQBrokerException, InterruptedException {
        RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);
        request.setLanguage(clientConfig.getLanguage());
        request.setBody(heartbeatData.encode());
//        同步执行
        RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
        assert response != null;
        switch (response.getCode()) {
            case ResponseCode.SUCCESS: {
                return response.getVersion();
            }
            default:
                break;
        }

        throw new MQBrokerException(response.getCode(), response.getRemark());
    }
 
Example #9
Source File: MQClientAPIImpl.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
public boolean registerClient(final String addr, final HeartbeatData heartbeat, final long timeoutMillis)
    throws RemotingException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);

    request.setBody(heartbeat.encode());

    String acturallyAddr = getActurallyBrokerAddr(addr);
    RemotingCommand response = this.remotingClient.invokeSync(acturallyAddr, request, timeoutMillis);
    return response.getCode() == ResponseCode.SUCCESS;
}
 
Example #10
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public boolean registerClient(final String addr, final HeartbeatData heartbeat, final long timeoutMillis)
    throws RemotingException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);

    request.setBody(heartbeat.encode());
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    return response.getCode() == ResponseCode.SUCCESS;
}
 
Example #11
Source File: PlainAccessValidatorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void validateHeartBeatTest() {
    HeartbeatData heartbeatData=new HeartbeatData();
    Set<ProducerData> producerDataSet=new HashSet<>();
    Set<ConsumerData> consumerDataSet=new HashSet<>();
    Set<SubscriptionData> subscriptionDataSet=new HashSet<>();
    ProducerData producerData=new ProducerData();
    producerData.setGroupName("producerGroupA");
    ConsumerData consumerData=new ConsumerData();
    consumerData.setGroupName("consumerGroupA");
    SubscriptionData subscriptionData=new SubscriptionData();
    subscriptionData.setTopic("topicC");
    producerDataSet.add(producerData);
    consumerDataSet.add(consumerData);
    subscriptionDataSet.add(subscriptionData);
    consumerData.setSubscriptionDataSet(subscriptionDataSet);
    heartbeatData.setProducerDataSet(producerDataSet);
    heartbeatData.setConsumerDataSet(consumerDataSet);
    RemotingCommand remotingCommand = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT,null);
    remotingCommand.setBody(heartbeatData.encode());
    aclClient.doBeforeRequest("", remotingCommand);
    ByteBuffer buf = remotingCommand.encode();
    buf.getInt();
    buf = ByteBuffer.allocate(buf.limit() - buf.position()).put(buf);
    buf.position(0);
    PlainAccessResource accessResource = (PlainAccessResource) plainAccessValidator.parse(RemotingCommand.decode(buf), "192.168.0.1:9876");
    plainAccessValidator.validate(accessResource);
}
 
Example #12
Source File: MQClientAPIImpl.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
public boolean registerClient(final String addr, final HeartbeatData heartbeat, final long timeoutMillis)
    throws RemotingException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);

    request.setBody(heartbeat.encode());
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    return response.getCode() == ResponseCode.SUCCESS;
}
 
Example #13
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public boolean registerClient(final String addr, final HeartbeatData heartbeat, final long timeoutMillis)
    throws RemotingException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);

    request.setBody(heartbeat.encode());
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    return response.getCode() == ResponseCode.SUCCESS;
}
 
Example #14
Source File: MQClientAPIImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public boolean registerClient(final String addr, final HeartbeatData heartbeat, final long timeoutMillis)
    throws RemotingException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);

    request.setBody(heartbeat.encode());
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    return response.getCode() == ResponseCode.SUCCESS;
}
 
Example #15
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public boolean registerClient(final String addr, final HeartbeatData heartbeat, final long timeoutMillis)
    throws RemotingException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);

    request.setBody(heartbeat.encode());
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    return response.getCode() == ResponseCode.SUCCESS;
}
 
Example #16
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public boolean registerClient(final String addr, final HeartbeatData heartbeat, final long timeoutMillis)
    throws RemotingException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);

    request.setBody(heartbeat.encode());
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    return response.getCode() == ResponseCode.SUCCESS;
}
 
Example #17
Source File: MQClientAPIImpl.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public boolean registerClient(final String addr, final HeartbeatData heartbeat, final long timeoutMillis)
    throws RemotingException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);

    request.setBody(heartbeat.encode());
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    return response.getCode() == ResponseCode.SUCCESS;
}
 
Example #18
Source File: MQClientInstance.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
private void sendHeartbeatToAllBroker() {
    final HeartbeatData heartbeatData = this.prepareHeartbeatData();
    final boolean producerEmpty = heartbeatData.getProducerDataSet().isEmpty();
    final boolean consumerEmpty = heartbeatData.getConsumerDataSet().isEmpty();
    if (producerEmpty && consumerEmpty) {
        log.warn("sending heartbeat, but no consumer and no producer");
        return;
    }

    if (!this.brokerAddrTable.isEmpty()) {
        long times = this.sendHeartbeatTimesTotal.getAndIncrement();
        Iterator<Entry<String, HashMap<Long, String>>> it = this.brokerAddrTable.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, HashMap<Long, String>> entry = it.next();
            String brokerName = entry.getKey();
            HashMap<Long, String> oneTable = entry.getValue();
            if (oneTable != null) {
                for (Map.Entry<Long, String> entry1 : oneTable.entrySet()) {
                    Long id = entry1.getKey();
                    String addr = entry1.getValue();
                    if (addr != null) {
                        if (consumerEmpty) {
                            if (id != MixAll.MASTER_ID)
                                continue;
                        }

                        try {
                            int version = this.mQClientAPIImpl.sendHearbeat(addr, heartbeatData, 3000);
                            if (!this.brokerVersionTable.containsKey(brokerName)) {
                                this.brokerVersionTable.put(brokerName, new HashMap<String, Integer>(4));
                            }
                            this.brokerVersionTable.get(brokerName).put(addr, version);
                            if (times % 20 == 0) {
                                log.info("send heart beat to broker[{} {} {}] success", brokerName, id, addr);
                                log.info(heartbeatData.toString());
                            }
                        } catch (Exception e) {
                            if (this.isBrokerInNameServer(addr)) {
                                log.info("send heart beat to broker[{} {} {}] failed", brokerName, id, addr);
                            } else {
                                log.info("send heart beat to broker[{} {} {}] exception, because the broker not up, forget it", brokerName,
                                    id, addr);
                            }
                        }
                    }
                }
            }
        }
    }
}
 
Example #19
Source File: MQClientInstance.java    From rocketmq-4.3.0 with Apache License 2.0 4 votes vote down vote up
private void sendHeartbeatToAllBroker() {
//        准备心跳检测数据
        final HeartbeatData heartbeatData = this.prepareHeartbeatData();
        final boolean producerEmpty = heartbeatData.getProducerDataSet().isEmpty();
        final boolean consumerEmpty = heartbeatData.getConsumerDataSet().isEmpty();
//        failfast
        if (producerEmpty && consumerEmpty) {
            log.warn("sending heartbeat, but no consumer and no producer");
            return;
        }

        if (!this.brokerAddrTable.isEmpty()) {
            long times = this.sendHeartbeatTimesTotal.getAndIncrement();
            Iterator<Entry<String, HashMap<Long, String>>> it = this.brokerAddrTable.entrySet().iterator();
            while (it.hasNext()) {
                Entry<String, HashMap<Long, String>> entry = it.next();
                String brokerName = entry.getKey();
                HashMap<Long, String> oneTable = entry.getValue();
                if (oneTable != null) {
                    for (Map.Entry<Long, String> entry1 : oneTable.entrySet()) {
                        Long id = entry1.getKey();
                        String addr = entry1.getValue();
                        if (addr != null) {
//                            非master broke人节点没有消费者
                            if (consumerEmpty) {
                                if (id != MixAll.MASTER_ID)
                                    continue;
                            }

                            try {
//                                心跳检测=》
                                int version = this.mQClientAPIImpl.sendHearbeat(addr, heartbeatData, 3000);
                                if (!this.brokerVersionTable.containsKey(brokerName)) {
                                    this.brokerVersionTable.put(brokerName, new HashMap<String, Integer>(4));
                                }
                                this.brokerVersionTable.get(brokerName).put(addr, version);
                                if (times % 20 == 0) {
                                    log.info("send heart beat to broker[{} {} {}] success", brokerName, id, addr);
                                    log.info(heartbeatData.toString());
                                }
                            } catch (Exception e) {
//                                broker是否存在=》
                                if (this.isBrokerInNameServer(addr)) {
                                    log.info("send heart beat to broker[{} {} {}] failed", brokerName, id, addr);
                                } else {
                                    log.info("send heart beat to broker[{} {} {}] exception, because the broker not up, forget it", brokerName,
                                        id, addr);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
 
Example #20
Source File: MQClientInstance.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
private void sendHeartbeatToAllBroker() {
    final HeartbeatData heartbeatData = this.prepareHeartbeatData();
    final boolean producerEmpty = heartbeatData.getProducerDataSet().isEmpty();
    final boolean consumerEmpty = heartbeatData.getConsumerDataSet().isEmpty();
    if (producerEmpty && consumerEmpty) {
        log.warn("sending heartbeat, but no consumer and no producer");
        return;
    }

    if (!this.brokerAddrTable.isEmpty()) {
        long times = this.sendHeartbeatTimesTotal.getAndIncrement();
        Iterator<Entry<String, HashMap<Long, String>>> it = this.brokerAddrTable.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, HashMap<Long, String>> entry = it.next();
            String brokerName = entry.getKey();
            HashMap<Long, String> oneTable = entry.getValue();
            if (oneTable != null) {
                for (Map.Entry<Long, String> entry1 : oneTable.entrySet()) {
                    Long id = entry1.getKey();
                    String addr = entry1.getValue();
                    if (addr != null) {
                        if (consumerEmpty) {
                            if (id != MixAll.MASTER_ID)
                                continue;
                        }

                        try {
                            int timeout = MixAll.MQTT_MODE ? 300000 : 3000;
                            int version = this.mQClientAPIImpl.sendHearbeat(addr, heartbeatData, timeout);
                            if (!this.brokerVersionTable.containsKey(brokerName)) {
                                this.brokerVersionTable.put(brokerName, new HashMap<String, Integer>(4));
                            }
                            this.brokerVersionTable.get(brokerName).put(addr, version);
                            if (times % 20 == 0) {
                                log.info("send heart beat to broker[{} {} {}] success", brokerName, id, addr);
                                log.info(heartbeatData.toString());
                            }
                        } catch (Exception e) {
                            if (this.isBrokerInNameServer(addr)) {
                                log.info("send heart beat to broker[{} {} {}] failed", brokerName, id, addr);
                            } else {
                                log.info("send heart beat to broker[{} {} {}] exception, because the broker not up, forget it", brokerName,
                                    id, addr);
                            }
                        }
                    }
                }
            }
        }
    }
}
 
Example #21
Source File: PlainAccessValidator.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Override
public AccessResource parse(RemotingCommand request, String remoteAddr) {
    PlainAccessResource accessResource = new PlainAccessResource();
    if (remoteAddr != null && remoteAddr.contains(":")) {
        accessResource.setWhiteRemoteAddress(remoteAddr.substring(0, remoteAddr.lastIndexOf(':')));
    } else {
        accessResource.setWhiteRemoteAddress(remoteAddr);
    }

    accessResource.setRequestCode(request.getCode());

    if (request.getExtFields() == null) {
        // If request's extFields is null,then return accessResource directly(users can use whiteAddress pattern)
        // The following logic codes depend on the request's extFields not to be null.
        return accessResource;
    }
    accessResource.setAccessKey(request.getExtFields().get(SessionCredentials.ACCESS_KEY));
    accessResource.setSignature(request.getExtFields().get(SessionCredentials.SIGNATURE));
    accessResource.setSecretToken(request.getExtFields().get(SessionCredentials.SECURITY_TOKEN));

    try {
        switch (request.getCode()) {
            case RequestCode.SEND_MESSAGE:
                accessResource.addResourceAndPerm(request.getExtFields().get("topic"), Permission.PUB);
                break;
            case RequestCode.SEND_MESSAGE_V2:
                accessResource.addResourceAndPerm(request.getExtFields().get("b"), Permission.PUB);
                break;
            case RequestCode.CONSUMER_SEND_MSG_BACK:
                accessResource.addResourceAndPerm(request.getExtFields().get("originTopic"), Permission.PUB);
                accessResource.addResourceAndPerm(getRetryTopic(request.getExtFields().get("group")), Permission.SUB);
                break;
            case RequestCode.PULL_MESSAGE:
                accessResource.addResourceAndPerm(request.getExtFields().get("topic"), Permission.SUB);
                accessResource.addResourceAndPerm(getRetryTopic(request.getExtFields().get("consumerGroup")), Permission.SUB);
                break;
            case RequestCode.QUERY_MESSAGE:
                accessResource.addResourceAndPerm(request.getExtFields().get("topic"), Permission.SUB);
                break;
            case RequestCode.HEART_BEAT:
                HeartbeatData heartbeatData = HeartbeatData.decode(request.getBody(), HeartbeatData.class);
                for (ConsumerData data : heartbeatData.getConsumerDataSet()) {
                    accessResource.addResourceAndPerm(getRetryTopic(data.getGroupName()), Permission.SUB);
                    for (SubscriptionData subscriptionData : data.getSubscriptionDataSet()) {
                        accessResource.addResourceAndPerm(subscriptionData.getTopic(), Permission.SUB);
                    }
                }
                break;
            case RequestCode.UNREGISTER_CLIENT:
                final UnregisterClientRequestHeader unregisterClientRequestHeader =
                    (UnregisterClientRequestHeader) request
                        .decodeCommandCustomHeader(UnregisterClientRequestHeader.class);
                accessResource.addResourceAndPerm(getRetryTopic(unregisterClientRequestHeader.getConsumerGroup()), Permission.SUB);
                break;
            case RequestCode.GET_CONSUMER_LIST_BY_GROUP:
                final GetConsumerListByGroupRequestHeader getConsumerListByGroupRequestHeader =
                    (GetConsumerListByGroupRequestHeader) request
                        .decodeCommandCustomHeader(GetConsumerListByGroupRequestHeader.class);
                accessResource.addResourceAndPerm(getRetryTopic(getConsumerListByGroupRequestHeader.getConsumerGroup()), Permission.SUB);
                break;
            case RequestCode.UPDATE_CONSUMER_OFFSET:
                final UpdateConsumerOffsetRequestHeader updateConsumerOffsetRequestHeader =
                    (UpdateConsumerOffsetRequestHeader) request
                        .decodeCommandCustomHeader(UpdateConsumerOffsetRequestHeader.class);
                accessResource.addResourceAndPerm(getRetryTopic(updateConsumerOffsetRequestHeader.getConsumerGroup()), Permission.SUB);
                accessResource.addResourceAndPerm(updateConsumerOffsetRequestHeader.getTopic(), Permission.SUB);
                break;
            default:
                break;

        }
    } catch (Throwable t) {
        throw new AclException(t.getMessage(), t);
    }

    // Content
    SortedMap<String, String> map = new TreeMap<String, String>();
    for (Map.Entry<String, String> entry : request.getExtFields().entrySet()) {
        if (!SessionCredentials.SIGNATURE.equals(entry.getKey())
                && !MixAll.UNIQUE_MSG_QUERY_FLAG.equals(entry.getKey())) {
            map.put(entry.getKey(), entry.getValue());
        }
    }
    accessResource.setContent(AclUtils.combineRequestContent(request, map));
    return accessResource;
}
 
Example #22
Source File: MQClientInstance.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
private void sendHeartbeatToAllBroker() {
    final HeartbeatData heartbeatData = this.prepareHeartbeatData();
    final boolean producerEmpty = heartbeatData.getProducerDataSet().isEmpty();
    final boolean consumerEmpty = heartbeatData.getConsumerDataSet().isEmpty();
    if (producerEmpty && consumerEmpty) {
        log.warn("sending heartbeat, but no consumer and no producer");
        return;
    }

    if (!this.brokerAddrTable.isEmpty()) {
        long times = this.sendHeartbeatTimesTotal.getAndIncrement();
        Iterator<Entry<String, HashMap<Long, String>>> it = this.brokerAddrTable.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, HashMap<Long, String>> entry = it.next();
            String brokerName = entry.getKey();
            HashMap<Long, String> oneTable = entry.getValue();
            if (oneTable != null) {
                for (Map.Entry<Long, String> entry1 : oneTable.entrySet()) {
                    Long id = entry1.getKey();
                    String addr = entry1.getValue();
                    if (addr != null) {
                        if (consumerEmpty) {
                            if (id != MixAll.MASTER_ID)
                                continue;
                        }

                        try {
                            int version = this.mQClientAPIImpl.sendHearbeat(addr, heartbeatData, 3000);
                            if (!this.brokerVersionTable.containsKey(brokerName)) {
                                this.brokerVersionTable.put(brokerName, new HashMap<String, Integer>(4));
                            }
                            this.brokerVersionTable.get(brokerName).put(addr, version);
                            if (times % 20 == 0) {
                                log.info("send heart beat to broker[{} {} {}] success", brokerName, id, addr);
                                log.info(heartbeatData.toString());
                            }
                        } catch (Exception e) {
                            if (this.isBrokerInNameServer(addr)) {
                                log.info("send heart beat to broker[{} {} {}] failed", brokerName, id, addr, e);
                            } else {
                                log.info("send heart beat to broker[{} {} {}] exception, because the broker not up, forget it", brokerName,
                                    id, addr, e);
                            }
                        }
                    }
                }
            }
        }
    }
}
 
Example #23
Source File: MQClientInstance.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
private void sendHeartbeatToAllBroker() {
    final HeartbeatData heartbeatData = this.prepareHeartbeatData();
    final boolean producerEmpty = heartbeatData.getProducerDataSet().isEmpty();
    final boolean consumerEmpty = heartbeatData.getConsumerDataSet().isEmpty();
    if (producerEmpty && consumerEmpty) {
        log.warn("sending heartbeat, but no consumer and no producer");
        return;
    }

    if (!this.brokerAddrTable.isEmpty()) {
        long times = this.sendHeartbeatTimesTotal.getAndIncrement();
        Iterator<Entry<String, HashMap<Long, String>>> it = this.brokerAddrTable.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, HashMap<Long, String>> entry = it.next();
            String brokerName = entry.getKey();
            HashMap<Long, String> oneTable = entry.getValue();
            if (oneTable != null) {
                for (Map.Entry<Long, String> entry1 : oneTable.entrySet()) {
                    Long id = entry1.getKey();
                    String addr = entry1.getValue();
                    if (addr != null) {
                        if (consumerEmpty) {
                            if (id != MixAll.MASTER_ID)
                                continue;
                        }

                        try {
                            int timeout = MixAll.MQTT_MODE ? 300000 : 3000;
                            int version = this.mQClientAPIImpl.sendHearbeat(addr, heartbeatData, timeout);
                            if (!this.brokerVersionTable.containsKey(brokerName)) {
                                this.brokerVersionTable.put(brokerName, new HashMap<String, Integer>(4));
                            }
                            this.brokerVersionTable.get(brokerName).put(addr, version);
                            if (times % 20 == 0) {
                                log.info("send heart beat to broker[{} {} {}] success", brokerName, id, addr);
                                log.info(heartbeatData.toString());
                            }
                        } catch (Exception e) {
                            if (this.isBrokerInNameServer(addr)) {
                                log.info("send heart beat to broker[{} {} {}] failed", brokerName, id, addr);
                            } else {
                                log.info("send heart beat to broker[{} {} {}] exception, because the broker not up, forget it", brokerName,
                                    id, addr);
                            }
                        }
                    }
                }
            }
        }
    }
}
 
Example #24
Source File: MQClientInstance.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
/**
 * 向所有的producer发送心跳
 */
private void sendHeartbeatToAllBroker() {

    final HeartbeatData heartbeatData = this.prepareHeartbeatData();
    final boolean producerEmpty = heartbeatData.getProducerDataSet().isEmpty();
    final boolean consumerEmpty = heartbeatData.getConsumerDataSet().isEmpty();

    if (producerEmpty && consumerEmpty) {
        log.warn("sending heartbeat, but no consumer and no producer");
        return;
    }

    /*
     * 如果所有的brokerAddrtable不为空
     */
    if (!this.brokerAddrTable.isEmpty()) {
        //心跳次数+1
        long times = this.sendHeartbeatTimesTotal.getAndIncrement();
        //key: brokerid,v:addr
        Iterator<Entry<String, HashMap<Long, String>>> it = this.brokerAddrTable.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, HashMap<Long, String>> entry = it.next();
            String brokerName = entry.getKey();
            HashMap<Long, String> oneTable = entry.getValue();
            if (oneTable != null) {
                for (Map.Entry<Long, String> entry1 : oneTable.entrySet()) {
                    //brokerId
                    Long id = entry1.getKey();
                    //brokerAddr
                    String addr = entry1.getValue();
                    if (addr != null) {
                        if (consumerEmpty) {
                            //producer只和master心跳
                            if (id != MixAll.MASTER_ID){
                                continue;
                            }
                        }

                        try {
                            //发送心跳
                            int version = this.mQClientAPIImpl.sendHearbeat(addr, heartbeatData, 3000);
                            if (!this.brokerVersionTable.containsKey(brokerName)) {
                                this.brokerVersionTable.put(brokerName, new HashMap<String, Integer>(4));
                            }
                            //地址的版本
                            this.brokerVersionTable.get(brokerName).put(addr, version);
                            //每心跳20次打印日志
                            if (times % 20 == 0) {
                                log.info("send heart beat to broker[{} {} {}] success", brokerName, id, addr);
                                log.info(heartbeatData.toString());
                            }
                        } catch (Exception e) {
                            if (this.isBrokerInNameServer(addr)) {
                                log.info("send heart beat to broker[{} {} {}] failed", brokerName, id, addr);
                            } else {
                                log.info("send heart beat to broker[{} {} {}] exception, because the broker not up, forget it", brokerName,
                                    id, addr);
                            }
                        }
                    }
                }
            }
        }
    }
}