Update calling routing to return urls only, no instance IPs

This commit is contained in:
adel-signal 2025-01-24 10:46:32 -08:00 committed by GitHub
parent c9e192564c
commit 7e616a4056
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 68 additions and 18 deletions

View File

@ -62,6 +62,11 @@ public class DynamicConfigTurnRouter {
return turnConfig.getRandomizeRate(); return turnConfig.getRandomizeRate();
} }
public int getDefaultInstanceIpCount() {
final DynamicTurnConfiguration turnConfig = dynamicConfigurationManager.getConfiguration().getTurnConfiguration();
return turnConfig.getDefaultInstanceIpCount();
}
public boolean shouldRandomize() { public boolean shouldRandomize() {
long rate = getRandomizeRate(); long rate = getRandomizeRate();
return rate >= RANDOMIZE_RATE_BASIS || rng.nextLong(0, DynamicConfigTurnRouter.RANDOMIZE_RATE_BASIS) < rate; return rate >= RANDOMIZE_RATE_BASIS || rng.nextLong(0, DynamicConfigTurnRouter.RANDOMIZE_RATE_BASIS) < rate;

View File

@ -52,6 +52,13 @@ public class TurnCallRouter {
this.stableSelect = stableSelect; this.stableSelect = stableSelect;
} }
public TurnServerOptions getRoutingFor(
@Nonnull final UUID aci,
@Nonnull final Optional<InetAddress> clientAddress
) {
return getRoutingFor(aci, clientAddress, this.configTurnRouter.getDefaultInstanceIpCount());
}
/** /**
* Gets Turn Instance addresses. Returns both the IPv4 and IPv6 addresses. Prefers to match the IP protocol of the * Gets Turn Instance addresses. Returns both the IPv4 and IPv6 addresses. Prefers to match the IP protocol of the
* client address in datacenter selection. Returns 2 instance options of the preferred protocol for every one instance * client address in datacenter selection. Returns 2 instance options of the preferred protocol for every one instance
@ -79,10 +86,6 @@ public class TurnCallRouter {
@Nonnull final Optional<InetAddress> clientAddress, @Nonnull final Optional<InetAddress> clientAddress,
final int instanceLimit final int instanceLimit
) { ) {
if (instanceLimit < 1) {
throw new IllegalArgumentException("Limit cannot be less than one");
}
String hostname = this.configTurnRouter.getHostname(); String hostname = this.configTurnRouter.getHostname();
List<String> targetedUrls = this.configTurnRouter.targetedUrls(aci); List<String> targetedUrls = this.configTurnRouter.targetedUrls(aci);
@ -90,7 +93,7 @@ public class TurnCallRouter {
return new TurnServerOptions(hostname, null, targetedUrls); return new TurnServerOptions(hostname, null, targetedUrls);
} }
if(clientAddress.isEmpty() || this.configTurnRouter.shouldRandomize()) { if(clientAddress.isEmpty() || this.configTurnRouter.shouldRandomize() || instanceLimit < 1) {
return new TurnServerOptions(hostname, null, this.configTurnRouter.randomUrls()); return new TurnServerOptions(hostname, null, this.configTurnRouter.randomUrls());
} }

View File

@ -23,6 +23,12 @@ public class DynamicTurnConfiguration {
@JsonProperty @JsonProperty
private long randomizeRate = 5_000; private long randomizeRate = 5_000;
/**
* Number of instance ips to return in TURN routing request
*/
@JsonProperty
private int defaultInstanceIpCount = 0;
@JsonProperty @JsonProperty
private List<@Valid TurnUriConfiguration> uriConfigs = Collections.emptyList(); private List<@Valid TurnUriConfiguration> uriConfigs = Collections.emptyList();
@ -34,6 +40,10 @@ public class DynamicTurnConfiguration {
return randomizeRate; return randomizeRate;
} }
public int getDefaultInstanceIpCount() {
return defaultInstanceIpCount;
}
public String getHostname() { public String getHostname() {
return hostname; return hostname;
} }

View File

