Skip to content

CASSANALYTICS-79: Make Token ranges calculation rack aware for spark.data.partitioner.CassandraRing - #149

Open
sklaha wants to merge 4 commits into
apache:trunkfrom
sklaha:CASSANALYTICS-79
Open

sklaha wants to merge 4 commits into
apache:trunkfrom
sklaha:CASSANALYTICS-79

Conversation

@sklaha

@sklaha sklaha commented Oct 23, 2025

Copy link
Copy Markdown
Contributor

Changes in the PR:

  • An integration test (BulkReaderTokenRangeReplicasTest.java) has been added to demonstrate that the replicas for token range calculation were incorrect in CassandraRing with NetworkAwareReplication strategy in a multi-DC, multi-rack scenario.
  • The sidecar token-range-replicas API has been utilized to retrieve replicas for token ranges, eliminating the need for token range calculation within the analytics-library.
  • Unit tests have been added to further validate the changes.

return new long[]{h1, h2};
}

protected static long invRShiftXor(long value, int shift)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are those new methods added?

Comment on lines +106 to +118
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
this.token = in.readUTF();
this.node = in.readUTF();
this.dataCenter = in.readUTF();
}

private void writeObject(ObjectOutputStream out) throws IOException
{
out.writeUTF(token());
out.writeUTF(nodeName());
out.writeUTF(dataCenter());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why adding the custom serialization code? The string fields should be serializable by default, and the readObject & writeObject methods are unnecessary.

Comment on lines -37 to +42
private final String token;
private final String node;
private final String dataCenter;
private String token;
private String node;
private String dataCenter;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once readObject & writeObject are removed, the final modifiers can be restored.

Comment on lines +77 to 78
private transient RangeMap<BigInteger, List<CassandraInstance>> replicasForRanges;
private transient RangeMap<BigInteger, List<CassandraInstance>> replicas;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is confusing to have 2 maps and they actually point to the same object if I am not mistaken.
More importantly, it leads to different init code paths. The patch is to fix the broken token calculation, but why leaving it half-fixed? The CDC code patch still calls the original constructor, which means CDC is still broken.

Comment on lines +152 to +153
//updateInternal(1, key, VALUE2);
//expectedValuesInNodes.put(1, VALUE2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are those 2 lines wanted?

mansikhara pushed a commit to mansikhara/cassandra-analytics that referenced this pull request Nov 3, 2026
…ranges and replicas from Cassandra

The bulk reader derived token ranges and replicas from node tokens and the replication factor, assuming racks are
not in use. NetworkTopologyStrategy is rack aware, so with uneven racks the derived replicas can differ from
Cassandra's and a read can return incomplete or stale data. CASSANALYTICS-197 took the ranges from Sidecar's
token-range-replicas endpoint only for keyspaces with transient replicas, as the ring held one token per node.

Take them from Cassandra for every keyspace. A node owning several tokens is represented once, carrying its lowest
token, as the reported ranges say which ranges it replicates. deriveTokenRanges falls back to the local derivation,
and is ignored for keyspaces with transient replicas; it replaces forceCassandraTokenRanges, which is now the
default. An incomplete token-range-replicas response fails with a clear message.

CDC had the same problem: SidecarCommitLogProvider chose which instances to list commit logs on from a ring derived
with the maximal replication factor, so with uneven racks it could skip a replica holding mutations. It now takes
the write replicas of every CDC-enabled keyspace from Sidecar, caches them for tokenRangeReplicasRefreshInterval or
until the cluster changes, and falls back to the previous replicas, then to the derived ring, if Sidecar fails.
SidecarCdcOptions.deriveTokenRanges keeps the previous behaviour.

Adds a data-level integration test on a two-datacenter cluster with uneven racks, where a replica that only
rack-aware placement includes holds the newest value, and a vnode variant of the multi-rack placement checks.
Builds on the approach in apache#149.
mansikhara pushed a commit to mansikhara/cassandra-analytics that referenced this pull request Nov 3, 2026
…ranges and replicas from Cassandra

The bulk reader derived token ranges and replicas from node tokens and the replication factor, assuming racks are
not in use. NetworkTopologyStrategy is rack aware, so with uneven racks the derived replicas can differ from
Cassandra's and a read can return incomplete or stale data. CASSANALYTICS-197 took the ranges from Sidecar's
token-range-replicas endpoint only for keyspaces with transient replicas, as the ring held one token per node.

Take them from Cassandra for every keyspace. A node owning several tokens is represented once, carrying its lowest
token, as the reported ranges say which ranges it replicates. deriveTokenRanges falls back to the local derivation,
and is ignored for keyspaces with transient replicas; it replaces forceCassandraTokenRanges, which is now the
default. An incomplete token-range-replicas response fails with a clear message.

CDC had the same problem: SidecarCommitLogProvider chose which instances to list commit logs on from a ring derived
with the maximal replication factor, so with uneven racks it could skip a replica holding mutations. It now takes
the write replicas of every CDC-enabled keyspace from Sidecar, caches them for tokenRangeReplicasRefreshInterval or
until the cluster changes, and falls back to the previous replicas, then to the derived ring, if Sidecar fails.
SidecarCdcOptions.deriveTokenRanges keeps the previous behaviour.

Adds a data-level integration test on a two-datacenter cluster with uneven racks, where a replica that only
rack-aware placement includes holds the newest value, and a vnode variant of the multi-rack placement checks.
Builds on the approach in apache#149.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants