|
| 1 | +package com.google.cloud.spanner.spi.v1; |
| 2 | + |
| 3 | +import com.google.api.gax.grpc.GrpcTransportChannel; |
| 4 | +import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; |
| 5 | +import io.grpc.ManagedChannel; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.concurrent.ConcurrentHashMap; |
| 9 | + |
| 10 | +class GrpcChannelFinderServerFactory implements ChannelFinderServerFactory { |
| 11 | + private final InstantiatingGrpcChannelProvider.Builder channelBuilder; |
| 12 | + private final Map<String, GrpcChannelFinderServer> servers = new ConcurrentHashMap<>(); |
| 13 | + private final GrpcChannelFinderServer defaultServer; |
| 14 | + |
| 15 | + public GrpcChannelFinderServerFactory(InstantiatingGrpcChannelProvider.Builder channelBuilder) |
| 16 | + throws IOException { |
| 17 | + this.channelBuilder = channelBuilder; |
| 18 | + // The "default" server will use the original endpoint from the builder. |
| 19 | + this.defaultServer = |
| 20 | + new GrpcChannelFinderServer(this.channelBuilder.getEndpoint(), channelBuilder.build()); |
| 21 | + this.servers.put(this.defaultServer.getAddress(), this.defaultServer); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public ChannelFinderServer defaultServer() { |
| 26 | + return defaultServer; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public ChannelFinderServer create(String address) { |
| 31 | + return servers.computeIfAbsent( |
| 32 | + address, |
| 33 | + addr -> { |
| 34 | + try { |
| 35 | + // Modify the builder to use the new address |
| 36 | + synchronized (channelBuilder) { |
| 37 | + InstantiatingGrpcChannelProvider.Builder newBuilder = |
| 38 | + channelBuilder.setEndpoint(addr); |
| 39 | + return new GrpcChannelFinderServer(addr, newBuilder.build()); |
| 40 | + } |
| 41 | + } catch (IOException e) { |
| 42 | + throw new RuntimeException("Failed to create channel for address: " + addr, e); |
| 43 | + } |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + static class GrpcChannelFinderServer implements ChannelFinderServer { |
| 48 | + private final String address; |
| 49 | + private final ManagedChannel channel; |
| 50 | + |
| 51 | + public GrpcChannelFinderServer(String address, InstantiatingGrpcChannelProvider provider) |
| 52 | + throws IOException { |
| 53 | + this.address = address; |
| 54 | + // It's assumed that getTransportChannel() returns a ManagedChannel or can be cast to one. |
| 55 | + // For this example, GrpcTransportChannel is used as in KeyAwareChannel. |
| 56 | + GrpcTransportChannel transportChannel = (GrpcTransportChannel) provider.getTransportChannel(); |
| 57 | + this.channel = (ManagedChannel) transportChannel.getChannel(); |
| 58 | + } |
| 59 | + |
| 60 | + // Constructor for the default server that already has a channel |
| 61 | + public GrpcChannelFinderServer(String address, ManagedChannel channel) { |
| 62 | + this.address = address; |
| 63 | + this.channel = channel; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String getAddress() { |
| 68 | + return address; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean isHealthy() { |
| 73 | + // A simple health check. In a real scenario, this might involve a ping or other checks. |
| 74 | + return !channel.isShutdown() && !channel.isTerminated(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public ManagedChannel getChannel() { |
| 79 | + return channel; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments