Search in sources :

Example 21 with InetAddressAndPort

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

the class SystemKeyspace method getTransferredRanges.

public static synchronized Map<InetAddressAndPort, Set<Range<Token>>> getTransferredRanges(String description, String keyspace, IPartitioner partitioner) {
    Map<InetAddressAndPort, Set<Range<Token>>> result = new HashMap<>();
    String query = "SELECT * FROM system.%s WHERE operation = ? AND keyspace_name = ?";
    UntypedResultSet rs = executeInternal(String.format(query, TRANSFERRED_RANGES_V2), description, keyspace);
    for (UntypedResultSet.Row row : rs) {
        InetAddress peerAddress = row.getInetAddress("peer");
        int port = row.getInt("peer_port");
        InetAddressAndPort peer = InetAddressAndPort.getByAddressOverrideDefaults(peerAddress, port);
        Set<ByteBuffer> rawRanges = row.getSet("ranges", BytesType.instance);
        Set<Range<Token>> ranges = Sets.newHashSetWithExpectedSize(rawRanges.size());
        for (ByteBuffer rawRange : rawRanges) {
            ranges.add(byteBufferToRange(rawRange, partitioner));
        }
        result.put(peer, ranges);
    }
    return ImmutableMap.copyOf(result);
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) ImmutableSet(com.google.common.collect.ImmutableSet) UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) ByteBuffer(java.nio.ByteBuffer) UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) InetAddress(java.net.InetAddress)

Example 22 with InetAddressAndPort

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

the class SystemKeyspace method loadDcRackInfo.

/**
 * Return a map of IP addresses containing a map of dc and rack info
 */
public static Map<InetAddressAndPort, Map<String, String>> loadDcRackInfo() {
    Map<InetAddressAndPort, Map<String, String>> result = new HashMap<>();
    for (UntypedResultSet.Row row : executeInternal("SELECT peer, peer_port, data_center, rack from system." + PEERS_V2)) {
        InetAddress address = row.getInetAddress("peer");
        Integer port = row.getInt("peer_port");
        InetAddressAndPort peer = InetAddressAndPort.getByAddressOverrideDefaults(address, port);
        if (row.has("data_center") && row.has("rack")) {
            Map<String, String> dcRack = new HashMap<>();
            dcRack.put("data_center", row.getString("data_center"));
            dcRack.put("rack", row.getString("rack"));
            result.put(peer, dcRack);
        }
    }
    return result;
}
Also used : UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) InetAddress(java.net.InetAddress)

Example 23 with InetAddressAndPort

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

the class StorageProxy method submitHint.

public static Future<Void> submitHint(Mutation mutation, EndpointsForToken targets, AbstractWriteResponseHandler<IMutation> responseHandler) {
    // hints should not be written for transient replicas
    Replicas.assertFull(targets);
    HintRunnable runnable = new HintRunnable(targets) {

        public void runMayThrow() {
            Set<InetAddressAndPort> validTargets = new HashSet<>(targets.size());
            Set<UUID> hostIds = new HashSet<>(targets.size());
            for (InetAddressAndPort target : targets.endpoints()) {
                UUID hostId = StorageService.instance.getHostIdForEndpoint(target);
                if (hostId != null) {
                    hostIds.add(hostId);
                    validTargets.add(target);
                } else
                    logger.debug("Discarding hint for endpoint not part of ring: {}", target);
            }
            logger.trace("Adding hints for {}", validTargets);
            HintsService.instance.write(hostIds, Hint.create(mutation, currentTimeMillis()));
            validTargets.forEach(HintsService.instance.metrics::incrCreatedHints);
            // Notify the handler only for CL == ANY
            if (responseHandler != null && responseHandler.replicaPlan.consistencyLevel() == ConsistencyLevel.ANY)
                responseHandler.onResponse(null);
        }
    };
    return submitHint(runnable);
}
Also used : HintsService(org.apache.cassandra.hints.HintsService) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) UUID(java.util.UUID) HashSet(java.util.HashSet)

Example 24 with InetAddressAndPort

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

the class StorageProxy method describeSchemaVersions.

