Skip to content

Commit 1ab752f

Browse files
committed
chore: Remove credential hashing from MetricServiceClient cache key
1 parent c982e17 commit 1ab752f

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/telemetry/DatastoreCloudMonitoringExporter.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,10 @@ static class CachedMetricsClient {
8585
}
8686

8787
/**
88-
* Shared cache for {@link MetricServiceClient} instances, keyed by
89-
* "projectId:databaseId:credentialsHashCode". Sharing a single gRPC channel across exporter
90-
* instances that target the same project, database, and credentials avoids per-client channel
91-
* overhead (threads, connections, memory). The credentials hash ensures that clients using
92-
* different credentials get their own isolated channel. Reference counting is used to safely shut
93-
* down the client when no longer needed.
88+
* Shared cache for {@link MetricServiceClient} instances, keyed by "projectId:databaseId".
89+
* Sharing a single gRPC channel across exporter instances that target the same project and
90+
* database avoids per-client channel overhead (threads, connections, memory). Reference counting
91+
* is used to safely shut down the client when no longer needed.
9492
*/
9593
static final ConcurrentHashMap<String, CachedMetricsClient> METRICS_CLIENT_CACHE =
9694
new ConcurrentHashMap<>();
@@ -136,8 +134,7 @@ static DatastoreCloudMonitoringExporter create(
136134
String databaseId,
137135
Credentials credentials,
138136
Map<String, String> clientAttributes) {
139-
int credHash = credentials != null ? credentials.hashCode() : 0;
140-
String key = projectId + ":" + databaseId + ":" + credHash;
137+
String key = projectId + ":" + databaseId;
141138

142139
// Use compute to acquire or create the client atomically with reference counting.
143140
// If creation fails, we log the error and return null so it's not added to the map.

java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/telemetry/DatastoreCloudMonitoringExporterTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void setUp() {
8383

8484
exporter =
8585
new DatastoreCloudMonitoringExporter(
86-
PROJECT_ID + ":" + DATABASE_ID + ":0",
86+
PROJECT_ID + ":" + DATABASE_ID,
8787
PROJECT_ID,
8888
DATABASE_ID,
8989
fakeMetricServiceClient,
@@ -166,22 +166,24 @@ public void testClientCacheReferenceCounting() {
166166
EasyMock.expectLastCall(); // Expect shutdown when refCount reaches 0
167167
replay(mockClient);
168168

169-
String key = PROJECT_ID + ":" + DATABASE_ID + ":0";
169+
String key = PROJECT_ID + ":" + DATABASE_ID;
170170
DatastoreCloudMonitoringExporter.CachedMetricsClient cachedMetricsClient =
171171
new DatastoreCloudMonitoringExporter.CachedMetricsClient(mockClient);
172172
cachedMetricsClient.refCount.set(2); // Simulate 2 references
173173
DatastoreCloudMonitoringExporter.METRICS_CLIENT_CACHE.put(key, cachedMetricsClient);
174174

175175
DatastoreCloudMonitoringExporter exporter1 =
176-
new DatastoreCloudMonitoringExporter(key, PROJECT_ID, DATABASE_ID, mockClient, Collections.emptyMap());
176+
new DatastoreCloudMonitoringExporter(
177+
key, PROJECT_ID, DATABASE_ID, mockClient, Collections.emptyMap());
177178

178179
// First shutdown should decrement refCount to 1, but not close client
179180
exporter1.shutdown();
180181
assertThat(cachedMetricsClient.refCount.get()).isEqualTo(1);
181182
assertThat(DatastoreCloudMonitoringExporter.METRICS_CLIENT_CACHE.containsKey(key)).isTrue();
182183

183184
DatastoreCloudMonitoringExporter exporter2 =
184-
new DatastoreCloudMonitoringExporter(key, PROJECT_ID, DATABASE_ID, mockClient, Collections.emptyMap());
185+
new DatastoreCloudMonitoringExporter(
186+
key, PROJECT_ID, DATABASE_ID, mockClient, Collections.emptyMap());
185187

186188
// Second shutdown should decrement refCount to 0, close client, and remove from cache
187189
exporter2.shutdown();

0 commit comments

Comments
 (0)