/** * Returns {@code true} if the explained shard is primary, {@code false} otherwise. */ public boolean isPrimary() { return shardRouting.primary(); }
public void add(ShardRouting shardRouting) { if(shardRouting.primary()) { primaries++; } unassigned.add(shardRouting); }
@Override public Decision canForceAllocatePrimary(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { assert shardRouting.primary() : "must not call canForceAllocatePrimary on a non-primary shard " + shardRouting; // check if we have passed the maximum retry threshold through canAllocate, // if so, we don't want to force the primary allocation here return canAllocate(shardRouting, node, allocation); } }
private boolean assertPrimaryMode() { assert shardRouting.primary() && replicationTracker.isPrimaryMode() : "shard " + shardRouting + " is not a primary shard in primary mode"; return true; }
@Override public Decision canForceAllocatePrimary(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { assert shardRouting.primary() : "must not call canForceAllocatePrimary on a non-primary shard " + shardRouting; return canAllocate(shardRouting, node, allocation); } }
/** * Returns whether the shard is a relocated primary, i.e. not in charge anymore of replicating changes (see {@link ReplicationTracker}). */ public boolean isRelocatedPrimary() { assert shardRouting.primary() : "only call isRelocatedPrimary on primary shard"; return replicationTracker.isRelocated(); }
private int numDocsOnPrimary(List<ShardRouting> shards, Map<String, PreSyncedFlushResponse> preSyncResponses) { for (ShardRouting shard : shards) { if (shard.primary()) { final PreSyncedFlushResponse resp = preSyncResponses.get(shard.currentNodeId()); if (resp != null) { return resp.numDocs; } } } return PreSyncedFlushResponse.UNKNOWN_NUM_DOCS; }
@Override public Decision canForceAllocatePrimary(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { assert shardRouting.primary() : "must not call canForceAllocatePrimary on a non-primary shard " + shardRouting; return canAllocate(shardRouting, node, allocation); } }
@Override public void startedPrimaryReinitialized(ShardRouting startedPrimaryShard, ShardRouting initializedShard) { assert startedPrimaryShard.primary() && startedPrimaryShard.started() : "expected started primary shard " + startedPrimaryShard; assert initializedShard.primary() && initializedShard.initializing(): "expected initializing primary shard " + initializedShard; setChanged(); }
/** * Computes and returns the design for allocating a single unassigned shard. If called on an assigned shard, * {@link AllocateUnassignedDecision#NOT_TAKEN} is returned. */ public AllocateUnassignedDecision decideUnassignedShardAllocation(ShardRouting unassignedShard, RoutingAllocation routingAllocation) { if (unassignedShard.primary()) { return primaryShardAllocator.makeAllocationDecision(unassignedShard, routingAllocation, logger); } else { return replicaShardAllocator.makeAllocationDecision(unassignedShard, routingAllocation, logger); } }
/** * Returns the active primary shard for the given shard id or <code>null</code> if * no primary is found or the primary is not active. */ public ShardRouting activePrimary(ShardId shardId) { for (ShardRouting shardRouting : assignedShards(shardId)) { if (shardRouting.primary() && shardRouting.active()) { return shardRouting; } } return null; }
@Override public void replicaPromoted(ShardRouting replicaShard) { assert replicaShard.started() && replicaShard.primary() == false : "expected started replica shard " + replicaShard; setChanged(); }
/** * Method that incorporates the ShardId for the shard into a string that * includes a 'p' or 'r' depending on whether the shard is a primary. */ static String shardIdentifierFromRouting(ShardRouting shardRouting) { return shardRouting.shardId().toString() + "[" + (shardRouting.primary() ? "p" : "r") + "]"; } }
@Override public void relocationSourceRemoved(ShardRouting removedReplicaRelocationSource) { assert removedReplicaRelocationSource.primary() == false && removedReplicaRelocationSource.isRelocationTarget() : "expected replica relocation target shard " + removedReplicaRelocationSource; setChanged(); }
private boolean canRecover(IndexShard indexShard) { if (indexShard.state() == IndexShardState.CLOSED) { // got closed on us, just ignore this recovery return false; } if (indexShard.routingEntry().primary() == false) { throw new IndexShardRecoveryException(shardId, "Trying to recover when the shard is in backup state", null); } return true; }
@Override public void initializedReplicaReinitialized(ShardRouting oldReplica, ShardRouting reinitializedReplica) { assert oldReplica.initializing() && oldReplica.primary() == false : "expected initializing replica shard " + oldReplica; assert reinitializedReplica.initializing() && reinitializedReplica.primary() == false : "expected reinitialized replica shard " + reinitializedReplica; assert oldReplica.allocationId().getId().equals(reinitializedReplica.allocationId().getId()) == false : "expected allocation id to change for reinitialized replica shard (old: " + oldReplica + " new: " + reinitializedReplica + ")"; setChanged(); }
@Override public void shardInitialized(ShardRouting unassignedShard, ShardRouting initializedShard) { assert initializedShard.isRelocationTarget() == false : "shardInitialized is not called on relocation target: " + initializedShard; if (initializedShard.primary()) { increasePrimaryTerm(initializedShard.shardId()); Updates updates = changes(initializedShard.shardId()); assert updates.initializedPrimary == null : "Primary cannot be initialized more than once in same allocation round: " + "(previous: " + updates.initializedPrimary + ", next: " + initializedShard + ")"; updates.initializedPrimary = initializedShard; } }
public boolean restoreFromRepository(Repository repository) { assert shardRouting.primary() : "recover from store only makes sense if the shard is a primary shard"; assert recoveryState.getRecoverySource().getType() == RecoverySource.Type.SNAPSHOT : "invalid recovery type: " + recoveryState.getRecoverySource(); StoreRecovery storeRecovery = new StoreRecovery(shardId, logger); return storeRecovery.recoverFromRepository(this, repository); }
/** * Is the allocator responsible for allocating the given {@link ShardRouting}? */ private static boolean isResponsibleFor(final ShardRouting shard) { return shard.primary() // must be primary && shard.unassigned() // must be unassigned // only handle either an existing store or a snapshot recovery && (shard.recoverySource().getType() == RecoverySource.Type.EXISTING_STORE || shard.recoverySource().getType() == RecoverySource.Type.SNAPSHOT); }
@Override public Decision canAllocate(ShardRouting shardRouting, RoutingAllocation allocation) { if (shardRouting.primary()) { return allocation.decision(Decision.YES, NAME, "shard is primary and can be allocated"); } ShardRouting primary = allocation.routingNodes().activePrimary(shardRouting.shardId()); if (primary == null) { return allocation.decision(Decision.NO, NAME, "primary shard for this replica is not yet active"); } return allocation.decision(Decision.YES, NAME, "primary shard for this replica is already active"); } }