This is an automated email from the ASF dual-hosted git repository. marcuse pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/cassandra.git
commit a2dad4ab1947971cb2c92995ab25a8adba7ef40b Author: Marcus Eriksson <[email protected]> AuthorDate: Thu Aug 15 10:49:52 2024 +0200 Allow getendpoints for system tables and make sure getNaturalReplicas work for MetaStrategy Patch by marcuse; reviewed by Sam Tunnicliffe for CASSANDRA-19846 --- CHANGES.txt | 1 + .../apache/cassandra/service/StorageService.java | 14 +++- .../distributed/test/GetEndpointsTest.java | 88 ++++++++++++++++++++++ 3 files changed, 99 insertions(+), 4 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 6b9ec7e10a..d3e54a69f7 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 5.1 + * Allow getendpoints for system tables and make sure getNaturalReplicas work for MetaStrategy (CASSANDRA-19846) * On upgrade, handle pre-existing tables with unexpected table ids (CASSANDRA-19845) * Reconfigure CMS before assassinate (CASSANDRA-19768) * Warn about unqualified prepared statement only if it is select or modification statement (CASSANDRA-18322) diff --git a/src/java/org/apache/cassandra/service/StorageService.java b/src/java/org/apache/cassandra/service/StorageService.java index 927aa0d5d9..a78786fb3f 100644 --- a/src/java/org/apache/cassandra/service/StorageService.java +++ b/src/java/org/apache/cassandra/service/StorageService.java @@ -3477,7 +3477,8 @@ public class StorageService extends NotificationBroadcasterSupport implements IE public DecoratedKey getKeyFromPartition(String keyspaceName, String table, String partitionKey) { - return ClusterMetadata.current().partitioner.decorateKey(partitionKeyToBytes(keyspaceName, table, partitionKey)); + IPartitioner partitioner = Keyspace.open(keyspaceName).getColumnFamilyStore(table).getPartitioner(); + return partitioner.decorateKey(partitionKeyToBytes(keyspaceName, table, partitionKey)); } private static ByteBuffer partitionKeyToBytes(String keyspaceName, String cf, String key) @@ -3496,14 +3497,19 @@ public class StorageService extends NotificationBroadcasterSupport implements IE @Override public String getToken(String keyspaceName, String table, String key) { - return ClusterMetadata.current().partitioner.getToken(partitionKeyToBytes(keyspaceName, table, key)).toString(); + ColumnFamilyStore cfs = Keyspace.open(keyspaceName).getColumnFamilyStore(table); + return cfs.getPartitioner().getToken(partitionKeyToBytes(keyspaceName, table, key)).toString(); } public EndpointsForToken getNaturalReplicasForToken(String keyspaceName, ByteBuffer key) { ClusterMetadata metadata = ClusterMetadata.current(); - Token token = metadata.partitioner.getToken(key); - KeyspaceMetadata keyspaceMetadata = metadata.schema.getKeyspaces().getNullable(keyspaceName); + KeyspaceMetadata keyspaceMetadata = Keyspace.open(keyspaceName).getMetadata(); + Token token; + if (keyspaceMetadata.params.replication.isMeta()) + token = MetaStrategy.partitioner.getToken(key); + else + token = metadata.partitioner.getToken(key); return metadata.placements.get(keyspaceMetadata.params.replication).reads.forToken(token).get(); } diff --git a/test/distributed/org/apache/cassandra/distributed/test/GetEndpointsTest.java b/test/distributed/org/apache/cassandra/distributed/test/GetEndpointsTest.java new file mode 100644 index 0000000000..c2fa538335 --- /dev/null +++ b/test/distributed/org/apache/cassandra/distributed/test/GetEndpointsTest.java @@ -0,0 +1,88 @@ +/* + * 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.distributed.test; + +import java.io.IOException; +import java.util.List; + +import org.junit.Test; + +import org.apache.cassandra.distributed.Cluster; +import org.apache.cassandra.distributed.api.Feature; +import org.apache.cassandra.distributed.api.IInvokableInstance; +import org.apache.cassandra.locator.Replicas; +import org.apache.cassandra.service.StorageService; +import org.apache.cassandra.tcm.ClusterMetadata; +import org.apache.cassandra.utils.FBUtilities; + +import static org.junit.Assert.assertEquals; + +public class GetEndpointsTest extends TestBaseImpl +{ + @Test + public void testGetEndpointsForLocalTable() throws IOException + { + try (Cluster cluster = init(Cluster.build(3) + .start())) + { + for (IInvokableInstance i : cluster) + { + i.runOnInstance(() -> { + List<String> endpoints = StorageService.instance.getNaturalEndpointsWithPort("system", "compaction_history", "7d431310-43c9-11ef-bd50-53ff742309a9"); + assertEquals(1, endpoints.size()); + assertEquals(FBUtilities.getBroadcastAddressAndPort().getHostAddressAndPort(), endpoints.get(0)); + }); + } + } + } + + @Test + public void testGetEndpointsForMetadataTables() throws IOException + { + try (Cluster cluster = init(Cluster.build(3) + .withConfig(c -> c.with(Feature.NETWORK)) + .start())) + { + for (IInvokableInstance i : cluster) + { + i.runOnInstance(() -> { + List<String> endpoints = StorageService.instance.getNaturalEndpointsWithPort("system", "local_metadata_log", "1"); + assertEquals(1, endpoints.size()); + assertEquals(FBUtilities.getBroadcastAddressAndPort().getHostAddressAndPort(), endpoints.get(0)); + }); + + i.runOnInstance(() -> { + List<String> endpoints = StorageService.instance.getNaturalEndpointsWithPort("system_cluster_metadata", "distributed_metadata_log", "1"); + assertEquals(1, endpoints.size()); + assertEquals(Replicas.stringify(ClusterMetadata.current().fullCMSMembersAsReplicas(), true), endpoints); + }); + } + cluster.get(1).nodetoolResult("cms", "reconfigure", "3").asserts().success(); + for (IInvokableInstance i : cluster) + { + i.runOnInstance(() -> { + List<String> endpoints = StorageService.instance.getNaturalEndpointsWithPort("system_cluster_metadata", "distributed_metadata_log", "1"); + assertEquals(endpoints.toString(), 3, endpoints.size()); + assertEquals(Replicas.stringify(ClusterMetadata.current().fullCMSMembersAsReplicas(), true), endpoints); + }); + + } + } + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
