Skip to content

don't exec create keyspace if it exists #2032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Wing924
Copy link
Contributor

@Wing924 Wing924 commented Jan 24, 2020

What this PR does:

Don't execute CREATE KEYSPACE IF NOT EXISTS if the given keyspace has already existed.

The CREATE KEYSPACE IF NOT EXISTS statement requires CREATE permission on <all keyspaces> even if it exists. It's not common that normal users have that privilege.

ref. how to check keyspace exist: https://github.com/gocql/gocql/blob/master/cassandra_test.go#L85-L97

Which issue(s) this PR fixes:

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
Signed-off-by: Wing924 <weihe924stephen@gmail.com>
Copy link
Contributor

@pstibrany pstibrany left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving, based on the https://github.com/gocql/gocql/blob/95d072f1b5bbc604ce10f41ce1c914578592ed00/cassandra_test.go#L85 which shows that ErrNoConnectionsStarted is expected if keyspace doesn't exist. It's weirdly named error though.

@pstibrany
Copy link
Contributor

Please update CHANGELOG.md as well.

@pstibrany
Copy link
Contributor

And thank you for your contribution!

Copy link
Contributor

@pracucci pracucci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! I left a couple of small comments. Please remember to add an entry to the CHANGELOG.md and mention the PR number too (see the format of other entries there).

@VineethReddy02 May you test this PR against your test cluster based on Cassandra, please?

