Skip to content
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

Added persistent settings store for dynamic settings configuration #1559

Closed
Closed
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"body-parser": "^1.14.2",
"colors": "^1.1.2",
"commander": "^2.9.0",
"deep-equal": "^1.0.1",
"deepcopy": "^0.6.1",
"express": "^4.13.4",
"intersect": "^1.0.1",
Expand Down
134 changes: 134 additions & 0 deletions spec/PersistentSettingsStore.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
'use strict';

var PersistentSettingsStore = require('../src/PersistentSettingsStore').default;
var DatabaseAdapter = require('../src/DatabaseAdapter');
var appId = 'test';
var collectionPrefix = 'test_';
var store;
var settingsCollection;

describe('PersistentSettingsStore', function () {
beforeEach(function () {
store = PersistentSettingsStore({
freshness: 0.1
}, {
defined: 0
});
});

describe('mocked db', function () {
beforeEach(function () {
settingsCollection = jasmine.createSpyObj('settingsCollection', ['find', 'upsertOne']);
settingsCollection.find.and.returnValue(Promise.resolve([]));
settingsCollection.upsertOne.and.returnValue(Promise.resolve());

spyOn(DatabaseAdapter, 'getDatabaseConnection').and.returnValue({
adaptiveCollection: _ => {
return Promise.resolve(settingsCollection);
}
});
});

it('does not persist locked settings', function() {
store.set(appId, {
applicationId: appId
});

expect(store.get(appId, 'persisted').applicationId).toBeUndefined;
expect(store.get(appId, 'locked').applicationId).toEqual(appId);
});

it('does not persist defined settings by default', function() {
store.set(appId, {
defined: 0
});

expect(store.get(appId, 'persisted').defined).toBeUndefined;
expect(store.get(appId, 'locked').defined).toBeDefined;
});

it('persists defined settings if lockDefinedSettings false', function() {
store = PersistentSettingsStore({
lockDefinedSettings: false
}, {
defined: 0
});

store.set(appId, {
defined: 0
});

expect(store.get(appId, 'persisted').defined).toBeDefined;
expect(store.get(appId, 'locked').defined).toBeUndefined;
});

it('does not allow modification of locked settings', function() {
store.set(appId, {
defined: 0
});

store.get(appId).defined = 2;

expect(store.get(appId).defined).toEqual(0);
});

it('allows modification of persisted settings', function() {
store.set(appId, {
modifiable: 0
});

store.get(appId).modifiable = 2;
expect(store.get(appId).modifiable).toEqual(2);
});

it('respects freshness option', function(done) {
// freshness 100 ms
store.set(appId, {
modifiable: 0
});

function get() {
store.get(appId);
}
setTimeout(get, 50);
// freshness expires
setTimeout(get, 150);
setTimeout(get, 200);
// freshness expires
setTimeout(get, 300)
setTimeout(function () {
// three calls: one for initial pull, two from expired freshness
expect(settingsCollection.find.calls.count()).toEqual(3);
done();
}, 350);
});

it('pushes on setting change', function(done) {
store.set(appId, {
applicationId: appId,
modifiable: 0
});

setTimeout(function () {
store.get(appId).modifiable = 2;
}, 100);

setTimeout(function () {
var calls = settingsCollection.upsertOne.calls;
expect(calls.count()).toEqual(2);
expect(calls.argsFor(0)[1]).toEqual({
applicationId: appId,
persisted: {
modifiable: 0
}
});
expect(calls.argsFor(1)[1]).toEqual({
$set: {
'persisted.modifiable': 2
}
});
done();
}, 200);
});
});
});
1 change: 1 addition & 0 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class Config {
this.liveQueryController = cacheInfo.liveQueryController;
this.sessionLength = cacheInfo.sessionLength;
this.generateSessionExpiresAt = this.generateSessionExpiresAt.bind(this);
this.settingsCacheOptions = cacheInfo.settingsCacheOptions;
}

static validate(options) {
Expand Down
85 changes: 48 additions & 37 deletions src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var batch = require('./batch'),
import { logger,
configureLogger } from './logger';
import cache from './cache';
import PersistentSettingsStore from './PersistentSettingsStore';
import Config from './Config';
import parseServerPackage from '../package.json';
import PromiseRouter from './PromiseRouter';
Expand Down Expand Up @@ -81,43 +82,48 @@ addParseCloud();

class ParseServer {

constructor({
appId = requiredParameter('You must provide an appId!'),
masterKey = requiredParameter('You must provide a masterKey!'),
appName,
databaseAdapter,
filesAdapter,
push,
loggerAdapter,
logsFolder,
databaseURI = DatabaseAdapter.defaultDatabaseURI,
databaseOptions,
cloud,
collectionPrefix = '',
clientKey,
javascriptKey,
dotNetKey,
restAPIKey,
fileKey = 'invalid-file-key',
facebookAppIds = [],
enableAnonymousUsers = true,
allowClientClassCreation = true,
oauth = {},
serverURL = requiredParameter('You must provide a serverURL!'),
maxUploadSize = '20mb',
verifyUserEmails = false,
emailAdapter,
publicServerURL,
customPages = {
invalidLink: undefined,
verifyEmailSuccess: undefined,
choosePassword: undefined,
passwordResetSuccess: undefined
},
liveQuery = {},
sessionLength = 31536000, // 1 Year in seconds
verbose = false,
}) {
constructor(definedOptions = {}) {
let {
appId = requiredParameter('You must provide an appId!'),
masterKey = requiredParameter('You must provide a masterKey!'),
appName,
databaseAdapter,
filesAdapter,
push,
loggerAdapter,
logsFolder,
databaseURI = DatabaseAdapter.defaultDatabaseURI,
databaseOptions,
cloud,
collectionPrefix = '',
clientKey,
javascriptKey,
dotNetKey,
restAPIKey,
fileKey = 'invalid-file-key',
facebookAppIds = [],
enableAnonymousUsers = true,
allowClientClassCreation = true,
oauth = {},
serverURL = requiredParameter('You must provide a serverURL!'),
maxUploadSize = '20mb',
verifyUserEmails = false,
emailAdapter,
publicServerURL,
customPages = {
invalidLink: undefined,
verifyEmailSuccess: undefined,
choosePassword: undefined,
passwordResetSuccess: undefined
},
liveQuery = {},
sessionLength = 31536000, // 1 Year in seconds
verbose = false,
settingsCacheOptions = {
lockDefinedSettings: true,
freshness: 15
}
} = definedOptions;
// Initialize the node client SDK automatically
Parse.initialize(appId, javascriptKey || 'unused', masterKey);
Parse.serverURL = serverURL;
Expand Down Expand Up @@ -171,7 +177,11 @@ class ParseServer {
const userController = new UserController(emailControllerAdapter, appId, { verifyUserEmails });
const liveQueryController = new LiveQueryController(liveQuery);

if (settingsCacheOptions) {
cache.apps = PersistentSettingsStore(settingsCacheOptions, definedOptions);
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the intended behavior upon server restart?

Copy link
Author

Choose a reason for hiding this comment

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

If settingsCacheOptions is false there is no persistence. Otherwise, cache.apps.set() will trigger a pull from the database, update persisted settings with whatever is returned, then push to the database. However, locked & defined properties won't be updated even if they exist in the database because I attach a dummy setter to those properties.

Copy link
Contributor

@flovilmart flovilmart Apr 20, 2016

Choose a reason for hiding this comment

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

so how do you load a new configuration, like a new masterKey or update a property that is locked or defined?

Copy link
Author

@mamaso mamaso Apr 20, 2016

Choose a reason for hiding this comment

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

Defined settings are modifiable if lockDefinedSettings is false (so they will be overwritten by their persisted values).

Locked settings (like masterKey) are settings where I wasn't sure how to approach modifying them without a restart due to side effects. For example, if you change the masterKey from the parse dashboard all the sudden your experience is broken as you can't connect to the server anymore because your masterKey in the parse-dashboard-config is invalid.

The only way to update these locked settings is in the code configuration you pass to the parse server constructor.

Copy link
Contributor

Choose a reason for hiding this comment

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

the other problem is the risk of error, rewriting from the passed options, and losing the changes (like masterKey) that has been changed from the dashboard.

}
cache.apps.set(appId, {
applicationId: appId,
masterKey: masterKey,
serverURL: serverURL,
collectionPrefix: collectionPrefix,
Expand All @@ -195,6 +205,7 @@ class ParseServer {
maxUploadSize: maxUploadSize,
liveQueryController: liveQueryController,
sessionLength : Number(sessionLength),
settingsCacheOptions: settingsCacheOptions
});

// To maintain compatibility. TODO: Remove in some version that breaks backwards compatability
Expand Down
Loading