Java Code Examples for org.apache.cassandra.service.MigrationManager#announceNewKeyspace()

The following examples show how to use org.apache.cassandra.service.MigrationManager#announceNewKeyspace() . 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: CassandraRunner.java    From staash with Apache License 2.0 6 votes vote down vote up
private void maybeCreateKeyspace(RequiresKeyspace rk, RequiresColumnFamily rcf) {
  logger.debug("RequiresKeyspace annotation has keyspace name: {}", rk.ksName());
  List<CFMetaData> cfs = extractColumnFamily(rcf);
  try {
    MigrationManager
            .announceNewKeyspace(KSMetaData.newKeyspace(rk.ksName(),
                    rk.strategy(), KSMetaData.optsWithRF(rk.replication()), false, cfs));
  } catch (AlreadyExistsException aee) {
    logger.info("using existing Keyspace for " + rk.ksName());
    if ( cfs.size() > 0 ) {
      maybeTruncateSafely(rcf);
    }
  } catch (Exception ex) {
    throw new RuntimeException("Failure creating keyspace for " + rk.ksName(),ex);
  }
}
 
Example 6
Source File: CreateKeyspaceStatement.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public boolean announceMigration(boolean isLocalOnly) throws RequestValidationException
{
    try
    {
        MigrationManager.announceNewKeyspace(attrs.asKSMetadata(name), isLocalOnly);
        return true;
    }
    catch (AlreadyExistsException e)
    {
        if (ifNotExists)
            return false;
        throw e;
    }
}
 
Example 7
Source File: CassandraServer.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public String system_add_keyspace(KsDef ks_def)
throws InvalidRequestException, SchemaDisagreementException, TException
{
    logger.debug("add_keyspace");

    try
    {
        ThriftValidation.validateKeyspaceNotSystem(ks_def.name);
        state().hasAllKeyspacesAccess(Permission.CREATE);
        ThriftValidation.validateKeyspaceNotYetExisting(ks_def.name);

        // generate a meaningful error if the user setup keyspace and/or column definition incorrectly
        for (CfDef cf : ks_def.cf_defs)
        {
            if (!cf.getKeyspace().equals(ks_def.getName()))
            {
                throw new InvalidRequestException("CfDef (" + cf.getName() +") had a keyspace definition that did not match KsDef");
            }
        }

        Collection<CFMetaData> cfDefs = new ArrayList<CFMetaData>(ks_def.cf_defs.size());
        for (CfDef cf_def : ks_def.cf_defs)
        {
            cf_def.unsetId(); // explicitly ignore any id set by client (same as system_add_column_family)
            CFMetaData cfm = CFMetaData.fromThrift(cf_def);
            cfm.addDefaultIndexNames();

            if (!cfm.getTriggers().isEmpty())
                state().ensureIsSuper("Only superusers are allowed to add triggers.");

            cfDefs.add(cfm);
        }
        MigrationManager.announceNewKeyspace(KSMetaData.fromThrift(ks_def, cfDefs.toArray(new CFMetaData[cfDefs.size()])));
        return Schema.instance.getVersion().toString();
    }
    catch (RequestValidationException e)
    {
        throw ThriftConversion.toThrift(e);
    }
}
 
Example 8
Source File: DatabaseDescriptorTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransKsMigration() throws ConfigurationException
{
    SchemaLoader.cleanupAndLeaveDirs();
    DatabaseDescriptor.loadSchemas();
    assertEquals(0, Schema.instance.getNonSystemKeyspaces().size());

    Gossiper.instance.start((int)(System.currentTimeMillis() / 1000));
    Keyspace.setInitialized();

    try
    {
        // add a few.
        MigrationManager.announceNewKeyspace(KSMetaData.testMetadata("ks0", SimpleStrategy.class, KSMetaData.optsWithRF(3)));
        MigrationManager.announceNewKeyspace(KSMetaData.testMetadata("ks1", SimpleStrategy.class, KSMetaData.optsWithRF(3)));

        assertNotNull(Schema.instance.getKSMetaData("ks0"));
        assertNotNull(Schema.instance.getKSMetaData("ks1"));

        Schema.instance.clearKeyspaceDefinition(Schema.instance.getKSMetaData("ks0"));
        Schema.instance.clearKeyspaceDefinition(Schema.instance.getKSMetaData("ks1"));

        assertNull(Schema.instance.getKSMetaData("ks0"));
        assertNull(Schema.instance.getKSMetaData("ks1"));

        DatabaseDescriptor.loadSchemas();

        assertNotNull(Schema.instance.getKSMetaData("ks0"));
        assertNotNull(Schema.instance.getKSMetaData("ks1"));
    }
    finally
    {
        Gossiper.instance.stop();
    }
}
 
Example 9
Source File: SchemaLoader.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void loadSchema() throws ConfigurationException
{
    prepareServer();

    // Migrations aren't happy if gossiper is not started.  Even if we don't use migrations though,
    // some tests now expect us to start gossip for them.
    startGossiper();

    // if you're messing with low-level sstable stuff, it can be useful to inject the schema directly
    // Schema.instance.load(schemaDefinition());
    for (KSMetaData ksm : schemaDefinition())
        MigrationManager.announceNewKeyspace(ksm);
}