Code Monkey home page Code Monkey logo

apex-mocks-stress-test's Issues

CrudMock_Tests?

Hey @jamessimone hope you had a nice Thanksgiving! I'm going through Joys of Apex top to bottom again. I noticed you referenced CrudMock_Tests in readme, but I couldn't find it in the source. Am I missing something?

Concurrent writes and thread safety

Hey I just read through your blog here about thread-safe reads/writes in Round Robin.

Could you clarify if/how this is truly read/write safe when 2 separate transactions commit at the same time?

The solution seems to use cache timestamps against the repository initialized timestamps when committing, but there's not a mutex/semaphore around preventing 2 repositories from committing at the same time. The platform cache also does not have a semaphore of its own to prevent concurrent reads/writes.

Org cache supports concurrent reads and writes across multiple simultaneous Apex transactions. For example, a transaction updates the key PetName with the value Fido. At the same time, another transaction updates the same key with the value Felix. Both writes succeed, but one of the two values is chosen arbitrarily as the winner, and later transactions read that one value. However, this arbitrary choice is per key rather than per transaction. For example, suppose one transaction writes PetType="Cat" and PetName="Felix". Then, at the same moment, another transaction writes PetType="Dog" and PetName="Fido". In this case, the PetType winning value could be from the first transaction, and the PetName winning value could be from the second transaction. Subsequent get() calls on those keys would return PetType="Cat" and PetName="Fido".

It seems it's possible for 2 repositories to commit at the same time, which would read the same timestamp from the cache, and they would both succeed in committing using the same index.

Is this an edge case that's not covered?

I think a more resolute solution is to do away with the cache/timestamps and instead query the RoundRobin__c record with the FOR UPDATE clause in assignOwners(), do the assignment, update/release the RoundRobin__c for the next thread. You can then implement your recursive retry attempts if the platform throws a DML exception Or make a queueable where the assignOwners() logic chains itself until it succeeds to avoid the recursive limit.

Interesting note from the docs for FOR UPDATE:

If you attempt to lock a record currently locked by another client, your process waits a maximum of 10 seconds for the lock to be released before acquiring a new lock. If the wait time exceeds 10 seconds, a QueryException is thrown. Similarly, if you attempt to update a record currently locked by another client and the lock isn’t released within a maximum of 10 seconds, a DmlException is thrown.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.