Search in sources :

Example 31 with TableId

use of org.apache.cassandra.schema.TableId in project cassandra by apache.

the class CommitLogReplayer method construct.

public static CommitLogReplayer construct(CommitLog commitLog) {
    // compute per-CF and global replay intervals
    Map<TableId, IntervalSet<CommitLogPosition>> cfPersisted = new HashMap<>();
    ReplayFilter replayFilter = ReplayFilter.create();
    for (ColumnFamilyStore cfs : ColumnFamilyStore.all()) {
        // but, if we've truncated the cf in question, then we need to need to start replay after the truncation
        CommitLogPosition truncatedAt = SystemKeyspace.getTruncatedPosition(cfs.metadata.id);
        if (truncatedAt != null) {
            // Point in time restore is taken to mean that the tables need to be replayed even if they were
            // deleted at a later point in time. Any truncation record after that point must thus be cleared prior
            // to replay (CASSANDRA-9195).
            long restoreTime = commitLog.archiver.restorePointInTime;
            long truncatedTime = SystemKeyspace.getTruncatedAt(cfs.metadata.id);
            if (truncatedTime > restoreTime) {
                if (replayFilter.includes(cfs.metadata)) {
                    logger.info("Restore point in time is before latest truncation of table {}.{}. Clearing truncation record.", cfs.metadata.keyspace, cfs.metadata.name);
                    SystemKeyspace.removeTruncationRecord(cfs.metadata.id);
                    truncatedAt = null;
                }
            }
        }
        IntervalSet<CommitLogPosition> filter = persistedIntervals(cfs.getLiveSSTables(), truncatedAt);
        cfPersisted.put(cfs.metadata.id, filter);
    }
    CommitLogPosition globalPosition = firstNotCovered(cfPersisted.values());
    logger.debug("Global replay position is {} from columnfamilies {}", globalPosition, FBUtilities.toString(cfPersisted));
    return new CommitLogReplayer(commitLog, globalPosition, cfPersisted, replayFilter);
}
Also used : TableId(org.apache.cassandra.schema.TableId) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore)

Example 32 with TableId

use of org.apache.cassandra.schema.TableId in project cassandra by apache.

the class RepairMessageVerbHandler method doVerb.

public void doVerb(final Message<RepairMessage> message) {
    // TODO add cancel/interrupt message
    RepairJobDesc desc = message.payload.desc;
    try {
        switch(message.verb()) {
            case PREPARE_MSG:
                PrepareMessage prepareMessage = (PrepareMessage) message.payload;
                logger.debug("Preparing, {}", prepareMessage);
                if (!ActiveRepairService.verifyCompactionsPendingThreshold(prepareMessage.parentRepairSession, prepareMessage.previewKind)) {
                    // error is logged in verifyCompactionsPendingThreshold
                    sendFailureResponse(message);
                    return;
                }
                List<ColumnFamilyStore> columnFamilyStores = new ArrayList<>(prepareMessage.tableIds.size());
                for (TableId tableId : prepareMessage.tableIds) {
                    ColumnFamilyStore columnFamilyStore = ColumnFamilyStore.getIfExists(tableId);
                    if (columnFamilyStore == null) {
                        logErrorAndSendFailureResponse(String.format("Table with id %s was dropped during prepare phase of repair", tableId), message);
                        return;
                    }
                    columnFamilyStores.add(columnFamilyStore);
                }
                ActiveRepairService.instance.registerParentRepairSession(prepareMessage.parentRepairSession, message.from(), columnFamilyStores, prepareMessage.ranges, prepareMessage.isIncremental, prepareMessage.timestamp, prepareMessage.isGlobal, prepareMessage.previewKind);
                MessagingService.instance().send(message.emptyResponse(), message.from());
                break;
            case SNAPSHOT_MSG:
                logger.debug("Snapshotting {}", desc);
                final ColumnFamilyStore cfs = ColumnFamilyStore.getIfExists(desc.keyspace, desc.columnFamily);
                if (cfs == null) {
                    logErrorAndSendFailureResponse(String.format("Table %s.%s was dropped during snapshot phase of repair %s", desc.keyspace, desc.columnFamily, desc.parentSessionId), message);
                    return;
                }
                ActiveRepairService.ParentRepairSession prs = ActiveRepairService.instance.getParentRepairSession(desc.parentSessionId);
                prs.setHasSnapshots();
                TableRepairManager repairManager = cfs.getRepairManager();
                if (prs.isGlobal) {
                    repairManager.snapshot(desc.parentSessionId.toString(), prs.getRanges(), false);
                } else {
                    repairManager.snapshot(desc.parentSessionId.toString(), desc.ranges, true);
                }
                logger.debug("Enqueuing response to snapshot request {} to {}", desc.sessionId, message.from());
                MessagingService.instance().send(message.emptyResponse(), message.from());
                break;
            case VALIDATION_REQ:
                ValidationRequest validationRequest = (ValidationRequest) message.payload;
                logger.debug("Validating {}", validationRequest);
                // trigger read-only compaction
                ColumnFamilyStore store = ColumnFamilyStore.getIfExists(desc.keyspace, desc.columnFamily);
                if (store == null) {
                    logger.error("Table {}.{} was dropped during snapshot phase of repair {}", desc.keyspace, desc.columnFamily, desc.parentSessionId);
                    MessagingService.instance().send(Message.out(VALIDATION_RSP, new ValidationResponse(desc)), message.from());
                    return;
                }
                ActiveRepairService.instance.consistent.local.maybeSetRepairing(desc.parentSessionId);
                PreviewKind previewKind;
                try {
                    previewKind = previewKind(desc.parentSessionId);
                } catch (NoSuchRepairSessionException e) {
                    logger.warn("Parent repair session {} has been removed, failing repair", desc.parentSessionId);
                    MessagingService.instance().send(Message.out(VALIDATION_RSP, new ValidationResponse(desc)), message.from());
                    return;
                }
                Validator validator = new Validator(desc, message.from(), validationRequest.nowInSec, isIncremental(desc.parentSessionId), previewKind);
                ValidationManager.instance.submitValidation(store, validator);
                break;
            case SYNC_REQ:
                // forwarded sync request
                SyncRequest request = (SyncRequest) message.payload;
                logger.debug("Syncing {}", request);
                StreamingRepairTask task = new StreamingRepairTask(desc, request.initiator, request.src, request.dst, request.ranges, isIncremental(desc.parentSessionId) ? desc.parentSessionId : null, request.previewKind, request.asymmetric);
                task.run();
                break;
            case CLEANUP_MSG:
                logger.debug("cleaning up repair");
                CleanupMessage cleanup = (CleanupMessage) message.payload;
                ActiveRepairService.instance.removeParentRepairSession(cleanup.parentRepairSession);
                MessagingService.instance().send(message.emptyResponse(), message.from());
                break;
            case PREPARE_CONSISTENT_REQ:
                ActiveRepairService.instance.consistent.local.handlePrepareMessage(message.from(), (PrepareConsistentRequest) message.payload);
                break;
            case PREPARE_CONSISTENT_RSP:
                ActiveRepairService.instance.consistent.coordinated.handlePrepareResponse((PrepareConsistentResponse) message.payload);
                break;
            case FINALIZE_PROPOSE_MSG:
                ActiveRepairService.instance.consistent.local.handleFinalizeProposeMessage(message.from(), (FinalizePropose) message.payload);
                break;
            case FINALIZE_PROMISE_MSG:
                ActiveRepairService.instance.consistent.coordinated.handleFinalizePromiseMessage((FinalizePromise) message.payload);
                break;
            case FINALIZE_COMMIT_MSG:
                ActiveRepairService.instance.consistent.local.handleFinalizeCommitMessage(message.from(), (FinalizeCommit) message.payload);
                break;
            case FAILED_SESSION_MSG:
                FailSession failure = (FailSession) message.payload;
                ActiveRepairService.instance.consistent.coordinated.handleFailSessionMessage(failure);
                ActiveRepairService.instance.consistent.local.handleFailSessionMessage(message.from(), failure);
                break;
            case STATUS_REQ:
                ActiveRepairService.instance.consistent.local.handleStatusRequest(message.from(), (StatusRequest) message.payload);
                break;
            case STATUS_RSP:
                ActiveRepairService.instance.consistent.local.handleStatusResponse(message.from(), (StatusResponse) message.payload);
                break;
            default:
                ActiveRepairService.instance.handleMessage(message);
                break;
        }
    } catch (Exception e) {
        logger.error("Got error, removing parent repair session");
        if (desc != null && desc.parentSessionId != null)
            ActiveRepairService.instance.removeParentRepairSession(desc.parentSessionId);
        throw new RuntimeException(e);
    }
}
Also used : TableId(org.apache.cassandra.schema.TableId) ActiveRepairService(org.apache.cassandra.service.ActiveRepairService) PreviewKind(org.apache.cassandra.streaming.PreviewKind) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore)

