Search in sources :

Example 81 with Token

use of org.apache.cassandra.dht.Token in project eiger by wlloyd.

the class StreamingTransferTest method transfer.

private void transfer(Table table, SSTableReader sstable) throws Exception {
    IPartitioner p = StorageService.getPartitioner();
    List<Range<Token>> ranges = new ArrayList<Range<Token>>();
    ranges.add(new Range<Token>(p.getMinimumToken(), p.getToken(ByteBufferUtil.bytes("key1"))));
    ranges.add(new Range<Token>(p.getToken(ByteBufferUtil.bytes("key2")), p.getMinimumToken()));
    StreamOutSession session = StreamOutSession.create(table.name, LOCAL, null);
    StreamOut.transferSSTables(session, Arrays.asList(sstable), ranges, OperationType.BOOTSTRAP);
    session.await();
}
Also used : Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) IPartitioner(org.apache.cassandra.dht.IPartitioner)

Example 82 with Token

use of org.apache.cassandra.dht.Token in project brisk by riptano.

the class BriskServer method getLocalSubBlock.

/**
 * Retrieves a local subBlock
 *
 * @param blockId row key
 * @param sblockId SubBlock column name
 * @param offset inside the sblock
 * @return a local sublock
 * @throws TException
 */
private LocalBlock getLocalSubBlock(String subBlockCFName, ByteBuffer blockId, ByteBuffer sblockId, int offset) throws TException {
    DecoratedKey<Token<?>> decoratedKey = new DecoratedKey<Token<?>>(StorageService.getPartitioner().getToken(blockId), blockId);
    Table table = Table.open(cfsKeyspace);
    ColumnFamilyStore sblockStore = table.getColumnFamilyStore(subBlockCFName);
    Collection<SSTableReader> sstables = sblockStore.getSSTables();
    for (SSTableReader sstable : sstables) {
        long position = sstable.getPosition(decoratedKey, Operator.EQ);
        if (position == -1)
            continue;
        String filename = sstable.descriptor.filenameFor(Component.DATA);
        RandomAccessFile raf = null;
        int mappedLength = -1;
        MappedByteBuffer mappedData = null;
        MappedFileDataInput file = null;
        try {
            raf = new RandomAccessFile(filename, "r");
            assert position < raf.length();
            mappedLength = (raf.length() - position) < Integer.MAX_VALUE ? (int) (raf.length() - position) : Integer.MAX_VALUE;
            mappedData = raf.getChannel().map(FileChannel.MapMode.READ_ONLY, position, mappedLength);
            file = new MappedFileDataInput(mappedData, filename, 0);
            if (file == null)
                continue;
            // Verify key was found in data file
            DecoratedKey keyInDisk = SSTableReader.decodeKey(sstable.partitioner, sstable.descriptor, ByteBufferUtil.readWithShortLength(file));
            assert keyInDisk.equals(decoratedKey) : String.format("%s != %s in %s", keyInDisk, decoratedKey, file.getPath());
            long rowSize = SSTableReader.readRowSize(file, sstable.descriptor);
            assert rowSize > 0;
            assert rowSize < mappedLength;
            Filter bf = IndexHelper.defreezeBloomFilter(file, sstable.descriptor.usesOldBloomFilter);
            // verify this column in in this version of the row.
            if (!bf.isPresent(sblockId))
                continue;
            List<IndexHelper.IndexInfo> indexList = IndexHelper.deserializeIndex(file);
            // we can stop early if bloom filter says none of the
            // columns actually exist -- but,
            // we can't stop before initializing the cf above, in
            // case there's a relevant tombstone
            ColumnFamilySerializer serializer = ColumnFamily.serializer();
            try {
                ColumnFamily cf = serializer.deserializeFromSSTableNoColumns(ColumnFamily.create(sstable.metadata), file);
                if (cf.isMarkedForDelete())
                    continue;
            } catch (Exception e) {
                e.printStackTrace();
                throw new IOException(serializer + " failed to deserialize " + sstable.getColumnFamilyName() + " with " + sstable.metadata + " from " + file, e);
            }
            Integer sblockLength = null;
            if (indexList == null)
                sblockLength = seekToSubColumn(sstable.metadata, file, sblockId);
            else
                sblockLength = seekToSubColumn(sstable.metadata, file, sblockId, indexList);
            if (sblockLength == null || sblockLength < 0)
                continue;
            int bytesReadFromStart = mappedLength - (int) file.bytesRemaining();
            if (logger.isDebugEnabled())
                logger.debug("BlockLength = " + sblockLength + " Availible " + file.bytesRemaining());
            assert offset <= sblockLength : String.format("%d > %d", offset, sblockLength);
            long dataOffset = position + bytesReadFromStart;
            if (file.bytesRemaining() == 0 || sblockLength == 0)
                continue;
            return new LocalBlock(file.getPath(), dataOffset + offset, sblockLength - offset);
        } catch (IOException e) {
            throw new TException(e);
        } finally {
            FileUtils.closeQuietly(raf);
        }
    }
    return null;
}
Also used : TException(org.apache.thrift.TException) Token(org.apache.cassandra.dht.Token) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) TException(org.apache.thrift.TException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) TrackerManagerException(org.apache.cassandra.hadoop.trackers.TrackerManagerException) SSTableReader(org.apache.cassandra.io.sstable.SSTableReader) RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) Filter(org.apache.cassandra.utils.Filter)

