Skip to content

Replication + Lua scripts calling select don't mix well #1811

Open

Description

@antirez

To replicate this problem create a master-slave pair and run the following Lua script:

redis.call("set","foo1","bar1")
redis.call("select","2")
redis.call("incr","x")
redis.call("select","3")
redis.call("incr","z")

The Lua behavior of setting the client selected DB to the DB selected by Lua does not play well with the current implementation of replication, causing the master client in the context of the slave to select DB 3 before EVAL is called, and even afterward.

I'm providing a patch in the context of this issue for further discussion. The patch changes the semantics of Lua scripting in a way that breaks (very rarely, if ever) compatibility with the past.

This is the commit message for further information. Any feedbacks about back porting this into 2.8?

Activity

  1. added a commit that references this issue on Jun 12, 2014
  2. antirez commented on Jun 12, 2014

    @antirez
    ContributorAuthor

    For simplicity I'm posting the commit message here:

    Fix semantics of Lua calls to SELECT.

    Lua scripts are executed in the context of the currently selected
    database (as selected by the caller of the script).

    However Lua scripts are also free to use the SELECT command in order to
    affect other DBs. When SELECT is called frm Lua, the old behavior, before
    this commit, was to automatically set the Lua caller selected DB to the
    last DB selected by Lua. See for example the following sequence of
    commands:

    SELECT 0
    SET x 10
    EVAL "redis.call('select','1')" 0
    SET x 20
    

    Before this commit after the execution of this sequence of commands,
    we'll have x=10 in DB 0, and x=20 in DB 1.

    Because of the problem above, there was a bug affecting replication of
    Lua scripts, because of the actual implementation of replication. It was
    possible to fix the implementation of Lua scripts in order to fix the
    issue, but looking closely, the bug is the consequence of the behavior
    of Lua ability to set the caller's DB.

    Under the old semantics, a script selecting a different DB, has no simple
    ways to restore the state and select back the previously selected DB.
    Moreover the script auhtor must remember that the restore is needed,
    otherwise the new commands executed by the caller, will be executed in
    the context of a different DB.

    So this commit fixes both the replication issue, and this hard-to-use
    semantics, by removing the ability of Lua, after the script execution,
    to force the caller to switch to the DB selected by the Lua script.

    The new behavior of the previous sequence of commadns is to just set
    X=20 in DB 0. However Lua scripts are still capable of writing / reading
    from different DBs if needed.

    WARNING: This is a semantical change that will break programs that are
    conceived to select the client selected DB via Lua scripts.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions