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

EMT-661: use new metadata current #74394

Merged
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
c7c37ff
EMT-661: use new metadata current
nnamdifrankie Aug 5, 2020
b222ae8
EMT-661: fix build
nnamdifrankie Aug 5, 2020
a518df4
EMT-661: fix build for now
nnamdifrankie Aug 5, 2020
c0a4d84
Merge branch 'master' into EMT-661-use-metadata-current-index
nnamdifrankie Aug 6, 2020
3603f5e
EMT-661: update schema and fix build
nnamdifrankie Aug 6, 2020
a356c92
EMT-661: fix test
nnamdifrankie Aug 6, 2020
70c5311
EMT-661: fix build
nnamdifrankie Aug 7, 2020
482c054
EMT-661: increase time for transform
nnamdifrankie Aug 7, 2020
614cfd8
EMT-661: disable frontend test
nnamdifrankie Aug 9, 2020
106628d
EMT-661: update build
nnamdifrankie Aug 10, 2020
30b7e7a
Merge branch 'master' into EMT-661-use-metadata-current-index
nnamdifrankie Aug 10, 2020
d5578b6
EMT-661: fix tests
nnamdifrankie Aug 10, 2020
74c4988
EMT-661: fix test and type check
nnamdifrankie Aug 10, 2020
0ad91e9
EMT-661: add missing file
nnamdifrankie Aug 10, 2020
b56369a
EMT-661: clean up and refactor
nnamdifrankie Aug 10, 2020
eca0a8d
Merge branch 'master' into EMT-661-use-metadata-current-index
nnamdifrankie Aug 13, 2020
edb1d30
more clean up
nnamdifrankie Aug 13, 2020
e09e512
Merge branch 'master' into EMT-661-use-metadata-current-index
elasticmachine Aug 18, 2020
13442a8
Merge branch 'master' into EMT-661-use-metadata-current-index
nnamdifrankie Aug 20, 2020
77e4395
Merge branch 'master' into EMT-661-use-metadata-current-index
nnamdifrankie Aug 29, 2020
d450f9d
EMT-661: add epm first steps.
nnamdifrankie Aug 31, 2020
b0a51a2
EMT-661: remove transform helper
nnamdifrankie Aug 31, 2020
cbf7d2d
EMT-661: fix tests
nnamdifrankie Sep 1, 2020
c21b7c8
EMT-661: fix test
nnamdifrankie Sep 1, 2020
349fcd1
EMT-661: add test for installing transform
nnamdifrankie Sep 2, 2020
4f8a9e6
EMT-661: fix build
nnamdifrankie Sep 3, 2020
d592bcb
EMT-661: fix build
nnamdifrankie Sep 3, 2020
07bc7ba
EMT-661: clean up
nnamdifrankie Sep 3, 2020
b00c65c
EMT-661: clean up
nnamdifrankie Sep 3, 2020
da3ad7c
EMT-661: fix build temp
nnamdifrankie Sep 3, 2020
eb0c4df
EMT-661: fix build
nnamdifrankie Sep 3, 2020
5e84d99
EMT-661: add more test, and remove 400 guard
nnamdifrankie Sep 3, 2020
2786976
Merge branch 'master' into EMT-661-use-metadata-current-index
elasticmachine Sep 3, 2020
a6225eb
EMT-661: fix build for now
nnamdifrankie Sep 3, 2020
7f26338
Merge branch 'master' into EMT-661-use-metadata-current-index
elasticmachine Sep 3, 2020
5e8ad6e
EMT-661: enable no data tests./
nnamdifrankie Sep 3, 2020
c2fae69
EMT-661: move things
nnamdifrankie Sep 3, 2020
cc1d123
EMT-661: clean up code, add more test, review comments.
nnamdifrankie Sep 4, 2020
5dafd4d
EMT-661: add integration test
nnamdifrankie Sep 4, 2020
4affdb1
EMT-661: change index pattern
nnamdifrankie Sep 4, 2020
301a299
EMT-661: more clean up
nnamdifrankie Sep 4, 2020
0d8ccee
EMT-661: fix build
nnamdifrankie Sep 4, 2020
bdf394c
EMT-661: fix transform name.
nnamdifrankie Sep 4, 2020
0bf0fa3
EMT-661: handle when current version is same as previous version
nnamdifrankie Sep 4, 2020
809a3d7
EMT-661: remove delete before put.
nnamdifrankie Sep 5, 2020
f22d28f
Merge branch 'master' into EMT-661-use-metadata-current-index
elasticmachine Sep 5, 2020
1bb921b
EMT-661: trigger build
nnamdifrankie Sep 6, 2020
952824e
EMT-661: skip polling test, should be moved.
nnamdifrankie Sep 6, 2020
1b769c3
Merge branch 'master' into EMT-661-use-metadata-current-index
elasticmachine Sep 7, 2020
51302c7
EMT-661: move position of transform reference in test
nnamdifrankie Sep 8, 2020
2bc5d3a
Merge branch 'master' into EMT-661-use-metadata-current-index
nnamdifrankie Sep 8, 2020
db0b200
EMT-661: Only delete when there are previous installation has a trans…
nnamdifrankie Sep 8, 2020
6e1532d
Merge branch 'master' into EMT-661-use-metadata-current-index
nnamdifrankie Sep 8, 2020
192a1c4
EMT-661: lint
nnamdifrankie Sep 8, 2020
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
1 change: 1 addition & 0 deletions x-pack/plugins/ingest_manager/common/types/models/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export enum ElasticsearchAssetType {
ingestPipeline = 'ingest_pipeline',
indexTemplate = 'index_template',
ilmPolicy = 'ilm_policy',
transform = 'transform',
}

