Search in sources :

Example 6 with KeyspaceMetadata

use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.

the class UFTest method testBrokenFunction.

@Test
public void testBrokenFunction() throws Throwable {
    createTable("CREATE TABLE %s (key int primary key, dval double)");
    execute("INSERT INTO %s (key, dval) VALUES (?, ?)", 1, 1d);
    String fName = createFunction(KEYSPACE_PER_TEST, "double", "CREATE OR REPLACE FUNCTION %s(val double) " + "RETURNS NULL ON NULL INPUT " + "RETURNS double " + "LANGUAGE JAVA\n" + "AS 'throw new RuntimeException();';");
    KeyspaceMetadata ksm = Schema.instance.getKeyspaceMetadata(KEYSPACE_PER_TEST);
    UDFunction f = (UDFunction) ksm.functions.get(parseFunctionName(fName)).iterator().next();
    UDFunction broken = UDFunction.createBrokenFunction(f.name(), f.argNames(), f.argTypes(), f.returnType(), true, "java", f.body(), new InvalidRequestException("foo bar is broken"));
    Schema.instance.load(ksm.withSwapped(ksm.functions.without(f.name(), f.argTypes()).with(broken)));
    assertInvalidThrowMessage("foo bar is broken", InvalidRequestException.class, "SELECT key, " + fName + "(dval) FROM %s");
}
Also used : InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) JavaBasedUDFunction(org.apache.cassandra.cql3.functions.JavaBasedUDFunction) UDFunction(org.apache.cassandra.cql3.functions.UDFunction) Test(org.junit.Test)

Example 7 with KeyspaceMetadata

use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.

the class MoveTest method addNetworkTopologyKeyspace.

private static void addNetworkTopologyKeyspace(String keyspaceName, Integer... replicas) throws Exception {
    DatabaseDescriptor.setEndpointSnitch(new AbstractNetworkTopologySnitch() {

        //Odd IPs are in DC1 and Even are in DC2. Endpoints upto .14 will have unique racks and
        // then will be same for a set of three.
        @Override
        public String getRack(InetAddress endpoint) {
            int ipLastPart = getIPLastPart(endpoint);
            if (ipLastPart <= 14)
                return UUID.randomUUID().toString();
            else
                return "RAC" + (ipLastPart % 3);
        }

        @Override
        public String getDatacenter(InetAddress endpoint) {
            if (getIPLastPart(endpoint) % 2 == 0)
                return "DC2";
            else
                return "DC1";
        }

        private int getIPLastPart(InetAddress endpoint) {
            String str = endpoint.toString();
            int index = str.lastIndexOf(".");
            return Integer.parseInt(str.substring(index + 1).trim());
        }
    });
    final TokenMetadata tmd = StorageService.instance.getTokenMetadata();
    tmd.clearUnsafe();
    tmd.updateHostId(UUID.randomUUID(), InetAddress.getByName("127.0.0.1"));
    tmd.updateHostId(UUID.randomUUID(), InetAddress.getByName("127.0.0.2"));
    KeyspaceMetadata keyspace = KeyspaceMetadata.create(keyspaceName, KeyspaceParams.nts(configOptions(replicas)), Tables.of(TableMetadata.builder(keyspaceName, "CF1").addPartitionKeyColumn("key", BytesType.instance).build()));
    MigrationManager.announceNewKeyspace(keyspace);
}
Also used : TokenMetadata(org.apache.cassandra.locator.TokenMetadata) InetAddress(java.net.InetAddress) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) AbstractNetworkTopologySnitch(org.apache.cassandra.locator.AbstractNetworkTopologySnitch)

Example 8 with KeyspaceMetadata

use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.

the class StorageServiceServerTest method testPrimaryRangesWithNetworkTopologyStrategy.

@Test
public void testPrimaryRangesWithNetworkTopologyStrategy() throws Exception {
    TokenMetadata metadata = StorageService.instance.getTokenMetadata();
    metadata.clearUnsafe();
    // DC1
    metadata.updateNormalToken(new StringToken("A"), InetAddress.getByName("127.0.0.1"));
    metadata.updateNormalToken(new StringToken("C"), InetAddress.getByName("127.0.0.2"));
    // DC2
    metadata.updateNormalToken(new StringToken("B"), InetAddress.getByName("127.0.0.4"));
    metadata.updateNormalToken(new StringToken("D"), InetAddress.getByName("127.0.0.5"));
    Map<String, String> configOptions = new HashMap<>();
    configOptions.put("DC1", "1");
    configOptions.put("DC2", "1");
    configOptions.put(ReplicationParams.CLASS, "NetworkTopologyStrategy");
    Keyspace.clear("Keyspace1");
    KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, configOptions));
    Schema.instance.load(meta);
    Collection<Range<Token>> primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddress.getByName("127.0.0.1"));
    assert primaryRanges.size() == 1;
    assert primaryRanges.contains(new Range<Token>(new StringToken("D"), new StringToken("A")));
    primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddress.getByName("127.0.0.2"));
    assert primaryRanges.size() == 1;
    assert primaryRanges.contains(new Range<Token>(new StringToken("B"), new StringToken("C")));
    primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddress.getByName("127.0.0.4"));
    assert primaryRanges.size() == 1;
    assert primaryRanges.contains(new Range<Token>(new StringToken("A"), new StringToken("B")));
    primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddress.getByName("127.0.0.5"));
    assert primaryRanges.size() == 1;
    assert primaryRanges.contains(new Range<Token>(new StringToken("C"), new StringToken("D")));
}
Also used : LongToken(org.apache.cassandra.dht.Murmur3Partitioner.LongToken) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Range(org.apache.cassandra.dht.Range) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Test(org.junit.Test)

Example 9 with KeyspaceMetadata

use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.

the class StorageServiceServerTest method testPrimaryRangesWithSimpleStrategy.

@Test
public void testPrimaryRangesWithSimpleStrategy() throws Exception {
    TokenMetadata metadata = StorageService.instance.getTokenMetadata();
    metadata.clearUnsafe();
    metadata.updateNormalToken(new StringToken("A"), InetAddress.getByName("127.0.0.1"));
    metadata.updateNormalToken(new StringToken("B"), InetAddress.getByName("127.0.0.2"));
    metadata.updateNormalToken(new StringToken("C"), InetAddress.getByName("127.0.0.3"));
    Keyspace.clear("Keyspace1");
    KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.simpleTransient(2));
    Schema.instance.load(meta);
    Collection<Range<Token>> primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddress.getByName("127.0.0.1"));
    assert primaryRanges.size() == 1;
    assert primaryRanges.contains(new Range<Token>(new StringToken("C"), new StringToken("A")));
    primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddress.getByName("127.0.0.2"));
    assert primaryRanges.size() == 1;
    assert primaryRanges.contains(new Range<Token>(new StringToken("A"), new StringToken("B")));
    primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddress.getByName("127.0.0.3"));
    assert primaryRanges.size() == 1;
    assert primaryRanges.contains(new Range<Token>(new StringToken("B"), new StringToken("C")));
}
Also used : LongToken(org.apache.cassandra.dht.Murmur3Partitioner.LongToken) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Range(org.apache.cassandra.dht.Range) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Test(org.junit.Test)

Example 10 with KeyspaceMetadata

use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.

the class StorageServiceServerTest method testPrimaryRangeForEndpointWithinDCWithNetworkTopologyStrategyOneDCOnly.

@Test
public void testPrimaryRangeForEndpointWithinDCWithNetworkTopologyStrategyOneDCOnly() throws Exception {
    TokenMetadata metadata = StorageService.instance.getTokenMetadata();
    metadata.clearUnsafe();
    // DC1
    metadata.updateNormalToken(new StringToken("A"), InetAddress.getByName("127.0.0.1"));
    metadata.updateNormalToken(new StringToken("C"), InetAddress.getByName("127.0.0.2"));
    // DC2
    metadata.updateNormalToken(new StringToken("B"), InetAddress.getByName("127.0.0.4"));
    metadata.updateNormalToken(new StringToken("D"), InetAddress.getByName("127.0.0.5"));
    Map<String, String> configOptions = new HashMap<>();
    configOptions.put("DC2", "2");
    configOptions.put(ReplicationParams.CLASS, "NetworkTopologyStrategy");
    Keyspace.clear("Keyspace1");
    KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, configOptions));
    Schema.instance.load(meta);
    // endpoints in DC1 should not have primary range
    Collection<Range<Token>> primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddress.getByName("127.0.0.1"));
    assertTrue(primaryRanges.isEmpty());
    primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddress.getByName("127.0.0.2"));
    assertTrue(primaryRanges.isEmpty());
    // endpoints in DC2 should have primary ranges which also cover DC1
    primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddress.getByName("127.0.0.4"));
    assertTrue(primaryRanges.size() == 2);
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("D"), new StringToken("A"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("A"), new StringToken("B"))));
    primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddress.getByName("127.0.0.5"));
    assertTrue(primaryRanges.size() == 2);
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("C"), new StringToken("D"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("B"), new StringToken("C"))));
}
Also used : TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Range(org.apache.cassandra.dht.Range) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Test(org.junit.Test)

Aggregations

KeyspaceMetadata (org.apache.cassandra.schema.KeyspaceMetadata)30 Test (org.junit.Test)17 TokenMetadata (org.apache.cassandra.locator.TokenMetadata)12 StringToken (org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken)11 Range (org.apache.cassandra.dht.Range)8 TableMetadata (org.apache.cassandra.schema.TableMetadata)8 InetAddress (java.net.InetAddress)6 LongToken (org.apache.cassandra.dht.Murmur3Partitioner.LongToken)6 Token (org.apache.cassandra.dht.Token)6 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)4 HashMap (java.util.HashMap)3 UserType (org.apache.cassandra.db.marshal.UserType)3 NetworkTopologyStrategy (org.apache.cassandra.locator.NetworkTopologyStrategy)3 TriggerMetadata (org.apache.cassandra.schema.TriggerMetadata)3 JavaBasedUDFunction (org.apache.cassandra.cql3.functions.JavaBasedUDFunction)1 UDAggregate (org.apache.cassandra.cql3.functions.UDAggregate)1 UDFunction (org.apache.cassandra.cql3.functions.UDFunction)1 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)1 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)1 DataOutputBufferFixed (org.apache.cassandra.io.util.DataOutputBufferFixed)1