Bug: Null pointer when running Upgrade Tests
Status: Open
Priority: Normal
Created: 08/Apr/24 22:42

h2. What happened

The `UpgradeTestBase.java` may throw null pointer exception when creating the version upgrade pairs in `upgradesTo()` method.

The problem happens in the for loop shown below. The `upgradesTo()` calls `versions.getLatest(Semver version)` method to create the `Version` class.
{code:java}
for (Semver start : vertices.subSet(lowerBound, true, to, false))
{
    // only include pairs that are allowed, and start or end on CURRENT
    if (SUPPORTED_UPGRADE_PATHS.hasEdge(start, to) && edgeTouchesTarget(start, to, CURRENT))
        upgrade.add(new TestVersions(versions.getLatest(start), Collections.singletonList(versions.getLatest(to))));
} {code}
However, in the `Version.java`, `getLatest()` function never checks whether the `first(version)` is in the `versions` map or not. When the version is not there, a null pointer exception will be thrown and crash all the upgrade tests.
{code:java}
public Version getLatest(Semver version)
{
    return versions.get(first(version))  // <--- Here might cause NPE
                        .stream()
                         .findFirst()
                        .orElseThrow(() -> new RuntimeException("No " + version + " versions found"));
}
{code}
h2. How to reproduce

To reproduce this bug, I'm running Cassandra with commit SHA `310d790ce4734727f943225eb951ab0d889c0a5b`; and dtest API with `dtest-api-0.0.16.jar`.

The versions I put under `build/` directory are:

dtest-4.0.9.jar, dtest-4.0.13.jar, dtest-4.1.4.jar, and dtest-5.1.jar.

The command I'm running is:
{code:java}
$ ant test-jvm-dtest-some -Duse.jdk11=true -Dtest.name=org.apache.cassandra.distributed.upgrade.UpgradeTest {code}
The error message I got was:
{code:java}
[junit-timeout] INFO  [main] <main> 2024-04-08 17:34:23,936 Versions.java:136 - Looking for dtest jars in /Users/xxx/Documents/xxx/cassandra/build
[junit-timeout] Found 4.0.13, 4.0.9
[junit-timeout] Found 4.1.4
[junit-timeout] Found 5.1
[junit-timeout] ------------- ---------------- ---------------
[junit-timeout] Testcase: simpleUpgradeWithNetworkAndGossipTest(org.apache.cassandra.distributed.upgrade.UpgradeTest)-_jdk11:    Caused an ERROR
[junit-timeout] null
[junit-timeout] java.lang.NullPointerException
[junit-timeout]     at org.apache.cassandra.distributed.shared.Versions.getLatest(Versions.java:127)
[junit-timeout]     at org.apache.cassandra.distributed.upgrade.UpgradeTestBase$TestCase.upgradesTo(UpgradeTestBase.java:218)
[junit-timeout]     at org.apache.cassandra.distributed.upgrade.UpgradeTestBase$TestCase.upgradesToCurrentFrom(UpgradeTestBase.java:203)
[junit-timeout]     at org.apache.cassandra.distributed.upgrade.UpgradeTest.simpleUpgradeWithNetworkAndGossipTest(UpgradeTest.java:37)
[junit-timeout]     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit-timeout]     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[junit-timeout]     at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[junit-timeout] 
[junit-timeout] 
[junit-timeout] Test org.apache.cassandra.distributed.upgrade.UpgradeTest FAILED
 {code}
With some debugging, the version causing the null pointer is `5.0-alpha1`, but this version is not shown in `build/` directory and should not be tested if I understand correctly.
h2. How to fix.

There are two ways to fix this problem. One is to add a null pointer checker in `UpgradeTestBase#upgradesTo()`, and the other approach is to add the null pointer in `Versions#getLatest()`.

I would love to provide a PR to fix this issue if you can tell me which fix looks better to you.