Java Code Examples for org.apache.cassandra.config.KSMetaData#newKeyspace()

The following examples show how to use org.apache.cassandra.config.KSMetaData#newKeyspace() . 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: TriggersSchemaTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void newKsContainsCfWithTrigger() throws Exception
{
    TriggerDefinition td = TriggerDefinition.create(triggerName, triggerClass);
    CFMetaData cfm1 = CFMetaData.compile(String.format("CREATE TABLE %s (k int PRIMARY KEY, v int)", cfName), ksName);
    cfm1.addTriggerDefinition(td);
    KSMetaData ksm = KSMetaData.newKeyspace(ksName,
                                            SimpleStrategy.class,
                                            Collections.singletonMap("replication_factor", "1"),
                                            true,
                                            Collections.singletonList(cfm1));
    MigrationManager.announceNewKeyspace(ksm);

    CFMetaData cfm2 = Schema.instance.getCFMetaData(ksName, cfName);
    assertFalse(cfm2.getTriggers().isEmpty());
    assertEquals(1, cfm2.getTriggers().size());
    assertEquals(td, cfm2.getTriggers().get(triggerName));
}
 
Example 2
Source File: TriggersSchemaTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void addNewCfWithTriggerToKs() throws Exception
{
    KSMetaData ksm = KSMetaData.newKeyspace(ksName,
                                            SimpleStrategy.class,
                                            Collections.singletonMap("replication_factor", "1"),
                                            true,
                                            Collections.EMPTY_LIST);
    MigrationManager.announceNewKeyspace(ksm);

    CFMetaData cfm1 = CFMetaData.compile(String.format("CREATE TABLE %s (k int PRIMARY KEY, v int)", cfName), ksName);
    TriggerDefinition td = TriggerDefinition.create(triggerName, triggerClass);
    cfm1.addTriggerDefinition(td);

    MigrationManager.announceNewColumnFamily(cfm1);

    CFMetaData cfm2 = Schema.instance.getCFMetaData(ksName, cfName);
    assertFalse(cfm2.getTriggers().isEmpty());
    assertEquals(1, cfm2.getTriggers().size());
    assertEquals(td, cfm2.getTriggers().get(triggerName));
}
 
Example 3
Source File: TriggersSchemaTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void addTriggerToCf() throws Exception
{
    CFMetaData cfm1 = CFMetaData.compile(String.format("CREATE TABLE %s (k int PRIMARY KEY, v int)", cfName), ksName);
    KSMetaData ksm = KSMetaData.newKeyspace(ksName,
                                            SimpleStrategy.class,
                                            Collections.singletonMap("replication_factor", "1"),
                                            true,
                                            Collections.singletonList(cfm1));
    MigrationManager.announceNewKeyspace(ksm);

    CFMetaData cfm2 = Schema.instance.getCFMetaData(ksName, cfName).copy();
    TriggerDefinition td = TriggerDefinition.create(triggerName, triggerClass);
    cfm2.addTriggerDefinition(td);
    MigrationManager.announceColumnFamilyUpdate(cfm2, false);

    CFMetaData cfm3 = Schema.instance.getCFMetaData(ksName, cfName);
    assertFalse(cfm3.getTriggers().isEmpty());
    assertEquals(1, cfm3.getTriggers().size());
    assertEquals(td, cfm3.getTriggers().get(triggerName));
}
 
Example 4
Source File: TriggersSchemaTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void removeTriggerFromCf() throws Exception
{
    TriggerDefinition td = TriggerDefinition.create(triggerName, triggerClass);
    CFMetaData cfm1 = CFMetaData.compile(String.format("CREATE TABLE %s (k int PRIMARY KEY, v int)", cfName), ksName);
    cfm1.addTriggerDefinition(td);
    KSMetaData ksm = KSMetaData.newKeyspace(ksName,
                                            SimpleStrategy.class,
                                            Collections.singletonMap("replication_factor", "1"),
                                            true,
                                            Collections.singletonList(cfm1));
    MigrationManager.announceNewKeyspace(ksm);

    CFMetaData cfm2 = Schema.instance.getCFMetaData(ksName, cfName).copy();
    cfm2.removeTrigger(triggerName);
    MigrationManager.announceColumnFamilyUpdate(cfm2, false);

    CFMetaData cfm3 = Schema.instance.getCFMetaData(ksName, cfName).copy();
    assertTrue(cfm3.getTriggers().isEmpty());
}
 
Example 5
Source File: KSPropDefs.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public KSMetaData asKSMetadataUpdate(KSMetaData old) throws RequestValidationException
{
    String sClass = strategyClass;
    Map<String, String> sOptions = getReplicationOptions();
    if (sClass == null)
    {
        sClass = old.strategyClass.getName();
        sOptions = old.strategyOptions;
    }
    return KSMetaData.newKeyspace(old.name, sClass, sOptions, getBoolean(KW_DURABLE_WRITES, old.durableWrites));
}
 
Example 6
Source File: Auth.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static void setupAuthKeyspace()
{
    if (Schema.instance.getKSMetaData(AUTH_KS) == null)
    {
        try
        {
            KSMetaData ksm = KSMetaData.newKeyspace(AUTH_KS, SimpleStrategy.class.getName(), ImmutableMap.of("replication_factor", "1"), true);
            MigrationManager.announceNewKeyspace(ksm, 0, false);
        }
        catch (Exception e)
        {
            throw new AssertionError(e); // shouldn't ever happen.
        }
    }
}
 
