private async Task <IList <TopicRouteInfo> > GetTopicRouteInfoListAsync(string topic) { var nameServerRemotingClient = GetAvailableNameServerRemotingClient(); if (nameServerRemotingClient == null) { return(null); } var request = new GetTopicRouteInfoRequest { ClientRole = _producer != null ? ClientRole.Producer : ClientRole.Consumer, ClusterName = _setting.ClusterName, OnlyFindMaster = _setting.OnlyFindMasterBroker, Topic = topic }; var data = _binarySerializer.Serialize(request); var remotingRequest = new RemotingRequest((int)NameServerRequestCode.GetTopicRouteInfo, data); var remotingResponse = await nameServerRemotingClient.InvokeAsync(remotingRequest, 5 * 1000); if (remotingResponse.ResponseCode != ResponseCode.Success) { _logger.ErrorFormat("Get topic route info from name server failed, topic: {0}, nameServerAddress: {1}, remoting response code: {2}, errorMessage: {3}", topic, nameServerRemotingClient.ServerEndPoint.ToAddress(), remotingResponse.ResponseCode, Encoding.UTF8.GetString(remotingResponse.ResponseBody)); return(null); } return(_binarySerializer.Deserialize <IList <TopicRouteInfo> >(remotingResponse.ResponseBody)); }
public async Task <IList <TopicRouteInfo> > GetTopicRouteInfo(GetTopicRouteInfoRequest request) { using (await _asyncLock.LockAsync()) { var returnList = new List <TopicRouteInfo>(); if (string.IsNullOrEmpty(request.ClusterName) || !_clusterDict.TryGetValue(request.ClusterName, out Cluster cluster)) { return(returnList); } foreach (var brokerGroup in cluster.BrokerGroups.Values) { foreach (var broker in brokerGroup.Brokers.Values) { if (request.OnlyFindMaster && broker.BrokerInfo.BrokerRole != (int)BrokerRole.Master) { continue; } var queueList = new List <int>(); var topicQueueInfoList = broker.TopicQueueInfoList.Where(x => x.Topic == request.Topic).ToList(); if (topicQueueInfoList.Count > 0) { if (request.ClientRole == ClientRole.Producer) { queueList = topicQueueInfoList.Where(x => x.ProducerVisible).Select(x => x.QueueId).ToList(); } else if (request.ClientRole == ClientRole.Consumer) { queueList = topicQueueInfoList.Where(x => x.ConsumerVisible).Select(x => x.QueueId).ToList(); } } else if (_nameServerController.Setting.AutoCreateTopic) { var queueIds = await CreateTopicOnBroker(request.Topic, broker); queueList = queueIds.ToList(); } returnList.Add(new TopicRouteInfo { BrokerInfo = broker.BrokerInfo, QueueInfo = queueList }); } } return(returnList); } }