forked from fippo/rtcstats-server
-
Notifications
You must be signed in to change notification settings - Fork 24
refactor identity usage #111
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
Open
tamasdomokos
wants to merge
5
commits into
jitsi:feature-reconnect
Choose a base branch
from
tamasdomokos:tdomokos/refactor_reuse_identity
base: feature-reconnect
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,17 +1,12 @@ | ||
const fs = require('fs'); | ||
const sizeof = require('object-sizeof'); | ||
const path = require('path'); | ||
const { Writable } = require('stream'); | ||
const util = require('util'); | ||
|
||
const PromCollector = require('./metrics/PromCollector.js'); | ||
const fileStore = require('./store/file'); | ||
const utils = require('./utils/utils'); | ||
const { uuidV4 } = require('./utils/utils.js'); | ||
|
||
|
||
const cwd = process.cwd(); | ||
|
||
// we are using this because we want the regular file descriptors returned, | ||
// not the FileHandle objects from fs.promises.open | ||
const fsOpen = util.promisify(fs.open); | ||
|
@@ -45,10 +40,11 @@ class DemuxSink extends Writable { | |
* @param {boolean} persistDump - Flag used for generating a complete dump of the data coming to the stream. | ||
* Required when creating mock tests. | ||
*/ | ||
constructor({ tempPath, dumpFolder, connectionInfo, log, persistDump = false }) { | ||
constructor({ tempPath, dumpFolder, dumpPath, connectionInfo, log, persistDump = false }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think tempPath and dumpFolder are no longer needed if we're going to rely on the newly added dumpPath right? |
||
super({ objectMode: true }); | ||
|
||
this.dumpFolder = dumpFolder; | ||
this.dumpPath = dumpPath; | ||
this.connectionInfo = connectionInfo; | ||
this.log = log; | ||
this.timeoutId = -1; | ||
|
@@ -144,16 +140,15 @@ class DemuxSink extends Writable { | |
* | ||
* @param {string} id - sink id as saved in the sinkMap | ||
*/ | ||
_handleSinkClose(id, meta) { | ||
_handleSinkClose(id) { | ||
const sinkData = this.sinkMap.get(id); | ||
|
||
// Sanity check, make sure the data is available if not log an error and just send the id such that any | ||
// listening client has s chance to handle the sink. | ||
if (sinkData) { | ||
// we need to emit this on file stream finish | ||
this.emit('close-sink', { | ||
id: sinkData.id, | ||
meta: this._updateMeta(sinkData.meta, meta) | ||
id: sinkData.id | ||
}); | ||
} else { | ||
this.log.error('[Demux] sink on close meta should be available id:', id); | ||
|
@@ -173,50 +168,29 @@ class DemuxSink extends Writable { | |
PromCollector.sessionCount.inc(); | ||
|
||
const resolvedId = id; | ||
let fd; | ||
|
||
const idealPath = path.resolve(cwd, this.dumpFolder, id); | ||
const filePath = idealPath; | ||
const isReconnect = fs.existsSync(filePath); | ||
|
||
// If a client reconnects the same client id will be provided thus cases can occur where the previous dump | ||
// with the same id is still present on the disk, in order to avoid conflicts and states where multiple | ||
// handles are taken on the same file, we establish a convention appending an incremental number at the end | ||
// of the file ${id}_${i}. Thus any client that needs to read the dumps can search for ${id} and get an | ||
// incremental list. | ||
// Warning. This will resolve local reconnect conflicts, when uploading the associated metadata to a store | ||
// logic that handles conflicts at the store level also needs to be added e.g. when uploading to dynamodb | ||
// if the entry already exists because some other instance uploaded first, the same incremental approach needs | ||
// to be taken. | ||
while (!fd) { | ||
fd = await fsOpen(filePath, 'a'); | ||
} | ||
const isReconnect = fs.existsSync(this.dumpPath); | ||
const fd = await fsOpen(this.dumpPath, 'a'); | ||
|
||
this.log.info('[Demux] open-sink id: %s; path %s; connection: %o', id, filePath, this.connectionInfo); | ||
this.log.info('[Demux] open-sink id: %s; path %s; connection: %o', id, this.dumpPath, this.connectionInfo); | ||
|
||
const sink = fs.createWriteStream(idealPath, { fd }); | ||
const sink = fs.createWriteStream(this.dumpPath, { fd }); | ||
|
||
// Add the associated data to a map in order to properly direct requests to the appropriate sink. | ||
const sinkData = { | ||
id: resolvedId, | ||
sink, | ||
meta: { | ||
startDate: Date.now(), | ||
dumpPath: filePath | ||
startDate: this.startDate, | ||
dumpPath: this.dumpPath | ||
} | ||
}; | ||
|
||
this.sinkMap.set(id, sinkData); | ||
|
||
sink.on('error', error => this.log.error('[Demux] sink on error id: ', id, ' error:', error)); | ||
let identity; | ||
|
||
if (isReconnect) { | ||
identity = await this._getIdentityFromFile(sinkData.id); | ||
} | ||
|
||
// The close event should be emitted both on error and happy flow. | ||
sink.on('close', this._handleSinkClose.bind(this, id, identity)); | ||
sink.on('close', this._handleSinkClose.bind(this, id)); | ||
|
||
if (!isReconnect) { | ||
// Initialize the dump file by adding the connection metadata at the beginning. This data is usually used | ||
|
@@ -237,48 +211,10 @@ class DemuxSink extends Writable { | |
* @param {Object} data - New metadata. | ||
*/ | ||
async _sinkUpdateMetadata(sinkData, data) { | ||
let metadata; | ||
|
||
// Browser clients will send identity data as an array so we need to extract the element that contains | ||
// the actual metadata | ||
if (Array.isArray(data)) { | ||
metadata = data[2]; | ||
} else { | ||
metadata = data; | ||
} | ||
|
||
const meta = sinkData.meta; | ||
|
||
// A first level update of the properties will suffice. | ||
sinkData.meta = this._updateMeta(meta, metadata); | ||
|
||
// We expect metadata to be objects thus we need to stringify them before writing to the sink. | ||
this._sinkWrite(sinkData.sink, JSON.stringify(data)); | ||
} | ||
|
||
/** | ||
* | ||
* @param {*} meta | ||
* @param {*} metadata | ||
* @returns | ||
*/ | ||
_updateMeta(meta, metadata) { | ||
return { | ||
...meta, | ||
...metadata | ||
}; | ||
} | ||
|
||
/** | ||
* Getting identity from file in case of reconnect. | ||
*/ | ||
async _getIdentityFromFile(fname) { | ||
const filePath = utils.getDumpPath(this.tempPath, fname); | ||
const { identity = '' } = await fileStore.getObjectsByKeys(filePath, [ 'identity' ]); | ||
|
||
return identity; | ||
} | ||
|
||
/** | ||
* Self explanatory. | ||
* | ||
|
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.
bellow this line we persist connections that are JVB and JIGASI these do not go through the
FeatureExtractor
instead they are just saved directly, so the dumpData is still needed at this point.Also line 77 calls
this.persistDumpData(dumpData);
I don't see that function defined in this class.