Skip to content

Fix ColumnFamilyStore.getIfExists handling of dropped keyspaces #1738

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/java/org/apache/cassandra/db/ColumnFamilyStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -3431,7 +3431,7 @@ public static ColumnFamilyStore getIfExists(TableId id)
if (metadata == null)
return null;

Keyspace keyspace = Keyspace.open(metadata.keyspace);
Keyspace keyspace = Keyspace.openIfExists(metadata.keyspace);
if (keyspace == null)
return null;

Expand All @@ -3449,7 +3449,7 @@ public static ColumnFamilyStore getIfExists(String ksName, String cfName)
if (ksName == null || cfName == null)
return null;

Keyspace keyspace = Keyspace.open(ksName);
Keyspace keyspace = Keyspace.openIfExists(ksName);
if (keyspace == null)
return null;

Expand Down
20 changes: 19 additions & 1 deletion src/java/org/apache/cassandra/db/Keyspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.util.function.Supplier;
import java.util.stream.Stream;

import javax.annotation.Nullable;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.RateLimiter;
Expand Down Expand Up @@ -149,7 +151,23 @@ public static void unsetInitialized()
}
}

public static Keyspace open(String keyspaceName)
/**
* Convenience function for when getting {@code null} if the keyspace does not exist is more convenient than a
* {@link UnknownKeyspaceException}.
*/
public static @Nullable Keyspace openIfExists(String keyspaceName)
{
try
{
return open(keyspaceName);
}
catch (UnknownKeyspaceException e)
{
return null;
}
}

public static Keyspace open(String keyspaceName) throws UnknownKeyspaceException
{
assert initialized || SchemaConstants.isLocalSystemKeyspace(keyspaceName) : "Initialized: " + initialized;
return open(keyspaceName, Schema.instance, true);
Expand Down
22 changes: 22 additions & 0 deletions test/unit/org/apache/cassandra/db/ColumnFamilyStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.apache.cassandra.db.partitions.Partition;
import org.apache.cassandra.db.partitions.PartitionUpdate;
import org.apache.cassandra.index.transactions.UpdateTransaction;
import org.apache.cassandra.schema.SchemaTestUtil;
import org.apache.cassandra.schema.TableMetadata;
import org.apache.cassandra.utils.concurrent.OpOrder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
Expand Down Expand Up @@ -803,4 +805,24 @@ public void performSnapshot(String snapshotName)
}
};
}

/**
* Ensures `getIfExists` returns `null` (and does not throw) after a keyspace is dropped.
*/
@Test
public void testGetIfExistsAfterKeyspaceDrop()
{
String testKS = "getIfExistsAfterKeyspaceDrop";
String testTable = "test_table";
SchemaLoader.createKeyspace(testKS,
KeyspaceParams.simple(1),
SchemaLoader.standardCFMD(testKS, testTable));

TableMetadata metadata = ColumnFamilyStore.getIfExists(testKS, testTable).metadata.get();

SchemaTestUtil.announceKeyspaceDrop(testKS);

assertNull(ColumnFamilyStore.getIfExists(metadata.id));
assertNull(ColumnFamilyStore.getIfExists(testKS, testTable));
}
}
2 changes: 1 addition & 1 deletion test/unit/org/apache/cassandra/schema/SchemaTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static void announceTableUpdate(TableMetadata updated)
Schema.instance.transform(schema -> schema.withAddedOrUpdated(ksm.withSwapped(ksm.tables.withSwapped(updated))));
}

static void announceKeyspaceDrop(String ksName)
public static void announceKeyspaceDrop(String ksName)
{
KeyspaceMetadata oldKsm = Schema.instance.getKeyspaceMetadata(ksName);
if (oldKsm == null)
Expand Down