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

The contains method in redissonDelayedQueue uses Lua script, but the time complexity of this Lua script is quite high, which seems unreasonable. #5868

Open
kangqing opened this issue May 13, 2024 · 4 comments
Labels

Comments

@kangqing
Copy link

Would it be possible to optimize it using zrank and zscore to achieve O(log(N)) time complexity?

@Override
    public RFuture<Boolean> containsAsync(Object o) {
        return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,
                        "local s = redis.call('llen', KEYS[1]);" +
                        "for i = 0, s-1, 1 do "
                            + "local v = redis.call('lindex', KEYS[1], i);"
                            + "local randomId, value = struct.unpack('dLc0', v);"
                            + "if ARGV[1] == value then "
                                + "return 1;"
                            + "end; "
                       + "end;" +
                       "return 0;",
                Collections.<Object>singletonList(queueName), encode(o));
    }
@kangqing
Copy link
Author

The remove method is also the same.

@mrniko
Copy link
Member

mrniko commented May 15, 2024

zrank won't help since randomId is unknown

@kangqing
Copy link
Author

zrank won't help since randomId is unknown

As shown below, I checked if abcd is in delay queue edisson_delay_queue_timeout:{TOKEN_TIMEOUT_HANDLE} ,

Just delay the queue name and the obj to be queried

➜  Desktop redis-cli
127.0.0.1:6379> zrank redisson_delay_queue_timeout:{TOKEN_TIMEOUT_HANDLE} abcd
(integer) 8
127.0.0.1:6379> zrank redisson_delay_queue_timeout:{TOKEN_TIMEOUT_HANDLE} abcde
(nil)
127.0.0.1:6379> 

Here is a Lua script impl

local exists = redis.call('ZRANK', KEYS[1], ARGV[1])
if exists == nil then
    return 0
else
    return 1 
end

@mrniko
Copy link
Member

mrniko commented May 16, 2024

how do you send ARGV[1] ? it's stored with extra randomId to ensure uniqueness

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants