Search in sources :

Example 11 with InetAddressAndPort

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

the class HintVerbHandler method doVerb.

public void doVerb(Message<HintMessage> message) {
    UUID hostId = message.payload.hostId;
    Hint hint = message.payload.hint;
    InetAddressAndPort address = StorageService.instance.getEndpointForHostId(hostId);
    // is schema agreement between the sender and the receiver.
    if (hint == null) {
        logger.trace("Failed to decode and apply a hint for {}: {} - table with id {} is unknown", address, hostId, message.payload.unknownTableID);
        respond(message);
        return;
    }
    // We must perform validation before applying the hint, and there is no other place to do it other than here.
    try {
        hint.mutation.getPartitionUpdates().forEach(PartitionUpdate::validate);
    } catch (MarshalException e) {
        logger.warn("Failed to validate a hint for {}: {} - skipped", address, hostId);
        respond(message);
        return;
    }
    if (!hostId.equals(StorageService.instance.getLocalHostUUID())) {
        // the node is not the final destination of the hint (must have gotten it from a decommissioning node),
        // so just store it locally, to be delivered later.
        HintsService.instance.write(hostId, hint);
        respond(message);
    } else if (!StorageProxy.instance.appliesLocally(hint.mutation)) {
        // the topology has changed, and we are no longer a replica of the mutation - since we don't know which node(s)
        // it has been handed over to, re-address the hint to all replicas; see CASSANDRA-5902.
        HintsService.instance.writeForAllReplicas(hint);
        respond(message);
    } else {
        // the common path - the node is both the destination and a valid replica for the hint.
        hint.applyFuture().addCallback(o -> respond(message), e -> logger.debug("Failed to apply hint", e));
    }
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) MessagingService(org.apache.cassandra.net.MessagingService) PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate) Logger(org.slf4j.Logger) StorageProxy(org.apache.cassandra.service.StorageProxy) LoggerFactory(org.slf4j.LoggerFactory) StorageService(org.apache.cassandra.service.StorageService) Message(org.apache.cassandra.net.Message) UUID(java.util.UUID) IVerbHandler(org.apache.cassandra.net.IVerbHandler) MarshalException(org.apache.cassandra.serializers.MarshalException) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) MarshalException(org.apache.cassandra.serializers.MarshalException) UUID(java.util.UUID) PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate)

Example 12 with InetAddressAndPort

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

the class HintsService method deleteAllHintsForEndpoint.

/**
 * Deletes all hints for the provided destination. Doesn't make snapshots - should be used with care.
 *
 * @param address inet address of the target node - encoded as a string for easier JMX consumption
 */
public void deleteAllHintsForEndpoint(String address) {
    InetAddressAndPort target;
    try {
        target = InetAddressAndPort.getByName(address);
    } catch (UnknownHostException e) {
        throw new IllegalArgumentException(e);
    }
    deleteAllHintsForEndpoint(target);
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) UnknownHostException(java.net.UnknownHostException)

Example 13 with InetAddressAndPort

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

the class ReadCallback method onResponse.

@Override
public void onResponse(Message<ReadResponse> message) {
    assertWaitingFor(message.from());
    Map<ParamType, Object> params = message.header.params();
    InetAddressAndPort from = message.from();
    if (WarningContext.isSupported(params.keySet())) {
        RequestFailureReason reason = getWarningContext().updateCounters(params, from);
        if (reason != null) {
            onFailure(message.from(), reason);
            return;
        }
    }
    resolver.preprocess(message);
    /*
         * Ensure that data is present and the response accumulator has properly published the
         * responses it has received. This may result in not signaling immediately when we receive
         * the minimum number of required results, but it guarantees at least the minimum will
         * be accessible when we do signal. (see CASSANDRA-16807)
         */
    if (resolver.isDataPresent() && resolver.responses.size() >= blockFor)
        condition.signalAll();
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) RequestFailureReason(org.apache.cassandra.exceptions.RequestFailureReason) ParamType(org.apache.cassandra.net.ParamType)

Example 14 with InetAddressAndPort

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

the class RowIteratorMergeListener method close.

public void close() {
    boolean hasRepairs = false;
    for (int i = 0; !hasRepairs && i < repairs.length; ++i) hasRepairs = repairs[i] != null;
    if (!hasRepairs)
        return;
    PartitionUpdate fullDiffRepair = null;
    if (buildFullDiff && repairs[repairs.length - 1] != null)
        fullDiffRepair = repairs[repairs.length - 1].build();
    Map<Replica, Mutation> mutations = Maps.newHashMapWithExpectedSize(writePlan.contacts().size());
    ObjectIntHashMap<InetAddressAndPort> sourceIds = new ObjectIntHashMap<>(((repairs.length + 1) * 4) / 3);
    for (int i = 0; i < readPlan.contacts().size(); ++i) sourceIds.put(readPlan.contacts().get(i).endpoint(), 1 + i);
    for (Replica replica : writePlan.contacts()) {
        PartitionUpdate update = null;
        int i = -1 + sourceIds.get(replica.endpoint());
        if (i < 0)
            update = fullDiffRepair;
        else if (repairs[i] != null)
            update = repairs[i].build();
        Mutation mutation = BlockingReadRepairs.createRepairMutation(update, readPlan.consistencyLevel(), replica.endpoint(), false);
        if (mutation == null)
            continue;
        mutations.put(replica, mutation);
    }
    readRepair.repairPartition(partitionKey, mutations, writePlan);
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) ObjectIntHashMap(com.carrotsearch.hppc.ObjectIntHashMap) Mutation(org.apache.cassandra.db.Mutation) Replica(org.apache.cassandra.locator.Replica) PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate)

Example 15 with InetAddressAndPort

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

the class GossipTest method nodeDownDuringMove.

