/** * Creates a partition identifier from an integer. * * @param group the group identifier * @param id input integer * @return partition identification */ public static PartitionId from(String group, int id) { return new PartitionId(group, id); }
@Override public boolean equals(Object object) { if (object instanceof PartitionMetadata) { PartitionMetadata partition = (PartitionMetadata) object; return partition.id.equals(id) && partition.members.equals(members); } return false; }
@Override public String toString() { return toStringHelper(this) .add("id", id()) .add("group", group) .toString(); } }
private static Collection<LogPartition> buildPartitions(LogPartitionGroupConfig config) { List<LogPartition> partitions = new ArrayList<>(config.getPartitions()); for (int i = 0; i < config.getPartitions(); i++) { partitions.add(new LogPartition(PartitionId.from(config.getName(), i + 1), config)); } return partitions; }
@Override public boolean isRelevant(PartitionGroupMembershipEvent event) { return event.membership().group().equals(partitionId.group()); } };
@Override public int id() { return session.partitionId().id(); }
private static Collection<RaftPartition> buildPartitions( RaftPartitionGroupConfig config, ThreadContextFactory threadContextFactory) { File partitionsDir = new File(config.getStorageConfig().getDirectory(config.getName()), "partitions"); List<RaftPartition> partitions = new ArrayList<>(config.getPartitions()); for (int i = 0; i < config.getPartitions(); i++) { partitions.add(new RaftPartition( PartitionId.from(config.getName(), i + 1), config, new File(partitionsDir, String.valueOf(i + 1)), threadContextFactory)); } return partitions; }
private void updateTerm(long term) { if (term > currentTerm.term()) { recomputeTerm(groupMembershipService.getMembership(partitionId.group())); } }
@Override public AsyncDistributedLogPartition<E> getPartition(E entry) { return partitions.get(client.getPartitionId(BaseEncoding.base16().encode(encode(entry))).id()); }
private static Collection<PrimaryBackupPartition> buildPartitions(PrimaryBackupPartitionGroupConfig config) { List<PrimaryBackupPartition> partitions = new ArrayList<>(config.getPartitions()); for (int i = 0; i < config.getPartitions(); i++) { partitions.add(new PrimaryBackupPartition(PartitionId.from(config.getName(), i + 1), config.getMemberGroupProvider())); } return partitions; }
/** * Handles a cluster membership event. */ private void handleClusterMembershipEvent(ClusterMembershipEvent event) { if (event.type() == ClusterMembershipEvent.Type.MEMBER_ADDED || event.type() == ClusterMembershipEvent.Type.MEMBER_REMOVED) { recomputeTerm(groupMembershipService.getMembership(partitionId.group())); } }
@Override public boolean equals(Object object) { if (object instanceof PartitionEvent) { PartitionEvent that = (PartitionEvent) object; return this.partitionId().equals(that.partitionId()) && this.members.equals(that.members) && Objects.equals(this.primary, that.primary) && this.backups.equals(that.backups); } return false; }
public DefaultAsyncDistributedLog(String name, LogClient client, Serializer serializer) { this.name = checkNotNull(name); this.client = checkNotNull(client); this.serializer = checkNotNull(serializer); client.getPartitions().forEach(partition -> { DefaultAsyncDistributedLogPartition<E> logPartition = new DefaultAsyncDistributedLogPartition<>(this, partition, serializer); partitions.put(partition.partitionId().id(), logPartition); sortedPartitions.add(logPartition); }); }
@Override public void run() { LOGGER.info("RaftClientThread run() >>>"); int raftClientPort = 0; try { raftClientPort = RemoteInterpreterUtils.findRandomAvailablePortOnAllLocalInterfaces(); } catch (IOException e) { LOGGER.error(e.getMessage()); } MemberId memberId = MemberId.from(ZEPL_CLIENT_ID + zeplServerHost + ":" + raftClientPort); Address address = Address.from(zeplServerHost, raftClientPort); raftAddressMap.put(memberId, address); MessagingService messagingManager = NettyMessagingService.builder().withAddress(address).build().start().join(); RaftClientProtocol protocol = new RaftClientMessagingProtocol( messagingManager, protocolSerializer, raftAddressMap::get); raftClient = RaftClient.builder() .withMemberId(memberId) .withPartitionId(PartitionId.from("partition", 1)) .withProtocol(protocol) .build(); raftClient.connect(clusterMemberIds).join(); raftSessionClient = createProxy(raftClient); LOGGER.info("RaftClientThread run() <<<"); } }).start();
int countPrimaries(Registration registration) { if (registration == null) { return 0; } return (int) elections.entrySet().stream() .filter(entry -> !entry.getKey().equals(partitionId)) .filter(entry -> entry.getValue().primary != null) .filter(entry -> { // Get the topic leader's identifier and a list of session identifiers. // Then return true if the leader's identifier matches any of the session's candidates. GroupMember leaderId = entry.getValue().primary(); List<GroupMember> sessionCandidates = entry.getValue().registrations.stream() .filter(r -> r.sessionId == registration.sessionId) .map(r -> r.member()) .collect(Collectors.toList()); return sessionCandidates.stream() .anyMatch(candidate -> Objects.equals(candidate, leaderId)); }) .count(); }
@Override public CompletableFuture<Void> produce(E entry) { byte[] bytes = encode(entry); return partitions.get(client.getPartitionId(BaseEncoding.base16().encode(bytes)).id()).produce(bytes); }