Skip to content
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

Alt-Ids intended to be unique per sample may repeat #96

Open
dmcmanam opened this issue Mar 23, 2020 · 1 comment
Open

Alt-Ids intended to be unique per sample may repeat #96

dmcmanam opened this issue Mar 23, 2020 · 1 comment
Assignees
Labels

Comments

@dmcmanam
Copy link
Member

We see some repeat Alt-ids in the database due to the code below in PromoteBanked.java returning the same value in two consecutive calls.

String uuid;
try {
        uuid = uuidGen.integerToUUID(Integer.parseInt(util.getNextBankedId()), 32);
} catch (Exception e) {
        throw new LimsException("UUID generation failed for sample due to " + e.getMessage());
}

the uuidGen.integerToUUID() does return distinct values from 1-100million so it appears the call to util.getNextBankedId() can, when called consecutively, return the same value.

@dmcmanam dmcmanam added the bug label Mar 23, 2020
@dmcmanam dmcmanam self-assigned this Mar 23, 2020
@jfkonecn
Copy link

jfkonecn commented Aug 8, 2021

Is this bug still an issue for you?

I was trying to see if I could build and debug that line of code.
I might be wrong, but it seems like some of the libraries required to build this repo are not available publicly.
I also don't do a lot of Java development so I could be doing something wrong on my end.

Anyway, if this is still an issue for you, my guess is that it's a race condition.
Here's a code snippet I wanted to use on the call to "util.getNextBankedId()".
Maybe it'll help you get to the bottom of it.

The results are random, but it always results in ID duplication in a few of the threads.

import java.util.concurrent.Semaphore;

class EvilRaceConditionClass {
    private int evilMutableInt;

    public int getNextBankedId() {
        evilMutableInt++;
        try {
            // simulate a non trivial method that will 
            // force more than one cpu core to be used
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return evilMutableInt;
    }
}

class RunnableRaceCondition extends Thread {
    private final int id;
    private final Semaphore semaphore;
    private final EvilRaceConditionClass evil;

    RunnableRaceCondition(int id, Semaphore semaphore, EvilRaceConditionClass evil) {
        this.id = id;
        this.semaphore = semaphore;
        this.evil = evil;
    }

    @Override
    public void run() {
        try {
            // hold all the threads here so that they try to hit getNextBankedId at the same time
            semaphore.acquire();
        } catch (Exception e) {
            e.printStackTrace();
        }
        var nextId = evil.getNextBankedId();
        System.out.println("Thread ID:" + id + " completed with ID:" + nextId);
    }
}

class Main {
    public static void main(String[] args) {
        int totalThreads = 10;
        var semaphore = new Semaphore(totalThreads);
        var evil = new EvilRaceConditionClass();
        var threads = new RunnableRaceCondition[totalThreads];

        for (int i = 0; i < totalThreads; i++) {
            threads[i] = new RunnableRaceCondition(i, semaphore, evil);
        }
            
        for (Thread thread : threads) {
            thread.start();
        }

        semaphore.release(totalThreads);

        for (Thread thread : threads) {
            try {
                thread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Here's an example output for the code snippet:

Thread ID:1 completed with ID:10
Thread ID:6 completed with ID:10
Thread ID:5 completed with ID:10
Thread ID:4 completed with ID:10
Thread ID:2 completed with ID:10
Thread ID:0 completed with ID:10
Thread ID:3 completed with ID:10
Thread ID:9 completed with ID:10
Thread ID:7 completed with ID:10
Thread ID:8 completed with ID:10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants