@Override protected TopologyResponseDto toResponseDto(DirectBuffer buffer) { final TopologyResponseDto dto = new TopologyResponseDto(); dto.wrap(buffer); return dto; } }
public boolean isLeader() { return getState() == PartitionState.LEADER; }
private static PartitionBrokerRole remapPartitionBrokerRoleEnum(final PartitionDto partition) { switch (partition.getState()) { case LEADER: return PartitionBrokerRole.LEADER; case FOLLOWER: return PartitionBrokerRole.FOLLOWER; default: throw new UnknownPartitionRoleException(partition.getPartitionId(), partition.getState()); } }
@Override public TopologyResponseDto asDto() { final TopologyResponseDto dto = new TopologyResponseDto(); dto.setClusterSize(clusterSize); dto.setPartitionsCount(partitionsCount); dto.setReplicationFactor(replicationFactor); final BrokerDto broker = dto.brokers().add(); final SocketAddress apiContactPoint = member.getClientApiAddress(); broker.setNodeId(member.getNodeId()); broker.setHost( apiContactPoint.getHostBuffer(), 0, apiContactPoint.getHostBuffer().capacity()); broker.setPort(apiContactPoint.port()); .partitionStates() .add() .setPartitionId(partition.getPartitionId()) .setReplicationFactor(partition.getReplicationFactor()) .setState(PartitionState.LEADER); .partitionStates() .add() .setPartitionId(partition.getPartitionId()) .setReplicationFactor(partition.getReplicationFactor()) .setState(PartitionState.FOLLOWER);
public BrokerClusterStateImpl( final TopologyResponseDto topologyDto, final BiConsumer<Integer, SocketAddress> endpointRegistry) { clusterSize = topologyDto.getClusterSize(); partitionsCount = topologyDto.getPartitionsCount(); replicationFactor = topologyDto.getReplicationFactor(); topologyDto .brokers() .forEach( b -> { final int nodeId = b.getNodeId(); endpointRegistry.accept( nodeId, new SocketAddress(bufferAsString(b.getHost()), b.getPort())); brokers.add(nodeId); b.partitionStates() .forEach( p -> { final int partitionId = p.getPartitionId(); partitions.add(partitionId); if (p.isLeader()) { partitionLeaders.put(partitionId, nodeId); } }); }); }
public PartitionDto() { this.declareProperty(stateProp) .declareProperty(partitionIdProp) .declareProperty(replicationFactorProp); }
public BrokerDto() { this.declareProperty(nodeIdProp) .declareProperty(hostProp) .declareProperty(portProp) .declareProperty(partitionStatesProp); }
public TopologyResponseDto() { this.declareProperty(brokersProp) .declareProperty(clusterSizeProp) .declareProperty(partitionsCountProp) .declareProperty(replicationFactorProp); }
@Override public TopologyResponseDto asDto() { final TopologyResponseDto dto = new TopologyResponseDto(); dto.setClusterSize(clusterSize); dto.setPartitionsCount(partitionsCount); dto.setReplicationFactor(replicationFactor); final BrokerDto broker = dto.brokers().add(); final SocketAddress apiContactPoint = member.getClientApiAddress(); broker.setNodeId(member.getNodeId()); broker.setHost( apiContactPoint.getHostBuffer(), 0, apiContactPoint.getHostBuffer().capacity()); broker.setPort(apiContactPoint.port()); .partitionStates() .add() .setPartitionId(partition.getPartitionId()) .setReplicationFactor(partition.getReplicationFactor()) .setState(PartitionState.LEADER); .partitionStates() .add() .setPartitionId(partition.getPartitionId()) .setReplicationFactor(partition.getReplicationFactor()) .setState(PartitionState.FOLLOWER);
public static TopologyResponse toTopologyResponse(long key, TopologyResponseDto brokerResponse) { final TopologyResponse.Builder topologyResponseBuilder = TopologyResponse.newBuilder(); topologyResponseBuilder .setClusterSize(brokerResponse.getClusterSize()) .setPartitionsCount(brokerResponse.getPartitionsCount()) .setReplicationFactor(brokerResponse.getReplicationFactor()); final ArrayList<BrokerInfo> infos = new ArrayList<>(); brokerResponse .brokers() .forEach( broker -> { final Builder brokerInfo = BrokerInfo.newBuilder() .setNodeId(broker.getNodeId()) .setHost(bufferAsString(broker.getHost())) .setPort(broker.getPort()); broker .partitionStates() .forEach( partition -> { final Partition.Builder partitionBuilder = Partition.newBuilder(); partitionBuilder.setPartitionId(partition.getPartitionId()); partitionBuilder.setRole(remapPartitionBrokerRoleEnum(partition)); brokerInfo.addPartitions(partitionBuilder); }); infos.add(brokerInfo.build()); }); topologyResponseBuilder.addAllBrokers(infos); return topologyResponseBuilder.build(); }