mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 17:33:03 -05:00
fix(ram): advance share update timestamps within one clock tick (#6674)
* fix(ram): advance share update timestamps within one clock tick * fix(ram): preserve equal create timestamps * fix(ram): serialize share timestamp updates
This commit is contained in:
1 parent
79e47cd7fc
commit
15e2c98957
2 files changed
+183
-5
No files matched your search
@@ -21,6 +21,7 @@ import jakarta.annotation.PostConstruct;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -73,14 +74,20 @@ public class RamService {
|
||||
|
||||
private final StorageFactory storageFactory;
|
||||
private final OrganizationsService organizationsService;
|
||||
private final Clock clock;
|
||||
private StorageBackend<String, ResourceShare> shares;
|
||||
private StorageBackend<String, Boolean> settings;
|
||||
private StorageBackend<String, ResourceShareInvitation> invitations;
|
||||
|
||||
@Inject
|
||||
public RamService(StorageFactory storageFactory, OrganizationsService organizationsService) {
|
||||
this(storageFactory, organizationsService, Clock.systemUTC());
|
||||
}
|
||||
|
||||
RamService(StorageFactory storageFactory, OrganizationsService organizationsService, Clock clock) {
|
||||
this.storageFactory = storageFactory;
|
||||
this.organizationsService = organizationsService;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
/** Constructor used by isolated service tests with an explicit Organizations test double. */
|
||||
@@ -575,12 +582,31 @@ public class RamService {
|
||||
}
|
||||
|
||||
/**
|
||||
* The single write seam: every create and every mutation lands here, so stamping
|
||||
* lastUpdatedTime once at this point keeps it advancing without six call sites having to
|
||||
* remember to do it. Returns the stamped share so callers respond with what was stored.
|
||||
* The single write seam: every create and every mutation lands here. Existing shares are
|
||||
* stamped here so lastUpdatedTime advances without six call sites having to remember to do it;
|
||||
* a fresh share keeps its equal creation and last-updated timestamps. The read, timestamp
|
||||
* calculation, and write are one critical section so concurrent mutations cannot reuse a stamp.
|
||||
* Returns the stored share.
|
||||
*/
|
||||
private ResourceShare putForOwner(ResourceShare share) {
|
||||
ResourceShare stamped = share.withLastUpdatedTime(Instant.now());
|
||||
private synchronized ResourceShare putForOwner(ResourceShare share) {
|
||||
String owningAccountId = share.getOwningAccountId();
|
||||
Optional<ResourceShare> previous;
|
||||
if (shares instanceof AccountAwareStorageBackend<ResourceShare> accountAware) {
|
||||
previous = accountAware.getForAccountMigratingLegacy(
|
||||
owningAccountId, share.getResourceShareArn(),
|
||||
candidate -> owningAccountId.equals(candidate.getOwningAccountId()));
|
||||
} else {
|
||||
previous = shares.get(share.getResourceShareArn());
|
||||
}
|
||||
ResourceShare stamped = share;
|
||||
if (previous.isPresent()) {
|
||||
Instant now = clock.instant();
|
||||
Instant previousLastUpdated = previous.get().getLastUpdatedTime();
|
||||
if (now.toEpochMilli() <= previousLastUpdated.toEpochMilli()) {
|
||||
now = Instant.ofEpochMilli(previousLastUpdated.toEpochMilli() + 1);
|
||||
}
|
||||
stamped = share.withLastUpdatedTime(now);
|
||||
}
|
||||
if (shares instanceof AccountAwareStorageBackend<ResourceShare> accountAware) {
|
||||
accountAware.putForAccount(
|
||||
stamped.getOwningAccountId(), stamped.getResourceShareArn(), stamped);
|
||||
|
||||
Reference in new issue
Block a user