Search in sources :

Example 26 with InetAddressAndPort

use of org.apache.cassandra.locator.InetAddressAndPort in project cassandra by apache.

the class StorageProxy method appliesLocally.

public boolean appliesLocally(Mutation mutation) {
    String keyspaceName = mutation.getKeyspaceName();
    Token token = mutation.key().getToken();
    InetAddressAndPort local = FBUtilities.getBroadcastAddressAndPort();
    return ReplicaLayout.forTokenWriteLiveAndDown(Keyspace.open(keyspaceName), token).all().endpoints().contains(local);
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) EndpointsForToken(org.apache.cassandra.locator.EndpointsForToken) Token(org.apache.cassandra.dht.Token)

Example 27 with InetAddressAndPort

use of org.apache.cassandra.locator.InetAddressAndPort in project cassandra by apache.

the class RangeRelocator method calculateToFromStreams.

public void calculateToFromStreams() {
    logger.debug("Current tmd: {}, Updated tmd: {}", tokenMetaClone, tokenMetaCloneAllSettled);
    for (String keyspace : keyspaceNames) {
        // replication strategy of the current keyspace
        AbstractReplicationStrategy strategy = Keyspace.open(keyspace).getReplicationStrategy();
        logger.info("Calculating ranges to stream and request for keyspace {}", keyspace);
        // From what I have seen we only ever call this with a single token from StorageService.move(Token)
        for (Token newToken : tokens) {
            Collection<Token> currentTokens = tokenMetaClone.getTokens(localAddress);
            if (currentTokens.size() > 1 || currentTokens.isEmpty()) {
                throw new AssertionError("Unexpected current tokens: " + currentTokens);
            }
            // calculated parts of the ranges to request/stream from/to nodes in the ring
            Pair<RangesAtEndpoint, RangesAtEndpoint> streamAndFetchOwnRanges;
            // so it's easier to just identify this case up front.
            if (tokenMetaClone.getTopology().getDatacenterEndpoints().get(DatabaseDescriptor.getEndpointSnitch().getLocalDatacenter()).size() > 1) {
                // getting collection of the currently used ranges by this keyspace
                RangesAtEndpoint currentReplicas = strategy.getAddressReplicas(localAddress);
                // collection of ranges which this node will serve after move to the new token
                RangesAtEndpoint updatedReplicas = strategy.getPendingAddressRanges(tokenMetaClone, newToken, localAddress);
                streamAndFetchOwnRanges = calculateStreamAndFetchRanges(currentReplicas, updatedReplicas);
            } else {
                streamAndFetchOwnRanges = Pair.create(RangesAtEndpoint.empty(localAddress), RangesAtEndpoint.empty(localAddress));
            }
            RangesByEndpoint rangesToStream = calculateRangesToStreamWithEndpoints(streamAndFetchOwnRanges.left, strategy, tokenMetaClone, tokenMetaCloneAllSettled);
            logger.info("Endpoint ranges to stream to " + rangesToStream);
            // stream ranges
            for (InetAddressAndPort address : rangesToStream.keySet()) {
                logger.debug("Will stream range {} of keyspace {} to endpoint {}", rangesToStream.get(address), keyspace, address);
                RangesAtEndpoint ranges = rangesToStream.get(address);
                streamPlan.transferRanges(address, keyspace, ranges);
            }
            Multimap<InetAddressAndPort, RangeStreamer.FetchReplica> rangesToFetch = calculateRangesToFetchWithPreferredEndpoints(streamAndFetchOwnRanges.right, strategy, keyspace, tokenMetaClone, tokenMetaCloneAllSettled);
            // stream requests
            rangesToFetch.asMap().forEach((address, sourceAndOurReplicas) -> {
                RangesAtEndpoint full = sourceAndOurReplicas.stream().filter(pair -> pair.remote.isFull()).map(pair -> pair.local).collect(RangesAtEndpoint.collector(localAddress));
                RangesAtEndpoint trans = sourceAndOurReplicas.stream().filter(pair -> pair.remote.isTransient()).map(pair -> pair.local).collect(RangesAtEndpoint.collector(localAddress));
                logger.debug("Will request range {} of keyspace {} from endpoint {}", rangesToFetch.get(address), keyspace, address);
                streamPlan.requestRanges(address, keyspace, full, trans);
            });
            logger.debug("Keyspace {}: work map {}.", keyspace, rangesToFetch);
        }
    }
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) Range(org.apache.cassandra.dht.Range) Multimap(com.google.common.collect.Multimap) Future(java.util.concurrent.Future) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Pair(org.apache.cassandra.utils.Pair) RangeStreamer(org.apache.cassandra.dht.RangeStreamer) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) StreamOperation(org.apache.cassandra.streaming.StreamOperation) Keyspace(org.apache.cassandra.db.Keyspace) EndpointsForRange(org.apache.cassandra.locator.EndpointsForRange) Logger(org.slf4j.Logger) FBUtilities(org.apache.cassandra.utils.FBUtilities) Collection(java.util.Collection) Set(java.util.Set) RangesByEndpoint(org.apache.cassandra.locator.RangesByEndpoint) RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) Replica(org.apache.cassandra.locator.Replica) StreamState(org.apache.cassandra.streaming.StreamState) List(java.util.List) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) StreamPlan(org.apache.cassandra.streaming.StreamPlan) EndpointsByReplica(org.apache.cassandra.locator.EndpointsByReplica) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FailureDetector(org.apache.cassandra.gms.FailureDetector) Collections(java.util.Collections) RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) Token(org.apache.cassandra.dht.Token) RangesByEndpoint(org.apache.cassandra.locator.RangesByEndpoint)

