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

Unable to achieve a quorum during its retry window #282

Open
touseef-haider-kodexo opened this issue Jul 12, 2023 · 1 comment
Open

Unable to achieve a quorum during its retry window #282

touseef-haider-kodexo opened this issue Jul 12, 2023 · 1 comment

Comments

@touseef-haider-kodexo
Copy link

`const Redis = require("redis");

const { default: Redlock } = require("redlock");

const redis = Redis.createClient({
password: "password",
socket: {
host: "host",
port: port,
},
});

const redlock = new Redlock([redis], {
retryCount: 5,
retryDelay: 100,
});

const connect = async (url, options) => {
await redis.connect();
await main();
};

redis.on("connect", () => {
console.log("Connected to Redis server");
});

redis.on("ready", () => {
console.log("Redis server is ready");
});

redis.on("error", (error) => {
console.error(Error occurred with Redis client: ${error});
});

async function main() {
console.log("executing");
const lock = await redlock.acquire(["lockkcck"], 300000);
console.log("lock", lock);
}
connect();
`

This is the code where I am getting this error, I have checked by degrading to v4 as well.

@abdullah230695
Copy link

This can be resolved with a workaround.

  1. simply go here : node_modules/redlock/dist/cjs/index.js
  2. replace this function : _attemptOperationOnClient with this updated code
async _attemptOperationOnClient(client, script, keys, args) {
        try {
            let result;
            try {
                // Attempt to evaluate the script by its hash using sendCommand.
                const shaResult = await client.sendCommand([
                    "EVALSHA",
                    script.hash,
                    keys.length.toString(), // Ensure length is a string
                    ...keys.map(String), // Convert keys to strings
                    ...args.map(String), // Convert args to strings
                ]);
                if (typeof shaResult !== "number") {
                    throw new Error(`Unexpected result of type ${typeof shaResult} returned from Redis.`);
                }
                result = shaResult;
            } catch (error) {
                // If the Redis server does not already have the script cached,
                // reattempt the request with the script's raw text.
                if (!(error instanceof Error) || !error.message.startsWith("NOSCRIPT")) {
                    throw error;
                }
                const rawResult = await client.sendCommand([
                    "EVAL",
                    script.value,
                    keys.length.toString(), // Ensure length is a string
                    ...keys.map(String), // Convert keys to strings
                    ...args.map(String), // Convert args to strings
                ]);
                if (typeof rawResult !== "number") {
                    throw new Error(`Unexpected result of type ${typeof rawResult} returned from Redis.`);
                }
                result = rawResult;
            }

            // One or more of the resources was already locked.
            if (result !== keys.length) {
                throw new ResourceLockedError(`The operation was applied to: ${result} of the ${keys.length} requested resources.`);
            }

            return {
                vote: "for",
                client,
                value: result,
            };
        } catch (error) {
            if (!(error instanceof Error)) {
                throw new Error(`Unexpected type ${typeof error} thrown with value: ${error}`);
            }
            // Emit the error on the redlock instance for observability.
            this.emit("error", error);
            return {
                vote: "against",
                client,
                error,
            };
        }
    }

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

No branches or pull requests

2 participants