Skip to content

fix/recovery mechanism for orphan streams #18

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

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,41 @@ export class Api {
url,
// scripting: https://github.com/redis/node-redis/#lua-scripts
scripts: {
checkAndRecoverWorkerStream: redis.defineScript({
NUMBER_OF_KEYS: 1,
SCRIPT: `
local found = false
local messages = redis.call("XREAD", "COUNT", 0, "STREAMS", "${this.redisWorkerStreamName}", "0-0")

if messages and #messages > 0 then
local entries = messages[1][2]
for _, entry in ipairs(entries) do
-- Each entry is an array where entry[2] is the message fields
if entry[2][2] == KEYS[1] then
found = true
break
end
end
end

-- If stream not found in y:worker and the stream exists, add it
if not found and redis.call("TYPE", KEYS[1]).ok == "stream" then
redis.call("XADD", "${this.redisWorkerStreamName}", "*", "compact", KEYS[1])
end
`,
Comment on lines +123 to +144
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加上這個 redis 的 CPU 或是記憶體會有顯著的用量提升嗎?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local 看不太出來,目前只讓他在連線時跑一次

/**
* @param {string} key
*/
transformArguments (key) {
return [key]
},
/**
* @param {null} x
*/
transformReply (x) {
return x
}
}),
addMessage: redis.defineScript({
NUMBER_OF_KEYS: 1,
SCRIPT: `
Expand Down Expand Up @@ -211,6 +246,16 @@ export class Api {
return this.redis.addMessage(computeRedisRoomStreamName(room, docid, this.prefix), m)
}

/**
* @param {string} room
* @param {string} docid
*/
async checkAndRecoveryWorkerStream (room, docid) {
await this.redis.checkAndRecoverWorkerStream(
computeRedisRoomStreamName(room, docid, this.prefix)
)
}

/**
* @param {string} room
* @param {string} docid
Expand Down
1 change: 1 addition & 0 deletions src/y-socket-io/y-socket-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export class YSocketIO {
this.initAwarenessListeners(socket)
this.initSocketListeners(socket)

await this.client.checkAndRecoveryWorkerStream(namespace, 'index')
const doc = await this.client.getDoc(namespace, 'index')

if (
Expand Down
Loading