Example 28 with InetAddressAndPort

use of org.apache.cassandra.locator.InetAddressAndPort in project cassandra by apache.

the class StorageProxy method shouldHint.

/**
 * Determines whether a hint should be stored or not.
 * It rejects early if any of the condition is met:
 * - Hints disabled entirely or for the belonging datacetner of the replica
 * - The replica is transient or is the self node
 * - The replica is no longer part of the ring
 * - The hint window has expired
 * - The hints have reached to the size limit for the node
 * Otherwise, it permits.
 *
 * @param replica, the replica for the hint
 * @param tryEnablePersistentWindow, true to consider hint_window_persistent_enabled; otherwise, ignores
 * @return true to permit or false to reject hint
 */
public static boolean shouldHint(Replica replica, boolean tryEnablePersistentWindow) {
    if (!DatabaseDescriptor.hintedHandoffEnabled() || replica.isTransient() || replica.isSelf())
        return false;
    Set<String> disabledDCs = DatabaseDescriptor.hintedHandoffDisabledDCs();
    if (!disabledDCs.isEmpty()) {
        final String dc = DatabaseDescriptor.getEndpointSnitch().getDatacenter(replica);
        if (disabledDCs.contains(dc)) {
            Tracing.trace("Not hinting {} since its data center {} has been disabled {}", replica, dc, disabledDCs);
            return false;
        }
    }
    InetAddressAndPort endpoint = replica.endpoint();
    int maxHintWindow = DatabaseDescriptor.getMaxHintWindow();
    long endpointDowntime = Gossiper.instance.getEndpointDowntime(endpoint);
    boolean hintWindowExpired = endpointDowntime > maxHintWindow;
    UUID hostIdForEndpoint = StorageService.instance.getHostIdForEndpoint(endpoint);
    if (hostIdForEndpoint == null) {
        Tracing.trace("Discarding hint for endpoint not part of ring: {}", endpoint);
        return false;
    }
    // if persisting hints window, hintWindowExpired might be updated according to the timestamp of the earliest hint
    if (tryEnablePersistentWindow && !hintWindowExpired && DatabaseDescriptor.hintWindowPersistentEnabled()) {
        long earliestHint = HintsService.instance.getEarliestHintForHost(hostIdForEndpoint);
        hintWindowExpired = Clock.Global.currentTimeMillis() - maxHintWindow > earliestHint;
        if (hintWindowExpired)
            Tracing.trace("Not hinting {} for which there is the earliest hint stored at {}", replica, earliestHint);
    }
    if (hintWindowExpired) {
        HintsService.instance.metrics.incrPastWindow(endpoint);
        Tracing.trace("Not hinting {} which has been down {} ms", endpoint, endpointDowntime);
        return false;
    }
    long maxHintsSize = DatabaseDescriptor.getMaxHintsSizePerHost();
    long actualTotalHintsSize = HintsService.instance.getTotalHintsSize(hostIdForEndpoint);
    boolean hasHintsReachedMaxSize = maxHintsSize > 0 && actualTotalHintsSize > maxHintsSize;
    if (hasHintsReachedMaxSize) {
        Tracing.trace("Not hinting {} which has reached to the max hints size {} bytes on disk. The actual hints size on disk: {}", endpoint, maxHintsSize, actualTotalHintsSize);
        return false;
    }
    return true;
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) UUID(java.util.UUID) Hint(org.apache.cassandra.hints.Hint)

