@@ -62,6 +62,24 @@ class SpannerImpl extends BaseService<SpannerOptions> implements Spanner {
|
62 | 62 | static final String QUERY = "CloudSpannerOperation.ExecuteQuery";
|
63 | 63 | static final String READ = "CloudSpannerOperation.ExecuteRead";
|
64 | 64 |
|
| 65 | +private static final Object CLIENT_ID_LOCK = new Object(); |
| 66 | + |
| 67 | +@GuardedBy("CLIENT_ID_LOCK") |
| 68 | +private static final Map<DatabaseId, Long> CLIENT_IDS = new HashMap<>(); |
| 69 | + |
| 70 | +private static String nextDatabaseClientId(DatabaseId databaseId) { |
| 71 | +synchronized (CLIENT_ID_LOCK) { |
| 72 | +Long id = CLIENT_IDS.get(databaseId); |
| 73 | +if (id == null) { |
| 74 | +id = 1L; |
| 75 | +} else { |
| 76 | +id++; |
| 77 | +} |
| 78 | +CLIENT_IDS.put(databaseId, id); |
| 79 | +return String.format("client-%d", id); |
| 80 | +} |
| 81 | +} |
| 82 | + |
65 | 83 | private final SpannerRpc gapicRpc;
|
66 | 84 |
|
67 | 85 | @GuardedBy("this")
|
@@ -153,24 +171,26 @@ public DatabaseClient getDatabaseClient(DatabaseId db) {
|
153 | 171 | if (dbClients.containsKey(db)) {
|
154 | 172 | return dbClients.get(db);
|
155 | 173 | } else {
|
| 174 | +String clientId = nextDatabaseClientId(db); |
156 | 175 | List<LabelValue> labelValues =
|
157 | 176 | ImmutableList.of(
|
| 177 | +LabelValue.create(clientId), |
158 | 178 | LabelValue.create(db.getDatabase()),
|
159 | 179 | LabelValue.create(db.getInstanceId().getName()),
|
160 | 180 | LabelValue.create(GaxProperties.getLibraryVersion(getOptions().getClass())));
|
161 | 181 | SessionPool pool =
|
162 | 182 | SessionPool.createPool(
|
163 | 183 | getOptions(), SpannerImpl.this.getSessionClient(db), labelValues);
|
164 |
| -DatabaseClientImpl dbClient = createDatabaseClient(pool); |
| 184 | +DatabaseClientImpl dbClient = createDatabaseClient(clientId, pool); |
165 | 185 | dbClients.put(db, dbClient);
|
166 | 186 | return dbClient;
|
167 | 187 | }
|
168 | 188 | }
|
169 | 189 | }
|
170 | 190 |
|
171 | 191 | @VisibleForTesting
|
172 |
| -DatabaseClientImpl createDatabaseClient(SessionPool pool) { |
173 |
| -return new DatabaseClientImpl(pool); |
| 192 | +DatabaseClientImpl createDatabaseClient(String clientId, SessionPool pool) { |
| 193 | +return new DatabaseClientImpl(clientId, pool); |
174 | 194 | }
|
175 | 195 |
|
176 | 196 | @Override
|
|
0 commit comments