diff --git a/x-pack/plugins/profiling/common/index.ts b/x-pack/plugins/profiling/common/index.ts index 01994865abafe..5c0e469585d53 100644 --- a/x-pack/plugins/profiling/common/index.ts +++ b/x-pack/plugins/profiling/common/index.ts @@ -27,6 +27,8 @@ export function getRoutePaths() { TopNThreads: `${BASE_ROUTE_PATH}/topn/threads`, TopNTraces: `${BASE_ROUTE_PATH}/topn/traces`, Flamechart: `${BASE_ROUTE_PATH}/flamechart`, + CacheExecutables: `${BASE_ROUTE_PATH}/cache/executables`, + CacheStackFrames: `${BASE_ROUTE_PATH}/cache/stackframes`, }; } diff --git a/x-pack/plugins/profiling/server/routes/cache.ts b/x-pack/plugins/profiling/server/routes/cache.ts new file mode 100644 index 0000000000000..21ba32667e55f --- /dev/null +++ b/x-pack/plugins/profiling/server/routes/cache.ts @@ -0,0 +1,53 @@ +/* + * 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 { RouteRegisterParameters } from '.'; +import { getRoutePaths } from '../../common'; +import { handleRouteHandlerError } from '../utils/handle_route_error_handler'; +import { clearExecutableCache, clearStackFrameCache } from './stacktrace'; + +export function registerCacheExecutablesRoute({ router, logger }: RouteRegisterParameters) { + const paths = getRoutePaths(); + router.delete( + { + path: paths.CacheExecutables, + validate: {}, + }, + async (context, request, response) => { + try { + logger.info(`clearing executable cache`); + const numDeleted = clearExecutableCache(); + logger.info(`removed ${numDeleted} executables from cache`); + + return response.ok({}); + } catch (error) { + return handleRouteHandlerError({ error, logger, response }); + } + } + ); +} + +export function registerCacheStackFramesRoute({ router, logger }: RouteRegisterParameters) { + const paths = getRoutePaths(); + router.delete( + { + path: paths.CacheStackFrames, + validate: {}, + }, + async (context, request, response) => { + try { + logger.info(`clearing stackframe cache`); + const numDeleted = clearStackFrameCache(); + logger.info(`removed ${numDeleted} stackframes from cache`); + + return response.ok({}); + } catch (error) { + return handleRouteHandlerError({ error, logger, response }); + } + } + ); +} diff --git a/x-pack/plugins/profiling/server/routes/index.ts b/x-pack/plugins/profiling/server/routes/index.ts index b6bd705ba0e07..a3692b213a0af 100644 --- a/x-pack/plugins/profiling/server/routes/index.ts +++ b/x-pack/plugins/profiling/server/routes/index.ts @@ -12,6 +12,8 @@ import { ProfilingRequestHandlerContext, } from '../types'; +import { registerCacheExecutablesRoute, registerCacheStackFramesRoute } from './cache'; + import { registerFlameChartSearchRoute } from './flamechart'; import { registerTopNFunctionsSearchRoute } from './functions'; @@ -33,6 +35,8 @@ export interface RouteRegisterParameters { } export function registerRoutes(params: RouteRegisterParameters) { + registerCacheExecutablesRoute(params); + registerCacheStackFramesRoute(params); registerFlameChartSearchRoute(params); registerTopNFunctionsSearchRoute(params); registerTraceEventsTopNContainersSearchRoute(params); diff --git a/x-pack/plugins/profiling/server/routes/stacktrace.ts b/x-pack/plugins/profiling/server/routes/stacktrace.ts index 1dc040c3d3f19..558c560f62192 100644 --- a/x-pack/plugins/profiling/server/routes/stacktrace.ts +++ b/x-pack/plugins/profiling/server/routes/stacktrace.ts @@ -278,6 +278,13 @@ const frameLRU = new LRUCache({ maxAge: CACHE_TTL_MILLISECONDS, }); +// clearStackFrameCache clears the entire cache and returns the number of deleted items +export function clearStackFrameCache(): number { + const numDeleted = frameLRU.length; + frameLRU.reset(); + return numDeleted; +} + export async function mgetStackFrames({ logger, client, @@ -350,6 +357,13 @@ const executableLRU = new LRUCache({ maxAge: CACHE_TTL_MILLISECONDS, }); +// clearExecutableCache clears the entire cache and returns the number of deleted items +export function clearExecutableCache(): number { + const numDeleted = executableLRU.length; + executableLRU.reset(); + return numDeleted; +} + export async function mgetExecutables({ logger, client,