All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cassandra.service.opstate.KeyspaceTableOpStateCache Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.cassandra.service.opstate;


import java.time.Instant;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Sets;

import org.apache.cassandra.config.Schema;

public class KeyspaceTableOpStateCache
{
    private final ConcurrentMap tableEntries;

    public KeyspaceTableOpStateCache(Map initialEntries)
    {
        this.tableEntries = new ConcurrentHashMap<>(initialEntries);
    }

    @VisibleForTesting
    Map getTableEntries()
    {
        return Collections.unmodifiableMap(tableEntries);
    }

    public boolean entryExists(KeyspaceTableKey entryKey)
    {
        return tableEntries.containsKey(entryKey);
    }

    public Map updateTsForEntry(KeyspaceTableKey entryKey, Instant value)
        throws IllegalArgumentException
    {
        if (tableEntries.containsKey(entryKey) && tableEntries.get(entryKey).compareTo(value) > 0)
            throw new IllegalArgumentException("Can only update cache entry with increasing timestamp");

        tableEntries.put(entryKey, value);
        return Collections.unmodifiableMap(tableEntries);
    }

    public Optional getMinimumTsOfAllEntries()
    {
        if (tableEntries.isEmpty())
            return Optional.empty();

        // Remove potentially deleted keyspaces and tables from entries
        Set validEntries = getValidKeyspaceTableEntries();
        Set invalidEntries =
            Sets.difference(tableEntries.keySet(), Sets.intersection(tableEntries.keySet(), validEntries));
        invalidEntries.forEach(tableEntries::remove);

        // If not all keyspaces/tables have a tracked last cleanup ts, default to empty value
        Set missingEntries = Sets.difference(validEntries, tableEntries.keySet());
        return missingEntries.isEmpty() ? Optional.of(Collections.min(tableEntries.values())) : Optional.empty();
    }


    @VisibleForTesting
    Set getValidKeyspaceTableEntries(){
        return Schema.instance.getNonSystemKeyspaces().stream()
                              .map(keyspace -> Schema.instance.getKeyspaceInstance(keyspace).getColumnFamilyStores()
                                                              .stream()
                                                              .map(cf -> KeyspaceTableKey.of(keyspace, cf.getColumnFamilyName()))
                                                              .collect(Collectors.toSet()))
                              .flatMap(Set::stream)
                              .collect(Collectors.toSet());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy