Java Code Examples for com.netflix.astyanax.model.ConsistencyLevel#CL_LOCAL_QUORUM

The following examples show how to use com.netflix.astyanax.model.ConsistencyLevel#CL_LOCAL_QUORUM . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConsistencyTopologyAdapter.java    From emodb with Apache License 2.0 6 votes vote down vote up
/**
 * Reduce the desired consistency level to be compatible with the deployed ring topology.
 */
public ConsistencyLevel clamp(ConsistencyLevel consistencyLevel) {
    // Cassandra only allows the use of LOCAL_QUORUM and EACH_QUORUM if the keyspace
    // placement strategy is NetworkTopologyStrategy
    if ((consistencyLevel == ConsistencyLevel.CL_LOCAL_QUORUM || consistencyLevel == ConsistencyLevel.CL_EACH_QUORUM) && !_networkTopology) {
        consistencyLevel = ConsistencyLevel.CL_QUORUM;
    }
    if (consistencyLevel == ConsistencyLevel.CL_LOCAL_ONE && !_networkTopology) {
        consistencyLevel = ConsistencyLevel.CL_ONE;
    }

    // we may want to write to at two or three servers to ensure the write survives the
    // permanent failure of any single server.  but if the ring has fewer servers to
    // begin with (ie. it's a test ring) we must reduce the consistency level.
    if (consistencyLevel == ConsistencyLevel.CL_THREE && _replicationFactor < 3) {
        consistencyLevel = ConsistencyLevel.CL_TWO;
    }
    if (consistencyLevel == ConsistencyLevel.CL_TWO && _replicationFactor < 2) {
        consistencyLevel = ConsistencyLevel.CL_ONE;
    }

    return consistencyLevel;
}
 
Example 2
Source File: AstyanaxEventWriterDAO.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Override
public void delete(String channel, Collection<EventId> eventIds) {
    checkNotNull(channel, "channel");
    checkNotNull(eventIds, "eventIds");

    ListMultimap<ByteBuffer, Integer> eventsBySlab = ArrayListMultimap.create();
    for (EventId eventId : eventIds) {
        AstyanaxEventId eventIdImpl = (AstyanaxEventId) eventId;
        checkArgument(channel.equals(eventIdImpl.getChannel()));
        eventsBySlab.put(eventIdImpl.getSlabId(), eventIdImpl.getEventIdx());
    }

    // We might be able to use weak consistency since we're allowed to forget deletes and repeat events.  But we'd
    // need to measure it in production to see how frequently weak consistency would cause events to repeat.
    BatchUpdate update = new BatchUpdate(_keyspace, ConsistencyLevel.CL_LOCAL_QUORUM,
            Constants.MUTATION_MAX_ROWS, Constants.MUTATION_MAX_COLUMNS);

    for (Map.Entry<ByteBuffer, Collection<Integer>> entry : eventsBySlab.asMap().entrySet()) {
        ByteBuffer slabId = entry.getKey();
        Collection<Integer> eventIdxs = entry.getValue();

        BatchUpdate.Row<ByteBuffer, Integer> row = update.updateRow(ColumnFamilies.SLAB, slabId);
        for (Integer eventIdx : eventIdxs) {
            row.deleteColumn(eventIdx);
        }
    }

    update.finish();
}
 
Example 3
Source File: AstyanaxEventReaderDAO.java    From emodb with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the ordered manifest for a channel.  The read can either be weak or strong.  A weak read will use CL1
 * and may use the cached oldest slab from a previous strong call to improve performance.  A strong read will use
 * CL local_quorum and will always read the entire manifest row.  This makes a weak read significantly faster than a
 * strong read but also means the call is not guaranteed to return the entire manifest.  Because of this at least
 * every 10 seconds a weak read for a channel is automatically promoted to a strong read.
 *
 * The vast majority of calls to this method are performed during a "peek" or "poll" operation.  Since these are
 * typically called repeatedly a weak call provides improved performance while guaranteeing that at least every
 * 10 seconds the manifest is strongly read so no slabs are missed over time.  Calls which must guarantee
 * the full manifest should explicitly request strong consistency.
 */
private Iterator<Column<ByteBuffer>> readManifestForChannel(final String channel, final boolean weak) {
    final ByteBuffer oldestSlab = weak ? _oldestSlab.getIfPresent(channel) : null;
    final ConsistencyLevel consistency;

    RangeBuilder range = new RangeBuilder().setLimit(50);
    if (oldestSlab != null) {
        range.setStart(oldestSlab);
        consistency = ConsistencyLevel.CL_LOCAL_ONE;
    } else {
        consistency = ConsistencyLevel.CL_LOCAL_QUORUM;
    }

    final Iterator<Column<ByteBuffer>> manifestColumns = executePaginated(
            _keyspace.prepareQuery(ColumnFamilies.MANIFEST, consistency)
                    .getKey(channel)
                    .withColumnRange(range.build())
                    .autoPaginate(true));

    if (oldestSlab != null) {
        // Query was executed weakly using the cached oldest slab, so don't update the cache with an unreliable oldest value
        return manifestColumns;
    } else {
        PeekingIterator<Column<ByteBuffer>> peekingManifestColumns = Iterators.peekingIterator(manifestColumns);
        if (peekingManifestColumns.hasNext()) {
            // Cache the first slab returned from querying the full manifest column family since it is the oldest.
            cacheOldestSlabForChannel(channel, TimeUUIDSerializer.get().fromByteBuffer(peekingManifestColumns.peek().getName()));
            return peekingManifestColumns;
        } else {
            // Channel was completely empty.  Cache a TimeUUID for the current time.  This will cause future calls
            // to read at most 1 minute of tombstones until the cache expires 10 seconds later.
            cacheOldestSlabForChannel(channel, TimeUUIDs.newUUID());
            return Iterators.emptyIterator();
        }
    }
}
 
