[8.6] [Profiling] Add APIs to clear frame and executable caches (#145499) (#145525)

# Backport

This will backport the following commits from `main` to `8.6`:
- [[Profiling] Add APIs to clear frame and executable caches
(#145499)](https://github.com/elastic/kibana/pull/145499)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Joseph
Crail","email":"joseph.crail@elastic.co"},"sourceCommit":{"committedDate":"2022-11-17T10:04:58Z","message":"[Profiling]
Add APIs to clear frame and executable caches (#145499)\n\nThis PR adds
two endpoints to clear each respective cache:\r\n\r\n* `DELETE
{BASE_KIBANA_PATH}/api/profiling/v1/cache/executables`\r\n* `DELETE
{BASE_KIBANA_PATH}/api/profiling/v1/cache/stackframes`\r\n\r\nRelated to
https://github.com/elastic/prodfiler/issues/2759\r\n\r\n#### Design
choices\r\n\r\n1. The `DELETE` method was chosen instead of `PUT` or
`POST` since the\r\ngiven semantics of `DELETE` matches the expected
behavior for the\r\nrelated issue.\r\n2. Each endpoint will remove all
items from the respective cache. A\r\nseparate API for each cache allows
us to selectively clear the necessary\r\ncache without the downsides of
a catch-all endpoint to clear all caches.\r\nThis gives us the
flexibility to add more endpoints if needed. Given the\r\ntradeoff
between complexity now or later, it was decided to implement\r\ngeneral
invalidation now with the option to invalidate specific
items\r\nlater.\r\n3. The RESTful design allows us to clear specific
items later (e.g.\r\n`DELETE
{BASE_KIBANA_PATH}/api/profiling/v1/cache/executables/{ID}`\r\ncould
clear only executable `ID` from the cache).\r\n4. Each endpoint returns
an empty payload on success. However, the\r\nKibana logs reflect the
actions taken and how many cache items were\r\naffected.\r\n5. The
stacktrace cache was ignored since it was not affected by
symbols\r\nwritten to Elasticsearch.\r\n6. Each endpoint is not directly
accessible from UI since it is expected\r\nthat the endpoints will be
called outside of Kibana. However, the\r\nendpoints can be called
manually from the browser's
console.","sha":"642ab74cfc1a4ef5c0a2dfdadf00bd3792f780a7","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v8.6.0","v8.7.0","v8.5.2"],"number":145499,"url":"https://github.com/elastic/kibana/pull/145499","mergeCommit":{"message":"[Profiling]
Add APIs to clear frame and executable caches (#145499)\n\nThis PR adds
two endpoints to clear each respective cache:\r\n\r\n* `DELETE
{BASE_KIBANA_PATH}/api/profiling/v1/cache/executables`\r\n* `DELETE
{BASE_KIBANA_PATH}/api/profiling/v1/cache/stackframes`\r\n\r\nRelated to
https://github.com/elastic/prodfiler/issues/2759\r\n\r\n#### Design
choices\r\n\r\n1. The `DELETE` method was chosen instead of `PUT` or
`POST` since the\r\ngiven semantics of `DELETE` matches the expected
behavior for the\r\nrelated issue.\r\n2. Each endpoint will remove all
items from the respective cache. A\r\nseparate API for each cache allows
us to selectively clear the necessary\r\ncache without the downsides of
a catch-all endpoint to clear all caches.\r\nThis gives us the
flexibility to add more endpoints if needed. Given the\r\ntradeoff
between complexity now or later, it was decided to implement\r\ngeneral
invalidation now with the option to invalidate specific
items\r\nlater.\r\n3. The RESTful design allows us to clear specific
items later (e.g.\r\n`DELETE
{BASE_KIBANA_PATH}/api/profiling/v1/cache/executables/{ID}`\r\ncould
clear only executable `ID` from the cache).\r\n4. Each endpoint returns
an empty payload on success. However, the\r\nKibana logs reflect the
actions taken and how many cache items were\r\naffected.\r\n5. The
stacktrace cache was ignored since it was not affected by
symbols\r\nwritten to Elasticsearch.\r\n6. Each endpoint is not directly
accessible from UI since it is expected\r\nthat the endpoints will be
called outside of Kibana. However, the\r\nendpoints can be called
manually from the browser's
console.","sha":"642ab74cfc1a4ef5c0a2dfdadf00bd3792f780a7"}},"sourceBranch":"main","suggestedTargetBranches":["8.6","8.5"],"targetPullRequestStates":[{"branch":"8.6","label":"v8.6.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.7.0","labelRegex":"^v8.7.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/145499","number":145499,"mergeCommit":{"message":"[Profiling]
Add APIs to clear frame and executable caches (#145499)\n\nThis PR adds
two endpoints to clear each respective cache:\r\n\r\n* `DELETE
{BASE_KIBANA_PATH}/api/profiling/v1/cache/executables`\r\n* `DELETE
{BASE_KIBANA_PATH}/api/profiling/v1/cache/stackframes`\r\n\r\nRelated to
https://github.com/elastic/prodfiler/issues/2759\r\n\r\n#### Design
choices\r\n\r\n1. The `DELETE` method was chosen instead of `PUT` or
`POST` since the\r\ngiven semantics of `DELETE` matches the expected
behavior for the\r\nrelated issue.\r\n2. Each endpoint will remove all
items from the respective cache. A\r\nseparate API for each cache allows
us to selectively clear the necessary\r\ncache without the downsides of
a catch-all endpoint to clear all caches.\r\nThis gives us the
flexibility to add more endpoints if needed. Given the\r\ntradeoff
between complexity now or later, it was decided to implement\r\ngeneral
invalidation now with the option to invalidate specific
items\r\nlater.\r\n3. The RESTful design allows us to clear specific
items later (e.g.\r\n`DELETE
{BASE_KIBANA_PATH}/api/profiling/v1/cache/executables/{ID}`\r\ncould
clear only executable `ID` from the cache).\r\n4. Each endpoint returns
an empty payload on success. However, the\r\nKibana logs reflect the
actions taken and how many cache items were\r\naffected.\r\n5. The
stacktrace cache was ignored since it was not affected by
symbols\r\nwritten to Elasticsearch.\r\n6. Each endpoint is not directly
accessible from UI since it is expected\r\nthat the endpoints will be
called outside of Kibana. However, the\r\nendpoints can be called
manually from the browser's
console.","sha":"642ab74cfc1a4ef5c0a2dfdadf00bd3792f780a7"}},{"branch":"8.5","label":"v8.5.2","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Joseph Crail <joseph.crail@elastic.co>
This commit is contained in:
Kibana Machine 2022-11-17 06:14:31 -05:00 committed by GitHub
parent cd4e770896
commit 444a45435d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 0 deletions

View file

@ -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`,
};
}

View file

@ -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 });
}
}
);
}

View file

@ -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);

View file

@ -278,6 +278,13 @@ const frameLRU = new LRUCache<StackFrameID, StackFrame>({
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<FileID, Executable>({
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,