Example 83 with Token

use of org.apache.cassandra.dht.Token in project cassandra by apache.

the class SizeEstimatesRecorder method recordSizeEstimates.

@SuppressWarnings("resource")
private void recordSizeEstimates(ColumnFamilyStore table, Collection<Range<Token>> localRanges) {
    // for each local primary range, estimate (crudely) mean partition size and partitions count.
    Map<Range<Token>, Pair<Long, Long>> estimates = new HashMap<>(localRanges.size());
    for (Range<Token> localRange : localRanges) {
        for (Range<Token> unwrappedRange : localRange.unwrap()) {
            // filter sstables that have partitions in this range.
            Refs<SSTableReader> refs = null;
            long partitionsCount, meanPartitionSize;
            try {
                while (refs == null) {
                    Iterable<SSTableReader> sstables = table.getTracker().getView().select(SSTableSet.CANONICAL);
                    SSTableIntervalTree tree = SSTableIntervalTree.build(sstables);
                    Range<PartitionPosition> r = Range.makeRowRange(unwrappedRange);
                    Iterable<SSTableReader> canonicalSSTables = View.sstablesInBounds(r.left, r.right, tree);
                    refs = Refs.tryRef(canonicalSSTables);
                }
                // calculate the estimates.
                partitionsCount = estimatePartitionsCount(refs, unwrappedRange);
                meanPartitionSize = estimateMeanPartitionSize(refs);
            } finally {
                if (refs != null)
                    refs.release();
            }
            estimates.put(unwrappedRange, Pair.create(partitionsCount, meanPartitionSize));
        }
    }
    // atomically update the estimates.
    SystemKeyspace.updateSizeEstimates(table.metadata.keyspace, table.metadata.name, estimates);
}
Also used : Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) SSTableIntervalTree(org.apache.cassandra.db.lifecycle.SSTableIntervalTree) Pair(org.apache.cassandra.utils.Pair)

Example 84 with Token

use of org.apache.cassandra.dht.Token in project cassandra by apache.

the class HintsService method writeForAllReplicas.

/**
     * Write a hint for all replicas. Used to re-dispatch hints whose destination is either missing or no longer correct.
     */
void writeForAllReplicas(Hint hint) {
    String keyspaceName = hint.mutation.getKeyspaceName();
    Token token = hint.mutation.key().getToken();
    Iterable<UUID> hostIds = transform(filter(StorageService.instance.getNaturalAndPendingEndpoints(keyspaceName, token), StorageProxy::shouldHint), StorageService.instance::getHostIdForEndpoint);
    write(hostIds, hint);
}
Also used : Token(org.apache.cassandra.dht.Token) UUID(java.util.UUID)

Example 85 with Token

use of org.apache.cassandra.dht.Token in project cassandra by apache.

the class TokenSerializer method serialize.

public static void serialize(IPartitioner partitioner, Collection<Token> tokens, DataOutput out) throws IOException {
    for (Token token : tokens) {
        ByteBuffer tokenBuffer = partitioner.getTokenFactory().toByteArray(token);
        assert tokenBuffer.arrayOffset() == 0;
        ByteBufferUtil.writeWithLength(tokenBuffer.array(), out);
    }
    out.writeInt(0);
}
Also used : Token(org.apache.cassandra.dht.Token) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Token (org.apache.cassandra.dht.Token)173 Range (org.apache.cassandra.dht.Range)73 InetAddress (java.net.InetAddress)66 Test (org.junit.Test)65 BigIntegerToken (org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken)27 TokenMetadata (org.apache.cassandra.locator.TokenMetadata)27 IPartitioner (org.apache.cassandra.dht.IPartitioner)26 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)23 ArrayList (java.util.ArrayList)16 UUID (java.util.UUID)16 VersionedValue (org.apache.cassandra.gms.VersionedValue)15 StringToken (org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken)14 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)9 IOException (java.io.IOException)8 ByteBuffer (java.nio.ByteBuffer)8 BytesToken (org.apache.cassandra.dht.ByteOrderedPartitioner.BytesToken)8 AbstractReplicationStrategy (org.apache.cassandra.locator.AbstractReplicationStrategy)8 Set (java.util.Set)7 LongToken (org.apache.cassandra.dht.Murmur3Partitioner.LongToken)7 HashSet (java.util.HashSet)6