Example 4
Source File: SorConsistencies.java    From emodb with Apache License 2.0 5 votes vote down vote up
public static ConsistencyLevel toAstyanax(ReadConsistency consistency) {
    switch (consistency) {
        case WEAK:
            return ConsistencyLevel.CL_LOCAL_ONE;           // first node to respond
        case STRONG:
            return ConsistencyLevel.CL_LOCAL_QUORUM;  // single data center quorum
        default:
            throw new UnsupportedOperationException(String.valueOf(consistency));
    }
}
 
Example 5
Source File: SorConsistencies.java    From emodb with Apache License 2.0 5 votes vote down vote up
public static ConsistencyLevel toAstyanax(WriteConsistency consistency) {
    switch (consistency) {
        case NON_DURABLE:
            return ConsistencyLevel.CL_ANY;  // at least 1 node, allow hinted handoff
        case WEAK:
            return ConsistencyLevel.CL_TWO;  // usually at least 2 nodes, survives the permanent failure of any single node
        case STRONG:
            return ConsistencyLevel.CL_LOCAL_QUORUM;   // single data center quorum
        case GLOBAL:
            return ConsistencyLevel.CL_EACH_QUORUM;    // all data center quorum
        default:
            throw new UnsupportedOperationException(String.valueOf(consistency));
    }
}
 
Example 6
Source File: AstyanaxEventWriterDAO.java    From emodb with Apache License 2.0 4 votes vote down vote up
@Override
public void deleteAll(final String channel) {
    checkNotNull(channel, "channel");

    // There isn't really a good way to delete events en masse because (a) there may be many processes writing to
    // the queue, (b) it's impractical to expect all those processes to close all their open slabs, therefore (c)
    // we must delete the events in a way that leaves the open slabs in a valid state, therefore (d) we can't
    // simply delete all the slab rows since we must preserve the OPEN_SLAB_MARKER column.  Therefore, purge
    // of open slabs is implemented by reading all the events one-by-one and deleting them.
    final BatchUpdate update = new BatchUpdate(_keyspace, ConsistencyLevel.CL_LOCAL_QUORUM,
            Constants.MUTATION_MAX_ROWS, Constants.MUTATION_MAX_COLUMNS);

    class Deleter implements SlabFilter, EventSink {
        private BatchUpdate.Row<String, ByteBuffer> _manifestRow;
        private BatchUpdate.Row<ByteBuffer, Integer> _slabRow;
        private ByteBuffer _currentSlabId;

        void run() {
            _manifestRow = update.updateRow(ColumnFamilies.MANIFEST, channel);
            _eventReaderDAO.readAll(channel, this, this, false);
        }

        // SlabFilter interface, called before scanning events in a slab.
        @Override
        public boolean accept(ByteBuffer slabId, boolean open, ByteBuffer nextSlabId) {
            _currentSlabId = slabId;
            _slabRow = update.updateRow(ColumnFamilies.SLAB, slabId);

            if (open) {
                // Open slab: read and delete the events individually.
                return true;
            } else {
                // Closed slab: delete the entire slab without reading it.
                _slabRow.deleteRow();
                _manifestRow.deleteColumn(slabId);
                return false;
            }
        }

        // EventSink interface, called on individual events in a slab.
        @Override
        public boolean accept(EventId eventId, ByteBuffer eventData) {
            AstyanaxEventId eventIdImpl = (AstyanaxEventId) eventId;
            checkState(_currentSlabId.equals(eventIdImpl.getSlabId()));
            int eventIdx = eventIdImpl.getEventIdx();

            _slabRow.deleteColumn(eventIdx);

            return true; // Keep looking for more events
        }
    }
    new Deleter().run();

    update.finish();
}
 
Example 7
Source File: AstyanaxLockManagerImpl.java    From usergrid with Apache License 2.0 2 votes vote down vote up
@Override
public Lock createLock(final UUID applicationId, final String... path ) {

    String lockPath = LockPathBuilder.buildPath( applicationId, path );

    ConsistencyLevel consistencyLevel;
    try{
        consistencyLevel = ConsistencyLevel.valueOf(cassandraFig.getLocksCl());
    }catch(IllegalArgumentException e){

        logger.warn( "Property {} value provided: {} is not valid", CassandraFig.LOCKS_CL,
            cassandraFig.getLocksCl() );

        // just default it to local quorum if we can't parse
        consistencyLevel = ConsistencyLevel.CL_LOCAL_QUORUM;
    }


    int lockExpiration;
    int lockConfigExpiration = cassandraFig.getLocksExpiration();
    if( lockConfigExpiration >= MINIMUM_LOCK_EXPIRATION ){

        lockExpiration = Math.min(cassandraFig.getLocksExpiration(), Integer.MAX_VALUE);

    }else{

        logger.warn("Property {} is not valid.  Choose a value greater than or equal to {}",
            CassandraFig.LOCKS_EXPIRATION,
            MINIMUM_LOCK_EXPIRATION);

        // use the default if seomthing below the minimum is provided
        lockExpiration = Integer.valueOf(CassandraFig.DEFAULT_LOCKS_EXPIRATION);
    }



    ColumnPrefixDistributedRowLock<String> lock =
        new ColumnPrefixDistributedRowLock<>(keyspace, getLocksColumnFamily(), lockPath)
            .expireLockAfter( lockExpiration, TimeUnit.MILLISECONDS)
            .withConsistencyLevel(consistencyLevel);


    return new AstyanaxLockImpl( lock );

}