Example 33 with TableId

use of org.apache.cassandra.schema.TableId in project cassandra by apache.

the class LocalSessions method getPendingStats.

public PendingStats getPendingStats(TableId tid, Collection<Range<Token>> ranges) {
    ColumnFamilyStore cfs = Schema.instance.getColumnFamilyStoreInstance(tid);
    Preconditions.checkArgument(cfs != null);
    PendingStat.Builder pending = new PendingStat.Builder();
    PendingStat.Builder finalized = new PendingStat.Builder();
    PendingStat.Builder failed = new PendingStat.Builder();
    Map<UUID, PendingStat> stats = cfs.getPendingRepairStats();
    for (Map.Entry<UUID, PendingStat> entry : stats.entrySet()) {
        UUID sessionID = entry.getKey();
        PendingStat stat = entry.getValue();
        Verify.verify(sessionID.equals(Iterables.getOnlyElement(stat.sessions)));
        LocalSession session = sessions.get(sessionID);
        Verify.verifyNotNull(session);
        if (!Iterables.any(ranges, r -> r.intersects(session.ranges)))
            continue;
        switch(session.getState()) {
            case FINALIZED:
                finalized.addStat(stat);
                break;
            case FAILED:
                failed.addStat(stat);
                break;
            default:
                pending.addStat(stat);
        }
    }
    return new PendingStats(cfs.keyspace.getName(), cfs.name, pending.build(), finalized.build(), failed.build());
}
Also used : NoSuchRepairSessionException(org.apache.cassandra.repair.NoSuchRepairSessionException) KeyspaceRepairManager(org.apache.cassandra.repair.KeyspaceRepairManager) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) STATUS_REQ(org.apache.cassandra.net.Verb.STATUS_REQ) CompactionInterruptedException(org.apache.cassandra.db.compaction.CompactionInterruptedException) ByteBuffer(java.nio.ByteBuffer) BooleanSupplier(java.util.function.BooleanSupplier) Map(java.util.Map) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) PendingStats(org.apache.cassandra.repair.consistent.admin.PendingStats) Verify(com.google.common.base.Verify) ImmutableSet(com.google.common.collect.ImmutableSet) FBUtilities(org.apache.cassandra.utils.FBUtilities) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) FinalizeCommit(org.apache.cassandra.repair.messages.FinalizeCommit) Set(java.util.Set) UUID(java.util.UUID) Instant(java.time.Instant) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) FinalizePropose(org.apache.cassandra.repair.messages.FinalizePropose) State(org.apache.cassandra.repair.consistent.ConsistentSession.State) List(java.util.List) FAILED_SESSION_MSG(org.apache.cassandra.net.Verb.FAILED_SESSION_MSG) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Future(org.apache.cassandra.utils.concurrent.Future) Throwables(org.apache.cassandra.utils.Throwables) FINALIZE_PROMISE_MSG(org.apache.cassandra.net.Verb.FINALIZE_PROMISE_MSG) FailureDetector(org.apache.cassandra.gms.FailureDetector) SchemaConstants(org.apache.cassandra.schema.SchemaConstants) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) Iterables(com.google.common.collect.Iterables) PendingStat(org.apache.cassandra.repair.consistent.admin.PendingStat) PrepareConsistentResponse(org.apache.cassandra.repair.messages.PrepareConsistentResponse) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) TableId(org.apache.cassandra.schema.TableId) PREPARE_CONSISTENT_RSP(org.apache.cassandra.net.Verb.PREPARE_CONSISTENT_RSP) Range(org.apache.cassandra.dht.Range) HashMap(java.util.HashMap) Message(org.apache.cassandra.net.Message) QueryProcessor(org.apache.cassandra.cql3.QueryProcessor) FinalizePromise(org.apache.cassandra.repair.messages.FinalizePromise) SystemKeyspace(org.apache.cassandra.db.SystemKeyspace) ArrayList(java.util.ArrayList) Schema(org.apache.cassandra.schema.Schema) HashSet(java.util.HashSet) UTF8Type(org.apache.cassandra.db.marshal.UTF8Type) Token(org.apache.cassandra.dht.Token) ActiveRepairService(org.apache.cassandra.service.ActiveRepairService) Lists(com.google.common.collect.Lists) CleanupSummary(org.apache.cassandra.repair.consistent.admin.CleanupSummary) STATUS_RSP(org.apache.cassandra.net.Verb.STATUS_RSP) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) MessagingService(org.apache.cassandra.net.MessagingService) Logger(org.slf4j.Logger) BytesType(org.apache.cassandra.db.marshal.BytesType) StatusResponse(org.apache.cassandra.repair.messages.StatusResponse) StorageService(org.apache.cassandra.service.StorageService) IOException(java.io.IOException) PrepareConsistentRequest(org.apache.cassandra.repair.messages.PrepareConsistentRequest) UnknownHostException(java.net.UnknownHostException) Ints(com.google.common.primitives.Ints) FutureCallback(com.google.common.util.concurrent.FutureCallback) Replica(org.apache.cassandra.locator.Replica) FailSession(org.apache.cassandra.repair.messages.FailSession) TimeUnit(java.util.concurrent.TimeUnit) RepairMessage(org.apache.cassandra.repair.messages.RepairMessage) UUIDType(org.apache.cassandra.db.marshal.UUIDType) IPartitioner(org.apache.cassandra.dht.IPartitioner) Global.executorFactory(org.apache.cassandra.concurrent.ExecutorFactory.Global.executorFactory) UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) StatusRequest(org.apache.cassandra.repair.messages.StatusRequest) PendingStats(org.apache.cassandra.repair.consistent.admin.PendingStats) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) UUID(java.util.UUID) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) PendingStat(org.apache.cassandra.repair.consistent.admin.PendingStat)

