-
Notifications
You must be signed in to change notification settings - Fork 37
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
fix(mapping): fix insert race conditions #255
Conversation
DEV-1618 Race condition of inserts with tunnelId
It seems that the cache should maybe be set before the insert. Sometimes mulitple tunnels with the same value try to get inserted at the same time |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just make sure to comment on the reason behind this semi-hack of having a cache of promise so we don't forget in the future.
Also, we talked about adding a retry in the mapping service. Is this out of the equation?
async map(tunnelId: uuid, threadId: uuid, userId: uuid): Promise<Convmap> { | ||
const convmap = await this.getByThreadId(tunnelId, threadId) | ||
if (convmap) { | ||
return convmap | ||
} | ||
|
||
return this.barrier.once(tunnelId, threadId, async () => { | ||
const tunnel = await this.tunnels.get(tunnelId) | ||
const conversation = await this.conversations.create(tunnel!.clientId, userId) | ||
return this.create(tunnelId, conversation.id, threadId) | ||
}) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@laurentlp here's a barrier in use. The code in the callback will be excuted only once, and return the same value for all Promises waiting on it. The way a single "task" is identified is with arguments before the callback. This particular barrier is a 2d barrier (similarly to our 2d caches), so the key is composed of two keys.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Nice work 👌
It's sometime possible that sometimes the server checks if a mapping exists, and then creates it if it doesn't. If this is done fast enough, it's possible that the check to see if the mapping exists returns false more than once, and the process of creating the mapping starts multiple times. The constraints in the db will prevent more than once request from succeeding, meaning that exceptions will be thrown.
This PR fixes this problem
Closes DEV-1618
Closes DEV-2104