+
+ if (isLeader()) {
+ // Try to assign owners for entities that have no current owner. It's possible the peer that is now up
+ // had previously registered as a candidate and was the only candidate but the owner write tx couldn't be
+ // committed due to a leader change. Eg, the leader is able to successfully commit the candidate add tx but
+ // becomes isolated before it can commit the owner change and switches to follower. The majority partition
+ // with a new leader has the candidate but the entity has no owner. When the partition is healed and the
+ // previously isolated leader reconnects, we'll receive onPeerUp and, if there's still no owner, the
+ // previous leader will gain ownership.
+ selectNewOwnerForEntitiesOwnedBy(ImmutableSet.of(""));
+ }
+ }
+
+ private static Collection<String> getCandidateNames(final MapEntryNode entity) {
+ Collection<MapEntryNode> candidates = ((MapNode)entity.getChild(CANDIDATE_NODE_ID).get()).getValue();
+ Collection<String> candidateNames = new ArrayList<>(candidates.size());
+ for (MapEntryNode candidate: candidates) {
+ candidateNames.add(candidate.getChild(CANDIDATE_NAME_NODE_ID).get().getValue().toString());
+ }
+
+ return candidateNames;
+ }
+
+ private void searchForEntitiesOwnedBy(final Set<String> ownedBy, final EntityWalker walker) {
+ LOG.debug("{}: Searching for entities owned by {}", persistenceId(), ownedBy);
+
+ searchForEntities((entityTypeNode, entityNode) -> {
+ Optional<DataContainerChild<? extends PathArgument, ?>> possibleOwner =
+ entityNode.getChild(ENTITY_OWNER_NODE_ID);
+ String currentOwner = possibleOwner.isPresent() ? possibleOwner.get().getValue().toString() : "";
+ if (ownedBy.contains(currentOwner)) {
+ walker.onEntity(entityTypeNode, entityNode);
+ }
+ });