Skip to content

Commit

Permalink
dummy implementation done
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Feb 15, 2023
1 parent b28424a commit 445058b
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export interface LogAwareState {
logs: MigrationLog[];
}


interface StateTransitionLogMeta extends LogMeta {
kibana: {
migrations: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export class KibanaMigrator implements IKibanaMigrator {
migrationConfig: this.soMigrationsConfig,
docLinks: this.docLinks,
serializer: this.serializer,
elasticsearchClient: this.client,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@

import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server';
import * as Actions from './actions';
import type { State } from './state';

export async function cleanup(client: ElasticsearchClient, state?: State) {
if (!state) return;
type CleanableState = { sourceIndexPitId: string } | {};

export async function cleanup(client: ElasticsearchClient, state?: CleanableState) {
if (!state) {
return;
}
if ('sourceIndexPitId' in state) {
await Actions.closePit({ client, pitId: state.sourceIndexPitId })();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,29 @@
*/

import type { MigratorContext } from './types';
import type { MigrateIndexOptions } from '../migrate_index';

export interface CreateContextOps {
// TODO
}
export type CreateContextOps = Omit<MigrateIndexOptions, 'logger'>;

export const createContext = ({}: CreateContextOps): MigratorContext => {
/**
* Create the context object that will be used for this index migration.
*/
export const createContext = ({
types,
docLinks,
migrationConfig,
elasticsearchClient,
indexPrefix,
typeRegistry,
serializer,
}: CreateContextOps): MigratorContext => {
return {
// TODO
}
}
indexPrefix,
types,
elasticsearchClient,
typeRegistry,
serializer,
maxRetryAttempts: migrationConfig.retryAttempts,
migrationDocLinks: docLinks.links.kibanaUpgradeSavedObjects,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
*/

import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server';
import type { Logger } from '@kbn/logging';
import type {
ISavedObjectTypeRegistry,
ISavedObjectsSerializer,
} from '@kbn/core-saved-objects-server';
import type { DocLinks } from '@kbn/doc-links';

/**
Expand All @@ -16,12 +19,16 @@ import type { DocLinks } from '@kbn/doc-links';
export interface MigratorContext {
/** The first part of the index name such as `.kibana` or `.kibana_task_manager` */
readonly indexPrefix: string;
/** Name of the types that are living in the index */
readonly types: string[];
/** The client to use for communications with ES */
readonly elasticsearchClient: ElasticsearchClient;
/** Preconfigured logger to use */
readonly logger: Logger;
/** The maximum number of retries to attempt for a failing action */
readonly maxRetryAttempts: number;
/** DocLinks for savedObjects. to reference online documentation */
readonly migrationDocLinks: DocLinks['kibanaUpgradeSavedObjects'];
/** SO serializer to use for migration */
readonly serializer: ISavedObjectsSerializer;
/** The SO type registry to use for the migration */
readonly typeRegistry: ISavedObjectTypeRegistry;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@
* Side Public License, v 1.
*/

export { runZeroDowntimeMigration } from './run_zdt_migration';
export type { RunZeroDowntimeMigrationOpts } from './run_zdt_migration';
export { runZeroDowntimeMigration, type RunZeroDowntimeMigrationOpts } from './run_zdt_migration';
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server';
import {
type SavedObjectsMigrationConfigType,
type MigrationResult,
Expand All @@ -16,14 +17,16 @@ import type {
} from '@kbn/core-saved-objects-server';
import type { Logger } from '@kbn/logging';
import type { DocLinksServiceStart } from '@kbn/core-doc-links-server';
import { stateActionMachine } from '../state_action_machine';
import { migrationStateActionMachine } from './migration_state_action_machine';
import type { VersionedTransformer } from '../document_migrator';
import { createContext } from './context';
import { next } from './next';
import { model } from './model';
import { createInitialState } from './state';

export interface MigrateIndexOptions {
indexPrefix: string;
types: string[];
/** The kibana system index prefix. e.g `.kibana` */
kibanaIndexPrefix: string;
/** The SO type registry to use for the migration */
typeRegistry: ISavedObjectTypeRegistry;
/** Logger to use for migration output */
Expand All @@ -34,10 +37,24 @@ export interface MigrateIndexOptions {
migrationConfig: SavedObjectsMigrationConfigType;
/** docLinks contract to use to link to documentation */
docLinks: DocLinksServiceStart;
/** */
/** SO serializer to use for migration */
serializer: ISavedObjectsSerializer;
/** The client to use for communications with ES */
elasticsearchClient: ElasticsearchClient;
}

export const migrateIndex = async ({}: MigrateIndexOptions): Promise<MigrationResult> => {
return { status: 'skipped' };
export const migrateIndex = async ({
logger,
...options
}: MigrateIndexOptions): Promise<MigrationResult> => {
const context = createContext(options);
const initialState = createInitialState(context);

return migrationStateActionMachine({
initialState,
next: next(context),
model,
context,
logger,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
*/

import { errors as EsErrors } from '@elastic/elasticsearch';
import * as Option from 'fp-ts/lib/Option';
import type { Logger } from '@kbn/logging';
import {
getErrorMessage,
getRequestDebugMeta,
} from '@kbn/core-elasticsearch-client-server-internal';
import type { SavedObjectsRawDoc } from '@kbn/core-saved-objects-server';
import { logStateTransition, logActionResponse } from '../common/utils';
import { type Model, type Next, stateActionMachine } from '../state_action_machine';
import { type Next, stateActionMachine } from '../state_action_machine';
import { cleanup } from '../migrations_state_machine_cleanup';
import type { State } from './state';
import type { MigratorContext } from './context';
Expand All @@ -33,13 +32,14 @@ export async function migrationStateActionMachine({
context,
next,
model,
logger,
}: {
initialState: State;
context: MigratorContext;
next: Next<State>;
model: Model<State>;
model: (state: State, res: any, context: MigratorContext) => State;
logger: Logger;
}) {
const logger = context.logger;
const startTime = Date.now();
// Since saved object index names usually start with a `.` and can be
// configured by users to include several `.`'s we can't use a logger tag to
Expand All @@ -54,12 +54,13 @@ export async function migrationStateActionMachine({
(state, res) => {
lastState = state;
logActionResponse(logger, logMessagePrefix, state, res);
const newState = model(state, res);
const newState = model(state, res, context);
// Redact the state to reduce the memory consumption and so that we
// don't log sensitive information inside documents by only keeping
// the _id's of documents
const redactedNewState = {
...newState,
/* TODO: commented until we have model stages that process outdated docs. (attrs not on model atm)
...{
outdatedDocuments: (
(newState as ReindexSourceToTempTransform).outdatedDocuments ?? []
Expand All @@ -75,6 +76,7 @@ export async function migrationStateActionMachine({
(newState as ReindexSourceToTempIndexBulk).transformedDocBatches ?? []
).map((batches) => batches.map((doc) => ({ _id: doc._id }))) as [SavedObjectsRawDoc[]],
},
*/
};

const now = Date.now();
Expand All @@ -93,20 +95,11 @@ export async function migrationStateActionMachine({
const elapsedMs = Date.now() - startTime;
if (finalState.controlState === 'DONE') {
logger.info(logMessagePrefix + `Migration completed after ${Math.round(elapsedMs)}ms`);
if (finalState.sourceIndex != null && Option.isSome(finalState.sourceIndex)) {
return {
status: 'migrated' as const,
destIndex: finalState.targetIndex,
sourceIndex: finalState.sourceIndex.value,
elapsedMs,
};
} else {
return {
status: 'patched' as const,
destIndex: finalState.targetIndex,
elapsedMs,
};
}
return {
status: 'patched' as const,
destIndex: context.indexPrefix,
elapsedMs,
};
} else if (finalState.controlState === 'FATAL') {
try {
await cleanup(context.elasticsearchClient, finalState);
Expand All @@ -124,7 +117,7 @@ export async function migrationStateActionMachine({
}
} catch (e) {
try {
await cleanup(client, lastState);
await cleanup(context.elasticsearchClient, lastState);
} catch (err) {
logger.warn('Failed to cleanup after migrations:', err.message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export { model } from './model';
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import type { Logger } from '@kbn/logging';
import type { DocLinksServiceStart } from '@kbn/core-doc-links-server';
import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server';
import type {
ISavedObjectTypeRegistry,
ISavedObjectsSerializer,
Expand All @@ -33,8 +34,10 @@ export interface RunZeroDowntimeMigrationOpts {
migrationConfig: SavedObjectsMigrationConfigType;
/** docLinks contract to use to link to documentation */
docLinks: DocLinksServiceStart;
/** */
/** SO serializer to use for migration */
serializer: ISavedObjectsSerializer;
/** The client to use for communications with ES */
elasticsearchClient: ElasticsearchClient;
}

export const runZeroDowntimeMigration = async (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { InitState, State } from './types';
import type { MigratorContext } from '../context';

export const createInitialState = (context: MigratorContext): State => {
const initialState: InitState = {
controlState: 'INIT',
logs: [],
retryCount: 0,
retryDelay: 0,
};
return initialState;
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export type {
AllControlStates,
StateFromActionState,
} from './types';
export { createInitialState } from './create_initial_state';

0 comments on commit 445058b

Please sign in to comment.