patka / cassandra-migration

Schema migration library for Cassandra
MIT License
152 stars 47 forks source link

configure timeout #60

Open achimgrimm opened 2 years ago

achimgrimm commented 2 years ago

I like to increase the timeouts for the migration tasks for my spring boot app. The default timeout for my app is set to 2 seconds. I don't want to change that, as it will change the application performance.

But for the actual migration during startup, I don't mind a bigger timeout. So it would be great to configure that specifically so that the startup does not fails.

Log message from the spring boot start:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'migrationTask' defined in class path resource [org/cognitor/cassandra/migration/spring/CassandraMigrationAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.cognitor.cassandra.migration.MigrationTask]: Factory method 'migrationTask' threw exception; nested exception is com.datastax.oss.driver.api.core.DriverTimeoutException: Query timed out after PT2S
dnsbtchr commented 2 years ago

We had the exact same requirement and achieved it by setting a longer timeout for a profile called cassandra-migration in the application.conf file (see https://docs.datastax.com/en/developer/java-driver/4.0/manual/core/configuration/). It looks like this

`datastax-java-driver {

profiles { schema-migration { basic.request.timeout = 60 seconds } } ... }`

The profile can then set on the DataStax Database class using setExecutionProfileName. Our code for this looks like this

`log.info("Starting schema migration for keyspace[{}]", keyspaceName);

Keyspace keyspace = new Keyspace(keyspaceName);
try (Database database = new Database(cqlSessionSupplier.get(), keyspace)
    .setExecutionProfileName("schema-migration")
) {
  new MigrationTask(database, createMigrationRepository(), true).migrate();
}

log.info("Finished schema migration for keyspace[{}]", keyspaceName);`
patka commented 2 years ago

Thanks @dnsbtchr for answering this. I will create a FAQ section in the README where I put this.

@achimgrimm based on your reaction I assume this works for you :)

achimgrimm commented 2 years ago

Hi, I did not give it a try, as I was waiting for some more reaction. The solution will probably work fine for my use case. But I think, adding the timeout (or maybe an optional profile) as a config would make it way easier to integrate into spring boot.

ikstewa commented 2 years ago

We were able to resolve this using profiles as mentioned above.

However there is still one case where the execution profile is not always used. When you create a new instance of the 'Database' object, it will attempt to create the 'schema_migration' and 'schema_migration_leader' tables.

Is it possible to allow the execution profile name to be passed in on construction or refactor so the table creation does not happen in the constructor?

Edit: I created a pull request here: https://github.com/patka/cassandra-migration/pull/62

ikstewa commented 2 years ago

As of 2.5.0_v4 the execution profile can now be configured via spring: https://github.com/patka/cassandra-migration/blob/master_v4/cassandra-migration-spring-boot-starter/src/main/java/org/cognitor/cassandra/migration/spring/CassandraMigrationConfigurationProperties.java#L128-L144