@@ -70,7 +66,19 @@ func (cfg *Config) session() (*gocql.Session, error) {
cluster.ConnectTimeout = cfg.ConnectTimeout
cfg.setClusterConfig(cluster)

return cluster.CreateSession()
session, err := cluster.CreateSession()
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would simplify this into a guard:

if err == nil {
return session, nil
}
if err := cfg.createKeyspace(); err != nil {
return nil, errors.WithStack(err)
}
session, err = cluster.CreateSession()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you can simplify this into return cluster.CreateSession()

Signed-off-by: Wing924 <weihe924stephen@gmail.com>
@VineethReddy02
Copy link
Contributor

VineethReddy02 commented Jan 26, 2020

@pracucci I will test this PR and will update here.

@VineethReddy02
Copy link
Contributor

VineethReddy02 commented Jan 26, 2020

@pracucci @Wing924

  1. Somehow I missed this issue while running cassandra locally.
    https://github.com/cortexproject/cortex/blob/master/docs/guides/cortex-with-cassandra.md
    In this doc, I am creating keyspace explicitly by cqlsh prompt. But my config.file has the same keyspace. which should ideally try to again create the keyspace. But I don't see any error log that you mentioned.
  2. But when I don't have keyspace already created. It is throwing some irrelevant error. The changes in this PR is automatically creating the keyspace provided in the config.file
@Wing924
Copy link
Contributor Author

Wing924 commented Jan 26, 2020

@VineethReddy02
The doc don't enable cassandra auth. Please test it with auth enabled.
ref. http://cassandra.apache.org/doc/latest/operating/security.html#enabling-password-authentication

Copy link
Contributor

@pracucci pracucci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at my comments. I think the current changes introduce a regression. Moreover, please do some manual tests and report the result (ie. Cortex logs); minimum tests to do:

  1. Keyspace already created and no CREATE privilege
  2. Keyspace NOT created and no CREATE privilege
  3. Keyspace NOT created and CREATE privilege
@@ -83,6 +79,14 @@ func (cfg *Config) session() (*gocql.Session, error) {
}
cfg.setClusterConfig(cluster)

session, err := cluster.CreateSession()
if err == nil || err != gocql.ErrNoConnectionsStarted {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment to explain what ErrNoConnectionsStarted is.

Signed-off-by: Wing924 <weihe924stephen@gmail.com>
@Wing924
Copy link
Contributor Author

Wing924 commented Feb 7, 2020

Manual Test Result

Summary

It works as expected.

Logs

Keyspace NOT created and no CREATE privilege

level=info ts=2020-02-07T09:15:30.567056987Z caller=cortex.go:240 msg=initialising module=server
level=info ts=2020-02-07T09:15:30.570438508Z caller=server.go:117 http=0.0.0.0:9009 grpc=0.0.0.0:9095 msg="server listening on addresses"
level=info ts=2020-02-07T09:15:30.57064428Z caller=cortex.go:240 msg=initialising module=runtime-config
level=info ts=2020-02-07T09:15:30.570667901Z caller=manager.go:63 msg="runtime config disabled: file not specified"
level=info ts=2020-02-07T09:15:30.57068235Z caller=cortex.go:240 msg=initialising module=overrides
level=info ts=2020-02-07T09:15:30.570690426Z caller=cortex.go:240 msg=initialising module=table-manager
level=error ts=2020-02-07T09:15:53.450998721Z caller=log.go:141 msg="error initializing cortex" err="User test_cortex has no CREATE permission on <all keyspaces> or any of its parents ..."
(exit with error)

Keyspace already created and no CREATE privilege

level=info ts=2020-02-07T09:24:47.68719324Z caller=cortex.go:247 msg=initialising module=runtime-config
level=info ts=2020-02-07T09:24:47.687254652Z caller=manager.go:63 msg="runtime config disabled: file not specified"
level=info ts=2020-02-07T09:24:47.687275176Z caller=cortex.go:247 msg=initialising module=server
level=info ts=2020-02-07T09:24:47.690203425Z caller=server.go:147 http=0.0.0.0:9009 grpc=0.0.0.0:9095 msg="server listening on addresses"
level=info ts=2020-02-07T09:24:47.690455183Z caller=cortex.go:247 msg=initialising module=ring
level=info ts=2020-02-07T09:24:47.690612906Z caller=cortex.go:247 msg=initialising module=overrides
level=info ts=2020-02-07T09:24:47.69064727Z caller=cortex.go:247 msg=initialising module=store
level=info ts=2020-02-07T09:24:47.691175592Z caller=client.go:212 msg="value is nil" key=collectors/ring index=1
level=info ts=2020-02-07T09:25:25.175172864Z caller=cortex.go:247 msg=initialising module=ingester
level=info ts=2020-02-07T09:25:25.179362475Z caller=cortex.go:247 msg=initialising module=distributor
level=info ts=2020-02-07T09:25:25.179461895Z caller=cortex.go:247 msg=initialising module=table-manager
level=info ts=2020-02-07T09:25:25.179556392Z caller=lifecycler.go:476 msg="not loading tokens from file, tokens file path is empty"
level=info ts=2020-02-07T09:25:25.180010436Z caller=lifecycler.go:500 msg="instance not found in ring, adding with no tokens" ring=ingester
level=info ts=2020-02-07T09:25:25.180313711Z caller=lifecycler.go:368 msg="auto-joining cluster after timeout" ring=ingester
level=info ts=2020-02-07T09:25:35.553827695Z caller=cortex.go:247 msg=initialising module=querier
level=info ts=2020-02-07T09:25:35.553988049Z caller=table_manager.go:220 msg="synching tables" expected_tables=2
level=info ts=2020-02-07T09:25:35.554530448Z caller=worker.go:72 msg="no address specified, not starting worker"
level=info ts=2020-02-07T09:25:35.554568037Z caller=cortex.go:247 msg=initialising module=all
level=info ts=2020-02-07T09:25:35.554582372Z caller=main.go:100 msg="Starting Cortex" version="(version=, branch=, revision=)"
level=info ts=2020-02-07T09:25:35.590321567Z caller=table_manager.go:364 msg="creating table" table=chunk_2614
...
(running)

Keyspace NOT created and CREATE privilege

level=info ts=2020-02-07T09:34:35.233093746Z caller=cortex.go:247 msg=initialising module=server
level=info ts=2020-02-07T09:34:35.235921256Z caller=server.go:147 http=0.0.0.0:9009 grpc=0.0.0.0:9095 msg="server listening on addresses"
level=info ts=2020-02-07T09:34:35.236107137Z caller=cortex.go:247 msg=initialising module=runtime-config
level=info ts=2020-02-07T09:34:35.236140945Z caller=manager.go:63 msg="runtime config disabled: file not specified"
level=info ts=2020-02-07T09:34:35.236160646Z caller=cortex.go:247 msg=initialising module=ring
level=info ts=2020-02-07T09:34:35.236227622Z caller=cortex.go:247 msg=initialising module=overrides
level=info ts=2020-02-07T09:34:35.236241035Z caller=cortex.go:247 msg=initialising module=table-manager
level=info ts=2020-02-07T09:34:35.236868018Z caller=client.go:212 msg="value is nil" key=collectors/ring index=1
2020-02-07 09:34:35.521554 I | error: failed to connect to 100.89.2.196:9042 due to error: Keyspace 'cortex_test3' does not exist
2020-02-07 09:34:35.658739 I | error: failed to connect to 100.78.39.31:9042 due to error: Keyspace 'cortex_test3' does not exist
2020-02-07 09:34:35.805070 I | error: failed to connect to 100.89.4.223:9042 due to error: Keyspace 'cortex_test3' does not exist
2020-02-07 09:34:35.993499 I | error: failed to connect to 100.78.39.84:9042 due to error: Keyspace 'cortex_test3' does not exist
2020-02-07 09:34:36.190253 I | error: failed to connect to 100.89.4.39:9042 due to error: Keyspace 'cortex_test3' does not exist
2020-02-07 09:34:36.308985 I | error: failed to connect to 100.89.2.196:9042 due to error: Keyspace 'cortex_test3' does not exist
2020-02-07 09:34:36.490566 I | error: failed to connect to 100.89.0.75:9042 due to error: Keyspace 'cortex_test3' does not exist
2020-02-07 09:36:09.814857 I | gocql: cluster schema versions not consistent: [2d2957e4-4456-3522-a945-0fa47f99ad4d 44bd4095-034c-342e-b99a-788ed1675b1b]
level=info ts=2020-02-07T09:36:28.548977067Z caller=cortex.go:247 msg=initialising module=distributor
level=info ts=2020-02-07T09:36:28.549158543Z caller=cortex.go:247 msg=initialising module=store
level=info ts=2020-02-07T09:36:28.549711683Z caller=table_manager.go:220 msg="synching tables" expected_tables=2
level=info ts=2020-02-07T09:36:28.58161796Z caller=table_manager.go:364 msg="creating table" table=chunk_2614
level=error ts=2020-02-07T09:36:43.549718646Z caller=pool.go:170 msg="error removing stale clients" err="empty ring"
level=info ts=2020-02-07T09:36:52.302659404Z caller=cortex.go:247 msg=initialising module=ingester
level=info ts=2020-02-07T09:36:52.303278785Z caller=cortex.go:247 msg=initialising module=querier
level=info ts=2020-02-07T09:36:52.303644135Z caller=worker.go:72 msg="no address specified, not starting worker"
level=info ts=2020-02-07T09:36:52.303688187Z caller=cortex.go:247 msg=initialising module=all
level=info ts=2020-02-07T09:36:52.303701327Z caller=main.go:100 msg="Starting Cortex" version="(version=, branch=, revision=)"
level=info ts=2020-02-07T09:36:52.307579708Z caller=lifecycler.go:476 msg="not loading tokens from file, tokens file path is empty"
level=info ts=2020-02-07T09:36:52.3076691Z caller=lifecycler.go:500 msg="instance not found in ring, adding with no tokens" ring=ingester
level=info ts=2020-02-07T09:36:52.308692624Z caller=lifecycler.go:368 msg="auto-joining cluster after timeout" ring=ingester
2020-02-07 09:37:29.148629 I | gocql: cluster schema versions not consistent: [e0684dff-d7ff-36b2-9cbc-deb8aae5583b 44bd4095-034c-342e-b99a-788ed1675b1b]
level=info ts=2020-02-07T09:37:29.148715011Z caller=table_manager.go:364 msg="creating table" table=index_2614
2020-02-07 09:38:29.466508 I | gocql: cluster schema versions not consistent: [d669c677-3fb1-3e72-8aaa-90de990af1ef 44bd4095-034c-342e-b99a-788ed1675b1b]
level=info ts=2020-02-07T09:39:36.821926199Z caller=table_manager.go:220 msg="synching tables" expected_tables=2
(running)
@VineethReddy02
Copy link
Contributor

Hey @Wing924 @pracucci
I just verified all the scenarios mentioned by @Wing924 from my end, All the mentioned scenarios work the same as @Wing924 mentioned.
Thanks @Wing924 for finding this issue and on fixing this :)

Copy link
Contributor

@pracucci pracucci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉 Thanks for the tests results!

@Wing924
Copy link
Contributor Author

Wing924 commented Feb 11, 2020

test failed but not from part of my changes.

@gouthamve gouthamve merged commit 220743a into cortexproject:master Feb 11, 2020
@Wing924 Wing924 deleted the dont_exec_create_keyspace_if_exist branch February 11, 2020 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants