Optimize saved objects getScopedClient and HTTP API (#68221)

* Create a single repository to be shared by all calls to getScopedClient

* Cache migrator.getActiveMappings to improve createRepository

* Use typeregistry.getAllTypes instead of getRootPropertiesObjects(mappings)

* Don't validate plugin's config every time it's read

* Fix saved_objects_mixin
This commit is contained in:
Rudolf Meijering 2020-06-06 01:05:02 +02:00 committed by GitHub
parent 77c8aee3cd
commit 2559dbe3ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 11 deletions

View file

@ -17,7 +17,7 @@
* under the License.
*/
import { map } from 'rxjs/operators';
import { map, shareReplay } from 'rxjs/operators';
import { combineLatest } from 'rxjs';
import { CoreContext } from '../core_context';
import { PluginWrapper } from './plugin';
@ -107,8 +107,8 @@ export function createPluginInitializerContext(
* @param ConfigClass A class (not an instance of a class) that contains a
* static `schema` that we validate the config at the given `path` against.
*/
create() {
return coreContext.configService.atPath(pluginManifest.configPath);
create<T>() {
return coreContext.configService.atPath<T>(pluginManifest.configPath).pipe(shareReplay(1));
},
createIfExists() {
return coreContext.configService.optionalAtPath(pluginManifest.configPath);

View file

@ -74,6 +74,7 @@ export class KibanaMigrator {
private readonly status$ = new BehaviorSubject<KibanaMigratorStatus>({
status: 'waiting',
});
private readonly activeMappings: IndexMapping;
/**
* Creates an instance of KibanaMigrator.
@ -100,6 +101,9 @@ export class KibanaMigrator {
validateDoc: docValidator(savedObjectValidations || {}),
log: this.log,
});
// Building the active mappings (and associated md5sums) is an expensive
// operation so we cache the result
this.activeMappings = buildActiveMappings(this.mappingProperties);
}
/**
@ -172,7 +176,7 @@ export class KibanaMigrator {
*
*/
public getActiveMappings(): IndexMapping {
return buildActiveMappings(this.mappingProperties);
return this.activeMappings;
}
/**

View file

@ -136,7 +136,7 @@ export class SavedObjectsRepository {
injectedConstructor: any = SavedObjectsRepository
): ISavedObjectsRepository {
const mappings = migrator.getActiveMappings();
const allTypes = Object.keys(getRootPropertiesObjects(mappings));
const allTypes = typeRegistry.getAllTypes().map((t) => t.name);
const serializer = new SavedObjectsSerializer(typeRegistry);
const visibleTypes = allTypes.filter((type) => !typeRegistry.isHidden(type));

View file

@ -27,14 +27,13 @@ import {
importSavedObjectsFromStream,
resolveSavedObjectsImportErrors,
} from '../../../core/server/saved_objects';
import { getRootPropertiesObjects } from '../../../core/server/saved_objects/mappings';
import { convertTypesToLegacySchema } from '../../../core/server/saved_objects/utils';
export function savedObjectsMixin(kbnServer, server) {
const migrator = kbnServer.newPlatform.__internals.kibanaMigrator;
const typeRegistry = kbnServer.newPlatform.start.core.savedObjects.getTypeRegistry();
const mappings = migrator.getActiveMappings();
const allTypes = Object.keys(getRootPropertiesObjects(mappings));
const allTypes = typeRegistry.getAllTypes().map((t) => t.name);
const schema = new SavedObjectsSchema(convertTypesToLegacySchema(typeRegistry.getAllTypes()));
const visibleTypes = allTypes.filter((type) => !schema.isHiddenType(type));

View file

@ -68,14 +68,18 @@ export class SpacesService {
return spaceId;
};
const internalRepositoryPromise = getStartServices().then(([coreStart]) =>
coreStart.savedObjects.createInternalRepository(['space'])
);
const getScopedClient = async (request: KibanaRequest) => {
const [coreStart] = await getStartServices();
const internalRepository = await internalRepositoryPromise;
return config$
.pipe(
take(1),
map((config) => {
const internalRepository = coreStart.savedObjects.createInternalRepository(['space']);
const callWithRequestRepository = coreStart.savedObjects.createScopedRepository(
request,
['space']
@ -92,8 +96,7 @@ export class SpacesService {
internalRepository,
request
);
}),
take(1)
})
)
.toPromise();
};