Example 29 with InetAddressAndPort

use of org.apache.cassandra.locator.InetAddressAndPort in project cassandra by apache.

the class ActiveRepairService method getNeighbors.

/**
 * Return all of the neighbors with whom we share the provided range.
 *
 * @param keyspaceName keyspace to repair
 * @param keyspaceLocalRanges local-range for given keyspaceName
 * @param toRepair token to repair
 * @param dataCenters the data centers to involve in the repair
 *
 * @return neighbors with whom we share the provided range
 */
public static EndpointsForRange getNeighbors(String keyspaceName, Iterable<Range<Token>> keyspaceLocalRanges, Range<Token> toRepair, Collection<String> dataCenters, Collection<String> hosts) {
    StorageService ss = StorageService.instance;
    EndpointsByRange replicaSets = ss.getRangeToAddressMap(keyspaceName);
    Range<Token> rangeSuperSet = null;
    for (Range<Token> range : keyspaceLocalRanges) {
        if (range.contains(toRepair)) {
            rangeSuperSet = range;
            break;
        } else if (range.intersects(toRepair)) {
            throw new IllegalArgumentException(String.format("Requested range %s intersects a local range (%s) " + "but is not fully contained in one; this would lead to " + "imprecise repair. keyspace: %s", toRepair.toString(), range.toString(), keyspaceName));
        }
    }
    if (rangeSuperSet == null || !replicaSets.containsKey(rangeSuperSet))
        return EndpointsForRange.empty(toRepair);
    EndpointsForRange neighbors = replicaSets.get(rangeSuperSet).withoutSelf();
    if (dataCenters != null && !dataCenters.isEmpty()) {
        TokenMetadata.Topology topology = ss.getTokenMetadata().cloneOnlyTokenMap().getTopology();
        Multimap<String, InetAddressAndPort> dcEndpointsMap = topology.getDatacenterEndpoints();
        Iterable<InetAddressAndPort> dcEndpoints = concat(transform(dataCenters, dcEndpointsMap::get));
        return neighbors.select(dcEndpoints, true);
    } else if (hosts != null && !hosts.isEmpty()) {
        Set<InetAddressAndPort> specifiedHost = new HashSet<>();
        for (final String host : hosts) {
            try {
                final InetAddressAndPort endpoint = InetAddressAndPort.getByName(host.trim());
                if (endpoint.equals(FBUtilities.getBroadcastAddressAndPort()) || neighbors.endpoints().contains(endpoint))
                    specifiedHost.add(endpoint);
            } catch (UnknownHostException e) {
                throw new IllegalArgumentException("Unknown host specified " + host, e);
            }
        }
        if (!specifiedHost.contains(FBUtilities.getBroadcastAddressAndPort()))
            throw new IllegalArgumentException("The current host must be part of the repair");
        if (specifiedHost.size() <= 1) {
            String msg = "Specified hosts %s do not share range %s needed for repair. Either restrict repair ranges " + "with -st/-et options, or specify one of the neighbors that share this range with " + "this node: %s.";
            throw new IllegalArgumentException(String.format(msg, hosts, toRepair, neighbors));
        }
        specifiedHost.remove(FBUtilities.getBroadcastAddressAndPort());
        return neighbors.keep(specifiedHost);
    }
    return neighbors;
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) Collections.synchronizedSet(java.util.Collections.synchronizedSet) ImmutableSet(com.google.common.collect.ImmutableSet) UnknownHostException(java.net.UnknownHostException) EndpointsByRange(org.apache.cassandra.locator.EndpointsByRange) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) EndpointsForRange(org.apache.cassandra.locator.EndpointsForRange)

