-
Notifications
You must be signed in to change notification settings - Fork 34
[CASSANALYTICS-181] - Support Sidecar behind a load balancer for time… #223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
323b84b
50d1d36
f4bc953
963fb71
b2adf4a
f7e0adf
c062112
07ad2d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.cassandra.spark.bulkwriter; | ||
|
|
||
| import java.math.BigInteger; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| import com.google.common.collect.Range; | ||
|
|
||
| import o.a.c.sidecar.client.shaded.common.response.TimeSkewResponse; | ||
|
|
||
| /** | ||
| * Variant of {@link CassandraClusterInfo} used when Sidecars are fronted by a load balancer. | ||
| * Replica FQDNs from the token map are not routable from Spark executors in that topology, | ||
| * so requests that would otherwise fan out to per-replica Sidecar addresses are routed through | ||
| * the configured contact points (load balancer endpoints) instead. | ||
| * <p> | ||
| * This applies to both single-cluster and coordinated writes. It is selected by | ||
| * {@link CassandraClusterInfo#create(BulkSparkConf, String)} and | ||
| * {@link CassandraClusterInfo#create(BroadcastableClusterInfo)} when | ||
| * {@link org.apache.cassandra.spark.bulkwriter.WriterOptions#SIDECAR_BEHIND_LOAD_BALANCER} | ||
| * is set. | ||
| */ | ||
| public class LoadBalancedCassandraClusterInfo extends CassandraClusterInfo | ||
| { | ||
| public LoadBalancedCassandraClusterInfo(BulkSparkConf conf, String clusterId) | ||
| { | ||
| super(conf, clusterId); | ||
| } | ||
|
|
||
| public LoadBalancedCassandraClusterInfo(BroadcastableClusterInfo broadcastable) | ||
| { | ||
| super(broadcastable); | ||
| } | ||
|
|
||
| @Override | ||
| protected CompletableFuture<TimeSkewResponse> fetchTimeSkew(Range<BigInteger> range) | ||
| { | ||
| // range is irrelevant; the load balancer contact points are queried directly rather | ||
| // than the per-range replicas, whose FQDNs are not routable from Spark executors. | ||
| return getCassandraContext().getSidecarClient().timeSkew(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same problem exists for Single cluster behind a load balancer. Hence instead of using 'Coordinated' word, good to develop this as a generic framework for handling cluster/clusters behind a load balancer, then we can invoke it from anywhere needed
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, range param is unused, hence mention a comment something like 'range is irrelevant; contact points are queried directly'
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made it generic: the load-balanced variant is now selected by CassandraClusterInfo.create(...), which is invoked from both the single-cluster path (CassandraBulkWriterContext / BroadcastableClusterInfo.reconstruct) and the coordinated path (CassandraClusterInfoGroup), so it can be reused wherever needed.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice work |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,29 +24,39 @@ | |
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.collect.Maps; | ||
| import com.google.common.util.concurrent.Uninterruptibles; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.Timeout; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| import o.a.c.sidecar.client.shaded.client.SidecarClient; | ||
| import o.a.c.sidecar.client.shaded.common.response.NodeSettings; | ||
| import o.a.c.sidecar.client.shaded.common.response.TimeSkewResponse; | ||
| import org.apache.cassandra.bridge.CassandraVersion; | ||
| import org.apache.cassandra.spark.bulkwriter.token.TokenRangeMapping; | ||
| import org.apache.cassandra.spark.data.partitioner.Partitioner; | ||
| import org.apache.cassandra.spark.exception.TimeSkewTooLargeException; | ||
| import org.apache.spark.SparkConf; | ||
|
|
||
| import static org.apache.cassandra.spark.TestUtils.range; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatNoException; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyList; | ||
| import static org.mockito.Mockito.RETURNS_DEEP_STUBS; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| public class CassandraClusterInfoTest | ||
|
|
@@ -63,6 +73,107 @@ void testTimeSkewAcceptable() | |
| .isThrownBy(() -> ci.validateTimeSkewWithLocalNow(range(10, 20), localNow)); | ||
| } | ||
|
|
||
| @Test | ||
| void testLoadBalancedClusterInfoUsesSidecarClientContactPointsForTimeSkew() | ||
| { | ||
| Instant localNow = Instant.now(); | ||
| int allowanceMinutes = 10; | ||
|
|
||
| SidecarClient sidecarClient = mock(SidecarClient.class); | ||
| CassandraContext ctx = mock(CassandraContext.class); | ||
| when(ctx.getSidecarClient()).thenReturn(sidecarClient); | ||
| TimeSkewResponse tsr = new TimeSkewResponse(localNow.toEpochMilli(), allowanceMinutes); | ||
| when(sidecarClient.timeSkew()).thenReturn(CompletableFuture.completedFuture(tsr)); | ||
|
|
||
| CassandraClusterInfo ci = new LoadBalancedCassandraClusterInfo((BulkSparkConf) null, null) | ||
| { | ||
| @Override | ||
| protected CassandraContext buildCassandraContext() | ||
| { | ||
| return ctx; | ||
| } | ||
|
|
||
| @Override | ||
| public TokenRangeMapping<RingInstance> getTokenRangeMapping(boolean cached) | ||
| { | ||
| return TokenRangeMappingUtils.buildTokenRangeMapping(0, ImmutableMap.of("dc1", 3), 5); | ||
| } | ||
| }; | ||
|
|
||
| assertThatNoException() | ||
| .describedAs("Load-balanced cluster info must use timeSkew() so load balancer contact points stay reachable") | ||
| .isThrownBy(() -> ci.validateTimeSkewWithLocalNow(range(10, 20), localNow)); | ||
| verify(sidecarClient).timeSkew(); | ||
| verify(sidecarClient, never()).timeSkew(anyList()); | ||
| } | ||
|
|
||
| @Test | ||
| void testCreateSelectsLoadBalancedClusterInfoWhenSidecarBehindLoadBalancer() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please write test cases to ensure
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, added the tests. The coordinated one checks |
||
| { | ||
| // Single-cluster path: the SIDECAR_BEHIND_LOAD_BALANCER flag must be honored, not just for coordinated writes | ||
| try (CassandraClusterInfo ci = CassandraClusterInfo.create(bulkSparkConf(true))) | ||
| { | ||
| assertThat(ci) | ||
| .describedAs("Sidecar behind a load balancer must select the load-balanced variant on the single-cluster path") | ||
| .isExactlyInstanceOf(LoadBalancedCassandraClusterInfo.class); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testCreateSelectsPlainClusterInfoByDefault() | ||
| { | ||
| try (CassandraClusterInfo ci = CassandraClusterInfo.create(bulkSparkConf(false))) | ||
| { | ||
| assertThat(ci) | ||
| .describedAs("Without the load balancer flag, the plain per-replica variant must be selected") | ||
| .isExactlyInstanceOf(CassandraClusterInfo.class); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testBroadcastableClusterInfoReconstructsLoadBalancedClusterInfoOnExecutorWhenSidecarBehindLoadBalancer() | ||
| { | ||
| // Executor path: reconstructing from a broadcastable whose conf has the flag set must yield the load-balanced | ||
| // variant, matching the driver-side selection so executors also route through the load balancer contact points. | ||
| ClusterInfo source = mock(ClusterInfo.class); | ||
| when(source.getPartitioner()).thenReturn(Partitioner.Murmur3Partitioner); | ||
| when(source.getBridgeVersion()).thenReturn(CassandraVersion.FIVEZERO); | ||
| BroadcastableClusterInfo broadcastable = BroadcastableClusterInfo.from(source, bulkSparkConf(true)); | ||
| try (CassandraClusterInfo ci = (CassandraClusterInfo) broadcastable.reconstruct()) | ||
| { | ||
| assertThat(ci) | ||
| .describedAs("Reconstructing on an executor with Sidecar behind a load balancer must select the load-balanced variant") | ||
| .isExactlyInstanceOf(LoadBalancedCassandraClusterInfo.class); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testBroadcastableClusterInfoReconstructsPlainClusterInfoOnExecutorByDefault() | ||
| { | ||
| ClusterInfo source = mock(ClusterInfo.class); | ||
| when(source.getPartitioner()).thenReturn(Partitioner.Murmur3Partitioner); | ||
| when(source.getBridgeVersion()).thenReturn(CassandraVersion.FIVEZERO); | ||
| BroadcastableClusterInfo broadcastable = BroadcastableClusterInfo.from(source, bulkSparkConf(false)); | ||
| try (CassandraClusterInfo ci = (CassandraClusterInfo) broadcastable.reconstruct()) | ||
| { | ||
| assertThat(ci) | ||
| .describedAs("Without the load balancer flag, reconstructing on an executor must use the plain per-replica variant") | ||
| .isExactlyInstanceOf(CassandraClusterInfo.class); | ||
| } | ||
| } | ||
|
|
||
| private static BulkSparkConf bulkSparkConf(boolean sidecarBehindLoadBalancer) | ||
| { | ||
| // No keystore options: this keeps SSL disabled so create() can build a real (non-TLS) Sidecar client | ||
| // offline. We only assert on the concrete type create() selects, never issuing a request. | ||
| Map<String, String> options = Maps.newTreeMap(String.CASE_INSENSITIVE_ORDER); | ||
| options.put(WriterOptions.SIDECAR_CONTACT_POINTS.name(), "127.0.0.1"); | ||
| options.put(WriterOptions.KEYSPACE.name(), "ks"); | ||
| options.put(WriterOptions.TABLE.name(), "table"); | ||
| options.put(WriterOptions.SIDECAR_BEHIND_LOAD_BALANCER.name(), String.valueOf(sidecarBehindLoadBalancer)); | ||
| return new BulkSparkConf(new SparkConf(), options); | ||
| } | ||
|
|
||
| @Test | ||
| void testTimeSkewTooLarge() | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a nit, but at least I'd like for us to consider.
We are adding a new class
LoadBalancedCassandraClusterInfothat extendsCassandraClusterInfo, which is fine, but:CassandraClusterInfomay return an instance of itself or an instance of the new class, forcing it to "know" (depend) onLoadBalancedCassandraClusterInfo. This feels like a factory pattern with a potential abstractCassandraClusterInfoclass and two classes extending it:LoadBalancedCassandraClusterInfoand maybeStandaloneCassandraClusterInfo, and the factory static create method returning one or the other? I think that approach would be cleaner. Opinions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, thanks.
I agree that having the base class reference its subclass in
create()isn’t ideal.I think the abstract CassandraClusterInfo + StandaloneCassandraClusterInfo / LoadBalancedCassandraClusterInfo approach makes sense. My only hesitation is the scope. CassandraClusterInfo is quite central, and making it abstract would mean introducing a separate StandaloneCassandraClusterInfo and updating the places that currently instantiate or mock CassandraClusterInfo directly. That would likely touch a number of tests and mocks as well.
Since the PR is otherwise ready, I’d prefer to keep this change smaller for now and revisit the full split separately. But happy to go with the cleaner approach if you think it’s worth doing as part of this PR.