patka / cassandra-migration

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

Getting the following warning when migrating: "This is an anti-pattern that should be avoided in production" #78

Open tadchristiansen opened 1 year ago

tadchristiansen commented 1 year ago

More context around the log:

{"timestamp":"2023-04-28 17:01:13.476","level":"INFO","thread":"main","logger":"org.cognitor.cassandra.migration.Database","message":"Changing keyspace of the session to 'payment'","context":"default"}
{"timestamp":"2023-04-28 17:01:13.478","level":"WARN","thread":"s0-io-11","logger":"com.datastax.oss.driver.internal.core.session.PoolManager","message":"[s0] Detected a keyspace change at runtime (<none> => payment). This is an anti-pattern that should be avoided in production (see 'advanced.request.warn-if-set-keyspace' in the configuration).","context":"default"}
{"timestamp":"2023-04-28 17:01:13.623","level":"INFO","thread":"main","logger":"org.cognitor.cassandra.migration.MigrationRepository","message":"Found 1 migration scripts","context":"default"}

The application.conf docs here: https://java-driver.docs.scylladb.com/stable/manual/core/configuration/reference/README.html Say:

 # Whether a warning is logged when a request (such as a CQL `USE ...`) changes the active
    # keyspace.
    # Switching keyspace at runtime is highly discouraged, because it is inherently unsafe (other
    # requests expecting the old keyspace might be running concurrently), and may cause statements
    # prepared before the change to fail.
    # It should only be done in very specific use cases where there is only a single client thread
    # executing synchronous queries (such as a cqlsh-like interpreter). In other cases, clients
    # should prefix table names in their queries instead.
    #
    # Note that CASSANDRA-10145 (scheduled for C* 4.0) will introduce a per-request keyspace option
    # as a workaround to this issue.
    #
    # Required: yes
    # Modifiable at runtime: yes, the new value will be used for keyspace switches occurring after
    #   the change.
    # Overridable in a profile: no
    warn-if-set-keyspace = true

The referenced per-request docs for Scylla (Cassandra compatible) are here https://java-driver.docs.scylladb.com/stable/manual/core/statements/per_query_keyspace/

Is there a better way to get rid of this warning?

patka commented 1 year ago

Hello,

I am not sure if this warning can be avoided from your end. The reason is that the Database executes a "use keyspace" statement. This is required if you would start with an empty database and use cassandra migration to create the keyspace in the first place. After it is created you need to use it if you do not want to rely for all scripts to always put the keyspace before the table name.

I can investigate if it is possible to check if we are already in the keyspace we want to modify and not always execute the "use keyspace" statement.

That being said, I would not configure Cassandra to not print this warning. In this context you know why the warning is coming but if you see it again outside of the context of Cassandra Migration it would be a good thing to investigate it further.

Cheers