Skip to content

ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap - #789

Merged
jbellis merged 3 commits into
vsearchfrom
ordinal-fix
Oct 4, 2023
Merged

jbellis merged 3 commits into
vsearchfrom
ordinal-fix

Conversation

@jbellis

@jbellis jbellis commented Oct 4, 2023

Copy link
Copy Markdown

No description provided.

…there is a race to add the vector to postingsMap
@jbellis jbellis self-assigned this Oct 4, 2023
@jbellis

jbellis commented Oct 4, 2023

Copy link
Copy Markdown
Author

I don't love splitting out the setOrdinal call but it's the only way I can think without a Big Lock to ensure that only the successful thread increments nextOrdinal

@jbellis

jbellis commented Oct 4, 2023

Copy link
Copy Markdown
Author

Note, I can reproduce the problem (and confirm this fixes it) using an external Python script, but moving it into Java code does not reproduce the problem. Possibly there is some junk in the test machinery that reduces the effective concurrency.

    @Test
    public void testSameVectorConcurrent()
    {
        // uses batch to increase concurrency
        createTable(KEYSPACE, "CREATE TABLE %s (pk int, v vector<float, 2>, PRIMARY KEY(pk))");
        createIndex("CREATE CUSTOM INDEX ON %s(v) USING 'StorageAttachedIndex'");
        waitForIndexQueryable();
        
        int pk = 0;
        for (int i = 0; i < 1000; i++) {
            String cql = "";
            for (int j = 0; j < 20; j++) {
                cql += formatQuery("INSERT INTO %s (pk, v) VALUES (" + pk++ + ", [1, 1]);");
            }
            executeFormattedQuery("BEGIN UNLOGGED BATCH " + cql + " APPLY BATCH");
        }
        flush();
    }

@michaeljmarshall michaeljmarshall left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't fully understand the fix yet, but since you can reproduce the bug and then show that this bug fixes it, I am approving the PR. I'll follow up if I have any questions.

// in the other structures as well.
var postings = postingsMap.computeIfAbsent(vector, v -> {
var bytes = RamEstimation.concurrentHashMapRamUsed(1); // the new posting Map entry
var ordinal = nextOrdinal.getAndIncrement();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why wasn't computeIfAbsent in conjunction with and AtomicInteger correct? I am trying to see it, but I haven't seen it yet.

@michaeljmarshall michaeljmarshall Oct 4, 2023 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Found it. The computeIfAbsent in ConcurrentSkipListMap is called optimistically, which resulted in skipping ordinal values, and incorrect state in the rest of the map class.

    /**
     * If the specified key is not already associated with a value,
     * attempts to compute its value using the given mapping function
     * and enters it into this map unless {@code null}.  The function
     * is <em>NOT</em> guaranteed to be applied once atomically only
     * if the value is not present.
     *
     * @param key key with which the specified value is to be associated
     * @param mappingFunction the function to compute a value
     * @return the current (existing or computed) value associated with
     *         the specified key, or null if the computed value is null
     * @throws NullPointerException if the specified key is null
     *         or the mappingFunction is null
     * @since 1.8
     */
    public V computeIfAbsent(K key,
                             Function<? super K, ? extends V> mappingFunction) {
        if (key == null || mappingFunction == null)
            throw new NullPointerException();
        V v, p, r;
        if ((v = doGet(key)) == null &&
            (r = mappingFunction.apply(key)) != null)
            v = (p = doPut(key, r, true)) == null ? r : p;
        return v;
    }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's particularly interesting because a concurrent hashmap does apply the function atomically once.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ah, no wonder that tripped me up. 99% of the time I'm using CHM and I could have sworn this method made an exactly-once guarantee. I feel slightly less dumb now.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Me too! I was reading your solution and couldn't see how it fixed the concurrency bug.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

As a side benefit, I think the new code is less fragile without the special casing around newVector

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Definitely. One last nit is that since we aren't able to write a test for it, would you add a comment to prevent incorrect refactoring?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

commented and merged

@jbellis
jbellis merged commit 9e3ca22 into vsearch Oct 4, 2023
@jbellis
jbellis deleted the ordinal-fix branch October 4, 2023 22:37
@sonarqubecloud

sonarqubecloud Bot commented Oct 4, 2023

Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

84.8% 84.8% Coverage
0.0% 0.0% Duplication

warning The version of Java (11.0.15) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 17.
Read more here

djatnieks pushed a commit that referenced this pull request May 22, 2024
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment
jacek-lewandowski pushed a commit that referenced this pull request Jul 17, 2024
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment
djatnieks pushed a commit that referenced this pull request Jan 30, 2025
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment
djatnieks pushed a commit that referenced this pull request May 18, 2025
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment
michaelsembwever pushed a commit that referenced this pull request Feb 6, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment
michaelsembwever pushed a commit that referenced this pull request Feb 10, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment

 (Rebase of commit 628952f)
michaelsembwever pushed a commit that referenced this pull request Feb 11, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment

 (Rebase of commit 628952f)
michaelsembwever pushed a commit that referenced this pull request Feb 12, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment

 (Rebase of commit 628952f)
michaelsembwever pushed a commit that referenced this pull request Feb 14, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment

 (Rebase of commit 628952f)
michaelsembwever pushed a commit that referenced this pull request Feb 16, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment

 (Rebase of commit 628952f)
michaelsembwever pushed a commit that referenced this pull request Feb 27, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment

 (Rebase of commit 628952f)
michaelsembwever pushed a commit that referenced this pull request Mar 2, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment

 (Rebase of commit 628952f)
michaelsembwever pushed a commit that referenced this pull request Mar 4, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment

 (Rebase of commit 628952f)
michaelsembwever pushed a commit that referenced this pull request Mar 5, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment

 (Rebase of commit 628952f)
michaelsembwever pushed a commit that referenced this pull request Mar 25, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment

 (Rebase of commit 628952f)
michaelsembwever pushed a commit that referenced this pull request Mar 27, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment

 (Rebase of commit 628952f)
michaelsembwever pushed a commit that referenced this pull request Apr 14, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment

 (Rebase of commit 628952f)
michaelsembwever pushed a commit that referenced this pull request Apr 15, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment

 (Rebase of commit 628952f)
JeremiahDJordan pushed a commit that referenced this pull request Aug 19, 2026
…there is a race to add the vector to postingsMap (#789)

* ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap

* mark ordinal volatile

* comment

 (Rebase of commit 628952f)

 (Rebase of commit 9492e12)
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.

3 participants