org.apache.cassandra.utils.concurrent.WeightedQueue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cassandra-all Show documentation
Show all versions of cassandra-all Show documentation
The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together Dynamo's fully distributed design and Bigtable's ColumnFamily-based data model.
/*
* 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.utils.concurrent;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Weighted queue is a wrapper around any blocking queue that turns it into a blocking weighted queue. The queue
* will weigh each element being added and removed. Adding to the queue is blocked if adding would violate
* the weight bound.
*
* If an element weighs in at larger than the capacity of the queue then exactly one such element will be allowed
* into the queue at a time.
*
* If the weight of an object changes after it is added you are going to have a bad time. Checking weight should be
* cheap so memoize expensive to compute weights. If weight throws that can also result in leaked permits so it's
* always a good idea to memoize weight so it doesn't throw.
*
* In the interests of not writing unit tests for methods no one uses there is a lot of UnsupportedOperationException.
* If you need them then add them and add proper unit tests to WeightedQueueTest. "Good" tests. 100% coverage including
* exception paths and resource leaks.
**/
public class WeightedQueue implements BlockingQueue
{
private static final Logger logger = LoggerFactory.getLogger(WeightedQueue.class);
public static final Weigher NATURAL_WEIGHER = (Weigher
© 2015 - 2024 Weber Informatics LLC | Privacy Policy