From 337118e0c753ab80b0955f634af1ce0d161781db Mon Sep 17 00:00:00 2001 From: Sonja Krause-Harder Date: Fri, 4 Sep 2020 13:52:23 +0200 Subject: [PATCH 1/3] On uninstall, remove package from cache. --- .../ingest_manager/server/services/epm/packages/remove.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts b/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts index bc71ead34c3d4..786f6daaea9fc 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts @@ -14,6 +14,7 @@ import { deletePipeline } from '../elasticsearch/ingest_pipeline/'; import { installIndexPatterns } from '../kibana/index_pattern/install'; import { packagePolicyService, appContextService } from '../..'; import { splitPkgKey } from '../registry'; +import { cacheDelete } from '../registry/cache'; export async function removeInstallation(options: { savedObjectsClient: SavedObjectsClientContract; @@ -50,6 +51,10 @@ export async function removeInstallation(options: { // could also update with [] or some other state await savedObjectsClient.delete(PACKAGES_SAVED_OBJECT_TYPE, pkgName); + // remove the package archive from the cache so that a reinstall fetches a fresh copy from the + // registry + cacheDelete(pkgkey); + // successful delete's in SO client return {}. return something more useful return installedAssets; } From 550356fea2aba44c39caeb2cfabcef42d7ea4ed5 Mon Sep 17 00:00:00 2001 From: Sonja Krause-Harder Date: Mon, 7 Sep 2020 15:03:19 +0200 Subject: [PATCH 2/3] Really remove package archive and contents from cache. --- .../server/services/epm/packages/remove.ts | 12 +++++++----- .../server/services/epm/registry/cache.ts | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts b/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts index 786f6daaea9fc..b8334a6740dbb 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts @@ -14,7 +14,8 @@ import { deletePipeline } from '../elasticsearch/ingest_pipeline/'; import { installIndexPatterns } from '../kibana/index_pattern/install'; import { packagePolicyService, appContextService } from '../..'; import { splitPkgKey } from '../registry'; -import { cacheDelete } from '../registry/cache'; +import { deletePackageCache } from '../registry/cache'; +import * as Registry from '../registry'; export async function removeInstallation(options: { savedObjectsClient: SavedObjectsClientContract; @@ -23,7 +24,7 @@ export async function removeInstallation(options: { }): Promise { const { savedObjectsClient, pkgkey, callCluster } = options; // TODO: the epm api should change to /name/version so we don't need to do this - const { pkgName } = splitPkgKey(pkgkey); + const { pkgName, pkgVersion } = splitPkgKey(pkgkey); const installation = await getInstallation({ savedObjectsClient, pkgName }); if (!installation) throw Boom.badRequest(`${pkgName} is not installed`); if (installation.removable === false) @@ -51,9 +52,10 @@ export async function removeInstallation(options: { // could also update with [] or some other state await savedObjectsClient.delete(PACKAGES_SAVED_OBJECT_TYPE, pkgName); - // remove the package archive from the cache so that a reinstall fetches a fresh copy from the - // registry - cacheDelete(pkgkey); + // remove the package archive and its contents from the cache so that a reinstall fetches + // a fresh copy from the registry + const paths = await Registry.getArchiveInfo(pkgName, pkgVersion); + deletePackageCache(pkgName, pkgVersion, paths); // successful delete's in SO client return {}. return something more useful return installedAssets; diff --git a/x-pack/plugins/ingest_manager/server/services/epm/registry/cache.ts b/x-pack/plugins/ingest_manager/server/services/epm/registry/cache.ts index e9c8317a6251d..017461a4c1dea 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/registry/cache.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/registry/cache.ts @@ -18,3 +18,21 @@ export const getArchiveLocation = (name: string, version: string) => export const setArchiveLocation = (name: string, version: string, location: string) => archiveLocationCache.set(pkgToPkgKey({ name, version }), location); + +export const deleteArchiveLocation = (name: string, version: string) => + archiveLocationCache.delete(pkgToPkgKey({ name, version })); + +export const deletePackageCache = (name: string, version: string, paths: string[]) => { + const archiveLocation = getArchiveLocation(name, version); + if (archiveLocation) { + // delete cached archive + cacheDelete(archiveLocation); + + // delete cached archive location + deleteArchiveLocation(name, version); + } + // delete cached archive contents + // this has been populated in Registry.getArchiveInfo() + // TODO: move cache population and cache cleanup closer together? + paths.forEach((path) => cacheDelete(path)); +}; From 897ea53dfa96407ce775fa3dfad5dbc4c6cd4501 Mon Sep 17 00:00:00 2001 From: Sonja Krause-Harder Date: Tue, 8 Sep 2020 11:25:32 +0200 Subject: [PATCH 3/3] Refactor * move deletePackageCache to registry/index.ts * clean up imports --- .../server/services/epm/packages/remove.ts | 6 ++--- .../server/services/epm/registry/cache.ts | 15 ------------ .../server/services/epm/registry/index.ts | 24 ++++++++++++++++++- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts b/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts index b8334a6740dbb..71eee1ee82c90 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts @@ -13,9 +13,7 @@ import { getInstallation, savedObjectTypes } from './index'; import { deletePipeline } from '../elasticsearch/ingest_pipeline/'; import { installIndexPatterns } from '../kibana/index_pattern/install'; import { packagePolicyService, appContextService } from '../..'; -import { splitPkgKey } from '../registry'; -import { deletePackageCache } from '../registry/cache'; -import * as Registry from '../registry'; +import { splitPkgKey, deletePackageCache, getArchiveInfo } from '../registry'; export async function removeInstallation(options: { savedObjectsClient: SavedObjectsClientContract; @@ -54,7 +52,7 @@ export async function removeInstallation(options: { // remove the package archive and its contents from the cache so that a reinstall fetches // a fresh copy from the registry - const paths = await Registry.getArchiveInfo(pkgName, pkgVersion); + const paths = await getArchiveInfo(pkgName, pkgVersion); deletePackageCache(pkgName, pkgVersion, paths); // successful delete's in SO client return {}. return something more useful diff --git a/x-pack/plugins/ingest_manager/server/services/epm/registry/cache.ts b/x-pack/plugins/ingest_manager/server/services/epm/registry/cache.ts index 017461a4c1dea..b7c1e8c2069d6 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/registry/cache.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/registry/cache.ts @@ -21,18 +21,3 @@ export const setArchiveLocation = (name: string, version: string, location: stri export const deleteArchiveLocation = (name: string, version: string) => archiveLocationCache.delete(pkgToPkgKey({ name, version })); - -export const deletePackageCache = (name: string, version: string, paths: string[]) => { - const archiveLocation = getArchiveLocation(name, version); - if (archiveLocation) { - // delete cached archive - cacheDelete(archiveLocation); - - // delete cached archive location - deleteArchiveLocation(name, version); - } - // delete cached archive contents - // this has been populated in Registry.getArchiveInfo() - // TODO: move cache population and cache cleanup closer together? - paths.forEach((path) => cacheDelete(path)); -}; diff --git a/x-pack/plugins/ingest_manager/server/services/epm/registry/index.ts b/x-pack/plugins/ingest_manager/server/services/epm/registry/index.ts index 61c8cd4aabb7b..96f7530641390 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/registry/index.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/registry/index.ts @@ -17,7 +17,15 @@ import { RegistrySearchResults, RegistrySearchResult, } from '../../../types'; -import { cacheGet, cacheSet, cacheHas, getArchiveLocation, setArchiveLocation } from './cache'; +import { + cacheGet, + cacheSet, + cacheDelete, + cacheHas, + getArchiveLocation, + setArchiveLocation, + deleteArchiveLocation, +} from './cache'; import { ArchiveEntry, untarBuffer, unzipBuffer } from './extract'; import { fetchUrl, getResponse, getResponseStream } from './requests'; import { streamToBuffer } from './streams'; @@ -241,3 +249,17 @@ export function groupPathsByService(paths: string[]): AssetsGroupedByServiceByTy // elasticsearch: assets.elasticsearch, }; } + +export const deletePackageCache = (name: string, version: string, paths: string[]) => { + const archiveLocation = getArchiveLocation(name, version); + if (archiveLocation) { + // delete cached archive + cacheDelete(archiveLocation); + + // delete cached archive location + deleteArchiveLocation(name, version); + } + // delete cached archive contents + // this has been populated in Registry.getArchiveInfo() + paths.forEach((path) => cacheDelete(path)); +};