export enum AgentAssetType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const AssetTitleMap: Record<AssetType, string> = {
dashboard: 'Dashboard',
ilm_policy: 'ILM Policy',
ingest_pipeline: 'Ingest Pipeline',
transform: 'Transform',
'index-pattern': 'Index Pattern',
index_template: 'Index Template',
component_template: 'Component Template',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import * as Registry from '../../registry';

export const getAsset = (path: string): Buffer => {
return Registry.getAsset(path);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedObjectsClientContract } from 'kibana/server';

import { saveInstalledEsRefs } from '../../packages/install';
import * as Registry from '../../registry';
import {
Dataset,
ElasticsearchAssetType,
EsAssetReference,
RegistryPackage,
} from '../../../../../common/types/models';
import { CallESAsCurrentUser } from '../../../../types';
import { getInstallation } from '../../packages';
import { deleteTransforms, deleteTransformRefs } from './remove';
import { getAsset } from './common';

interface TransformInstallation {
installationName: string;
content: string;
}

interface TransformPathDataset {
path: string;
dataset: Dataset;
}

export const installTransformForDataset = async (
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI @ruflin

registryPackage: RegistryPackage,
paths: string[],
callCluster: CallESAsCurrentUser,
savedObjectsClient: SavedObjectsClientContract
) => {
const installation = await getInstallation({ savedObjectsClient, pkgName: registryPackage.name });
let previousInstalledTransformEsAssets: EsAssetReference[] = [];
if (installation) {
previousInstalledTransformEsAssets = installation.installed_es.filter(
({ type, id }) => type === ElasticsearchAssetType.transform
);
}

// delete all previous transform
await deleteTransforms(
Copy link
Contributor

Choose a reason for hiding this comment

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

The thing that jumps out at me here is that there are no try {} catch(){} blocks that would allow you to do a "revert" in case of failure. Especially since we are deleting prior transforms, which could then leave the DB in an unstable state. Unless that is something that is already addressed by the stack by having some form of "backout".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, even if I put a try catch I will still have to re-throw the error. As per discussions I have had, the is supposed to be a global catch that will initiate a rollback. My initial plan was to restart the old transform, but that required that transform for the same version be differentiated .

Copy link
Member

Choose a reason for hiding this comment

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

yes, it should be handle globally as otherwise we have a package which is partially new and partially old. Either all the assets need to be new or all need to be old.

callCluster,
previousInstalledTransformEsAssets.map((asset) => asset.id)
);
// install the latest dataset
const datasets = registryPackage.datasets;
if (!datasets?.length) return [];
const installNameSuffix = `${registryPackage.version}`;

const transformPaths = paths.filter((path) => isTransform(path));
let installedTransforms: EsAssetReference[] = [];
if (transformPaths.length > 0) {
const transformPathDatasets = datasets.reduce<TransformPathDataset[]>((acc, dataset) => {
transformPaths.forEach((path) => {
if (isDatasetTransform(path, dataset.path)) {
acc.push({ path, dataset });
}
});
return acc;
}, []);

const transformRefs = transformPathDatasets.reduce<EsAssetReference[]>(
(acc, transformPathDataset) => {
if (transformPathDataset) {
acc.push({
id: getTransformNameForInstallation(transformPathDataset, installNameSuffix),
type: ElasticsearchAssetType.transform,
});
}
return acc;
},
[]
);

// get and save transform refs before installing transforms
await saveInstalledEsRefs(savedObjectsClient, registryPackage.name, transformRefs);

const transforms: TransformInstallation[] = transformPathDatasets.map(
(transformPathDataset: TransformPathDataset) => {
return {
installationName: getTransformNameForInstallation(
transformPathDataset,
installNameSuffix
),
content: getAsset(transformPathDataset.path).toString('utf-8'),
};
}
);

const installationPromises = transforms.map(async (transform) => {
return installTransform({ callCluster, transform });
});

installedTransforms = await Promise.all(installationPromises).then((results) => results.flat());
}

const currentInstallation = await getInstallation({
neptunian marked this conversation as resolved.
Show resolved Hide resolved
savedObjectsClient,
pkgName: registryPackage.name,
});

// remove the saved object reference
await deleteTransformRefs(
savedObjectsClient,
currentInstallation?.installed_es || [],
registryPackage.name,
previousInstalledTransformEsAssets.map((asset) => asset.id),
installedTransforms.map((installed) => installed.id)
);

return installedTransforms;
};

const isTransform = (path: string) => {
const pathParts = Registry.pathParts(path);
return pathParts.type === ElasticsearchAssetType.transform;
};

const isDatasetTransform = (path: string, datasetName: string) => {
const pathParts = Registry.pathParts(path);
return (
!path.endsWith('/') &&
pathParts.type === ElasticsearchAssetType.transform &&
pathParts.dataset !== undefined &&
datasetName === pathParts.dataset
);
};

async function installTransform({
callCluster,
transform,
}: {
callCluster: CallESAsCurrentUser;
transform: TransformInstallation;
}): Promise<EsAssetReference> {
// defer validation on put if the source index is not available
await callCluster('transport.request', {
method: 'PUT',
path: `_transform/${transform.installationName}`,
query: 'defer_validation=true',
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps worth a comment that this is because the source index does not exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added comment.

body: transform.content,
});

await callCluster('transport.request', {
method: 'POST',
path: `_transform/${transform.installationName}/_start`,
});

return { id: transform.installationName, type: ElasticsearchAssetType.transform };
}

const getTransformNameForInstallation = (
transformDataset: TransformPathDataset,
suffix: string
) => {
const filename = transformDataset?.path.split('/')?.pop()?.split('.')[0];
neptunian marked this conversation as resolved.
Show resolved Hide resolved
return `${transformDataset.dataset.type}-${transformDataset.dataset.name}-${filename}-${suffix}`;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedObjectsClientContract } from 'kibana/server';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { savedObjectsClientMock } from '../../../../../../../../src/core/server/saved_objects/service/saved_objects_client.mock';
import { deleteTransformRefs } from './remove';
import { EsAssetReference } from '../../../../../common/types/models';

describe('test transform install', () => {
let savedObjectsClient: jest.Mocked<SavedObjectsClientContract>;
beforeEach(() => {
savedObjectsClient = savedObjectsClientMock.create();
});

test('can delete transform ref and handle duplicate when previous version and current version are the same', async () => {
await deleteTransformRefs(
savedObjectsClient,
[
{ id: 'metrics-endpoint.policy-0.16.0-dev.0', type: 'ingest_pipeline' },
{ id: 'metrics-endpoint.metadata-current-default-0.16.0-dev.0', type: 'transform' },
] as EsAssetReference[],
'endpoint',
['metrics-endpoint.metadata-current-default-0.16.0-dev.0'],
['metrics-endpoint.metadata-current-default-0.16.0-dev.0']
);
expect(savedObjectsClient.update.mock.calls).toEqual([
[
'epm-packages',
'endpoint',
{
installed_es: [
{ id: 'metrics-endpoint.policy-0.16.0-dev.0', type: 'ingest_pipeline' },
{ id: 'metrics-endpoint.metadata-current-default-0.16.0-dev.0', type: 'transform' },
],
},
],
]);
});

test('can delete transform ref when previous version and current version are not the same', async () => {
await deleteTransformRefs(
savedObjectsClient,
[
{ id: 'metrics-endpoint.policy-0.16.0-dev.0', type: 'ingest_pipeline' },
{ id: 'metrics-endpoint.metadata-current-default-0.16.0-dev.0', type: 'transform' },
] as EsAssetReference[],
'endpoint',
['metrics-endpoint.metadata-current-default-0.15.0-dev.0'],
['metrics-endpoint.metadata-current-default-0.16.0-dev.0']
);

expect(savedObjectsClient.update.mock.calls).toEqual([
[
'epm-packages',
'endpoint',
{
installed_es: [
{ id: 'metrics-endpoint.policy-0.16.0-dev.0', type: 'ingest_pipeline' },
{ id: 'metrics-endpoint.metadata-current-default-0.16.0-dev.0', type: 'transform' },
],
},
],
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedObjectsClientContract } from 'kibana/server';
import { CallESAsCurrentUser, ElasticsearchAssetType, EsAssetReference } from '../../../../types';
import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../../../common/constants';

export const stopTransforms = async (transformIds: string[], callCluster: CallESAsCurrentUser) => {
for (const transformId of transformIds) {
await callCluster('transport.request', {
ruflin marked this conversation as resolved.
Show resolved Hide resolved
method: 'POST',
path: `_transform/${transformId}/_stop`,
query: 'force=true',
ignore: [404],
});
}
};

export const deleteTransforms = async (
callCluster: CallESAsCurrentUser,
transformIds: string[]
) => {
await Promise.all(
transformIds.map(async (transformId) => {
await stopTransforms([transformId], callCluster);
await callCluster('transport.request', {
method: 'DELETE',
query: 'force=true',
path: `_transform/${transformId}`,
ignore: [404],
});
})
);
};

export const deleteTransformRefs = async (
savedObjectsClient: SavedObjectsClientContract,
installedEsAssets: EsAssetReference[],
pkgName: string,
installedEsIdToRemove: string[],
currentInstalledEsTransformIds: string[]
) => {
const seen = new Set<string>();
const filteredAssets = installedEsAssets.filter(({ type, id }) => {
if (type !== ElasticsearchAssetType.transform) return true;
const add =
(currentInstalledEsTransformIds.includes(id) || !installedEsIdToRemove.includes(id)) &&
!seen.has(id);
seen.add(id);
return add;
});
return savedObjectsClient.update(PACKAGES_SAVED_OBJECT_TYPE, pkgName, {
installed_es: filteredAssets,
});
};
Loading