Adds schema caching capabilities (5s by default) (#2286)
* Adds schema caching capabilities (off by default) * Use InMemoryCacheAdapter * Uses proper adapter to generate a cache * Fix bugs when running disabled cache * nits * nits * Use options object instead of boolean * Imrpove concurrency of loadSchema * Adds testing with SCHEMA_CACHE_ON * Use CacheController instead of generator - Makes caching SchemaCache use a generated prefix - Makes clearing the SchemaCache clear only the cached schema keys - Enable cache by default (ttl 5s)
This commit is contained in:
@@ -273,19 +273,25 @@ class SchemaController {
|
||||
data;
|
||||
perms;
|
||||
|
||||
constructor(databaseAdapter) {
|
||||
constructor(databaseAdapter, schemaCache) {
|
||||
this._dbAdapter = databaseAdapter;
|
||||
|
||||
this._cache = schemaCache;
|
||||
// this.data[className][fieldName] tells you the type of that field, in mongo format
|
||||
this.data = {};
|
||||
// this.perms[className][operation] tells you the acl-style permissions
|
||||
this.perms = {};
|
||||
}
|
||||
|
||||
reloadData() {
|
||||
reloadData(options = {clearCache: false}) {
|
||||
if (options.clearCache) {
|
||||
this._cache.clear();
|
||||
}
|
||||
if (this.reloadDataPromise && !options.clearCache) {
|
||||
return this.reloadDataPromise;
|
||||
}
|
||||
this.data = {};
|
||||
this.perms = {};
|
||||
return this.getAllClasses()
|
||||
this.reloadDataPromise = this.getAllClasses(options)
|
||||
.then(allSchemas => {
|
||||
allSchemas.forEach(schema => {
|
||||
this.data[schema.className] = injectDefaultSchema(schema).fields;
|
||||
@@ -300,20 +306,51 @@ class SchemaController {
|
||||
classLevelPermissions: {}
|
||||
});
|
||||
});
|
||||
delete this.reloadDataPromise;
|
||||
}, (err) => {
|
||||
delete this.reloadDataPromise;
|
||||
throw err;
|
||||
});
|
||||
return this.reloadDataPromise;
|
||||
}
|
||||
|
||||
getAllClasses(options = {clearCache: false}) {
|
||||
if (options.clearCache) {
|
||||
this._cache.clear();
|
||||
}
|
||||
return this._cache.getAllClasses().then((allClasses) => {
|
||||
if (allClasses && allClasses.length && !options.clearCache) {
|
||||
return Promise.resolve(allClasses);
|
||||
}
|
||||
return this._dbAdapter.getAllClasses()
|
||||
.then(allSchemas => allSchemas.map(injectDefaultSchema))
|
||||
.then(allSchemas => {
|
||||
return this._cache.setAllClasses(allSchemas).then(() => {
|
||||
return allSchemas;
|
||||
});
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
getAllClasses() {
|
||||
return this._dbAdapter.getAllClasses()
|
||||
.then(allSchemas => allSchemas.map(injectDefaultSchema));
|
||||
}
|
||||
|
||||
getOneSchema(className, allowVolatileClasses = false) {
|
||||
if (allowVolatileClasses && volatileClasses.indexOf(className) > -1) {
|
||||
return Promise.resolve(this.data[className]);
|
||||
getOneSchema(className, allowVolatileClasses = false, options = {clearCache: false}) {
|
||||
if (options.clearCache) {
|
||||
this._cache.clear();
|
||||
}
|
||||
return this._dbAdapter.getClass(className)
|
||||
.then(injectDefaultSchema)
|
||||
if (allowVolatileClasses && volatileClasses.indexOf(className) > -1) {
|
||||
return Promise.resolve(this.data[className]);
|
||||
}
|
||||
return this._cache.getOneSchema(className).then((cached) => {
|
||||
if (cached && !options.clearCache) {
|
||||
return Promise.resolve(cached);
|
||||
}
|
||||
return this._dbAdapter.getClass(className)
|
||||
.then(injectDefaultSchema)
|
||||
.then((result) => {
|
||||
return this._cache.setOneSchema(className, result).then(() => {
|
||||
return result;
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Create a new class that includes the three default fields.
|
||||
@@ -331,6 +368,10 @@ class SchemaController {
|
||||
|
||||
return this._dbAdapter.createClass(className, convertSchemaToAdapterSchema({ fields, classLevelPermissions, className }))
|
||||
.then(convertAdapterSchemaToParseSchema)
|
||||
.then((res) => {
|
||||
this._cache.clear();
|
||||
return res;
|
||||
})
|
||||
.catch(error => {
|
||||
if (error && error.code === Parse.Error.DUPLICATE_VALUE) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
|
||||
@@ -376,7 +417,7 @@ class SchemaController {
|
||||
});
|
||||
|
||||
return Promise.all(deletePromises) // Delete Everything
|
||||
.then(() => this.reloadData()) // Reload our Schema, so we have all the new values
|
||||
.then(() => this.reloadData({ clearCache: true })) // Reload our Schema, so we have all the new values
|
||||
.then(() => {
|
||||
let promises = insertedFields.map(fieldName => {
|
||||
const type = submittedFields[fieldName];
|
||||
@@ -410,13 +451,13 @@ class SchemaController {
|
||||
// We don't have this class. Update the schema
|
||||
return this.addClassIfNotExists(className)
|
||||
// The schema update succeeded. Reload the schema
|
||||
.then(() => this.reloadData())
|
||||
.then(() => this.reloadData({ clearCache: true }))
|
||||
.catch(error => {
|
||||
// The schema update failed. This can be okay - it might
|
||||
// have failed because there's a race condition and a different
|
||||
// client is making the exact same schema update that we want.
|
||||
// So just reload the schema.
|
||||
return this.reloadData();
|
||||
return this.reloadData({ clearCache: true });
|
||||
})
|
||||
.then(() => {
|
||||
// Ensure that the schema now validates
|
||||
@@ -486,7 +527,7 @@ class SchemaController {
|
||||
}
|
||||
validateCLP(perms, newSchema);
|
||||
return this._dbAdapter.setClassLevelPermissions(className, perms)
|
||||
.then(() => this.reloadData());
|
||||
.then(() => this.reloadData({ clearCache: true }));
|
||||
}
|
||||
|
||||
// Returns a promise that resolves successfully to the new schema
|
||||
@@ -521,23 +562,26 @@ class SchemaController {
|
||||
`schema mismatch for ${className}.${fieldName}; expected ${expectedType.type || expectedType} but got ${type.type}`
|
||||
);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
return this._dbAdapter.addFieldIfNotExists(className, fieldName, type).then(() => {
|
||||
// The update succeeded. Reload the schema
|
||||
return this.reloadData();
|
||||
return this.reloadData({ clearCache: true });
|
||||
}, error => {
|
||||
//TODO: introspect the error and only reload if the error is one for which is makes sense to reload
|
||||
|
||||
// The update failed. This can be okay - it might have been a race
|
||||
// condition where another client updated the schema in the same
|
||||
// way that we wanted to. So, just reload the schema
|
||||
return this.reloadData();
|
||||
return this.reloadData({ clearCache: true });
|
||||
}).then(error => {
|
||||
// Ensure that the schema now validates
|
||||
if (!dbTypeMatchesObjectType(this.getExpectedType(className, fieldName), type)) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_JSON, `Could not add field ${fieldName}`);
|
||||
}
|
||||
// Remove the cached schema
|
||||
this._cache.clear();
|
||||
return this;
|
||||
});
|
||||
});
|
||||
@@ -562,7 +606,7 @@ class SchemaController {
|
||||
throw new Parse.Error(136, `field ${fieldName} cannot be changed`);
|
||||
}
|
||||
|
||||
return this.getOneSchema(className)
|
||||
return this.getOneSchema(className, false, {clearCache: true})
|
||||
.catch(error => {
|
||||
if (error === undefined) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} does not exist.`);
|
||||
@@ -579,8 +623,9 @@ class SchemaController {
|
||||
return database.adapter.deleteFields(className, schema, [fieldName])
|
||||
.then(() => database.adapter.deleteClass(`_Join:${fieldName}:${className}`));
|
||||
}
|
||||
|
||||
return database.adapter.deleteFields(className, schema, [fieldName]);
|
||||
}).then(() => {
|
||||
this._cache.clear();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -711,9 +756,9 @@ class SchemaController {
|
||||
}
|
||||
|
||||
// Returns a promise for a new Schema.
|
||||
const load = dbAdapter => {
|
||||
let schema = new SchemaController(dbAdapter);
|
||||
return schema.reloadData().then(() => schema);
|
||||
const load = (dbAdapter, schemaCache, options) => {
|
||||
let schema = new SchemaController(dbAdapter, schemaCache);
|
||||
return schema.reloadData(options).then(() => schema);
|
||||
}
|
||||
|
||||
// Builds a new schema (in schema API response format) out of an
|
||||
|
||||
Reference in New Issue
Block a user