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

feat: enable dynamic mapping when migrate #262

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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import crypto from 'crypto';
import { cloneDeep, mapValues } from 'lodash';
import { Config } from 'packages/osd-config/target';
Copy link
Collaborator

Choose a reason for hiding this comment

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

this import seems not correct, might need double check

import {
IndexMapping,
SavedObjectsFieldMapping,
Expand All @@ -48,11 +49,20 @@ import {
* @param typeDefinitions - the type definitions to build mapping from.
*/
export function buildActiveMappings(
typeDefinitions: SavedObjectsTypeMappingDefinitions | SavedObjectsMappingProperties
typeDefinitions: SavedObjectsTypeMappingDefinitions | SavedObjectsMappingProperties,
opensearchDashboardsRawConfig?: Config
): IndexMapping {
const mapping = defaultMapping();

const mergedProperties = validateAndMerge(mapping.properties, typeDefinitions);
let mergedProperties = validateAndMerge(mapping.properties, typeDefinitions);

if (opensearchDashboardsRawConfig?.get('workspace.enabled')) {
mergedProperties = validateAndMerge(mapping.properties, {
workspaces: {
type: 'keyword',
},
});
}

return cloneDeep({
...mapping,
Expand Down Expand Up @@ -186,9 +196,6 @@ function defaultMapping(): IndexMapping {
},
},
},
workspaces: {
type: 'keyword',
},
permissions: {
properties: {
read: principals,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*/

import { Logger } from 'src/core/server/logging';
import { Config } from 'packages/osd-config/target';
import { MigrationOpenSearchClient } from './migration_opensearch_client';
import { SavedObjectsSerializer } from '../../serialization';
import {
Expand Down Expand Up @@ -65,6 +66,7 @@ export interface MigrationOpts {
* prior to running migrations. For example: 'opensearch_dashboards_index_template*'
*/
obsoleteIndexTemplatePattern?: string;
opensearchDashboardsRawConfig?: Config;
}

/**
Expand All @@ -90,10 +92,15 @@ export interface Context {
* and various info needed to migrate the source index.
*/
export async function migrationContext(opts: MigrationOpts): Promise<Context> {
const { log, client } = opts;
const { log, client, opensearchDashboardsRawConfig } = opts;
const alias = opts.index;
const source = createSourceContext(await Index.fetchInfo(client, alias), alias);
const dest = createDestContext(source, alias, opts.mappingProperties);
const dest = createDestContext(
source,
alias,
opts.mappingProperties,
opensearchDashboardsRawConfig
);

return {
client,
Expand Down Expand Up @@ -125,10 +132,11 @@ function createSourceContext(source: Index.FullIndexInfo, alias: string) {
function createDestContext(
source: Index.FullIndexInfo,
alias: string,
typeMappingDefinitions: SavedObjectsTypeMappingDefinitions
typeMappingDefinitions: SavedObjectsTypeMappingDefinitions,
opensearchDashboardsRawConfig?: Config
): Index.FullIndexInfo {
const targetMappings = disableUnknownTypeMappingFields(
buildActiveMappings(typeMappingDefinitions),
buildActiveMappings(typeMappingDefinitions, opensearchDashboardsRawConfig),
source.mappings
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
import { loggingSystemMock } from '../../../logging/logging_system.mock';
import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry';
import { SavedObjectsType } from '../../types';
import { configMock } from '../../../config/mocks';

const createRegistry = (types: Array<Partial<SavedObjectsType>>) => {
const registry = new SavedObjectTypeRegistry();
Expand Down Expand Up @@ -147,6 +148,8 @@ type MockedOptions = OpenSearchDashboardsMigratorOptions & {
};

const mockOptions = () => {
const rawConfig = configMock.create();
rawConfig.get.mockReturnValue(true);
const options: MockedOptions = {
logger: loggingSystemMock.create().get(),
opensearchDashboardsVersion: '8.2.3',
Expand Down Expand Up @@ -186,6 +189,7 @@ const mockOptions = () => {
skip: false,
},
client: opensearchClientMock.createOpenSearchClient(),
opensearchDashboardsRawConfig: rawConfig,
};
return options;
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import { OpenSearchDashboardsConfigType } from 'src/core/server/opensearch_dashboards_config';
import { BehaviorSubject } from 'rxjs';
import { Config } from 'packages/osd-config/target';

import { Logger } from '../../../logging';
import { IndexMapping, SavedObjectsTypeMappingDefinitions } from '../../mappings';
Expand All @@ -54,6 +55,7 @@ export interface OpenSearchDashboardsMigratorOptions {
opensearchDashboardsConfig: OpenSearchDashboardsConfigType;
opensearchDashboardsVersion: string;
logger: Logger;
opensearchDashboardsRawConfig: Config;
}

export type IOpenSearchDashboardsMigrator = Pick<
Expand Down Expand Up @@ -83,6 +85,7 @@ export class OpenSearchDashboardsMigrator {
status: 'waiting',
});
private readonly activeMappings: IndexMapping;
private readonly opensearchDashboardsRawConfig: Config;

/**
* Creates an instance of OpenSearchDashboardsMigrator.
Expand All @@ -94,6 +97,7 @@ export class OpenSearchDashboardsMigrator {
savedObjectsConfig,
opensearchDashboardsVersion,
logger,
opensearchDashboardsRawConfig,
}: OpenSearchDashboardsMigratorOptions) {
this.client = client;
this.opensearchDashboardsConfig = opensearchDashboardsConfig;
Expand All @@ -109,7 +113,11 @@ export class OpenSearchDashboardsMigrator {
});
// Building the active mappings (and associated md5sums) is an expensive
// operation so we cache the result
this.activeMappings = buildActiveMappings(this.mappingProperties);
this.opensearchDashboardsRawConfig = opensearchDashboardsRawConfig;
this.activeMappings = buildActiveMappings(
this.mappingProperties,
this.opensearchDashboardsRawConfig
);
}

/**
Expand Down Expand Up @@ -181,6 +189,7 @@ export class OpenSearchDashboardsMigrator {
? 'opensearch_dashboards_index_template*'
: undefined,
convertToAliasScript: indexMap[index].script,
opensearchDashboardsRawConfig: this.opensearchDashboardsRawConfig,
});
});

Expand Down
8 changes: 8 additions & 0 deletions src/core/server/saved_objects/saved_objects_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import { registerRoutes } from './routes';
import { ServiceStatus, ServiceStatusLevels } from '../status';
import { calculateStatus$ } from './status';
import { createMigrationOpenSearchClient } from './migrations/core/';
import { Config } from '../config';
/**
* Saved Objects is OpenSearchDashboards's data persistence mechanism allowing plugins to
* use OpenSearch for storing and querying state. The SavedObjectsServiceSetup API exposes methods
Expand Down Expand Up @@ -315,6 +316,8 @@ export class SavedObjectsService
summary: `waiting`,
});

private opensearchDashboardsRawConfig?: Config;

constructor(private readonly coreContext: CoreContext) {
this.logger = coreContext.logger.get('savedobjects-service');
}
Expand All @@ -332,6 +335,10 @@ export class SavedObjectsService
.atPath<SavedObjectsMigrationConfigType>('migrations')
.pipe(first())
.toPromise();
this.opensearchDashboardsRawConfig = await this.coreContext.configService
.getConfig$()
.pipe(first())
.toPromise();
this.config = new SavedObjectConfig(savedObjectsConfig, savedObjectsMigrationConfig);

registerRoutes({
Expand Down Expand Up @@ -559,6 +566,7 @@ export class SavedObjectsService
this.logger,
migrationsRetryDelay
),
opensearchDashboardsRawConfig: this.opensearchDashboardsRawConfig as Config,
});
}
}
Loading