Example 30 with InetAddressAndPort

use of org.apache.cassandra.locator.InetAddressAndPort in project cassandra by apache.

the class ActiveRepairService method cleanUp.

/**
 * Send Verb.CLEANUP_MSG to the given endpoints. This results in removing parent session object from the
 * endpoint's cache.
 * This method does not throw an exception in case of a messaging failure.
 */
public void cleanUp(UUID parentRepairSession, Set<InetAddressAndPort> endpoints) {
    for (InetAddressAndPort endpoint : endpoints) {
        try {
            if (FailureDetector.instance.isAlive(endpoint)) {
                CleanupMessage message = new CleanupMessage(parentRepairSession);
                Message<CleanupMessage> msg = Message.out(Verb.CLEANUP_MSG, message);
                RequestCallback loggingCallback = new RequestCallback() {

                    @Override
                    public void onResponse(Message msg) {
                        logger.trace("Successfully cleaned up {} parent repair session on {}.", parentRepairSession, endpoint);
                    }

                    @Override
                    public void onFailure(InetAddressAndPort from, RequestFailureReason failureReason) {
                        logger.debug("Failed to clean up parent repair session {} on {}. The uncleaned sessions will " + "be removed on a node restart. This should not be a problem unless you see thousands " + "of messages like this.", parentRepairSession, endpoint);
                    }
                };
                MessagingService.instance().sendWithCallback(msg, endpoint, loggingCallback);
            }
        } catch (Exception exc) {
            logger.warn("Failed to send a clean up message to {}", endpoint, exc);
        }
    }
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) RequestFailureReason(org.apache.cassandra.exceptions.RequestFailureReason) RequestCallback(org.apache.cassandra.net.RequestCallback) PrepareMessage(org.apache.cassandra.repair.messages.PrepareMessage) Message(org.apache.cassandra.net.Message) CleanupMessage(org.apache.cassandra.repair.messages.CleanupMessage) RepairMessage(org.apache.cassandra.repair.messages.RepairMessage) CleanupMessage(org.apache.cassandra.repair.messages.CleanupMessage) NoSuchRepairSessionException(org.apache.cassandra.repair.NoSuchRepairSessionException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Aggregations

InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)281 Test (org.junit.Test)129 Token (org.apache.cassandra.dht.Token)65 TokenMetadata (org.apache.cassandra.locator.TokenMetadata)43 EndpointsForRange (org.apache.cassandra.locator.EndpointsForRange)39 Range (org.apache.cassandra.dht.Range)28 Replica (org.apache.cassandra.locator.Replica)25 ArrayList (java.util.ArrayList)24 ByteBuffer (java.nio.ByteBuffer)23 HashMap (java.util.HashMap)23 UUID (java.util.UUID)22 HashSet (java.util.HashSet)20 Map (java.util.Map)20 Mutation (org.apache.cassandra.db.Mutation)17 PartitionIterator (org.apache.cassandra.db.partitions.PartitionIterator)17 UnfilteredPartitionIterator (org.apache.cassandra.db.partitions.UnfilteredPartitionIterator)16 VersionedValue (org.apache.cassandra.gms.VersionedValue)16 VisibleForTesting (com.google.common.annotations.VisibleForTesting)15 IPartitioner (org.apache.cassandra.dht.IPartitioner)15 BigIntegerToken (org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken)15