@Test
public void nodeDownDuringMove() throws Throwable {
    int liveCount = 1;
    try (Cluster cluster = Cluster.build(2 + liveCount).withConfig(config -> config.with(NETWORK).with(GOSSIP)).createWithoutStarting()) {
        int fail = liveCount + 1;
        int late = fail + 1;
        for (int i = 1; i <= liveCount; ++i) cluster.get(i).startup();
        cluster.get(fail).startup();
        Collection<String> expectTokens = cluster.get(fail).callsOnInstance(() -> StorageService.instance.getTokenMetadata().getTokens(FBUtilities.getBroadcastAddressAndPort()).stream().map(Object::toString).collect(Collectors.toList())).call();
        InetSocketAddress failAddress = cluster.get(fail).broadcastAddress();
        // wait for NORMAL state
        for (int i = 1; i <= liveCount; ++i) {
            cluster.get(i).acceptsOnInstance((InetSocketAddress address) -> {
                EndpointState ep;
                InetAddressAndPort endpoint = toCassandraInetAddressAndPort(address);
                while (null == (ep = Gossiper.instance.getEndpointStateForEndpoint(endpoint)) || ep.getApplicationState(ApplicationState.STATUS_WITH_PORT) == null || !ep.getApplicationState(ApplicationState.STATUS_WITH_PORT).value.startsWith("NORMAL")) LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(10L));
            }).accept(failAddress);
        }
        // set ourselves to MOVING, and wait for it to propagate
        cluster.get(fail).runOnInstance(() -> {
            Token token = Iterables.getFirst(StorageService.instance.getTokenMetadata().getTokens(FBUtilities.getBroadcastAddressAndPort()), null);
            Gossiper.instance.addLocalApplicationState(ApplicationState.STATUS_WITH_PORT, StorageService.instance.valueFactory.moving(token));
        });
        for (int i = 1; i <= liveCount; ++i) {
            cluster.get(i).acceptsOnInstance((InetSocketAddress address) -> {
                EndpointState ep;
                InetAddressAndPort endpoint = toCassandraInetAddressAndPort(address);
                while (null == (ep = Gossiper.instance.getEndpointStateForEndpoint(endpoint)) || (ep.getApplicationState(ApplicationState.STATUS_WITH_PORT) == null || !ep.getApplicationState(ApplicationState.STATUS_WITH_PORT).value.startsWith("MOVING"))) LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100L));
            }).accept(failAddress);
        }
        cluster.get(fail).shutdown(false).get();
        cluster.get(late).startup();
        cluster.get(late).acceptsOnInstance((InetSocketAddress address) -> {
            EndpointState ep;
            InetAddressAndPort endpoint = toCassandraInetAddressAndPort(address);
            while (null == (ep = Gossiper.instance.getEndpointStateForEndpoint(endpoint)) || !ep.getApplicationState(ApplicationState.STATUS_WITH_PORT).value.startsWith("MOVING")) LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100L));
        }).accept(failAddress);
        Collection<String> tokens = cluster.get(late).appliesOnInstance((InetSocketAddress address) -> StorageService.instance.getTokenMetadata().getTokens(toCassandraInetAddressAndPort(address)).stream().map(Object::toString).collect(Collectors.toList())).apply(failAddress);
        Assert.assertEquals(expectTokens, tokens);
    }
}
Also used : EndpointState(org.apache.cassandra.gms.EndpointState) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) Iterables(com.google.common.collect.Iterables) MethodDelegation(net.bytebuddy.implementation.MethodDelegation) ByteBuddy(net.bytebuddy.ByteBuddy) ElementMatchers.takesArguments(net.bytebuddy.matcher.ElementMatchers.takesArguments) PendingRangeCalculatorService(org.apache.cassandra.service.PendingRangeCalculatorService) Gossiper(org.apache.cassandra.gms.Gossiper) ClusterUtils.runAndWaitForLogs(org.apache.cassandra.distributed.shared.ClusterUtils.runAndWaitForLogs) Token(org.apache.cassandra.dht.Token) StreamResultFuture(org.apache.cassandra.streaming.StreamResultFuture) DistributedTestSnitch.toCassandraInetAddressAndPort(org.apache.cassandra.distributed.impl.DistributedTestSnitch.toCassandraInetAddressAndPort) ClusterUtils.getLocalToken(org.apache.cassandra.distributed.shared.ClusterUtils.getLocalToken) NETWORK(org.apache.cassandra.distributed.api.Feature.NETWORK) ApplicationState(org.apache.cassandra.gms.ApplicationState) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) FBUtilities(org.apache.cassandra.utils.FBUtilities) ElementMatchers.named(net.bytebuddy.matcher.ElementMatchers.named) java.util.concurrent(java.util.concurrent) org.apache.cassandra.distributed.api(org.apache.cassandra.distributed.api) Collection(java.util.Collection) StorageService(org.apache.cassandra.service.StorageService) Test(org.junit.Test) ClassLoadingStrategy(net.bytebuddy.dynamic.loading.ClassLoadingStrategy) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) LockSupport(java.util.concurrent.locks.LockSupport) Futures(com.google.common.util.concurrent.Futures) StreamPlan(org.apache.cassandra.streaming.StreamPlan) Closeable(java.io.Closeable) Cluster(org.apache.cassandra.distributed.Cluster) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) GOSSIP(org.apache.cassandra.distributed.api.Feature.GOSSIP) EndpointState(org.apache.cassandra.gms.EndpointState) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) DistributedTestSnitch.toCassandraInetAddressAndPort(org.apache.cassandra.distributed.impl.DistributedTestSnitch.toCassandraInetAddressAndPort) InetSocketAddress(java.net.InetSocketAddress) Cluster(org.apache.cassandra.distributed.Cluster) Token(org.apache.cassandra.dht.Token) ClusterUtils.getLocalToken(org.apache.cassandra.distributed.shared.ClusterUtils.getLocalToken) Test(org.junit.Test)

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