Search in sources :

Example 96 with Token

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

the class SSTableRewriterTest method testSSTableSectionsForRanges.

@Test
public void testSSTableSectionsForRanges() throws IOException, InterruptedException, ExecutionException {
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    final ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF);
    truncate(cfs);
    cfs.addSSTable(writeFile(cfs, 1000));
    Collection<SSTableReader> allSSTables = cfs.getLiveSSTables();
    assertEquals(1, allSSTables.size());
    final Token firstToken = allSSTables.iterator().next().first.getToken();
    DatabaseDescriptor.setSSTablePreempiveOpenIntervalInMB(1);
    List<StreamSession.SSTableStreamingSections> sectionsBeforeRewrite = StreamSession.getSSTableSectionsForRanges(Collections.singleton(new Range<Token>(firstToken, firstToken)), Collections.singleton(cfs), 0L, null);
    assertEquals(1, sectionsBeforeRewrite.size());
    for (StreamSession.SSTableStreamingSections section : sectionsBeforeRewrite) section.ref.release();
    final AtomicInteger checkCount = new AtomicInteger();
    // needed since we get notified when compaction is done as well - we can't get sections for ranges for obsoleted sstables
    final AtomicBoolean done = new AtomicBoolean(false);
    final AtomicBoolean failed = new AtomicBoolean(false);
    Runnable r = new Runnable() {

        public void run() {
            while (!done.get()) {
                Set<Range<Token>> range = Collections.singleton(new Range<Token>(firstToken, firstToken));
                List<StreamSession.SSTableStreamingSections> sections = StreamSession.getSSTableSectionsForRanges(range, Collections.singleton(cfs), 0L, null);
                if (sections.size() != 1)
                    failed.set(true);
                for (StreamSession.SSTableStreamingSections section : sections) section.ref.release();
                checkCount.incrementAndGet();
                Uninterruptibles.sleepUninterruptibly(5, TimeUnit.MILLISECONDS);
            }
        }
    };
    Thread t = NamedThreadFactory.createThread(r);
    try {
        t.start();
        cfs.forceMajorCompaction();
    // reset
    } finally {
        DatabaseDescriptor.setSSTablePreempiveOpenIntervalInMB(50);
        done.set(true);
        t.join(20);
    }
    assertFalse(failed.get());
    assertTrue(checkCount.get() >= 2);
    truncate(cfs);
}
Also used : Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) StreamSession(org.apache.cassandra.streaming.StreamSession) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

Example 97 with Token

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

the class SSTableScannerTest method testMultipleRanges.

@Test
public void testMultipleRanges() throws IOException {
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore store = keyspace.getColumnFamilyStore(TABLE);
    store.clearUnsafe();
    // disable compaction while flushing
    store.disableAutoCompaction();
    for (int i = 0; i < 3; i++) for (int j = 2; j < 10; j++) insertRowWithKey(store.metadata(), i * 100 + j);
    store.forceBlockingFlush();
    assertEquals(1, store.getLiveSSTables().size());
    SSTableReader sstable = store.getLiveSSTables().iterator().next();
    // full range scan
    ISSTableScanner fullScanner = sstable.getScanner();
    assertScanContainsRanges(fullScanner, 2, 9, 102, 109, 202, 209);
    // scan all three ranges separately
    ISSTableScanner scanner = sstable.getScanner(makeRanges(1, 9, 101, 109, 201, 209));
    assertScanContainsRanges(scanner, 2, 9, 102, 109, 202, 209);
    // skip the first range
    scanner = sstable.getScanner(makeRanges(101, 109, 201, 209));
    assertScanContainsRanges(scanner, 102, 109, 202, 209);
    // skip the second range
    scanner = sstable.getScanner(makeRanges(1, 9, 201, 209));
    assertScanContainsRanges(scanner, 2, 9, 202, 209);
    // skip the last range
    scanner = sstable.getScanner(makeRanges(1, 9, 101, 109));
    assertScanContainsRanges(scanner, 2, 9, 102, 109);
    // the first scanned range stops short of the actual data in the first range
    scanner = sstable.getScanner(makeRanges(1, 5, 101, 109, 201, 209));
    assertScanContainsRanges(scanner, 2, 5, 102, 109, 202, 209);
    // the first scanned range requests data beyond actual data in the first range
    scanner = sstable.getScanner(makeRanges(1, 20, 101, 109, 201, 209));
    assertScanContainsRanges(scanner, 2, 9, 102, 109, 202, 209);
    // the middle scan range splits the outside two data ranges
    scanner = sstable.getScanner(makeRanges(1, 5, 6, 205, 206, 209));
    assertScanContainsRanges(scanner, 2, 5, 7, 9, 102, 109, 202, 205, 207, 209);
    // empty ranges
    scanner = sstable.getScanner(makeRanges(0, 1, 2, 20, 101, 109, 150, 159, 201, 209, 1000, 1001));
    assertScanContainsRanges(scanner, 3, 9, 102, 109, 202, 209);
    // out of order ranges
    scanner = sstable.getScanner(makeRanges(201, 209, 1, 20, 201, 209, 101, 109, 1000, 1001, 150, 159));
    assertScanContainsRanges(scanner, 2, 9, 102, 109, 202, 209);
    // only empty ranges
    scanner = sstable.getScanner(makeRanges(0, 1, 150, 159, 250, 259));
    assertFalse(scanner.hasNext());
    // no ranges is equivalent to a full scan
    scanner = sstable.getScanner(new ArrayList<Range<Token>>());
    assertFalse(scanner.hasNext());
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) ArrayList(java.util.ArrayList) Token(org.apache.cassandra.dht.Token) Test(org.junit.Test)

