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

Blocking wait in transaction execution #2683

Closed
rayao opened this issue Mar 22, 2024 · 7 comments
Closed

Blocking wait in transaction execution #2683

rayao opened this issue Mar 22, 2024 · 7 comments

Comments

@rayao
Copy link

rayao commented Mar 22, 2024

image
It's unexpected to me when all methods I call are async, is there any setting to tune to avoid this blocking wait?
I also found similar stack in TakeLock/ReleaseLock calls, seems they're implemented upon transactions.
We have a service exposes APIs that do Redis lock take/release, we're afraid that when request rate increases, blocking wait will put the service in danger.

@mgravell
Copy link
Collaborator

What is the scenario here? What exactly is your code doing? Is this a transaction with constraints, perhaps? If so: that may be a factor here - I'm a little surprised it didn't offload the entire thing to the write loop, though.

I'm right now in the middle of a big overhaul of the write loop, which may help with the latter, but there will always be a pinch point using transactions with constraints, do to (grungy details that aren't very interesting). However, in most cases Lua will be a much easier, simpler, and more direct mechanism for constrained batches - I wonder if refactoring to use a Lua script would be the best option.

Without example code, it is hard to say more.

@rayao
Copy link
Author

rayao commented Mar 22, 2024

Code corresponds to stack above

            var transaction = cache.CreateTransaction();
            if (comparand.HasValue)
            {
                transaction.AddCondition(Condition.HashEqual(key, VersionField, comparand.Version));
            }
            else
            {
                transaction.AddCondition(Condition.HashNotExists(key, VersionField));
            }

            var entries = new HashEntry[]
            {
                new HashEntry(ValueField, value),
                new HashEntry(VersionField, version.Version),
            };
            var redisKey = key;

            transaction.HashSetAsync(redisKey, entries);
            transaction.KeyExpireAsync(redisKey, expiry);
            return transaction.ExecuteAsync();

@rayao
Copy link
Author

rayao commented Mar 22, 2024

And even more simple with *Lock methods

        public Task<bool> ExtendLock(string formId)
        {
            return _redisCache.ExtendLock(
                $"{ExcelLockFormat}-{formId}",
                _contextProvider.User.UserSessionId,
                SdxEventLockDuration);
        }

image

@rayao
Copy link
Author

rayao commented Mar 22, 2024

Seems there's a lock to take to execute the transaction. Maybe the entire transaction should be queued to run in write loop?
Is it possible to replace *Lock with Lua scripts?

@mgravell
Copy link
Collaborator

Yes, that is suitable for Lua. Not at PC right now, but will try to provide a translation when I am at desk.

@aniprasad
Copy link

aniprasad commented Mar 26, 2024

Hi @mgravell
Would you happen to have any updates on this?

Would something like this work? for taking the lock, extending the lock, and releasing the lock respectively

  const string TakeLock = @"
      local lockDuration = tonumber(ARGV[4])
      local setResult = redis.call('SET', KEYS[1], ARGV[1], ARGV[2], ARGV[3], lockDuration)
      return setResult";
      string[] keys = new string[] { "key" };
      string[] args = new string[] {
        "value",
        "NX",
        "EX",
        duration
  };
const string ExtendLock = @"
    local session = redis.call('GET', KEYS[1])
    if session == ARGV[1]
    then
        local lockDuration = tonumber(ARGV[2])
        return redis.call('SET', KEYS[1], ARGV[1], 'EX', lockDuration)
    end
    return session";
    string[] keys = new string[] { "key };
    string[] args = new string[]
    {
        "value"
        duration
    };
const string ReleaseLock = @"
    local session = redis.call('GET', KEYS[1])
    if session == ARGV[1]
    then
        return redis.call('DEL', KEYS[1])
    end";
    string[] keys = new string[] { "key" };
    string[] args = new string[] { "value" };

I plan on using the ScriptEvaluateAsync method for these scripts like ScriptEvaluateAsync(script, keys, args, CommandFlags.None)

@mgravell
Copy link
Collaborator

mgravell commented May 7, 2024

Sorry, slipped off my plate; that looks broadly OK to me and is a preferable solution than MULTI/EXEC ; some nits:

  • in ReleaseLock you might want to use UNLINK instead of DEL, but if we assume modest value (ARGV[1]) it won't matter hugely
  • in ExtendLock you might just want to use EXPIRE rather than SET ... EX, to avoid having to rewrite the payload
  • TakeLock can probably be replaces with the StringSet with When.NotExists, but it is fine as written too

@mgravell mgravell closed this as completed May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants