-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fleet] Upgrade managed package policies in a background task (#191097)
- Loading branch information
Showing
15 changed files
with
511 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
x-pack/plugins/fleet/server/services/epm/packages/cache.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { | ||
getPackageAssetsMapCache, | ||
getPackageInfoCache, | ||
runWithCache, | ||
setPackageAssetsMapCache, | ||
setPackageInfoCache, | ||
} from './cache'; | ||
|
||
const PKG_NAME = 'test'; | ||
const PKG_VERSION = '1.0.0'; | ||
|
||
describe('EPM CacheSession', () => { | ||
describe('outside of a cache session', () => { | ||
it('should not cache package info', () => { | ||
setPackageInfoCache(PKG_NAME, PKG_VERSION, { | ||
name: 'test', | ||
} as any); | ||
const cache = getPackageInfoCache(PKG_NAME, PKG_VERSION); | ||
expect(cache).toBeUndefined(); | ||
}); | ||
|
||
it('should not cache assetsMap', () => { | ||
setPackageAssetsMapCache(PKG_NAME, PKG_VERSION, new Map()); | ||
const cache = getPackageAssetsMapCache(PKG_NAME, PKG_VERSION); | ||
expect(cache).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('in of a cache session', () => { | ||
it('should cache package info', async () => { | ||
function setCache() { | ||
setPackageInfoCache(PKG_NAME, PKG_VERSION, { | ||
name: 'test', | ||
} as any); | ||
} | ||
function getCache() { | ||
const cache = getPackageInfoCache(PKG_NAME, PKG_VERSION); | ||
expect(cache).toEqual({ name: 'test' }); | ||
} | ||
|
||
await runWithCache(async () => { | ||
setCache(); | ||
getCache(); | ||
}); | ||
}); | ||
|
||
it('should cache assetsMap', async () => { | ||
function setCache() { | ||
const map = new Map(); | ||
map.set('test.yaml', Buffer.from('name: test')); | ||
setPackageAssetsMapCache(PKG_NAME, PKG_VERSION, map); | ||
} | ||
function getCache() { | ||
const cache = getPackageAssetsMapCache(PKG_NAME, PKG_VERSION); | ||
expect(cache).not.toBeUndefined(); | ||
expect(cache?.get('test.yaml')?.toString()).toEqual('name: test'); | ||
} | ||
|
||
await runWithCache(async () => { | ||
setCache(); | ||
getCache(); | ||
}); | ||
}); | ||
}); | ||
}); |
72 changes: 72 additions & 0 deletions
72
x-pack/plugins/fleet/server/services/epm/packages/cache.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { AsyncLocalStorage } from 'async_hooks'; | ||
|
||
import LRUCache from 'lru-cache'; | ||
|
||
import type { AssetsMap } from '../../../../common/types'; | ||
|
||
import type { PackageInfo } from '../../../../common'; | ||
|
||
const cacheStore = new AsyncLocalStorage<CacheSession>(); | ||
|
||
const PACKAGE_INFO_CACHE_SIZE = 20; | ||
const PACKAGE_ASSETS_MAP_CACHE_SIZE = 1; | ||
|
||
class CacheSession { | ||
private _packageInfoCache?: LRUCache<string, PackageInfo>; | ||
|
||
private _packageAssetsMap?: LRUCache<string, AssetsMap>; | ||
|
||
getPackageInfoCache() { | ||
if (!this._packageInfoCache) { | ||
this._packageInfoCache = new LRUCache<string, PackageInfo>({ | ||
max: PACKAGE_INFO_CACHE_SIZE, | ||
}); | ||
} | ||
return this._packageInfoCache; | ||
} | ||
|
||
getPackageAssetsMapCache() { | ||
if (!this._packageAssetsMap) { | ||
this._packageAssetsMap = new LRUCache<string, AssetsMap>({ | ||
max: PACKAGE_ASSETS_MAP_CACHE_SIZE, | ||
}); | ||
} | ||
return this._packageAssetsMap; | ||
} | ||
} | ||
|
||
export function getPackageInfoCache(pkgName: string, pkgVersion: string) { | ||
return cacheStore.getStore()?.getPackageInfoCache()?.get(`${pkgName}:${pkgVersion}`); | ||
} | ||
|
||
export function setPackageInfoCache(pkgName: string, pkgVersion: string, packageInfo: PackageInfo) { | ||
return cacheStore.getStore()?.getPackageInfoCache()?.set(`${pkgName}:${pkgVersion}`, packageInfo); | ||
} | ||
|
||
export function getPackageAssetsMapCache(pkgName: string, pkgVersion: string) { | ||
return cacheStore.getStore()?.getPackageAssetsMapCache()?.get(`${pkgName}:${pkgVersion}`); | ||
} | ||
|
||
export function setPackageAssetsMapCache( | ||
pkgName: string, | ||
pkgVersion: string, | ||
assetsMap: AssetsMap | ||
) { | ||
return cacheStore | ||
.getStore() | ||
?.getPackageAssetsMapCache() | ||
?.set(`${pkgName}:${pkgVersion}`, assetsMap); | ||
} | ||
|
||
export async function runWithCache<T = any>(cb: () => Promise<T>): Promise<T> { | ||
const cache = new CacheSession(); | ||
|
||
return cacheStore.run(cache, cb); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.