Example 98 with Token

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

the class PropertyFileSnitchTest method setup.

@Before
public void setup() throws ConfigurationException, IOException {
    String confFile = FBUtilities.resourceToFile(PropertyFileSnitch.SNITCH_PROPERTIES_FILENAME);
    effectiveFile = Paths.get(confFile);
    backupFile = Paths.get(confFile + ".bak");
    restoreOrigConfigFile();
    InetAddress[] hosts = { // this exists in the config file
    InetAddress.getByName("127.0.0.1"), // this exists in the config file
    InetAddress.getByName("127.0.0.2"), // this does not exist in the config file
    InetAddress.getByName("127.0.0.9") };
    IPartitioner partitioner = new RandomPartitioner();
    valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
    tokenMap = new HashMap<>();
    for (InetAddress host : hosts) {
        Set<Token> tokens = Collections.singleton(partitioner.getRandomToken());
        Gossiper.instance.initializeNodeUnsafe(host, UUID.randomUUID(), 1);
        Gossiper.instance.injectApplicationState(host, ApplicationState.TOKENS, valueFactory.tokens(tokens));
        setNodeShutdown(host);
        tokenMap.put(host, tokens);
    }
}
Also used : RandomPartitioner(org.apache.cassandra.dht.RandomPartitioner) VersionedValue(org.apache.cassandra.gms.VersionedValue) Token(org.apache.cassandra.dht.Token) InetAddress(java.net.InetAddress) IPartitioner(org.apache.cassandra.dht.IPartitioner) Before(org.junit.Before)

Example 99 with Token

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

the class SimpleStrategyTest method testStringEndpoints.

@Test
public void testStringEndpoints() throws UnknownHostException {
    IPartitioner partitioner = OrderPreservingPartitioner.instance;
    List<Token> endpointTokens = new ArrayList<Token>();
    List<Token> keyTokens = new ArrayList<Token>();
    for (int i = 0; i < 5; i++) {
        endpointTokens.add(new StringToken(String.valueOf((char) ('a' + i * 2))));
        keyTokens.add(partitioner.getToken(ByteBufferUtil.bytes(String.valueOf((char) ('a' + i * 2 + 1)))));
    }
    verifyGetNaturalEndpoints(endpointTokens.toArray(new Token[0]), keyTokens.toArray(new Token[0]));
}
Also used : ArrayList(java.util.ArrayList) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Token(org.apache.cassandra.dht.Token) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) IPartitioner(org.apache.cassandra.dht.IPartitioner) Test(org.junit.Test)

Example 100 with Token

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

the class NetworkTopologyStrategyTest method testLargeCluster.

@Test
public void testLargeCluster() throws UnknownHostException, ConfigurationException {
    int[] dcRacks = new int[] { 2, 4, 8 };
    int[] dcEndpoints = new int[] { 128, 256, 512 };
    int[] dcReplication = new int[] { 2, 6, 6 };
    IEndpointSnitch snitch = new RackInferringSnitch();
    DatabaseDescriptor.setEndpointSnitch(snitch);
    TokenMetadata metadata = new TokenMetadata();
    Map<String, String> configOptions = new HashMap<String, String>();
    Multimap<InetAddress, Token> tokens = HashMultimap.create();
    int totalRF = 0;
    for (int dc = 0; dc < dcRacks.length; ++dc) {
        totalRF += dcReplication[dc];
        configOptions.put(Integer.toString(dc), Integer.toString(dcReplication[dc]));
        for (int rack = 0; rack < dcRacks[dc]; ++rack) {
            for (int ep = 1; ep <= dcEndpoints[dc] / dcRacks[dc]; ++ep) {
                byte[] ipBytes = new byte[] { 10, (byte) dc, (byte) rack, (byte) ep };
                InetAddress address = InetAddress.getByAddress(ipBytes);
                StringToken token = new StringToken(String.format("%02x%02x%02x", ep, rack, dc));
                logger.debug("adding node {} at {}", address, token);
                tokens.put(address, token);
            }
        }
    }
    metadata.updateNormalTokens(tokens);
    NetworkTopologyStrategy strategy = new NetworkTopologyStrategy(keyspaceName, metadata, snitch, configOptions);
    for (String testToken : new String[] { "123456", "200000", "000402", "ffffff", "400200" }) {
        List<InetAddress> endpoints = strategy.calculateNaturalEndpoints(new StringToken(testToken), metadata);
        Set<InetAddress> epSet = new HashSet<InetAddress>(endpoints);
        Assert.assertEquals(totalRF, endpoints.size());
        Assert.assertEquals(totalRF, epSet.size());
        logger.debug("{}: {}", testToken, endpoints);
    }
}
Also used : StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Token(org.apache.cassandra.dht.Token) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) InetAddress(java.net.InetAddress) Test(org.junit.Test)

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