|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package com.datastax.oss.driver.api.core.config; |
| 19 | + |
| 20 | +import com.datastax.oss.driver.api.core.session.SessionBuilder; |
| 21 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 22 | +import edu.umd.cs.findbugs.annotations.Nullable; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Objects; |
| 27 | +import net.jcip.annotations.Immutable; |
| 28 | + |
| 29 | +/** |
| 30 | + * Configuration for client routes, used in PrivateLink-style deployments. |
| 31 | + * |
| 32 | + * <p>Client routes enable the driver to discover and connect to nodes through a load balancer (such |
| 33 | + * as AWS PrivateLink) by reading endpoint mappings from the {@code system.client_routes} table. |
| 34 | + * Each endpoint is identified by a connection ID and maps to specific node addresses. |
| 35 | + * |
| 36 | + * <p>This configuration is mutually exclusive with a user-provided {@link |
| 37 | + * com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator}. If client routes are |
| 38 | + * configured, the driver will use its internal client routes handler for address translation. |
| 39 | + * |
| 40 | + * <p>Example usage: |
| 41 | + * |
| 42 | + * <pre>{@code |
| 43 | + * ClientRoutesConfig config = ClientRoutesConfig.builder() |
| 44 | + * .addEndpoint(new ClientRoutesEndpoint( |
| 45 | + * UUID.fromString("12345678-1234-1234-1234-123456789012"), |
| 46 | + * "my-privatelink.us-east-1.aws.scylladb.com:9042")) |
| 47 | + * .build(); |
| 48 | + * |
| 49 | + * CqlSession session = CqlSession.builder() |
| 50 | + * .withClientRoutesConfig(config) |
| 51 | + * .build(); |
| 52 | + * }</pre> |
| 53 | + * |
| 54 | + * @see SessionBuilder#withClientRoutesConfig(ClientRoutesConfig) |
| 55 | + * @see ClientRoutesEndpoint |
| 56 | + */ |
| 57 | +@Immutable |
| 58 | +public class ClientRoutesConfig { |
| 59 | + |
| 60 | + private final List<ClientRoutesEndpoint> endpoints; |
| 61 | + private final String tableName; |
| 62 | + |
| 63 | + private ClientRoutesConfig(List<ClientRoutesEndpoint> endpoints, String tableName) { |
| 64 | + if (endpoints == null || endpoints.isEmpty()) { |
| 65 | + throw new IllegalArgumentException("At least one endpoint must be specified"); |
| 66 | + } |
| 67 | + this.endpoints = Collections.unmodifiableList(new ArrayList<>(endpoints)); |
| 68 | + this.tableName = tableName; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns the list of configured endpoints. |
| 73 | + * |
| 74 | + * @return an immutable list of endpoints. |
| 75 | + */ |
| 76 | + @NonNull |
| 77 | + public List<ClientRoutesEndpoint> getEndpoints() { |
| 78 | + return endpoints; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns the name of the system table to query for client routes. |
| 83 | + * |
| 84 | + * @return the table name, or null to use the default ({@code system.client_routes}). |
| 85 | + */ |
| 86 | + @Nullable |
| 87 | + public String getTableName() { |
| 88 | + return tableName; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Creates a new builder for constructing a {@link ClientRoutesConfig}. |
| 93 | + * |
| 94 | + * @return a new builder instance. |
| 95 | + */ |
| 96 | + @NonNull |
| 97 | + public static Builder builder() { |
| 98 | + return new Builder(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public boolean equals(Object o) { |
| 103 | + if (this == o) { |
| 104 | + return true; |
| 105 | + } |
| 106 | + if (!(o instanceof ClientRoutesConfig)) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + ClientRoutesConfig that = (ClientRoutesConfig) o; |
| 110 | + return endpoints.equals(that.endpoints) && Objects.equals(tableName, that.tableName); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public int hashCode() { |
| 115 | + return Objects.hash(endpoints, tableName); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String toString() { |
| 120 | + return "ClientRoutesConfig{" |
| 121 | + + "endpoints=" |
| 122 | + + endpoints |
| 123 | + + ", tableName='" |
| 124 | + + tableName |
| 125 | + + '\'' |
| 126 | + + '}'; |
| 127 | + } |
| 128 | + |
| 129 | + /** Builder for {@link ClientRoutesConfig}. */ |
| 130 | + public static class Builder { |
| 131 | + private final List<ClientRoutesEndpoint> endpoints = new ArrayList<>(); |
| 132 | + private String tableName; |
| 133 | + |
| 134 | + /** |
| 135 | + * Adds an endpoint to the configuration. |
| 136 | + * |
| 137 | + * @param endpoint the endpoint to add (must not be null). |
| 138 | + * @return this builder. |
| 139 | + */ |
| 140 | + @NonNull |
| 141 | + public Builder addEndpoint(@NonNull ClientRoutesEndpoint endpoint) { |
| 142 | + this.endpoints.add(Objects.requireNonNull(endpoint, "endpoint must not be null")); |
| 143 | + return this; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Sets the endpoints for the configuration, replacing any previously added endpoints. |
| 148 | + * |
| 149 | + * @param endpoints the endpoints to set (must not be null or empty). |
| 150 | + * @return this builder. |
| 151 | + */ |
| 152 | + @NonNull |
| 153 | + public Builder withEndpoints(@NonNull List<ClientRoutesEndpoint> endpoints) { |
| 154 | + Objects.requireNonNull(endpoints, "endpoints must not be null"); |
| 155 | + this.endpoints.clear(); |
| 156 | + this.endpoints.addAll(endpoints); |
| 157 | + return this; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Sets the name of the system table to query for client routes. |
| 162 | + * |
| 163 | + * <p>This is primarily useful for testing. If not set, the driver will use the default table |
| 164 | + * name from the configuration ({@code system.client_routes}). |
| 165 | + * |
| 166 | + * @param tableName the table name to use. |
| 167 | + * @return this builder. |
| 168 | + */ |
| 169 | + @NonNull |
| 170 | + public Builder withTableName(@Nullable String tableName) { |
| 171 | + this.tableName = tableName; |
| 172 | + return this; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Builds the {@link ClientRoutesConfig} with the configured endpoints and table name. |
| 177 | + * |
| 178 | + * @return the new configuration instance. |
| 179 | + * @throws IllegalArgumentException if no endpoints have been added. |
| 180 | + */ |
| 181 | + @NonNull |
| 182 | + public ClientRoutesConfig build() { |
| 183 | + return new ClientRoutesConfig(endpoints, tableName); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments