googleapis / java-spanner

Apache License 2.0
55 stars 110 forks source link

Spanner Client Multiple Databases & Session Pools Support #2176

Open spicydev opened 1 year ago

spicydev commented 1 year ago

Is your feature request related to a problem? Please describe. Yes, Recently in my project I've tried to configure spanner java client to connect to multiple databases from different spanner instances using the Spring Data Framework with multiple SpannerTemplate Beans. This worked flawlessly but the problem started when we instrumented the app to export Opencensus metrics to Stackdriver using the Opencensus Implementation & exporter. Then we started to notice below blocking error that makes the second database inoperable to write or read data from.

StackTrace: java.lang.IllegalArgumentException: A different metric with the same name already registered. at io.opencensus.implcore.metrics.MetricRegistryImpl$RegisteredMeters.registerMeter(MetricRegistryImpl.java:172) at io.opencensus.implcore.metrics.MetricRegistryImpl$RegisteredMeters.access$200(MetricRegistryImpl.java:161) at io.opencensus.implcore.metrics.MetricRegistryImpl.addDerivedLongGauge(MetricRegistryImpl.java:88) at com.google.cloud.spanner.SessionPool.initMetricsCollection(SessionPool.java:2309) at com.google.cloud.spanner.SessionPool.(SessionPool.java:1845) at com.google.cloud.spanner.SessionPool.createPool(SessionPool.java:1822) at com.google.cloud.spanner.SessionPool.createPool(SessionPool.java:1781) at com.google.cloud.spanner.SpannerImpl.getDatabaseClient(SpannerImpl.java:225) at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) at org.springframework.cloud.gcp.data.spanner.core.admin.CachingComposingSupplier.get(CachingComposingSupplier.java:52) ... 114 frames truncated

Describe the solution you'd like I would like to see support to at least specify the metrics prefix when creating Database Client Bean using com.google.cloud.spanner.SpannerImpl#getDatabaseClient. That would resolve multiple databases issue and make the library support without sacrificing the metrics completely.

Describe alternatives you've considered Temporary solution is to completely remove the Opencensus Impl library from maven dependencies in the runtime or Disable configuring multiple databases in spanner client and connect to only one database at a time.

Any Feedback on the above issue or improving this feature request is appreciated.

spicydev commented 1 year ago

@olavloite and @rahul2393 . What are your thoughts on the above issue.

ansh0l commented 1 year ago

@spicydev : Could you reach out to us through a support ticket? we would like to better understand your use case.

gauravpurohit06 commented 1 year ago

@spicydev, Have you logged a support ticket for it?

spicydev commented 1 year ago

Yes. I did log a support ticket for this issue.

olavloite commented 1 year ago

@spicydev Sorry, I missed this the last time I was tagged. Do you know what version of the Java client Spring Data pulls in in your case?

spicydev commented 1 year ago

Hi @olavloite , To your question, we are currently using the v3.1.1 Spanner java client based on spring boot 2.3.12.RELEASE

But the above bug is still replicable with latest version of the Spanner java client. Then problem is that when we are trying to create multiple spanner database-clients with Opencensus metrics instrumented. The second database-client fails to create the session pool at the following method: SessionPool.initMetricsCollection().

Possible fix would be allowing the consumers of library to add metrics name prefix or adding the database name to metrics by default so that two database-clients can use different metric names and we can monitor both session pools separately in GCP Metrics Explorer.

Let me know if more information is needed. Thanks