Skip to content

Commit

Permalink
Fix instrumenting compareWith
Browse files Browse the repository at this point in the history
  • Loading branch information
zodern committed Jun 20, 2024
1 parent 323b801 commit c544b43
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
30 changes: 18 additions & 12 deletions lib/hijack/redis_oplog.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,26 @@ function protectAgainstRaceConditions (collection) {
);
}

function rewriteReloadRequeryFuncs () {
let WrappedSymbol = Symbol('MontiCompareWithWrapped');
let InfoSymbol = Symbol('MontiCompareWithInfo');
export function rewriteReloadRequeryFuncs (observableCollection) {
if (observableCollection._ownerInfo) {
observableCollection.store[InfoSymbol] = observableCollection._ownerInfo;
}

if (observableCollection.store.constructor[WrappedSymbol]) {
return;
}

observableCollection.store.constructor[WrappedSymbol] = true;

// handle reload/requery cases
const originalCompareWith = this.store.constructor.prototype.compareWith;
this.store.constructor.prototype.compareWith = (other, callbacks) => {
Kadira.models.pubsub.trackPolledDocuments(this._ownerInfo, other.size());
const originalCompareWith = observableCollection.store.constructor.prototype.compareWith;

return originalCompareWith.call(this.store, other, callbacks);
observableCollection.store.constructor.prototype.compareWith = function (other) {
Kadira.models.pubsub.trackPolledDocuments(this[InfoSymbol], other.size());

return originalCompareWith.apply(this, arguments);
};
}

Expand Down Expand Up @@ -55,7 +68,6 @@ export function wrapRedisOplogObserveDriver (driver) {
// it is set to true in the initial add and synthetic mutations
observableCollectionProto.add = function (doc, safe) {
// handle reload/requery cases
rewriteReloadRequeryFuncs.call(this);
let coll = this.cursorDescription.collectionName;
let query = this.cursorDescription.selector;
let opts = this.cursorDescription.options;
Expand Down Expand Up @@ -96,19 +108,13 @@ export function wrapRedisOplogObserveDriver (driver) {
};

observableCollectionProto.change = function (doc, modifiedFields) {
// handle reload/requery cases
rewriteReloadRequeryFuncs.call(this);

if (this._ownerInfo) {
Kadira.models.pubsub.trackLiveUpdates(this._ownerInfo, '_changePublished', 1);
}
originalChange.call(this, doc, modifiedFields);
};

observableCollectionProto.remove = function (docId) {
// handle reload/requery cases
rewriteReloadRequeryFuncs.call(this);

if (this._ownerInfo) {
Kadira.models.pubsub.trackLiveUpdates(this._ownerInfo, '_removePublished', 1);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/hijack/wrap_observers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { _ } from 'meteor/underscore';
import { rewriteReloadRequeryFuncs } from './redis_oplog';

export function wrapOplogObserveDriver (proto) {
// Track the polled documents. This is reflected to the RAM size and
Expand Down Expand Up @@ -183,6 +184,10 @@ export function wrapForCountingObservers () {

ownerStorer._ownerInfo = ownerInfo;

if (observerDriver.observableCollection) {
rewriteReloadRequeryFuncs(observerDriver.observableCollection);
}

Kadira.EventBus.emit('pubsub', 'observerCreated', ownerInfo);
Kadira.models.pubsub.trackCreatedObserver(ownerInfo);

Expand Down
33 changes: 33 additions & 0 deletions tests/hijack/redis_oplog.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,36 @@ addTestWithRoundedTime('Database - Redis Oplog - Changed', function (test) {

Meteor._sleepForMs(100);
});

addTestWithRoundedTime('Database - Redis Oplog - Remove with limit', function (test) {
const pub = RegisterPublication(() => TestData.find({}, { limit: 100 }));
TestData.remove({});
const client = GetMeteorClient();

const sub = SubscribeAndWait(client, pub);

Check failure on line 280 in tests/hijack/redis_oplog.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 2 spaces but found 4

TestData.insert({ name: 'test1' });

Check failure on line 282 in tests/hijack/redis_oplog.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 2 spaces but found 4
TestData.insert({ name: 'test2' });

Check failure on line 283 in tests/hijack/redis_oplog.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 2 spaces but found 4
TestData.insert({ name: 'test3' });

Check failure on line 284 in tests/hijack/redis_oplog.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 2 spaces but found 4

Meteor._sleepForMs(100);

Check failure on line 286 in tests/hijack/redis_oplog.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 2 spaces but found 4
TestData.remove({ name: 'test2' });

Check failure on line 287 in tests/hijack/redis_oplog.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 2 spaces but found 4

Meteor._sleepForMs(100);

Check failure on line 289 in tests/hijack/redis_oplog.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 2 spaces but found 4

let metrics = FindMetricsForPub(pub);

Check failure on line 291 in tests/hijack/redis_oplog.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 2 spaces but found 4

test.equal(metrics.totalObservers, 1, 'observers');

Check failure on line 293 in tests/hijack/redis_oplog.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 2 spaces but found 4
test.equal(metrics.liveRemovedDocuments, 1, 'removed');

Check failure on line 294 in tests/hijack/redis_oplog.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 2 spaces but found 4

TestData.remove({});

Meteor._sleepForMs(100);

metrics = FindMetricsForPub(pub);

test.equal(metrics.totalObservers, 1, 'observers');
test.equal(metrics.liveRemovedDocuments, 3, 'removed');

sub.stop();
});

0 comments on commit c544b43

Please sign in to comment.