Example 34 with TableId

use of org.apache.cassandra.schema.TableId in project cassandra by apache.

the class StorageProxy method mutateWithTriggers.

@SuppressWarnings("unchecked")
public static void mutateWithTriggers(List<? extends IMutation> mutations, ConsistencyLevel consistencyLevel, boolean mutateAtomically, long queryStartNanoTime) throws WriteTimeoutException, WriteFailureException, UnavailableException, OverloadedException, InvalidRequestException {
    if (DatabaseDescriptor.getPartitionDenylistEnabled() && DatabaseDescriptor.getDenylistWritesEnabled()) {
        for (final IMutation mutation : mutations) {
            for (final TableId tid : mutation.getTableIds()) {
                if (!partitionDenylist.isKeyPermitted(tid, mutation.key().getKey())) {
                    denylistMetrics.incrementWritesRejected();
                    // While Schema.instance.getTableMetadata() can return a null value, in this case the isKeyPermitted
                    // call above ensures that we cannot have a null associated tid at this point.
                    final TableMetadata tmd = Schema.instance.getTableMetadata(tid);
                    throw new InvalidRequestException(String.format("Unable to write to denylisted partition [0x%s] in %s/%s", mutation.key().toString(), tmd.keyspace, tmd.name));
                }
            }
        }
    }
    Collection<Mutation> augmented = TriggerExecutor.instance.execute(mutations);
    boolean updatesView = Keyspace.open(mutations.iterator().next().getKeyspaceName()).viewManager.updatesAffectView(mutations, true);
    long size = IMutation.dataSize(mutations);
    writeMetrics.mutationSize.update(size);
    writeMetricsForLevel(consistencyLevel).mutationSize.update(size);
    if (augmented != null)
        mutateAtomically(augmented, consistencyLevel, updatesView, queryStartNanoTime);
    else {
        if (mutateAtomically || updatesView)
            mutateAtomically((Collection<Mutation>) mutations, consistencyLevel, updatesView, queryStartNanoTime);
        else
            mutate(mutations, consistencyLevel, queryStartNanoTime);
    }
}
Also used : TableId(org.apache.cassandra.schema.TableId) TableMetadata(org.apache.cassandra.schema.TableMetadata) IMutation(org.apache.cassandra.db.IMutation) Collection(java.util.Collection) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) Mutation(org.apache.cassandra.db.Mutation) CounterMutation(org.apache.cassandra.db.CounterMutation) IMutation(org.apache.cassandra.db.IMutation)

Example 35 with TableId

use of org.apache.cassandra.schema.TableId in project cassandra by apache.

the class ActiveRepairService method prepareForRepair.

public UUID prepareForRepair(UUID parentRepairSession, InetAddressAndPort coordinator, Set<InetAddressAndPort> endpoints, RepairOption options, boolean isForcedRepair, List<ColumnFamilyStore> columnFamilyStores) {
    if (!verifyCompactionsPendingThreshold(parentRepairSession, options.getPreviewKind()))
        // failRepair throws exception
        failRepair(parentRepairSession, "Rejecting incoming repair, pending compactions above threshold");
    long repairedAt = getRepairedAt(options, isForcedRepair);
    registerParentRepairSession(parentRepairSession, coordinator, columnFamilyStores, options.getRanges(), options.isIncremental(), repairedAt, options.isGlobal(), options.getPreviewKind());
    final CountDownLatch prepareLatch = newCountDownLatch(endpoints.size());
    final AtomicBoolean status = new AtomicBoolean(true);
    final Set<String> failedNodes = synchronizedSet(new HashSet<String>());
    final AtomicInteger timeouts = new AtomicInteger(0);
    RequestCallback callback = new RequestCallback() {

        @Override
        public void onResponse(Message msg) {
            prepareLatch.decrement();
        }

        @Override
        public void onFailure(InetAddressAndPort from, RequestFailureReason failureReason) {
            status.set(false);
            failedNodes.add(from.toString());
            if (failureReason == RequestFailureReason.TIMEOUT)
                timeouts.incrementAndGet();
            prepareLatch.decrement();
        }

        @Override
        public boolean invokeOnFailure() {
            return true;
        }
    };
    List<TableId> tableIds = new ArrayList<>(columnFamilyStores.size());
    for (ColumnFamilyStore cfs : columnFamilyStores) tableIds.add(cfs.metadata.id);
    for (InetAddressAndPort neighbour : endpoints) {
        if (FailureDetector.instance.isAlive(neighbour)) {
            PrepareMessage message = new PrepareMessage(parentRepairSession, tableIds, options.getRanges(), options.isIncremental(), repairedAt, options.isGlobal(), options.getPreviewKind());
            Message<RepairMessage> msg = out(PREPARE_MSG, message);
            MessagingService.instance().sendWithCallback(msg, neighbour, callback);
        } else {
            // remaining ones go down, we still want to fail so we don't create repair sessions that can't complete
            if (isForcedRepair && !options.isIncremental()) {
                prepareLatch.decrement();
            } else {
                // bailout early to avoid potentially waiting for a long time.
                failRepair(parentRepairSession, "Endpoint not alive: " + neighbour);
            }
        }
    }
    try {
        if (!prepareLatch.await(getRpcTimeout(MILLISECONDS), MILLISECONDS) || timeouts.get() > 0)
            failRepair(parentRepairSession, "Did not get replies from all endpoints.");
    } catch (InterruptedException e) {
        failRepair(parentRepairSession, "Interrupted while waiting for prepare repair response.");
    }
    if (!status.get()) {
        failRepair(parentRepairSession, "Got negative replies from endpoints " + failedNodes);
    }
    return parentRepairSession;
}
Also used : TableId(org.apache.cassandra.schema.TableId) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) 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) CountDownLatch(org.apache.cassandra.utils.concurrent.CountDownLatch) CountDownLatch.newCountDownLatch(org.apache.cassandra.utils.concurrent.CountDownLatch.newCountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RepairMessage(org.apache.cassandra.repair.messages.RepairMessage) RequestFailureReason(org.apache.cassandra.exceptions.RequestFailureReason) PrepareMessage(org.apache.cassandra.repair.messages.PrepareMessage) RequestCallback(org.apache.cassandra.net.RequestCallback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore)

Aggregations

TableId (org.apache.cassandra.schema.TableId)46 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)15 Test (org.junit.Test)9 TableMetadata (org.apache.cassandra.schema.TableMetadata)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)5 IOException (java.io.IOException)4 Collection (java.util.Collection)4 HashMap (java.util.HashMap)4 LifecycleTransaction (org.apache.cassandra.db.lifecycle.LifecycleTransaction)4 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)4 ActiveRepairService (org.apache.cassandra.service.ActiveRepairService)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 Iterables (com.google.common.collect.Iterables)3 Sets (com.google.common.collect.Sets)3 ByteBuffer (java.nio.ByteBuffer)3 List (java.util.List)3 Set (java.util.Set)3