-
Notifications
You must be signed in to change notification settings - Fork 73
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
#15321: Only one tab writes to the DB #382
Merged
roryabraham
merged 44 commits into
Expensify:main
from
BeeMargarida:feat/tab_leader_write
Dec 1, 2023
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
5c50c31
feat: ActiveClientManager port
BeeMargarida 870d715
feat: broadcast
BeeMargarida 55a1394
feat: only leader can write to DB
BeeMargarida bb8be0a
Merge branch 'main' into feat/tab_leader_write
BeeMargarida c61749e
fix: typo in import
BeeMargarida fb4cd2f
feat: e2e test app setup
BeeMargarida 24e4cb8
test: playwright e2e tests setup
BeeMargarida c78e71a
revert: added unsubscrive
BeeMargarida d1fbe0d
test: tabs e2e tests
BeeMargarida e4a318e
feat: playwright config
BeeMargarida d839fab
revert: test ignore
BeeMargarida 8e7aa3b
Merge branch 'main' into feat/tab_leader_write
BeeMargarida 685c8f7
chore: update package-lock
BeeMargarida ce15ce8
test: cleanup simple tests
BeeMargarida 86fb581
test: use getByRole instead of getByTestId
BeeMargarida 37580f3
test: comment out webkit from playwright config
BeeMargarida 88ee143
fix: ignore e2e in jest config
BeeMargarida 63dd112
Merge branch 'main' into feat/tab_leader_write
BeeMargarida c38b935
fix: set correct default values and better docs
BeeMargarida d12b355
test: rename test file and suite
BeeMargarida 76fd53f
refactor: simplify conditional
BeeMargarida 8c4bfd1
fix: review fixes
BeeMargarida f0d3cf4
fix: remove unsubscribe unused method
BeeMargarida b6db16e
fix: small regression
BeeMargarida 9efe824
docs: add comment
BeeMargarida 8205872
Merge branch 'main' into feat/tab_leader_write
BeeMargarida 5234f41
Merge branch 'main' into feat/tab_leader_write
BeeMargarida 4e9a741
Merge branch 'main' into feat/tab_leader_write
BeeMargarida 8bd842c
Merge branch 'main' into feat/tab_leader_write
BeeMargarida b0ae970
Merge branch 'main' into feat/tab_leader_write
BeeMargarida 842789a
Merge branch 'main' into feat/tab_leader_write
BeeMargarida 8f4b3b0
fix: resolve conflicts
koko57 cee3473
fix: rename a variable
koko57 4f0638e
Merge branch 'main' into feat/tab_leader_write
koko57 cd9e9e9
fix: resolve conflicts
koko57 831b8bc
feat: add e2e workflow
koko57 7127185
feat: add the info about tests to readme
koko57 f57f3f9
fix: minor fix
koko57 0dbd4b0
fix: remove unused directories
koko57 dd5dea9
fix: resolve conflicts
koko57 cdb2680
fix: remove unnecesary subscriber
koko57 833d39a
fix: resolve conflicts
koko57 d8a1e22
fix: resolve conflicts
koko57 c72152c
fix: apply requested changes
koko57 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: e2e | ||
|
||
on: | ||
pull_request: | ||
types: [opened, synchronize] | ||
paths: ['tests/e2e/**', 'lib/**', 'package.json', 'package-lock.json'] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/cache@v3 | ||
with: | ||
path: ~/.npm | ||
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | ||
restore-keys: ${{ runner.os }}-node- | ||
|
||
- name: Setup Node | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version-file: '.nvmrc' | ||
|
||
- run: npm ci | ||
|
||
- run: npx playwright install --with-deps | ||
|
||
- run: npm run e2e | ||
env: | ||
CI: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Determines when the client is ready. We need to wait till the init method is called and the leader message is sent. | ||
*/ | ||
declare function isReady(): Promise<void>; | ||
|
||
/** | ||
* Subscribes to the broadcast channel to listen for messages from other tabs, so that | ||
* all tabs agree on who the leader is, which should always be the last tab to open. | ||
*/ | ||
declare function init(): void; | ||
|
||
/** | ||
* Returns a boolean indicating if the current client is the leader. | ||
*/ | ||
declare function isClientTheLeader(): boolean; | ||
|
||
/** | ||
* Subscribes to when the client changes. | ||
*/ | ||
declare function subscribeToClientChange(callback: () => {}): void; | ||
|
||
export {isReady, init, isClientTheLeader, subscribeToClientChange}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* For native devices, there will never be more than one | ||
* client running at a time, so this lib is a big no-op | ||
*/ | ||
|
||
function isReady() { | ||
return Promise.resolve(); | ||
} | ||
|
||
function isClientTheLeader() { | ||
return true; | ||
} | ||
|
||
function init() {} | ||
|
||
function subscribeToClientChange() {} | ||
|
||
export { | ||
isClientTheLeader, | ||
init, | ||
isReady, | ||
subscribeToClientChange, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* When you have many tabs in one browser, the data of Onyx is shared between all of them. Since we persist write requests in Onyx, we need to ensure that | ||
* only one tab is processing those saved requests or we would be duplicating data (or creating errors). | ||
* This file ensures exactly that by tracking all the clientIDs connected, storing the most recent one last and it considers that last clientID the "leader". | ||
*/ | ||
|
||
import * as Str from '../Str'; | ||
import * as Broadcast from '../broadcast'; | ||
|
||
const NEW_LEADER_MESSAGE = 'NEW_LEADER'; | ||
const REMOVED_LEADER_MESSAGE = 'REMOVE_LEADER'; | ||
|
||
const clientID = Str.guid(); | ||
const subscribers = []; | ||
let timestamp = null; | ||
|
||
let activeClientID = null; | ||
let setIsReady = () => {}; | ||
const isReadyPromise = new Promise((resolve) => { | ||
setIsReady = resolve; | ||
}); | ||
|
||
/** | ||
* Determines when the client is ready. We need to wait both till we saved our ID in onyx AND the init method was called | ||
* @returns {Promise} | ||
*/ | ||
function isReady() { | ||
return isReadyPromise; | ||
} | ||
|
||
/** | ||
* Returns a boolean indicating if the current client is the leader. | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
function isClientTheLeader() { | ||
return activeClientID === clientID; | ||
} | ||
|
||
/** | ||
* Subscribes to when the client changes. | ||
* @param {Function} callback | ||
*/ | ||
function subscribeToClientChange(callback) { | ||
subscribers.push(callback); | ||
} | ||
|
||
/** | ||
* Subscribe to the broadcast channel to listen for messages from other tabs, so that | ||
* all tabs agree on who the leader is, which should always be the last tab to open. | ||
*/ | ||
function init() { | ||
Broadcast.subscribe((message) => { | ||
switch (message.data.type) { | ||
case NEW_LEADER_MESSAGE: { | ||
// Only update the active leader if the message received was from another | ||
// tab that initialized after the current one; if the timestamps are the | ||
// same, it uses the client ID to tie-break | ||
const isTimestampEqual = timestamp === message.data.timestamp; | ||
const isTimestampNewer = timestamp > message.data.timestamp; | ||
if (isClientTheLeader() && (isTimestampNewer || (isTimestampEqual && clientID > message.data.clientID))) { | ||
return; | ||
} | ||
activeClientID = message.data.clientID; | ||
|
||
subscribers.forEach(callback => callback()); | ||
BeeMargarida marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break; | ||
} | ||
case REMOVED_LEADER_MESSAGE: | ||
activeClientID = clientID; | ||
timestamp = Date.now(); | ||
Broadcast.sendMessage({type: NEW_LEADER_MESSAGE, clientID, timestamp}); | ||
subscribers.forEach(callback => callback()); | ||
BeeMargarida marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break; | ||
default: | ||
break; | ||
} | ||
}); | ||
|
||
activeClientID = clientID; | ||
timestamp = Date.now(); | ||
|
||
Broadcast.sendMessage({type: NEW_LEADER_MESSAGE, clientID, timestamp}); | ||
setIsReady(); | ||
|
||
window.addEventListener('beforeunload', () => { | ||
if (!isClientTheLeader()) { | ||
return; | ||
} | ||
Broadcast.sendMessage({type: REMOVED_LEADER_MESSAGE, clientID}); | ||
}); | ||
} | ||
|
||
export { | ||
isClientTheLeader, | ||
init, | ||
isReady, | ||
subscribeToClientChange, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just realizing that this comment is a bit out-of-date. This code no longer has anything to do with processing write requests.