@ -18,7 +18,6 @@ import jakarta.ws.rs.Produces;
import jakarta.ws.rs.container.ContainerRequestContext; import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.MediaType;
import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
@ -40,8 +39,8 @@ import org.whispersystems.websocket.auth.ReadOnly;
@Path("/v2/calling") @Path("/v2/calling")
public class CallRoutingControllerV2 { public class CallRoutingControllerV2 {
private static final int TURN_INSTANCE_LIMIT = 2;
private static final Counter INVALID_IP_COUNTER = Metrics.counter(name(CallRoutingControllerV2.class, "invalidIP")); private static final Counter INVALID_IP_COUNTER = Metrics.counter(name(CallRoutingControllerV2.class, "invalidIP"));
private static final Counter CLOUDFLARE_TURN_ERROR_COUNTER = Metrics.counter(name(CallRoutingController.class, "cloudflareTurnError"));
private final RateLimiters rateLimiters; private final RateLimiters rateLimiters;
private final TurnCallRouter turnCallRouter; private final TurnCallRouter turnCallRouter;
private final TurnTokenGenerator tokenGenerator; private final TurnTokenGenerator tokenGenerator;
@ -79,14 +78,19 @@ public class CallRoutingControllerV2 {
public GetCallingRelaysResponse getCallingRelays( public GetCallingRelaysResponse getCallingRelays(
final @ReadOnly @Auth AuthenticatedDevice auth, final @ReadOnly @Auth AuthenticatedDevice auth,
@Context ContainerRequestContext requestContext @Context ContainerRequestContext requestContext
) throws RateLimitExceededException, IOException { ) throws RateLimitExceededException {
UUID aci = auth.getAccount().getUuid(); UUID aci = auth.getAccount().getUuid();
rateLimiters.getCallEndpointLimiter().validate(aci); rateLimiters.getCallEndpointLimiter().validate(aci);
List<TurnToken> tokens = new ArrayList<>(); List<TurnToken> tokens = new ArrayList<>();
try {
if (experimentEnrollmentManager.isEnrolled(auth.getAccount().getNumber(), aci, "cloudflareTurn")) { if (experimentEnrollmentManager.isEnrolled(auth.getAccount().getNumber(), aci, "cloudflareTurn")) {
tokens.add(cloudflareTurnCredentialsManager.retrieveFromCloudflare()); tokens.add(cloudflareTurnCredentialsManager.retrieveFromCloudflare());
} }
} catch (Exception e) {
// emit counter, rely on Signal URL fallback
CallRoutingControllerV2.CLOUDFLARE_TURN_ERROR_COUNTER.increment();
}
Optional<InetAddress> address = Optional.empty(); Optional<InetAddress> address = Optional.empty();
try { try {
@ -97,7 +101,7 @@ public class CallRoutingControllerV2 {
INVALID_IP_COUNTER.increment(); INVALID_IP_COUNTER.increment();
} }
TurnServerOptions options = turnCallRouter.getRoutingFor(aci, address, TURN_INSTANCE_LIMIT); TurnServerOptions options = turnCallRouter.getRoutingFor(aci, address);
tokens.add(tokenGenerator.generateWithTurnServerOptions(options)); tokens.add(tokenGenerator.generateWithTurnServerOptions(options));
return new GetCallingRelaysResponse(tokens); return new GetCallingRelaysResponse(tokens);

View File

@ -162,6 +162,21 @@ public class TurnCallRouterTest {
)); ));
} }
@Test
public void testUrlsOnlyNoInstanceIps() throws UnknownHostException {
when(performanceTable.getDatacentersFor(any(), any(), any(), any()))
.thenReturn(List.of("dc-performance2", "dc-performance1"));
when(configTurnRouter.shouldRandomize())
.thenReturn(false);
assertThat(router().getRoutingFor(aci, Optional.of(InetAddress.getByName("0.0.0.1")), 0))
.isEqualTo(new TurnServerOptions(
TEST_HOSTNAME,
null,
TEST_URLS_WITH_HOSTS
));
}
@Test @Test
public void testOrderedByPerformance() throws UnknownHostException { public void testOrderedByPerformance() throws UnknownHostException {
when(performanceTable.getDatacentersFor(any(), any(), any(), any())) when(performanceTable.getDatacentersFor(any(), any(), any(), any()))

View File

@ -376,6 +376,23 @@ class DynamicConfigurationTest {
assertThat(turnConfiguration.getHostname()).isEqualTo("test.domain.org"); assertThat(turnConfiguration.getHostname()).isEqualTo("test.domain.org");
assertThat(turnConfiguration.getRandomizeRate()).isEqualTo(100_000L); assertThat(turnConfiguration.getRandomizeRate()).isEqualTo(100_000L);
assertThat(turnConfiguration.getDefaultInstanceIpCount()).isEqualTo(0);
}
{
final String config = REQUIRED_CONFIG.concat("""
turn:
uriConfigs:
- uris:
- turn:test0.org
- turn:test1.org
defaultInstanceIpCount: 5
""");
DynamicTurnConfiguration turnConfiguration = DynamicConfigurationManager
.parseConfiguration(config, DynamicConfiguration.class)
.orElseThrow()
.getTurnConfiguration();
assertThat(turnConfiguration.getDefaultInstanceIpCount()).isEqualTo(5);
} }
} }