/**
 * initiate a request/response session with each live node to check whether or not everybody is using the same
 * migration id. This is useful for determining if a schema change has propagated through the cluster. Disagreement
 * is assumed if any node fails to respond.
 */
public static Map<String, List<String>> describeSchemaVersions(boolean withPort) {
    final String myVersion = Schema.instance.getVersion().toString();
    final Map<InetAddressAndPort, UUID> versions = new ConcurrentHashMap<>();
    final Set<InetAddressAndPort> liveHosts = Gossiper.instance.getLiveMembers();
    final CountDownLatch latch = newCountDownLatch(liveHosts.size());
    RequestCallback<UUID> cb = message -> {
        // record the response from the remote node.
        versions.put(message.from(), message.payload);
        latch.decrement();
    };
    // an empty message acts as a request to the SchemaVersionVerbHandler.
    Message message = out(SCHEMA_VERSION_REQ, noPayload);
    for (InetAddressAndPort endpoint : liveHosts) MessagingService.instance().sendWithCallback(message, endpoint, cb);
    try {
        // wait for as long as possible. timeout-1s if possible.
        latch.await(DatabaseDescriptor.getRpcTimeout(NANOSECONDS), NANOSECONDS);
    } catch (InterruptedException e) {
        throw new UncheckedInterruptedException(e);
    }
    // maps versions to hosts that are on that version.
    Map<String, List<String>> results = new HashMap<String, List<String>>();
    Iterable<InetAddressAndPort> allHosts = concat(Gossiper.instance.getLiveMembers(), Gossiper.instance.getUnreachableMembers());
    for (InetAddressAndPort host : allHosts) {
        UUID version = versions.get(host);
        String stringVersion = version == null ? UNREACHABLE : version.toString();
        List<String> hosts = results.get(stringVersion);
        if (hosts == null) {
            hosts = new ArrayList<String>();
            results.put(stringVersion, hosts);
        }
        hosts.add(host.getHostAddress(withPort));
    }
    // we're done: the results map is ready to return to the client.  the rest is just debug logging:
    if (results.get(UNREACHABLE) != null)
        logger.debug("Hosts not in agreement. Didn't get a response from everybody: {}", join(results.get(UNREACHABLE), ","));
    for (Map.Entry<String, List<String>> entry : results.entrySet()) {
        // check for version disagreement. log the hosts that don't agree.
        if (entry.getKey().equals(UNREACHABLE) || entry.getKey().equals(myVersion))
            continue;
        for (String host : entry.getValue()) logger.debug("{} disagrees ({})", host, entry.getKey());
    }
    if (results.size() == 1)
        logger.debug("Schemas are in agreement.");
    return results;
}
Also used : Arrays(java.util.Arrays) StorageMetrics(org.apache.cassandra.metrics.StorageMetrics) Stage(org.apache.cassandra.concurrent.Stage) EndpointsForToken(org.apache.cassandra.locator.EndpointsForToken) ClientRequestsMetricsHolder.readMetrics(org.apache.cassandra.metrics.ClientRequestsMetricsHolder.readMetrics) ReadResponse(org.apache.cassandra.db.ReadResponse) Global.nanoTime(org.apache.cassandra.utils.Clock.Global.nanoTime) ReadRepairMetrics(org.apache.cassandra.metrics.ReadRepairMetrics) UnfilteredPartitionIterator(org.apache.cassandra.db.partitions.UnfilteredPartitionIterator) Future(java.util.concurrent.Future) OverloadedException(org.apache.cassandra.exceptions.OverloadedException) Replicas(org.apache.cassandra.locator.Replicas) Mutation(org.apache.cassandra.db.Mutation) Pair(org.apache.cassandra.utils.Pair) Map(java.util.Map) HintsService(org.apache.cassandra.hints.HintsService) SinglePartitionReadCommand(org.apache.cassandra.db.SinglePartitionReadCommand) PartitionIterators(org.apache.cassandra.db.partitions.PartitionIterators) ReplicaPlans(org.apache.cassandra.locator.ReplicaPlans) ReplicaPlan(org.apache.cassandra.locator.ReplicaPlan) Keyspace(org.apache.cassandra.db.Keyspace) ReadAbortException(org.apache.cassandra.exceptions.ReadAbortException) PartitionRangeReadCommand(org.apache.cassandra.db.PartitionRangeReadCommand) FBUtilities(org.apache.cassandra.utils.FBUtilities) RejectException(org.apache.cassandra.db.RejectException) CasWriteTimeoutException(org.apache.cassandra.exceptions.CasWriteTimeoutException) Set(java.util.Set) WriteType(org.apache.cassandra.db.WriteType) Verb(org.apache.cassandra.net.Verb) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) UUIDGen(org.apache.cassandra.utils.UUIDGen) CassandraRelevantProperties(org.apache.cassandra.config.CassandraRelevantProperties) WriteFailureException(org.apache.cassandra.exceptions.WriteFailureException) ReplicaLayout(org.apache.cassandra.locator.ReplicaLayout) NoPayload.noPayload(org.apache.cassandra.net.NoPayload.noPayload) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) Iterables(com.google.common.collect.Iterables) TableId(org.apache.cassandra.schema.TableId) PrepareVerbHandler.doPrepare(org.apache.cassandra.service.paxos.PrepareVerbHandler.doPrepare) Global.currentTimeMillis(org.apache.cassandra.utils.Clock.Global.currentTimeMillis) ReadCommand(org.apache.cassandra.db.ReadCommand) Message(org.apache.cassandra.net.Message) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) TruncateRequest(org.apache.cassandra.db.TruncateRequest) Schema(org.apache.cassandra.schema.Schema) IEndpointSnitch(org.apache.cassandra.locator.IEndpointSnitch) Token(org.apache.cassandra.dht.Token) CountDownLatch.newCountDownLatch(org.apache.cassandra.utils.concurrent.CountDownLatch.newCountDownLatch) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BatchlogManager(org.apache.cassandra.batchlog.BatchlogManager) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) RequestTimeoutException(org.apache.cassandra.exceptions.RequestTimeoutException) TriggerExecutor(org.apache.cassandra.triggers.TriggerExecutor) ConsistencyLevel(org.apache.cassandra.db.ConsistencyLevel) MessagingService(org.apache.cassandra.net.MessagingService) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) ReadTimeoutException(org.apache.cassandra.exceptions.ReadTimeoutException) MessageParams(org.apache.cassandra.db.MessageParams) Hint(org.apache.cassandra.hints.Hint) FilteredPartition(org.apache.cassandra.db.partitions.FilteredPartition) RequestFailureReason(org.apache.cassandra.exceptions.RequestFailureReason) AtomicLong(java.util.concurrent.atomic.AtomicLong) ViewUtils(org.apache.cassandra.db.view.ViewUtils) CasWriteUnknownResultException(org.apache.cassandra.exceptions.CasWriteUnknownResultException) Preconditions(com.google.common.base.Preconditions) ClientRequestsMetricsHolder.casReadMetrics(org.apache.cassandra.metrics.ClientRequestsMetricsHolder.casReadMetrics) ClientRequestsMetricsHolder.casWriteMetrics(org.apache.cassandra.metrics.ClientRequestsMetricsHolder.casWriteMetrics) CounterMutation(org.apache.cassandra.db.CounterMutation) BatchlogCleanup(org.apache.cassandra.service.BatchlogResponseHandler.BatchlogCleanup) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) CountDownLatch(org.apache.cassandra.utils.concurrent.CountDownLatch) Message.out(org.apache.cassandra.net.Message.out) ByteBuffer(java.nio.ByteBuffer) Gossiper(org.apache.cassandra.gms.Gossiper) CASClientRequestMetrics(org.apache.cassandra.metrics.CASClientRequestMetrics) DecoratedKey(org.apache.cassandra.db.DecoratedKey) ClientRequestsMetricsHolder.readMetricsForLevel(org.apache.cassandra.metrics.ClientRequestsMetricsHolder.readMetricsForLevel) ReadRepair(org.apache.cassandra.service.reads.repair.ReadRepair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractReadExecutor(org.apache.cassandra.service.reads.AbstractReadExecutor) Iterables.concat(com.google.common.collect.Iterables.concat) RowIterator(org.apache.cassandra.db.rows.RowIterator) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) Global.randomBallot(org.apache.cassandra.service.paxos.BallotGenerator.Global.randomBallot) UnavailableException(org.apache.cassandra.exceptions.UnavailableException) MessageFlag(org.apache.cassandra.net.MessageFlag) Collection(java.util.Collection) ClientRequestsMetricsHolder.writeMetrics(org.apache.cassandra.metrics.ClientRequestsMetricsHolder.writeMetrics) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) WriteTimeoutException(org.apache.cassandra.exceptions.WriteTimeoutException) UUID(java.util.UUID) NoSpamLogger(org.apache.cassandra.utils.NoSpamLogger) RequestCallback(org.apache.cassandra.net.RequestCallback) Collectors(java.util.stream.Collectors) SERIAL(org.apache.cassandra.db.ConsistencyLevel.SERIAL) Objects(java.util.Objects) CacheLoader(com.google.common.cache.CacheLoader) UncheckedInterruptedException(org.apache.cassandra.utils.concurrent.UncheckedInterruptedException) List(java.util.List) ProposeVerbHandler.doPropose(org.apache.cassandra.service.paxos.ProposeVerbHandler.doPropose) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) ForwardingInfo(org.apache.cassandra.net.ForwardingInfo) TableMetadata(org.apache.cassandra.schema.TableMetadata) Optional(java.util.Optional) PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator) ClientRequestsMetricsHolder.writeMetricsForLevel(org.apache.cassandra.metrics.ClientRequestsMetricsHolder.writeMetricsForLevel) SchemaConstants(org.apache.cassandra.schema.SchemaConstants) RangeCommands(org.apache.cassandra.service.reads.range.RangeCommands) org.apache.cassandra.service.paxos(org.apache.cassandra.service.paxos) ReadExecutionController(org.apache.cassandra.db.ReadExecutionController) PartitionDenylist(org.apache.cassandra.schema.PartitionDenylist) PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) Config(org.apache.cassandra.config.Config) HashMap(java.util.HashMap) TombstoneOverwhelmingException(org.apache.cassandra.db.filter.TombstoneOverwhelmingException) RequestFailureException(org.apache.cassandra.exceptions.RequestFailureException) HashSet(java.util.HashSet) ClientRequestsMetricsHolder.viewWriteMetrics(org.apache.cassandra.metrics.ClientRequestsMetricsHolder.viewWriteMetrics) StringUtils.join(org.apache.commons.lang3.StringUtils.join) IsBootstrappingException(org.apache.cassandra.exceptions.IsBootstrappingException) Clock(org.apache.cassandra.utils.Clock) Global.nextBallotTimestampMicros(org.apache.cassandra.service.paxos.BallotGenerator.Global.nextBallotTimestampMicros) DenylistMetrics(org.apache.cassandra.metrics.DenylistMetrics) Logger(org.slf4j.Logger) ReadFailureException(org.apache.cassandra.exceptions.ReadFailureException) Tracing(org.apache.cassandra.tracing.Tracing) MonotonicClock(org.apache.cassandra.utils.MonotonicClock) Ints(com.google.common.primitives.Ints) Batch(org.apache.cassandra.batchlog.Batch) Replica(org.apache.cassandra.locator.Replica) TimeUnit(java.util.concurrent.TimeUnit) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) ReadCallback(org.apache.cassandra.service.reads.ReadCallback) MBeanWrapper(org.apache.cassandra.utils.MBeanWrapper) IMutation(org.apache.cassandra.db.IMutation) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) Message(org.apache.cassandra.net.Message) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CountDownLatch.newCountDownLatch(org.apache.cassandra.utils.concurrent.CountDownLatch.newCountDownLatch) CountDownLatch(org.apache.cassandra.utils.concurrent.CountDownLatch) UncheckedInterruptedException(org.apache.cassandra.utils.concurrent.UncheckedInterruptedException) UncheckedInterruptedException(org.apache.cassandra.utils.concurrent.UncheckedInterruptedException) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 25 with InetAddressAndPort

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

the class StorageProxy method sendToHintedReplicas.

/**
 * Send the mutations to the right targets, write it locally if it corresponds or writes a hint when the node
 * is not available.
 *
 * Note about hints:
 * <pre>
 * {@code
 * | Hinted Handoff | Consist. Level |
 * | on             |       >=1      | --> wait for hints. We DO NOT notify the handler with handler.response() for hints;
 * | on             |       ANY      | --> wait for hints. Responses count towards consistency.
 * | off            |       >=1      | --> DO NOT fire hints. And DO NOT wait for them to complete.
 * | off            |       ANY      | --> DO NOT fire hints. And DO NOT wait for them to complete.
 * }
 * </pre>
 *
 * @throws OverloadedException if the hints cannot be written/enqueued
 */
public static void sendToHintedReplicas(final Mutation mutation, ReplicaPlan.ForTokenWrite plan, AbstractWriteResponseHandler<IMutation> responseHandler, String localDataCenter, Stage stage) throws OverloadedException {
    // this dc replicas:
    Collection<Replica> localDc = null;
    // extra-datacenter replicas, grouped by dc
    Map<String, Collection<Replica>> dcGroups = null;
    // only need to create a Message for non-local writes
    Message<Mutation> message = null;
    boolean insertLocal = false;
    Replica localReplica = null;
    Collection<Replica> endpointsToHint = null;
    List<InetAddressAndPort> backPressureHosts = null;
    for (Replica destination : plan.contacts()) {
        checkHintOverload(destination);
        if (plan.isAlive(destination)) {
            if (destination.isSelf()) {
                insertLocal = true;
                localReplica = destination;
            } else {
                // belongs on a different server
                if (message == null)
                    message = Message.outWithFlag(MUTATION_REQ, mutation, MessageFlag.CALL_BACK_ON_FAILURE);
                String dc = DatabaseDescriptor.getEndpointSnitch().getDatacenter(destination);
                // (1.1 knows how to forward old-style String message IDs; updated to int in 2.0)
                if (localDataCenter.equals(dc)) {
                    if (localDc == null)
                        localDc = new ArrayList<>(plan.contacts().size());
                    localDc.add(destination);
                } else {
                    if (dcGroups == null)
                        dcGroups = new HashMap<>();
                    Collection<Replica> messages = dcGroups.get(dc);
                    if (messages == null)
                        // most DCs will have <= 3 replicas
                        messages = dcGroups.computeIfAbsent(dc, (v) -> new ArrayList<>(3));
                    messages.add(destination);
                }
                if (backPressureHosts == null)
                    backPressureHosts = new ArrayList<>(plan.contacts().size());
                backPressureHosts.add(destination.endpoint());
            }
        } else {
            // Immediately mark the response as expired since the request will not be sent
            responseHandler.expired();
            if (shouldHint(destination)) {
                if (endpointsToHint == null)
                    endpointsToHint = new ArrayList<>();
                endpointsToHint.add(destination);
            }
        }
    }
    if (endpointsToHint != null)
        submitHint(mutation, EndpointsForToken.copyOf(mutation.key().getToken(), endpointsToHint), responseHandler);
    if (insertLocal) {
        Preconditions.checkNotNull(localReplica);
        performLocally(stage, localReplica, mutation::apply, responseHandler);
    }
    if (localDc != null) {
        for (Replica destination : localDc) MessagingService.instance().sendWriteWithCallback(message, destination, responseHandler, true);
    }
    if (dcGroups != null) {
        // for each datacenter, send the message to one node to relay the write to other replicas
        for (Collection<Replica> dcTargets : dcGroups.values()) sendMessagesToNonlocalDC(message, EndpointsForToken.copyOf(mutation.key().getToken(), dcTargets), responseHandler);
    }
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Replica(org.apache.cassandra.locator.Replica) Collection(java.util.Collection) Mutation(org.apache.cassandra.db.Mutation) CounterMutation(org.apache.cassandra.db.CounterMutation) IMutation(org.apache.cassandra.db.IMutation)

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