org.apache.cassandra.db.CounterMutation 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
A fork of the Apache Cassandra Project that uses Lucene indexes for providing near real time search such as ElasticSearch or Solr, including full text search capabilities, multi-dimensional queries, and relevance scoring.
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.db;
import java.io.DataInput;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.Striped;
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.db.composites.CellName;
import org.apache.cassandra.db.context.CounterContext;
import org.apache.cassandra.db.filter.NamesQueryFilter;
import org.apache.cassandra.exceptions.WriteTimeoutException;
import org.apache.cassandra.io.IVersionedSerializer;
import org.apache.cassandra.io.util.DataOutputPlus;
import org.apache.cassandra.net.MessageOut;
import org.apache.cassandra.net.MessagingService;
import org.apache.cassandra.service.CacheService;
import org.apache.cassandra.tracing.Tracing;
import org.apache.cassandra.utils.*;
public class CounterMutation implements IMutation
{
public static final CounterMutationSerializer serializer = new CounterMutationSerializer();
private static final Striped LOCKS = Striped.lazyWeakLock(DatabaseDescriptor.getConcurrentCounterWriters() * 1024);
private final Mutation mutation;
private final ConsistencyLevel consistency;
public CounterMutation(Mutation mutation, ConsistencyLevel consistency)
{
this.mutation = mutation;
this.consistency = consistency;
}
public String getKeyspaceName()
{
return mutation.getKeyspaceName();
}
public Collection getColumnFamilyIds()
{
return mutation.getColumnFamilyIds();
}
public Collection getColumnFamilies()
{
return mutation.getColumnFamilies();
}
public Mutation getMutation()
{
return mutation;
}
public ByteBuffer key()
{
return mutation.key();
}
public ConsistencyLevel consistency()
{
return consistency;
}
public MessageOut makeMutationMessage()
{
return new MessageOut<>(MessagingService.Verb.COUNTER_MUTATION, this, serializer);
}
/**
* Applies the counter mutation, returns the result Mutation (for replication to other nodes).
*
* 1. Grabs the striped cell-level locks in the proper order
* 2. Gets the current values of the counters-to-be-modified from the counter cache
* 3. Reads the rest of the current values (cache misses) from the CF
* 4. Writes the updated counter values
* 5. Updates the counter cache
* 6. Releases the lock(s)
*
* See CASSANDRA-4775 and CASSANDRA-6504 for further details.
*
* @return the applied resulting Mutation
*/
public Mutation apply() throws WriteTimeoutException
{
Mutation result = new Mutation(getKeyspaceName(), key());
Keyspace keyspace = Keyspace.open(getKeyspaceName());
int count = 0;
for (ColumnFamily cf : getColumnFamilies())
count += cf.getColumnCount();
List locks = new ArrayList<>(count);
Tracing.trace("Acquiring {} counter locks", count);
try
{
grabCounterLocks(keyspace, locks);
for (ColumnFamily cf : getColumnFamilies())
result.add(processModifications(cf));
result.apply();
updateCounterCache(result, keyspace);
return result;
}
finally
{
for (Lock lock : locks)
lock.unlock();
}
}
private void grabCounterLocks(Keyspace keyspace, List locks) throws WriteTimeoutException
{
long startTime = System.nanoTime();
for (Lock lock : LOCKS.bulkGet(getCounterLockKeys()))
{
long timeout = TimeUnit.MILLISECONDS.toNanos(getTimeout()) - (System.nanoTime() - startTime);
try
{
if (!lock.tryLock(timeout, TimeUnit.NANOSECONDS))
throw new WriteTimeoutException(WriteType.COUNTER, consistency(), 0, consistency().blockFor(keyspace));
locks.add(lock);
}
catch (InterruptedException e)
{
throw new WriteTimeoutException(WriteType.COUNTER, consistency(), 0, consistency().blockFor(keyspace));
}
}
}
/**
* Returns a wrapper for the Striped#bulkGet() call (via Keyspace#counterLocksFor())
* Striped#bulkGet() depends on Object#hashCode(), so here we make sure that the cf id and the partition key
* all get to be part of the hashCode() calculation, not just the cell name.
*/
private Iterable