-
Notifications
You must be signed in to change notification settings - Fork 692
Add SpannerOptions auto-configuration for emulator #2356
Add SpannerOptions auto-configuration for emulator #2356
Conversation
Currently, in order to execute against the emulator, Spanner relies on `SPANNER_EMULATOR_HOST` environment variable. This commit introduces new configuration properties in order to enable and set the emulator host.
Codecov Report
@@ Coverage Diff @@
## master #2356 +/- ##
============================================
- Coverage 81.10% 74.08% -7.03%
+ Complexity 2315 2102 -213
============================================
Files 259 260 +1
Lines 7563 7574 +11
Branches 785 785
============================================
- Hits 6134 5611 -523
- Misses 1103 1603 +500
- Partials 326 360 +34
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, Eddu, this is a really useful change for people who are starting/stopping the emulator manually. We just have to be careful to not connect to production Cloud Spanner when developers think they are starting an emulator.
SpannerOptions.Builder builder = SpannerOptions.newBuilder() | ||
.setProjectId(this.properties.getProjectId()) | ||
.setCredentials(NoCredentials.getInstance()); | ||
if (this.properties.getEmulatorHost() != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't set SpannerOptions.emulatorHost, then the client library will connect to the real Spanner instance, which may incur costs.
I'd rather fail loudly here if emulator host is not available.
@dmitry-s @meltsufin I recall a discussion recently about using .enabled vs the property used for something. Do you remember which style was decided on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the client library resolves the SPANNER_EMULATOR_HOST
environment variable. But, I agree it can be dismiss. I think we can validate if the environment variable or the property is set, otherwise we can fail and say neither environment variable or property was set
. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're providing NoCredentials
, there is no risk of connecting to the real Spanner and incurring costs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In regard to ".enabled vs the property" - .enabled is the way to go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, let's fail if "enabled" is set but "emulatorHost" is missing, since there is no way to make that configuration work. Assert.notEmpty
should work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks good overall, but can we please update the refdoc (spanner.adoc
) as well?
Thanks!
SpannerOptions.Builder builder = SpannerOptions.newBuilder() | ||
.setProjectId(this.properties.getProjectId()) | ||
.setCredentials(NoCredentials.getInstance()); | ||
if (this.properties.getEmulatorHost() != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're providing NoCredentials
, there is no risk of connecting to the real Spanner and incurring costs.
SpannerOptions.Builder builder = SpannerOptions.newBuilder() | ||
.setProjectId(this.properties.getProjectId()) | ||
.setCredentials(NoCredentials.getInstance()); | ||
if (this.properties.getEmulatorHost() != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In regard to ".enabled vs the property" - .enabled is the way to go.
PR is updated with docs and also in order to keep the consistency between emulators, I renamed the previous |
@@ -1435,10 +1437,11 @@ This command can be used to create Cloud Spanner instances: | |||
$ gcloud spanner instances create <instance-name> --config=emulator-config --description="<description>" --nodes=1 | |||
---- | |||
|
|||
Remember that you need to set the `SPANNER_EMULATOR_HOST` environment variable to use the emulator: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we want to remove this.
Setting/unsetting the variable is a quick way to switch all your tests to using the emulator and back to normal. Might be still useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with removing it. Even though it's available, it's not consistent with the other emulators. We can leave a note about it, but I'm afraid it will only confuse people.
docs/src/main/asciidoc/spanner.adoc
Outdated
@@ -88,6 +88,8 @@ The schema for the tables will be "ON DELETE NO ACTION" if `false`. | No | `true | |||
| `spring.cloud.gcp.spanner.writeSessionsFraction` | Fraction of sessions to be kept prepared for write transactions | No | 0.2 - Determined by Cloud Spanner client library | |||
| `spring.cloud.gcp.spanner.keepAliveIntervalMinutes` | How long to keep idle sessions alive | No | 30 - Determined by Cloud Spanner client library | |||
| `spring.cloud.gcp.spanner.failIfPoolExhausted` | If all sessions are in use, fail the request by throwing an exception. Otherwise, by default, block until a session becomes available. | No | `false` | |||
| `spring.cloud.gcp.spanner.emulator.enabled` | Enables the usage of an emulator. If this is set to true, then you should set the `spring.cloud.gcp.spanner.host-port` to the host:port of your locally running emulator instance. | No | `false` | |||
| `spring.cloud.gcp.spanner.host-port` | The host and port of the Spanner service; can be overridden to specify connecting to an already-running https://cloud.google.com/spanner/docs/emulator#installing_and_running_the_emulator[Spanner emulator] instance. | No | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -1435,10 +1437,11 @@ This command can be used to create Cloud Spanner instances: | |||
$ gcloud spanner instances create <instance-name> --config=emulator-config --description="<description>" --nodes=1 | |||
---- | |||
|
|||
Remember that you need to set the `SPANNER_EMULATOR_HOST` environment variable to use the emulator: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with removing it. Even though it's available, it's not consistent with the other emulators. We can leave a note about it, but I'm afraid it will only confuse people.
...org/springframework/cloud/gcp/autoconfigure/spanner/GcpSpannerEmulatorAutoConfiguration.java
Show resolved
Hide resolved
@@ -70,6 +71,8 @@ | |||
// Otherwise, by default, block until a session becomes available. | |||
private boolean failIfPoolExhausted = false; | |||
|
|||
private String hostPort; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment above like the other props.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eddumelendez I reverted back to emulator-host
because it's only used for the emulator. Also, since the default emulator host is localhost:9010
, I made that a default.
I hope you don't mind me adding commits to your fork. :)
don't mind at all. I'd prefer to use |
Currently, in order to execute against the emulator, Spanner relies
on
SPANNER_EMULATOR_HOST
environment variable. This commit introducesnew configuration properties in order to enable and set the emulator host.