A small service keeps user profiles in memory and allows updating the record by userId if the asynchronous loader returned a newer version. After a recent PR, behavior under concurrent load became unstable: sometimes the cache keeps a value that is not the newest one, and for the same missing user more than one load starts.
getOrRefresh method must return Future[Profile].userId at any moment, and the other calls must wait for the same Future.version is not less than the one already stored.ProfileCache.scala file.getOrRefresh method.getOrRefresh("u1") when the cache is empty;Profile("u1", 5, "new"), and later an older loader completes with Profile("u1", 4, "old").userId, one shared Future must be created, and the loader must be called once.3.3.121scala.concurrent.FutureExecutionContext: ExecutionContext.globaljava.util.concurrent.ConcurrentHashMapversion grows monotonically, and the same userId always refers to one logical recordUse line comments to explain the problem, its impact, the fix and how to test it.