Skip to content

Commit

Permalink
Support preferable location for connections (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
a1kaigorodov authored Feb 8, 2025
1 parent 3e2a5d7 commit e35a85c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tech.ydb.yoj.repository.ydb;

import com.google.common.net.HostAndPort;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NonNull;
import lombok.Value;
Expand All @@ -26,6 +28,7 @@ public static YdbConfig createForTesting(String host, int port, String tablespac
database,
null,
HostAndPort.fromParts(host, port),
null,
SESSION_CREATE_TIMEOUT_DEFAULT,
SESSION_CREATE_RETRY_COUNT_DEFAULT,
SESSION_KEEP_ALIVE_TIME_DEFAULT,
Expand Down Expand Up @@ -61,6 +64,9 @@ public static YdbConfig createForTesting(String host, int port, String tablespac
@With
HostAndPort hostAndPort;

@With
BalancingConfig balancingConfig;

@With
Duration sessionCreationTimeout;
@With
Expand Down Expand Up @@ -136,4 +142,40 @@ public Duration getTcpKeepaliveTimeout() {
public boolean isUseSingleChannelTransport() {
return Optional.ofNullable(useSingleChannelTransport).orElse(false);
}

@Value
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public static class BalancingConfig {
Policy policy;
String preferableLocation;

/**
* Use all available cluster nodes regardless datacenter locality
*/
static BalancingConfig useAllNodes() {
return new BalancingConfig(Policy.USE_ALL_NODES, null);
}

/**
* Use preferable location (data center)
*
* @param preferableLocation a name of location
*/
static BalancingConfig usePreferableLocation(@NonNull String preferableLocation) {
return new BalancingConfig(Policy.USE_PREFERABLE_LOCATION, preferableLocation);
}

/**
* Detecting of local DC by the latency measuring
*/
static BalancingConfig detectLocalDc() {
return new BalancingConfig(Policy.DETECT_LOCAL_DC, null);
}

public enum Policy {
USE_ALL_NODES,
USE_PREFERABLE_LOCATION,
DETECT_LOCAL_DC,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.LoggerFactory;
import tech.ydb.auth.AuthProvider;
import tech.ydb.auth.NopAuthProvider;
import tech.ydb.core.grpc.BalancingSettings;
import tech.ydb.core.grpc.GrpcTransport;
import tech.ydb.core.grpc.GrpcTransportBuilder;
import tech.ydb.core.impl.SingleChannelTransport;
Expand Down Expand Up @@ -117,6 +118,16 @@ private static GrpcTransportBuilder makeGrpcTransportBuilder(@NonNull YdbConfig
throw new IllegalArgumentException("one of [discoveryEndpoint, hostAndPort] must be set");
}

if (config.getBalancingConfig() != null) {
transportBuilder.withBalancingSettings(
switch (config.getBalancingConfig().getPolicy()) {
case USE_ALL_NODES -> BalancingSettings.fromPolicy(BalancingSettings.Policy.USE_ALL_NODES);
case USE_PREFERABLE_LOCATION -> BalancingSettings.fromLocation(config.getBalancingConfig().getPreferableLocation());
case DETECT_LOCAL_DC -> BalancingSettings.detectLocalDs();
}
);
}

if (config.isUseTLS()) {
if (config.isUseTrustStore()) {
transportBuilder.withSecureConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

import static java.lang.String.format;
import static java.util.Arrays.stream;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Stream.concat;
import static tech.ydb.yoj.repository.ydb.yql.YqlOrderBy.SortOrder.ASC;
import static tech.ydb.yoj.repository.ydb.yql.YqlOrderBy.SortOrder.DESC;
Expand Down

0 comments on commit e35a85c

Please sign in to comment.