|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.spanner.spi.v1; |
| 18 | + |
| 19 | +import com.google.api.core.InternalApi; |
| 20 | + |
| 21 | +/** |
| 22 | + * Cache for server connections used in location-aware routing. |
| 23 | + * |
| 24 | + * <p>Implementations are expected to cache {@link ChannelEndpoint} instances such that repeated |
| 25 | + * calls with the same address return the same instance. This allows routing components to |
| 26 | + * efficiently manage server references. |
| 27 | + * |
| 28 | + * <p>Implementations must be thread-safe. Multiple threads may concurrently call {@link |
| 29 | + * #get(String)} with different addresses. |
| 30 | + */ |
| 31 | +@InternalApi |
| 32 | +public interface ChannelEndpointCache { |
| 33 | + |
| 34 | + /** |
| 35 | + * Returns the default channel endpoint. |
| 36 | + * |
| 37 | + * <p>The default channel is the original endpoint configured in {@link |
| 38 | + * com.google.cloud.spanner.SpannerOptions}. It is used as a fallback when the location cache does |
| 39 | + * not have routing information for a request. |
| 40 | + * |
| 41 | + * @return the default channel, never null |
| 42 | + */ |
| 43 | + ChannelEndpoint defaultChannel(); |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns a cached server for the given address, creating it if needed. |
| 47 | + * |
| 48 | + * <p>If a server for this address already exists in the cache, the cached instance is returned. |
| 49 | + * Otherwise, a new server connection is created and cached. |
| 50 | + * |
| 51 | + * @param address the server address in "host:port" format |
| 52 | + * @return a server instance for the address, never null |
| 53 | + * @throws com.google.cloud.spanner.SpannerException if the channel cannot be created |
| 54 | + */ |
| 55 | + ChannelEndpoint get(String address); |
| 56 | + |
| 57 | + /** |
| 58 | + * Evicts a server from the cache and gracefully shuts down its channel. |
| 59 | + * |
| 60 | + * <p>This method should be called when a server becomes unhealthy or is no longer needed. The |
| 61 | + * channel shutdown is graceful: existing RPCs are allowed to complete, but new RPCs will not be |
| 62 | + * accepted on this channel. |
| 63 | + * |
| 64 | + * <p>If the address is not in the cache, this method does nothing. |
| 65 | + * |
| 66 | + * @param address the server address to evict |
| 67 | + */ |
| 68 | + void evict(String address); |
| 69 | + |
| 70 | + /** |
| 71 | + * Shuts down all cached server connections. |
| 72 | + * |
| 73 | + * <p>This method should be called when the Spanner client is closed to release all resources. |
| 74 | + * Each channel is shut down gracefully, allowing in-flight RPCs to complete. |
| 75 | + * |
| 76 | + * <p>After calling this method, the cache should not be used to create new connections. |
| 77 | + */ |
| 78 | + void shutdown(); |
| 79 | +} |
0 commit comments