View File

@ -34,12 +34,10 @@ import org.whispersystems.textsecuregcm.auth.TurnToken;
import org.whispersystems.textsecuregcm.auth.TurnTokenGenerator; import org.whispersystems.textsecuregcm.auth.TurnTokenGenerator;
import org.whispersystems.textsecuregcm.calls.routing.TurnCallRouter; import org.whispersystems.textsecuregcm.calls.routing.TurnCallRouter;
import org.whispersystems.textsecuregcm.calls.routing.TurnServerOptions; import org.whispersystems.textsecuregcm.calls.routing.TurnServerOptions;
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicConfiguration;
import org.whispersystems.textsecuregcm.experiment.ExperimentEnrollmentManager; import org.whispersystems.textsecuregcm.experiment.ExperimentEnrollmentManager;
import org.whispersystems.textsecuregcm.limits.RateLimiter; import org.whispersystems.textsecuregcm.limits.RateLimiter;
import org.whispersystems.textsecuregcm.limits.RateLimiters; import org.whispersystems.textsecuregcm.limits.RateLimiters;
import org.whispersystems.textsecuregcm.mappers.RateLimitExceededExceptionMapper; import org.whispersystems.textsecuregcm.mappers.RateLimitExceededExceptionMapper;
import org.whispersystems.textsecuregcm.storage.DynamicConfigurationManager;
import org.whispersystems.textsecuregcm.tests.util.AuthHelper; import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
import org.whispersystems.textsecuregcm.util.SystemMapper; import org.whispersystems.textsecuregcm.util.SystemMapper;
import org.whispersystems.textsecuregcm.util.TestRemoteAddressFilterProvider; import org.whispersystems.textsecuregcm.util.TestRemoteAddressFilterProvider;
@ -96,8 +94,7 @@ class CallRoutingControllerV2Test {
try { try {
when(turnCallRouter.getRoutingFor( when(turnCallRouter.getRoutingFor(
eq(AuthHelper.VALID_UUID), eq(AuthHelper.VALID_UUID),
eq(Optional.of(InetAddress.getByName(REMOTE_ADDRESS))), eq(Optional.of(InetAddress.getByName(REMOTE_ADDRESS))))
anyInt())
).thenReturn(options); ).thenReturn(options);
} catch (UnknownHostException ignored) { } catch (UnknownHostException ignored) {
} }
@ -177,8 +174,7 @@ class CallRoutingControllerV2Test {
when(turnCallRouter.getRoutingFor( when(turnCallRouter.getRoutingFor(
eq(AuthHelper.VALID_UUID), eq(AuthHelper.VALID_UUID),
eq(Optional.of(InetAddress.getByName(REMOTE_ADDRESS))), eq(Optional.of(InetAddress.getByName(REMOTE_ADDRESS))))
anyInt())
).thenReturn(options); ).thenReturn(options);
try (Response rawResponse = resources.getJerseyTest() try (Response rawResponse = resources.getJerseyTest()
.target(GET_CALL_RELAYS_PATH) .target(GET_CALL_RELAYS_PATH)