org.apache.rocketmq.common.protocol.header.namesrv.RegisterBrokerRequestHeader Java Examples

The following examples show how to use org.apache.rocketmq.common.protocol.header.namesrv.RegisterBrokerRequestHeader. 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: MQProtosHelper.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static boolean registerBrokerToNameServer(final String nsaddr, final String brokerAddr,
    final long timeoutMillis) {
    RegisterBrokerRequestHeader requestHeader = new RegisterBrokerRequestHeader();
    requestHeader.setBrokerAddr(brokerAddr);

    RemotingCommand request =
        RemotingCommand.createRequestCommand(RequestCode.REGISTER_BROKER, requestHeader);

    try {
        RemotingCommand response = RemotingHelper.invokeSync(nsaddr, request, timeoutMillis);
        if (response != null) {
            return ResponseCode.SUCCESS == response.getCode();
        }
    } catch (Exception e) {
        log.error("Failed to register broker", e);
    }

    return false;
}
 
Example #2
Source File: MQProtosHelper.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public static boolean registerBrokerToNameServer(final String nsaddr, final String brokerAddr,
    final long timeoutMillis) {
    RegisterBrokerRequestHeader requestHeader = new RegisterBrokerRequestHeader();
    requestHeader.setBrokerAddr(brokerAddr);

    RemotingCommand request =
        RemotingCommand.createRequestCommand(RequestCode.REGISTER_BROKER, requestHeader);

    try {
        RemotingCommand response = RemotingHelper.invokeSync(nsaddr, request, timeoutMillis);
        if (response != null) {
            return ResponseCode.SUCCESS == response.getCode();
        }
    } catch (Exception e) {
        log.error("Failed to register broker", e);
    }

    return false;
}
 
Example #3
Source File: MQProtosHelper.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
public static boolean registerBrokerToNameServer(final String nsaddr, final String brokerAddr,
    final long timeoutMillis) {
    RegisterBrokerRequestHeader requestHeader = new RegisterBrokerRequestHeader();
    requestHeader.setBrokerAddr(brokerAddr);

    RemotingCommand request =
        RemotingCommand.createRequestCommand(RequestCode.REGISTER_BROKER, requestHeader);

    try {
        RemotingCommand response = RemotingHelper.invokeSync(nsaddr, request, timeoutMillis);
        if (response != null) {
            return ResponseCode.SUCCESS == response.getCode();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}
 
Example #4
Source File: MQProtosHelper.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
public static boolean registerBrokerToNameServer(final String nsaddr, final String brokerAddr,
    final long timeoutMillis) {
    RegisterBrokerRequestHeader requestHeader = new RegisterBrokerRequestHeader();
    requestHeader.setBrokerAddr(brokerAddr);

    RemotingCommand request =
        RemotingCommand.createRequestCommand(RequestCode.REGISTER_BROKER, requestHeader);

    try {
        RemotingCommand response = RemotingHelper.invokeSync(nsaddr, request, timeoutMillis);
        if (response != null) {
            return ResponseCode.SUCCESS == response.getCode();
        }
    } catch (Exception e) {
        log.error("Failed to register broker", e);
    }

    return false;
}
 
Example #5
Source File: MQProtosHelper.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
public static boolean registerBrokerToNameServer(final String nsaddr, final String brokerAddr,
    final long timeoutMillis) {
    RegisterBrokerRequestHeader requestHeader = new RegisterBrokerRequestHeader();
    requestHeader.setBrokerAddr(brokerAddr);

    RemotingCommand request =
        RemotingCommand.createRequestCommand(RequestCode.REGISTER_BROKER, requestHeader);

    try {
        RemotingCommand response = RemotingHelper.invokeSync(nsaddr, request, timeoutMillis);
        if (response != null) {
            return ResponseCode.SUCCESS == response.getCode();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}
 
Example #6
Source File: MQProtosHelper.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public static boolean registerBrokerToNameServer(final String nsaddr, final String brokerAddr,
    final long timeoutMillis) {
    RegisterBrokerRequestHeader requestHeader = new RegisterBrokerRequestHeader();
    requestHeader.setBrokerAddr(brokerAddr);

    RemotingCommand request =
        RemotingCommand.createRequestCommand(RequestCode.REGISTER_BROKER, requestHeader);

    try {
        RemotingCommand response = RemotingHelper.invokeSync(nsaddr, request, timeoutMillis);
        if (response != null) {
            return ResponseCode.SUCCESS == response.getCode();
        }
    } catch (Exception e) {
        log.error("Failed to register broker", e);
    }

    return false;
}
 
Example #7
Source File: MQProtosHelper.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
public static boolean registerBrokerToNameServer(final String nsaddr, final String brokerAddr,
    final long timeoutMillis) {
    RegisterBrokerRequestHeader requestHeader = new RegisterBrokerRequestHeader();
    requestHeader.setBrokerAddr(brokerAddr);

    RemotingCommand request =
        RemotingCommand.createRequestCommand(RequestCode.REGISTER_BROKER, requestHeader);

    try {
        RemotingCommand response = RemotingHelper.invokeSync(nsaddr, request, timeoutMillis);
        if (response != null) {
            return ResponseCode.SUCCESS == response.getCode();
        }
    } catch (Exception e) {
        log.error("Failed to register broker", e);
    }

    return false;
}
 
Example #8
Source File: MQProtosHelper.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static boolean registerBrokerToNameServer(final String nsaddr, final String brokerAddr,
    final long timeoutMillis) {
    RegisterBrokerRequestHeader requestHeader = new RegisterBrokerRequestHeader();
    requestHeader.setBrokerAddr(brokerAddr);

    RemotingCommand request =
        RemotingCommand.createRequestCommand(RequestCode.REGISTER_BROKER, requestHeader);

    try {
        RemotingCommand response = RemotingHelper.invokeSync(nsaddr, request, timeoutMillis);
        if (response != null) {
            return ResponseCode.SUCCESS == response.getCode();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}
 
Example #9
Source File: DefaultRequestProcessorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private static RemotingCommand genSampleRegisterCmd(boolean reg) {
    RegisterBrokerRequestHeader header = new RegisterBrokerRequestHeader();
    header.setBrokerName("broker");
    RemotingCommand request = RemotingCommand.createRequestCommand(
        reg ? RequestCode.REGISTER_BROKER : RequestCode.UNREGISTER_BROKER, header);
    request.addExtField("brokerName", "broker");
    request.addExtField("brokerAddr", "10.10.1.1");
    request.addExtField("clusterName", "cluster");
    request.addExtField("haServerAddr", "10.10.2.1");
    request.addExtField("brokerId", "2333");
    return request;
}
 
Example #10
Source File: DefaultRequestProcessorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private static RemotingCommand genSampleRegisterCmd(boolean reg) {
    RegisterBrokerRequestHeader header = new RegisterBrokerRequestHeader();
    header.setBrokerName("broker");
    RemotingCommand request = RemotingCommand.createRequestCommand(
        reg ? RequestCode.REGISTER_BROKER : RequestCode.UNREGISTER_BROKER, header);
    request.addExtField("brokerName", "broker");
    request.addExtField("brokerAddr", "10.10.1.1");
    request.addExtField("clusterName", "cluster");
    request.addExtField("haServerAddr", "10.10.2.1");
    request.addExtField("brokerId", "2333");
    return request;
}
 
Example #11
Source File: DefaultRequestProcessor.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private boolean checksum(ChannelHandlerContext ctx, RemotingCommand request,
    RegisterBrokerRequestHeader requestHeader) {
    if (requestHeader.getBodyCrc32() != 0) {
        final int crc32 = UtilAll.crc32(request.getBody());
        if (crc32 != requestHeader.getBodyCrc32()) {
            log.warn(String.format("receive registerBroker request,crc32 not match,from %s",
                RemotingHelper.parseChannelRemoteAddr(ctx.channel())));
            return false;
        }
    }
    return true;
}
 
Example #12
Source File: BrokerOuterAPI.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private RegisterBrokerResult registerBroker(
    final String namesrvAddr,
    final boolean oneway,
    final int timeoutMills,
    final RegisterBrokerRequestHeader requestHeader,
    final byte[] body
) throws RemotingCommandException, MQBrokerException, RemotingConnectException, RemotingSendRequestException, RemotingTimeoutException,
    InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.REGISTER_BROKER, requestHeader);
    request.setBody(body);

    if (oneway) {
        try {
            this.remotingClient.invokeOneway(namesrvAddr, request, timeoutMills);
        } catch (RemotingTooMuchRequestException e) {
            // Ignore
        }
        return null;
    }

    RemotingCommand response = this.remotingClient.invokeSync(namesrvAddr, request, timeoutMills);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            RegisterBrokerResponseHeader responseHeader =
                (RegisterBrokerResponseHeader) response.decodeCommandCustomHeader(RegisterBrokerResponseHeader.class);
            RegisterBrokerResult result = new RegisterBrokerResult();
            result.setMasterAddr(responseHeader.getMasterAddr());
            result.setHaServerAddr(responseHeader.getHaServerAddr());
            if (response.getBody() != null) {
                result.setKvTable(KVTable.decode(response.getBody(), KVTable.class));
            }
            return result;
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #13
Source File: DefaultRequestProcessorTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
private static RemotingCommand genSampleRegisterCmd(boolean reg) {
    RegisterBrokerRequestHeader header = new RegisterBrokerRequestHeader();
    header.setBrokerName("broker");
    RemotingCommand request = RemotingCommand.createRequestCommand(
        reg ? RequestCode.REGISTER_BROKER : RequestCode.UNREGISTER_BROKER, header);
    request.addExtField("brokerName", "broker");
    request.addExtField("brokerAddr", "10.10.1.1");
    request.addExtField("clusterName", "cluster");
    request.addExtField("haServerAddr", "10.10.2.1");
    request.addExtField("brokerId", "2333");
    return request;
}
 
Example #14
Source File: DefaultRequestProcessor.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public RemotingCommand registerBrokerWithFilterServer(ChannelHandlerContext ctx, RemotingCommand request)
    throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(RegisterBrokerResponseHeader.class);
    final RegisterBrokerResponseHeader responseHeader = (RegisterBrokerResponseHeader) response.readCustomHeader();
    final RegisterBrokerRequestHeader requestHeader =
        (RegisterBrokerRequestHeader) request.decodeCommandCustomHeader(RegisterBrokerRequestHeader.class);

    RegisterBrokerBody registerBrokerBody = new RegisterBrokerBody();

    if (request.getBody() != null) {
        //把请求体 转换json之后转对象 body里面是什么到时候去broker里面去看他怎么和namesrv交互即可。
        registerBrokerBody = RegisterBrokerBody.decode(request.getBody(), RegisterBrokerBody.class);
    } else {
        registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setCounter(new AtomicLong(0));
        registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setTimestamp(0);
    }

    RegisterBrokerResult result = this.namesrvController.getRouteInfoManager().registerBroker(
        requestHeader.getClusterName(),
        requestHeader.getBrokerAddr(),
        requestHeader.getBrokerName(),
        requestHeader.getBrokerId(),
        requestHeader.getHaServerAddr(),
        registerBrokerBody.getTopicConfigSerializeWrapper(),
        registerBrokerBody.getFilterServerList(),
        ctx.channel());

    responseHeader.setHaServerAddr(result.getHaServerAddr());
    responseHeader.setMasterAddr(result.getMasterAddr());

    // 获取顺序消息 topic 列表
    byte[] jsonValue = this.namesrvController.getKvConfigManager().getKVListByNamespace(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG);
    response.setBody(jsonValue);

    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
 
Example #15
Source File: DefaultRequestProcessorTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private static RemotingCommand genSampleRegisterCmd(boolean reg) {
    RegisterBrokerRequestHeader header = new RegisterBrokerRequestHeader();
    header.setBrokerName("broker");
    RemotingCommand request = RemotingCommand.createRequestCommand(
        reg ? RequestCode.REGISTER_BROKER : RequestCode.UNREGISTER_BROKER, header);
    request.addExtField("brokerName", "broker");
    request.addExtField("brokerAddr", "10.10.1.1");
    request.addExtField("clusterName", "cluster");
    request.addExtField("haServerAddr", "10.10.2.1");
    request.addExtField("brokerId", "2333");
    return request;
}
 
Example #16
Source File: DefaultRequestProcessorTest.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
private static RemotingCommand genSampleRegisterCmd(boolean reg) {
    RegisterBrokerRequestHeader header = new RegisterBrokerRequestHeader();
    header.setBrokerName("broker");
    RemotingCommand request = RemotingCommand.createRequestCommand(
        reg ? RequestCode.REGISTER_BROKER : RequestCode.UNREGISTER_BROKER, header);
    request.addExtField("brokerName", "broker");
    request.addExtField("brokerAddr", "10.10.1.1");
    request.addExtField("clusterName", "cluster");
    request.addExtField("haServerAddr", "10.10.2.1");
    request.addExtField("brokerId", "2333");
    return request;
}
 
Example #17
Source File: DefaultRequestProcessor.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 进行crc32的校验,保证请求的完整性
 * @param ctx ctx
 * @param request  request
 * @param requestHeader request
 * @return ;
 */
private boolean checksum(ChannelHandlerContext ctx, RemotingCommand request,
    RegisterBrokerRequestHeader requestHeader) {
    if (requestHeader.getBodyCrc32() != 0) {
        final int crc32 = UtilAll.crc32(request.getBody());
        if (crc32 != requestHeader.getBodyCrc32()) {
            log.warn(String.format("receive registerBroker request,crc32 not match,from %s",
                RemotingHelper.parseChannelRemoteAddr(ctx.channel())));
            return false;
        }
    }
    return true;
}
 
Example #18
Source File: DefaultRequestProcessorTest.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private static RemotingCommand genSampleRegisterCmd(boolean reg) {
    RegisterBrokerRequestHeader header = new RegisterBrokerRequestHeader();
    header.setBrokerName("broker");
    RemotingCommand request = RemotingCommand.createRequestCommand(
        reg ? RequestCode.REGISTER_BROKER : RequestCode.UNREGISTER_BROKER, header);
    request.addExtField("brokerName", "broker");
    request.addExtField("brokerAddr", "10.10.1.1");
    request.addExtField("clusterName", "cluster");
    request.addExtField("haServerAddr", "10.10.2.1");
    request.addExtField("brokerId", "2333");
    return request;
}
 
Example #19
Source File: DefaultRequestProcessor.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private boolean checksum(ChannelHandlerContext ctx, RemotingCommand request,
    RegisterBrokerRequestHeader requestHeader) {
    if (requestHeader.getBodyCrc32() != 0) {
        final int crc32 = UtilAll.crc32(request.getBody());
        if (crc32 != requestHeader.getBodyCrc32()) {
            log.warn(String.format("receive registerBroker request,crc32 not match,from %s",
                RemotingHelper.parseChannelRemoteAddr(ctx.channel())));
            return false;
        }
    }
    return true;
}
 
Example #20
Source File: DefaultRequestProcessorTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private static RemotingCommand genSampleRegisterCmd(boolean reg) {
    RegisterBrokerRequestHeader header = new RegisterBrokerRequestHeader();
    header.setBrokerName("broker");
    RemotingCommand request = RemotingCommand.createRequestCommand(
        reg ? RequestCode.REGISTER_BROKER : RequestCode.UNREGISTER_BROKER, header);
    request.addExtField("brokerName", "broker");
    request.addExtField("brokerAddr", "10.10.1.1");
    request.addExtField("clusterName", "cluster");
    request.addExtField("haServerAddr", "10.10.2.1");
    request.addExtField("brokerId", "2333");
    return request;
}
 
Example #21
Source File: DefaultRequestProcessor.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public RemotingCommand registerBrokerWithFilterServer(ChannelHandlerContext ctx, RemotingCommand request)
    throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(RegisterBrokerResponseHeader.class);
    final RegisterBrokerResponseHeader responseHeader = (RegisterBrokerResponseHeader) response.readCustomHeader();

    long start = System.currentTimeMillis();
    final RegisterBrokerRequestHeader requestHeader =
        (RegisterBrokerRequestHeader) request.decodeCommandCustomHeader(RegisterBrokerRequestHeader.class);
    long decodeCost = System.currentTimeMillis() - start;

    RegisterBrokerBody registerBrokerBody = new RegisterBrokerBody();

    long decodeBodyCost = 0;
    if (request.getBody() != null) {
        long startDecodeBody = System.currentTimeMillis();
        registerBrokerBody = RegisterBrokerBody.decode(request.getBody(), RegisterBrokerBody.class);
        decodeBodyCost = System.currentTimeMillis() - startDecodeBody;
    } else {
        registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setCounter(new AtomicLong(0));
        registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setTimestamp(0);
    }

    long startRegister = System.currentTimeMillis();
    RegisterBrokerResult result = this.namesrvController.getRouteInfoManager().registerBroker(
        requestHeader.getClusterName(),
        requestHeader.getBrokerAddr(),
        requestHeader.getBrokerName(),
        requestHeader.getBrokerId(),
        requestHeader.getHaServerAddr(),
        requestHeader.getMaxPhyOffset(),
        requestHeader.getTerm(),
        registerBrokerBody.getTopicConfigSerializeWrapper(),
        registerBrokerBody.getFilterServerList(),
        ctx.channel());
    long registerCost = System.currentTimeMillis() - startRegister;

    responseHeader.setHaServerAddr(result.getHaServerAddr());
    responseHeader.setMasterAddr(result.getMasterAddr());

    byte[] jsonValue = this.namesrvController.getKvConfigManager().getKVListByNamespace(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG);
    response.setBody(jsonValue);

    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    log.info("registerBrokerWithFilterServer cost:{},decodeRequest:{},decodeBody:{},registerCost:{}",
        System.currentTimeMillis() - start,
        decodeCost,
        decodeBodyCost,
        registerCost);
    return response;
}
 
Example #22
Source File: DefaultRequestProcessor.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public RemotingCommand registerBrokerWithFilterServer(ChannelHandlerContext ctx, RemotingCommand request)
    throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(RegisterBrokerResponseHeader.class);
    final RegisterBrokerResponseHeader responseHeader = (RegisterBrokerResponseHeader) response.readCustomHeader();

    long start = System.currentTimeMillis();
    final RegisterBrokerRequestHeader requestHeader =
        (RegisterBrokerRequestHeader) request.decodeCommandCustomHeader(RegisterBrokerRequestHeader.class);
    long decodeCost = System.currentTimeMillis() - start;

    RegisterBrokerBody registerBrokerBody = new RegisterBrokerBody();

    long decodeBodyCost = 0;
    if (request.getBody() != null) {
        long startDecodeBody = System.currentTimeMillis();
        registerBrokerBody = RegisterBrokerBody.decode(request.getBody(), RegisterBrokerBody.class);
        decodeBodyCost = System.currentTimeMillis() - startDecodeBody;
    } else {
        registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setCounter(new AtomicLong(0));
        registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setTimestamp(0);
    }

    long startRegister = System.currentTimeMillis();
    RegisterBrokerResult result = this.namesrvController.getRouteInfoManager().registerBroker(
        requestHeader.getClusterName(),
        requestHeader.getBrokerAddr(),
        requestHeader.getBrokerName(),
        requestHeader.getBrokerId(),
        requestHeader.getHaServerAddr(),
        requestHeader.getMaxPhyOffset(),
        requestHeader.getTerm(),
        registerBrokerBody.getTopicConfigSerializeWrapper(),
        registerBrokerBody.getFilterServerList(),
        ctx.channel());
    long registerCost = System.currentTimeMillis() - startRegister;

    responseHeader.setHaServerAddr(result.getHaServerAddr());
    responseHeader.setMasterAddr(result.getMasterAddr());

    byte[] jsonValue = this.namesrvController.getKvConfigManager().getKVListByNamespace(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG);
    response.setBody(jsonValue);

    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    log.info("registerBrokerWithFilterServer cost:{},decodeRequest:{},decodeBody:{},registerCost:{}",
        System.currentTimeMillis() - start,
        decodeCost,
        decodeBodyCost,
        registerCost);
    return response;
}
 
Example #23
Source File: DefaultRequestProcessor.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
/**
 * 新版本的注册请求
 * @param ctx ctx
 * @param request request
 * @return ;
 * @throws RemotingCommandException ;
 */
public RemotingCommand registerBrokerWithFilterServer(ChannelHandlerContext ctx, RemotingCommand request)
    throws RemotingCommandException {

    final RemotingCommand response = RemotingCommand.createResponseCommand(RegisterBrokerResponseHeader.class);
    final RegisterBrokerResponseHeader responseHeader = (RegisterBrokerResponseHeader) response.readCustomHeader();
    final RegisterBrokerRequestHeader requestHeader =
        (RegisterBrokerRequestHeader) request.decodeCommandCustomHeader(RegisterBrokerRequestHeader.class);

    //校验crc32
    if (!checksum(ctx, request, requestHeader)) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("crc32 not match");
        return response;
    }

    RegisterBrokerBody registerBrokerBody = new RegisterBrokerBody();

    if (request.getBody() != null) {
        try {
            registerBrokerBody = RegisterBrokerBody.decode(request.getBody(), requestHeader.isCompressed());
        } catch (Exception e) {
            throw new RemotingCommandException("Failed to decode RegisterBrokerBody", e);
        }
    } else {
        registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setCounter(new AtomicLong(0));
        registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setTimestamp(0);
    }

    /*
     * 注册Broker,存在filerServerList
     */
    RegisterBrokerResult result = this.namesrvController.getRouteInfoManager().registerBroker(
        requestHeader.getClusterName(),
        requestHeader.getBrokerAddr(),
        requestHeader.getBackUpBrokerAddr(),
        requestHeader.getBrokerName(),
        requestHeader.getBrokerId(),
        requestHeader.getHaServerAddr(),
        registerBrokerBody.getTopicConfigSerializeWrapper(),
        registerBrokerBody.getFilterServerList(),
        ctx.channel());

    //返回结果
    responseHeader.setHaServerAddr(result.getHaServerAddr());
    responseHeader.setMasterAddr(result.getMasterAddr());

    byte[] jsonValue = this.namesrvController.getKvConfigManager().getKVListByNamespace(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG);
    response.setBody(jsonValue);

    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
 
Example #24
Source File: BrokerOuterAPI.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
/**
 * 向某一个NameServer注册Broker的信息
 * @param namesrvAddr namesrvAddr
 * @param oneway oneway
 * @param timeoutMills timeoutMills
 * @param requestHeader 请求的header
 * @param body body
 * @return ;
 * @throws RemotingCommandException ;
 * @throws MQBrokerException ;
 * @throws RemotingConnectException ;
 * @throws RemotingSendRequestException ;
 * @throws RemotingTimeoutException ;
 * @throws InterruptedException ;
 */
private RegisterBrokerResult registerBroker(
    final String namesrvAddr,
    final boolean oneway,
    final int timeoutMills,
    final RegisterBrokerRequestHeader requestHeader,
    final byte[] body
) throws RemotingCommandException,
        MQBrokerException,
        RemotingConnectException,
        RemotingSendRequestException,
        RemotingTimeoutException,
            InterruptedException {


    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.REGISTER_BROKER, requestHeader);
    request.setBody(body);

    if (oneway) {
        try {
            this.remotingClient.invokeOneway(namesrvAddr, request, timeoutMills);
        } catch (RemotingTooMuchRequestException e) {
            // Ignore
        }
        return null;
    }

    RemotingCommand response = this.remotingClient.invokeSync(namesrvAddr, request, timeoutMills);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {

            RegisterBrokerResponseHeader responseHeader =
                (RegisterBrokerResponseHeader) response.decodeCommandCustomHeader(RegisterBrokerResponseHeader.class);
            RegisterBrokerResult result = new RegisterBrokerResult();
            result.setMasterAddr(responseHeader.getMasterAddr());
            result.setHaServerAddr(responseHeader.getHaServerAddr());
            if (response.getBody() != null) {
                //ORDER_TOPIC_CONFIG
                result.setKvTable(KVTable.decode(response.getBody(), KVTable.class));
            }
            return result;
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #25
Source File: DefaultRequestProcessor.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public RemotingCommand registerBrokerWithFilterServer(ChannelHandlerContext ctx, RemotingCommand request)
    throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(RegisterBrokerResponseHeader.class);
    final RegisterBrokerResponseHeader responseHeader = (RegisterBrokerResponseHeader) response.readCustomHeader();
    final RegisterBrokerRequestHeader requestHeader =
        (RegisterBrokerRequestHeader) request.decodeCommandCustomHeader(RegisterBrokerRequestHeader.class);

    if (!checksum(ctx, request, requestHeader)) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("crc32 not match");
        return response;
    }

    RegisterBrokerBody registerBrokerBody = new RegisterBrokerBody();

    if (request.getBody() != null) {
        try {
            registerBrokerBody = RegisterBrokerBody.decode(request.getBody(), requestHeader.isCompressed());
        } catch (Exception e) {
            throw new RemotingCommandException("Failed to decode RegisterBrokerBody", e);
        }
    } else {
        registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setCounter(new AtomicLong(0));
        registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setTimestamp(0);
    }

    RegisterBrokerResult result = this.namesrvController.getRouteInfoManager().registerBroker(
        requestHeader.getClusterName(),
        requestHeader.getBrokerAddr(),
        requestHeader.getBrokerName(),
        requestHeader.getBrokerId(),
        requestHeader.getHaServerAddr(),
        registerBrokerBody.getTopicConfigSerializeWrapper(),
        registerBrokerBody.getFilterServerList(),
        ctx.channel());

    responseHeader.setHaServerAddr(result.getHaServerAddr());
    responseHeader.setMasterAddr(result.getMasterAddr());

    byte[] jsonValue = this.namesrvController.getKvConfigManager().getKVListByNamespace(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG);
    response.setBody(jsonValue);

    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
 
Example #26
Source File: DefaultRequestProcessor.java    From rocketmq-4.3.0 with Apache License 2.0 4 votes vote down vote up
public RemotingCommand registerBrokerWithFilterServer(ChannelHandlerContext ctx, RemotingCommand request)
        throws RemotingCommandException {
        final RemotingCommand response = RemotingCommand.createResponseCommand(RegisterBrokerResponseHeader.class);
        final RegisterBrokerResponseHeader responseHeader = (RegisterBrokerResponseHeader) response.readCustomHeader();
        final RegisterBrokerRequestHeader requestHeader =
            (RegisterBrokerRequestHeader) request.decodeCommandCustomHeader(RegisterBrokerRequestHeader.class);

        if (!checksum(ctx, request, requestHeader)) {
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setRemark("crc32 not match");
            return response;
        }

        RegisterBrokerBody registerBrokerBody = new RegisterBrokerBody();

        if (request.getBody() != null) {
            try {
                registerBrokerBody = RegisterBrokerBody.decode(request.getBody(), requestHeader.isCompressed());
            } catch (Exception e) {
                throw new RemotingCommandException("Failed to decode RegisterBrokerBody", e);
            }
        } else {
            registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setCounter(new AtomicLong(0));
            registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setTimestamp(0);
        }

//        =》
        RegisterBrokerResult result = this.namesrvController.getRouteInfoManager().registerBroker(
            requestHeader.getClusterName(),
            requestHeader.getBrokerAddr(),
            requestHeader.getBrokerName(),
            requestHeader.getBrokerId(),
            requestHeader.getHaServerAddr(),
            registerBrokerBody.getTopicConfigSerializeWrapper(),
            registerBrokerBody.getFilterServerList(),
            ctx.channel());

        responseHeader.setHaServerAddr(result.getHaServerAddr());
        responseHeader.setMasterAddr(result.getMasterAddr());

//        获取ORDER_TOPIC_CONFIG命名空间的配置
        byte[] jsonValue = this.namesrvController.getKvConfigManager().getKVListByNamespace(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG);
        response.setBody(jsonValue);

        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);
        return response;
    }
 
Example #27
Source File: BrokerOuterAPI.java    From rocketmq-4.3.0 with Apache License 2.0 4 votes vote down vote up
private RegisterBrokerResult registerBroker(
        final String namesrvAddr,
        final boolean oneway,
        final int timeoutMills,
        final RegisterBrokerRequestHeader requestHeader,
        final byte[] body
    ) throws RemotingCommandException, MQBrokerException, RemotingConnectException, RemotingSendRequestException, RemotingTimeoutException,
        InterruptedException {
        RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.REGISTER_BROKER, requestHeader);
        request.setBody(body);

        if (oneway) {
            try {
//                单线请求,不关心结果 =》
                this.remotingClient.invokeOneway(namesrvAddr, request, timeoutMills);
            } catch (RemotingTooMuchRequestException e) {
                // Ignore
            }
            return null;
        }

//        broker同步向namesrv注册broker=》
        RemotingCommand response = this.remotingClient.invokeSync(namesrvAddr, request, timeoutMills);
        assert response != null;
        switch (response.getCode()) {
            case ResponseCode.SUCCESS: {
                RegisterBrokerResponseHeader responseHeader =
                    (RegisterBrokerResponseHeader) response.decodeCommandCustomHeader(RegisterBrokerResponseHeader.class);
                RegisterBrokerResult result = new RegisterBrokerResult();
                result.setMasterAddr(responseHeader.getMasterAddr());
                result.setHaServerAddr(responseHeader.getHaServerAddr());
                if (response.getBody() != null) {
                    result.setKvTable(KVTable.decode(response.getBody(), KVTable.class));
                }
                return result;
            }
            default:
                break;
        }

        throw new MQBrokerException(response.getCode(), response.getRemark());
    }