Example 7
Source File: StorageServiceServerTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@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");

    Keyspace.clear("Keyspace1");
    KSMetaData meta = KSMetaData.newKeyspace("Keyspace1", "NetworkTopologyStrategy", configOptions, false);
    Schema.instance.setKeyspaceDefinition(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"))));
}
 
Example 8
Source File: KSPropDefs.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public KSMetaData asKSMetadata(String ksName) throws RequestValidationException
{
    return KSMetaData.newKeyspace(ksName, getReplicationStrategyClass(), getReplicationOptions(), getBoolean(KW_DURABLE_WRITES, true));
}
 
Example 9
Source File: StorageServiceServerTest.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@Test
public void testPrimaryRangeForEndpointWithinDCWithNetworkTopologyStrategy() 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");

    Keyspace.clear("Keyspace1");
    KSMetaData meta = KSMetaData.newKeyspace("Keyspace1", "NetworkTopologyStrategy", configOptions, false);
    Schema.instance.setKeyspaceDefinition(meta);

    Collection<Range<Token>> primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name,
                                                                                                        InetAddress.getByName("127.0.0.1"));
    assertEquals(2, primaryRanges.size());
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("D"), new StringToken("A"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("C"), new StringToken("D"))));

    primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddress.getByName("127.0.0.2"));
    assertEquals(2, primaryRanges.size());
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("A"), new StringToken("B"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("B"), new StringToken("C"))));

    primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddress.getByName("127.0.0.4"));
    assertEquals(2, primaryRanges.size());
    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"));
    assertEquals(2, primaryRanges.size());
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("B"), new StringToken("C"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("C"), new StringToken("D"))));
}
 
Example 10
Source File: StorageServiceServerTest.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@Test
public void testPrimaryRangeForEndpointWithinDCWithVnodes() throws Exception
{
    TokenMetadata metadata = StorageService.instance.getTokenMetadata();
    metadata.clearUnsafe();

    // DC1
    Multimap<InetAddress, Token> dc1 = HashMultimap.create();
    dc1.put(InetAddress.getByName("127.0.0.1"), new StringToken("A"));
    dc1.put(InetAddress.getByName("127.0.0.1"), new StringToken("E"));
    dc1.put(InetAddress.getByName("127.0.0.1"), new StringToken("H"));
    dc1.put(InetAddress.getByName("127.0.0.2"), new StringToken("C"));
    dc1.put(InetAddress.getByName("127.0.0.2"), new StringToken("I"));
    dc1.put(InetAddress.getByName("127.0.0.2"), new StringToken("J"));
    metadata.updateNormalTokens(dc1);

    // DC2
    Multimap<InetAddress, Token> dc2 = HashMultimap.create();
    dc2.put(InetAddress.getByName("127.0.0.4"), new StringToken("B"));
    dc2.put(InetAddress.getByName("127.0.0.4"), new StringToken("G"));
    dc2.put(InetAddress.getByName("127.0.0.4"), new StringToken("L"));
    dc2.put(InetAddress.getByName("127.0.0.5"), new StringToken("D"));
    dc2.put(InetAddress.getByName("127.0.0.5"), new StringToken("F"));
    dc2.put(InetAddress.getByName("127.0.0.5"), new StringToken("K"));
    metadata.updateNormalTokens(dc2);

    Map<String, String> configOptions = new HashMap<>();
    configOptions.put("DC1", "1");
    configOptions.put("DC2", "2");

    Keyspace.clear("Keyspace1");
    KSMetaData meta = KSMetaData.newKeyspace("Keyspace1", "NetworkTopologyStrategy", configOptions, false);
    Schema.instance.setKeyspaceDefinition(meta);

    // endpoints in DC1 should have primary ranges which also cover DC2
    Collection<Range<Token>> primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddress.getByName("127.0.0.1"));
    assertEquals(8, primaryRanges.size());
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("J"), new StringToken("K"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("K"), new StringToken("L"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("L"), new StringToken("A"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("C"), new StringToken("D"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("D"), new StringToken("E"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("E"), new StringToken("F"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("F"), new StringToken("G"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("G"), new StringToken("H"))));

    // endpoints in DC1 should have primary ranges which also cover DC2
    primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddress.getByName("127.0.0.2"));
    assertEquals(4, primaryRanges.size());
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("B"), new StringToken("C"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("A"), new StringToken("B"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("H"), new StringToken("I"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("I"), new StringToken("J"))));

    // endpoints in DC2 should have primary ranges which also cover DC1
    primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddress.getByName("127.0.0.4"));
    assertEquals(4, primaryRanges.size());
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("A"), new StringToken("B"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("F"), new StringToken("G"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("K"), new StringToken("L"))));
    // because /127.0.0.4 holds token "B" which is the next to token "A" from /127.0.0.1,
    // the node covers range (L, A]
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("L"), new StringToken("A"))));

    primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddress.getByName("127.0.0.5"));
    assertTrue(primaryRanges.size() == 8);
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("C"), new StringToken("D"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("E"), new StringToken("F"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("J"), new StringToken("K"))));
    // ranges from /127.0.0.1
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("D"), new StringToken("E"))));
    // the next token to "H" in DC2 is "K" in /127.0.0.5, so (G, H] goes to /127.0.0.5
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("G"), new StringToken("H"))));
    // ranges from /127.0.0.2
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("B"), new StringToken("C"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("H"), new StringToken("I"))));
    assertTrue(primaryRanges.contains(new Range<Token>(new StringToken("I"), new StringToken("J"))));
}