Skip to content

Fix read preference for aggregate #6585

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
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
80 changes: 80 additions & 0 deletions spec/ReadPreferenceOption.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,86 @@ describe_only_db('mongo')('Read preference option', () => {
});
});

it('should change read preference for `aggregate` using `beforeFind`', async() => {
// Save objects
const obj0 = new Parse.Object('MyObject');
obj0.set('boolKey', false);
const obj1 = new Parse.Object('MyObject');
obj1.set('boolKey', true);
await Parse.Object.saveAll([obj0, obj1]);
// Add trigger
Parse.Cloud.beforeFind('MyObject', req => {
req.readPreference = 'SECONDARY';
});
// Spy on DB adapter
const databaseAdapter = Config.get(Parse.applicationId).database.adapter;
spyOn(databaseAdapter.database.serverConfig, 'startSession').and.callThrough();
// Query
const query = new Parse.Query('MyObject');
const results = await query.aggregate([{match:{boolKey: false}}]);
// Validate
expect(results.length).toBe(1);
let readPreference = null;
databaseAdapter.database.serverConfig.startSession.calls.all().forEach(call => {
if (call.args[0].owner.ns.indexOf('MyObject') > -1) {
readPreference = call.args[0].owner.operation.readPreference.mode;
}
});
expect(readPreference).toEqual(ReadPreference.SECONDARY);
});

it('should change read preference for `find` using query option', async() => {
// Save objects
const obj0 = new Parse.Object('MyObject');
obj0.set('boolKey', false);
const obj1 = new Parse.Object('MyObject');
obj1.set('boolKey', true);
await Parse.Object.saveAll([obj0, obj1]);
// Spy on DB adapter
const databaseAdapter = Config.get(Parse.applicationId).database.adapter;
spyOn(databaseAdapter.database.serverConfig, 'cursor').and.callThrough();
// Query
const query = new Parse.Query('MyObject');
query.equalTo('boolKey', false);
query.readPreference('SECONDARY');
const results = await query.find();
// Validate
expect(results.length).toBe(1);
let myObjectReadPreference = null;
databaseAdapter.database.serverConfig.cursor.calls.all().forEach(call => {
if (call.args[0].ns.collection.indexOf('MyObject') >= 0) {
myObjectReadPreference =
call.args[0].options.readPreference.mode;
}
});
expect(myObjectReadPreference).toEqual(ReadPreference.SECONDARY);
});

it('should change read preference for `aggregate` using query option', async() => {
// Save objects
const obj0 = new Parse.Object('MyObject');
obj0.set('boolKey', false);
const obj1 = new Parse.Object('MyObject');
obj1.set('boolKey', true);
await Parse.Object.saveAll([obj0, obj1]);
// Spy on DB adapter
const databaseAdapter = Config.get(Parse.applicationId).database.adapter;
spyOn(databaseAdapter.database.serverConfig, 'startSession').and.callThrough();
// Query
const query = new Parse.Query('MyObject');
query.readPreference('SECONDARY');
const results = await query.aggregate([{match:{boolKey: false}}]);
// Validate
expect(results.length).toBe(1);
let readPreference = null;
databaseAdapter.database.serverConfig.startSession.calls.all().forEach(call => {
if (call.args[0].owner.ns.indexOf('MyObject') > -1) {
readPreference = call.args[0].owner.operation.readPreference.mode;
}
});
expect(readPreference).toEqual(ReadPreference.SECONDARY);
});

it('should find includes in same replica of readPreference by default', done => {
const databaseAdapter = Config.get(Parse.applicationId).database.adapter;

Expand Down
4 changes: 4 additions & 0 deletions src/Routers/AggregateRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export class AggregateRouter extends ClassesRouter {
options.explain = body.explain;
delete body.explain;
}
if (body.readPreference) {
options.readPreference = body.readPreference;
delete body.readPreference;
}
options.pipeline = AggregateRouter.getPipeline(body);
if (typeof body.where === 'string') {
body.where = JSON.parse(body.where);
Expand Down