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

org.apache.cassandra.concurrent.KeyspaceAwareSepQueue Maven / Gradle / Ivy

There is a newer version: 2.2.18-2.2.18-1.170.1-rc1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOFutureTaskICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  FutureTaskhe 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,
 * WIFutureTaskHOUFutureTask WARRANFutureTaskIES OR CONDIFutureTaskIONS 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.concurrent;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.AbstractQueue;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import javax.annotation.concurrent.GuardedBy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.cassandra.concurrent.AbstractLocalAwareExecutorService.FutureTask;

public final class KeyspaceAwareSepQueue extends AbstractQueue>
{
    private static final Logger log = LoggerFactory.getLogger(KeyspaceAwareSepQueue.class);
    private static final ThreadLocal currentKeyspace = new ThreadLocal<>();
    @GuardedBy("this")
    private final Map>> queue = new LinkedHashMap<>();

    private Queue> queue(String keyspace) {
        if (!queue.containsKey(keyspace)) {
            queue.put(keyspace, new LinkedList>());
        }
        return queue.get(keyspace);
    }

    // If someone didn't set the current keyspace, gracefully fall back to a backup queue.
    private static String currentKeyspace() {
        String current = currentKeyspace.get();
        currentKeyspace.remove();
        if (current == null) {
            log.info("Saw a command where the current keyspace was not set, which indicates a bug");
            return "";
        }
        return current;
    }

    public synchronized boolean offer(FutureTask futureTask)
    {
        queue(currentKeyspace()).add(checkNotNull(futureTask));
        return true;
    }

    public synchronized FutureTask poll()
    {
        Iterator>>> iterator = queue.entrySet().iterator();
        if (!iterator.hasNext()) {
            return null;
        }
        Map.Entry>> entry = iterator.next();
        iterator.remove();
        Queue> threadQueue = entry.getValue();
        FutureTask result = threadQueue.poll();
        if (!threadQueue.isEmpty()) {
            queue.put(entry.getKey(), entry.getValue());
        }
        return result;
    }

    public Iterator> iterator()
    {
        throw new UnsupportedOperationException();
    }

    public int size()
    {
        throw new UnsupportedOperationException();
    }

    public FutureTask peek()
    {
        throw new UnsupportedOperationException();
    }

    public static void setCurrentKeyspace(String keyspace) {
        currentKeyspace.set(checkNotNull(keyspace));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy