From c5e446adbdde5c23bc06f326792aaff3e35dd937 Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Thu, 4 Jun 2020 10:07:12 +0200 Subject: [PATCH 1/5] Create a single repository to be shared by all calls to getScopedClient --- .../spaces/server/spaces_service/spaces_service.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/spaces/server/spaces_service/spaces_service.ts b/x-pack/plugins/spaces/server/spaces_service/spaces_service.ts index 759b0606a5e8b..cf181a78efcb8 100644 --- a/x-pack/plugins/spaces/server/spaces_service/spaces_service.ts +++ b/x-pack/plugins/spaces/server/spaces_service/spaces_service.ts @@ -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(); }; From ef30e36fbdd3073a07227f6597b9456bd793fee1 Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Thu, 4 Jun 2020 10:26:17 +0200 Subject: [PATCH 2/5] Cache migrator.getActiveMappings to improve createRepository --- .../saved_objects/migrations/kibana/kibana_migrator.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts index 4f69d45c192e9..69b57a498936e 100644 --- a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts +++ b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts @@ -74,6 +74,7 @@ export class KibanaMigrator { private readonly status$ = new BehaviorSubject({ 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; } /** From fa04609959d00f79af3b5354ad714ce13cc40774 Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Thu, 4 Jun 2020 14:15:37 +0200 Subject: [PATCH 3/5] Use typeregistry.getAllTypes instead of getRootPropertiesObjects(mappings) --- src/core/server/saved_objects/service/lib/repository.ts | 2 +- src/legacy/server/saved_objects/saved_objects_mixin.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/server/saved_objects/service/lib/repository.ts b/src/core/server/saved_objects/service/lib/repository.ts index e23f8dec5927c..b093fe779cab7 100644 --- a/src/core/server/saved_objects/service/lib/repository.ts +++ b/src/core/server/saved_objects/service/lib/repository.ts @@ -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)); diff --git a/src/legacy/server/saved_objects/saved_objects_mixin.js b/src/legacy/server/saved_objects/saved_objects_mixin.js index 7d84c27bd1ef0..e79631462f90e 100644 --- a/src/legacy/server/saved_objects/saved_objects_mixin.js +++ b/src/legacy/server/saved_objects/saved_objects_mixin.js @@ -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(); const schema = new SavedObjectsSchema(convertTypesToLegacySchema(typeRegistry.getAllTypes())); const visibleTypes = allTypes.filter((type) => !schema.isHiddenType(type)); From cc98fa6a58b7d55aa1b76554f9fb25806aa89834 Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Thu, 4 Jun 2020 15:31:02 +0200 Subject: [PATCH 4/5] Don't validate plugin's config every time it's read --- src/core/server/plugins/plugin_context.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/server/plugins/plugin_context.ts b/src/core/server/plugins/plugin_context.ts index 7afb607192cae..f0db3a25e313d 100644 --- a/src/core/server/plugins/plugin_context.ts +++ b/src/core/server/plugins/plugin_context.ts @@ -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() { + return coreContext.configService.atPath(pluginManifest.configPath).pipe(shareReplay(1)); }, createIfExists() { return coreContext.configService.optionalAtPath(pluginManifest.configPath); From 9ffcf55434be799b906e190d551ea9dfe9d1e0b3 Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Thu, 4 Jun 2020 18:28:44 +0200 Subject: [PATCH 5/5] Fix saved_objects_mixin --- src/legacy/server/saved_objects/saved_objects_mixin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/legacy/server/saved_objects/saved_objects_mixin.js b/src/legacy/server/saved_objects/saved_objects_mixin.js index e79631462f90e..63839b9d0f1d7 100644 --- a/src/legacy/server/saved_objects/saved_objects_mixin.js +++ b/src/legacy/server/saved_objects/saved_objects_mixin.js @@ -33,7 +33,7 @@ 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 = typeRegistry.getAllTypes(); + const allTypes = typeRegistry.getAllTypes().map((t) => t.name); const schema = new SavedObjectsSchema(convertTypesToLegacySchema(typeRegistry.getAllTypes())); const visibleTypes = allTypes.filter((type) => !schema.isHiddenType(type));