Skip to content

Commit

Permalink
Avoid caching loggers data
Browse files Browse the repository at this point in the history
  • Loading branch information
V Udayani authored and V Udayani committed Dec 19, 2023
1 parent 8c00f7e commit 59fc4ac
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ public interface STS4LanguageClient extends LanguageClient, SpringIndexLanguageC
@JsonNotification("sts/liveprocess/gcpauses/metrics/updated")
void liveProcessGcPausesMetricsDataUpdated(LiveProcessSummary processKey);

@JsonNotification("sts/liveprocess/loggers/updated")
void liveProcessLoggersDataUpdated(LiveProcessSummary liveProcessSummary);

@JsonNotification("sts/liveprocess/loglevel/updated")
void liveProcessLogLevelUpdated(LiveProcessLoggersSummary liveProcessLoggersSummary);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,6 @@ public void liveProcessMemoryMetricsDataUpdated(LiveProcessSummary processKey) {
public void liveProcessGcPausesMetricsDataUpdated(LiveProcessSummary processKey) {
}

@Override
public void liveProcessLoggersDataUpdated(LiveProcessSummary processKey) {
}

@Override
public void indexUpdated() {
receiveIndexUpdated();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public class SpringProcessCommandHandler {
private static final String COMMAND_GET_METRICS = "sts/livedata/get/metrics";
private static final String COMMAND_GET_REFRESH_METRICS = "sts/livedata/refresh/metrics";
private static final String COMMAND_GET_LOGGERS = "sts/livedata/getLoggers";
private static final String COMMAND_FETCH_LOGGERS_DATA = "sts/livedata/fetch/loggersData";
private static final String COMMAND_CONFIGURE_LOGLEVEL = "sts/livedata/configure/logLevel";

private final SpringProcessConnectorService connectorService;
Expand Down Expand Up @@ -98,11 +97,6 @@ public SpringProcessCommandHandler(SimpleLanguageServer server, SpringProcessCon
});
log.info("Registered command handler: {}",COMMAND_GET_LOGGERS);

server.onCommand(COMMAND_FETCH_LOGGERS_DATA, (params) -> {
return handleFetchLoggersDataRequest(params);
});
log.info("Registered command handler: {}",COMMAND_FETCH_LOGGERS_DATA);

server.onCommand(COMMAND_CONFIGURE_LOGLEVEL, (params) -> {
return configureLogLevel(params);
});
Expand Down Expand Up @@ -341,24 +335,16 @@ private CompletableFuture<Object> handleLiveMetricsProcessRequest(ExecuteCommand


private CompletableFuture<Object> getLoggers(ExecuteCommandParams params) {
SpringProcessLoggersData loggersData = null;
SpringProcessParams springProcessParams = new SpringProcessParams();
springProcessParams.setProcessKey(getProcessKey(params));
springProcessParams.setEndpoint(getArgumentByKey(params, "endpoint"));
if (springProcessParams.getProcessKey() != null) {
connectorService.getLoggers(springProcessParams);
loggersData = connectorService.getLoggers(springProcessParams);
return CompletableFuture.completedFuture(loggersData);
}

return CompletableFuture.completedFuture(null);
}

private CompletableFuture<Object> handleFetchLoggersDataRequest(ExecuteCommandParams params) {
String processKey = getProcessKey(params);
if (processKey != null) {
SpringProcessLoggersData data = connectorService.getLoggersData(processKey);
return CompletableFuture.completedFuture(data.getLoggers());
}

return CompletableFuture.completedFuture(null);
return CompletableFuture.completedFuture(loggersData);
}

private CompletableFuture<Object> configureLogLevel(ExecuteCommandParams params) {
Expand All @@ -368,7 +354,6 @@ private CompletableFuture<Object> configureLogLevel(ExecuteCommandParams params)
args.put("effectiveLevel", getArgumentByKey(params, "effectiveLevel"));
SpringProcessParams springProcessParams = new SpringProcessParams();
springProcessParams.setProcessKey(getProcessKey(params));
springProcessParams.setEndpoint(getArgumentByKey(params, "endpoint"));
springProcessParams.setArgs(args);

if (springProcessParams.getProcessKey() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.livehover.v2;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -126,10 +128,6 @@ public SpringProcessMemoryMetricsLiveData getMemoryMetricsLiveData(String proces
public SpringProcessGcPausesMetricsLiveData getGcPausesMetricsLiveData(String processKey) {
return this.liveDataProvider.getGcPausesMetrics(processKey);
}

public SpringProcessLoggersData getLoggersData(String processKey) {
return this.liveDataProvider.getLoggersData(processKey);
}

public void disconnectProcess(String processKey) {
log.info("disconnect from process: " + processKey);
Expand Down Expand Up @@ -298,19 +296,24 @@ private IndefiniteProgressTask getProgressTask(String prefixId, String title, St
return this.progressService.createIndefiniteProgressTask(prefixId + progressIdKey++, title, message);
}

public void getLoggers(SpringProcessParams springProcessParams) {
public SpringProcessLoggersData getLoggers(SpringProcessParams springProcessParams) {
log.info("get loggers data: " + springProcessParams.getProcessKey());

CompletableFuture<SpringProcessLoggersData> loggerData = new CompletableFuture<>();
SpringProcessConnector connector = this.connectors.get(springProcessParams.getProcessKey());
if (connector != null) {
final IndefiniteProgressTask progressTask = getProgressTask(
"spring-process-connector-service-get-loggers-data" + springProcessParams.getProcessKey(), "Loggers", null);
System.out.println(progressTask);
getLoggersData(progressTask, springProcessParams, connector, 0, TimeUnit.SECONDS, 0);
getLoggersData(progressTask, springProcessParams, connector, loggerData, 0, TimeUnit.SECONDS, 0);
try {
return loggerData.get();
} catch (InterruptedException | ExecutionException e) {
log.error("Failed to fetch loggers data for the process "+ springProcessParams.getProcessKey());
}
}
return null;
}

public void getLoggersData(IndefiniteProgressTask progressTask, SpringProcessParams springProcessParams, SpringProcessConnector connector, long delay, TimeUnit unit, int retryNo) {
public void getLoggersData(IndefiniteProgressTask progressTask, SpringProcessParams springProcessParams, SpringProcessConnector connector, CompletableFuture<SpringProcessLoggersData> loggerData, long delay, TimeUnit unit, int retryNo) {
String processKey = springProcessParams.getProcessKey();
String endpoint = springProcessParams.getEndpoint();

Expand All @@ -325,10 +328,7 @@ public void getLoggersData(IndefiniteProgressTask progressTask, SpringProcessPar
SpringProcessLoggersData loggersData = connector.getLoggers(this.liveDataProvider.getCurrent(processKey));

if (loggersData != null) {
if (!this.liveDataProvider.addLoggers(processKey, loggersData)) {
this.liveDataProvider.updateLoggers(processKey, loggersData);
}

loggerData.complete(loggersData);
this.connectedSuccess.put(processKey, true);
}

Expand All @@ -340,7 +340,7 @@ public void getLoggersData(IndefiniteProgressTask progressTask, SpringProcessPar
log.info("problem occured during process live data refresh", e);

if (retryNo < maxRetryCount && isKnownProcessKey(processKey)) {
getLoggersData(progressTask, springProcessParams, connector, retryDelayInSeconds, TimeUnit.SECONDS,
getLoggersData(progressTask, springProcessParams, connector, loggerData, retryDelayInSeconds, TimeUnit.SECONDS,
retryNo + 1);
}
else {
Expand All @@ -352,6 +352,7 @@ public void getLoggersData(IndefiniteProgressTask progressTask, SpringProcessPar
.error("Failed to refresh live data from process " + processKey + " after retries: " + retryNo, e));

if (!connectedSuccess.containsKey(connector.getProcessKey())) {
loggerData.complete(null);
disconnectProcess(processKey);
}
}
Expand All @@ -369,15 +370,13 @@ public void configureLogLevel(SpringProcessParams springProcessParams) {
if (connector != null) {
final IndefiniteProgressTask progressTask = getProgressTask(
"spring-process-connector-service-configure-log-level" + springProcessParams.getProcessKey(), "Loggers", null);
System.out.println(progressTask);
configureLogLevel(progressTask, springProcessParams, connector, 0, TimeUnit.SECONDS, 0);
}
}

private void configureLogLevel(IndefiniteProgressTask progressTask, SpringProcessParams springProcessParams,
SpringProcessConnector connector, long delay, TimeUnit unit, int retryNo) {
String processKey = springProcessParams.getProcessKey();
String endpoint = springProcessParams.getEndpoint();

String progressMessage = "configure log level for Spring process: " + processKey + " - retry no: " + retryNo;
log.info(progressMessage);
Expand All @@ -386,14 +385,12 @@ private void configureLogLevel(IndefiniteProgressTask progressTask, SpringProces

try {
progressTask.progressEvent(progressMessage);
if(LOGGERS.equals(endpoint)) {
SpringProcessUpdatedLogLevelData springProcessUpdatedLoggersData = connector.configureLogLevel(this.liveDataProvider.getCurrent(processKey), springProcessParams.getArgs());
SpringProcessUpdatedLogLevelData springProcessUpdatedLoggersData = connector.configureLogLevel(this.liveDataProvider.getCurrent(processKey), springProcessParams.getArgs());

if(springProcessUpdatedLoggersData != null) {
this.liveDataProvider.updateLogLevel(processKey, springProcessUpdatedLoggersData);
this.connectedSuccess.put(processKey, true);
}
}
if(springProcessUpdatedLoggersData != null) {
this.liveDataProvider.updateLogLevel(processKey, springProcessUpdatedLoggersData);
this.connectedSuccess.put(processKey, true);
}

progressTask.done();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@ public boolean addGcPausesMetrics(String processKey, SpringProcessGcPausesMetric
}
return oldData == null;
}

public boolean addLoggers(String processKey, SpringProcessLoggersData loggerData) {
SpringProcessLoggersData oldData = this.loggersData.putIfAbsent(processKey, loggerData);
if (oldData == null) {
getClient().liveProcessLoggersDataUpdated(createLoggersSummary(processKey, loggerData));
}
return oldData == null;
}

private STS4LanguageClient getClient() {
STS4LanguageClient client = server.getClient();
Expand Down Expand Up @@ -115,11 +107,6 @@ public void updateGcPausesMetrics(String processKey, SpringProcessGcPausesMetric
getClient().liveProcessGcPausesMetricsDataUpdated(createGcPausesMetricsSummary(processKey, liveData));
}

public void updateLoggers(String processKey, SpringProcessLoggersData loggerData) {
this.loggersData.put(processKey, loggerData);
getClient().liveProcessLoggersDataUpdated(createLoggersSummary(processKey, loggerData));
}

public void updateLogLevel(String processKey, SpringProcessUpdatedLogLevelData updatedLogLevelData) {
getClient().liveProcessLogLevelUpdated(createUpdatedLogLevelSummary(processKey, updatedLogLevelData));
}
Expand Down Expand Up @@ -183,15 +170,6 @@ public static LiveProcessSummary createGcPausesMetricsSummary(String processKey,
p.setPid(liveData.getProcessID());
return p;
}

public static LiveProcessSummary createLoggersSummary(String processKey, SpringProcessLoggersData loggersData) {
LiveProcessSummary p = new LiveProcessSummary();
p.setType(loggersData.getProcessType().jsonName());
p.setProcessKey(processKey);
p.setProcessName(loggersData.getProcessName());
p.setPid(loggersData.getProcessID());
return p;
}

public static LiveProcessLoggersSummary createUpdatedLogLevelSummary(String processKey, SpringProcessUpdatedLogLevelData updatedLogLevelData) {
LiveProcessLoggersSummary p = new LiveProcessLoggersSummary(updatedLogLevelData.getProcessType().jsonName(),
Expand Down
4 changes: 0 additions & 4 deletions vscode-extensions/vscode-spring-boot/lib/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ export namespace SpringIndexUpdatedNotification {
export const type = new NotificationType<void>('spring/index/updated');
}

export namespace LiveProcessLoggersUpdatedNotification {
export const type = new NotificationType<LiveProcess>('sts/liveprocess/loggers/updated');
}

export namespace LiveProcessLogLevelUpdatedNotification {
export const type = new NotificationType<LiveProcessUpdatedLogLevel>('sts/liveprocess/loglevel/updated');
}
50 changes: 40 additions & 10 deletions vscode-extensions/vscode-spring-boot/lib/set-log-levels-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as VSCode from 'vscode';
import { LanguageClient } from "vscode-languageclient/node";
import { ActivatorOptions } from '@pivotal-tools/commons-vscode';
import { LiveProcess, LiveProcessLoggersUpdatedNotification, LiveProcessLogLevelUpdatedNotification, LiveProcessUpdatedLogLevel } from './notification';
import { LiveProcess, LiveProcessLogLevelUpdatedNotification, LiveProcessUpdatedLogLevel } from './notification';

interface ProcessCommandInfo {
processKey : string;
Expand All @@ -26,6 +26,13 @@ export interface LoggersData {
loggers: Loggers;
}

export interface ProcessLoggersData {
loggers: LoggersData;
processID: string;
processName: string;
processType: number;
}

export interface LoggerItem {
logger: Logger;
name: string;
Expand All @@ -48,17 +55,41 @@ async function setLogLevelHandler() {
const picked = await VSCode.window.showQuickPick(choices);
if (picked) {
const chosen = choiceMap.get(picked);
const loggers = await VSCode.commands.executeCommand('sts/livedata/getLoggers', chosen, {"endpoint": "loggers"});
try {
const loggers: ProcessLoggersData = await getLoggers(chosen);
await displayLoggers(loggers, chosen.processKey);
} catch (error) {
VSCode.window.showErrorMessage("Failed to fetch loggers for the process " + chosen.processKey);
}
}
}
}

async function getLoggersList(process: LiveProcess) {
const loggers: LoggersData = await VSCode.commands.executeCommand('sts/livedata/fetch/loggersData', process);
async function getLoggers(processInfo: ProcessCommandInfo): Promise<ProcessLoggersData> {

return new Promise(async (resolve, reject) => {
await VSCode.window.withProgress({
location: VSCode.ProgressLocation.Window,
title: "Fetching Loggers Data for process "+processInfo.processKey,
cancellable: false
}, async (progress) => {
try {
const loggers: ProcessLoggersData = await VSCode.commands.executeCommand('sts/livedata/getLoggers', processInfo, {"endpoint": "loggers"});
progress.report({});
resolve(loggers);
} catch (error) {
reject(error);
}
});
});
}

async function displayLoggers(processLoggersData: ProcessLoggersData, processKey: string) {
let items;
if(loggers) {
items = Object.keys(loggers.loggers).map(packageName => {
const logger: Logger = loggers.loggers[packageName];
const loggersData = processLoggersData.loggers;
if(loggersData.loggers) {
items = Object.keys(loggersData.loggers).map(packageName => {
const logger: Logger = loggersData.loggers[packageName];
const effectiveLevel = logger.effectiveLevel;
const label = packageName + ' (' + effectiveLevel + ')';
return {
Expand All @@ -71,8 +102,8 @@ async function getLoggersList(process: LiveProcess) {
if(items) {
const chosenPackage = await VSCode.window.showQuickPick(items);
if (chosenPackage) {
const chosenlogLevel = await VSCode.window.showQuickPick(loggers.levels);
const changeLogLevel = await VSCode.commands.executeCommand('sts/livedata/configure/logLevel', {"endpoint": "loggers"}, process, chosenPackage, {"configuredLevel":chosenlogLevel});
const chosenlogLevel = await VSCode.window.showQuickPick(loggersData.levels);
await VSCode.commands.executeCommand('sts/livedata/configure/logLevel', {"processKey": processKey}, chosenPackage, {"configuredLevel":chosenlogLevel});
}
}

Expand All @@ -89,7 +120,6 @@ export function activate(
options: ActivatorOptions,
context: VSCode.ExtensionContext
) {
client.onNotification(LiveProcessLoggersUpdatedNotification.type, getLoggersList)
client.onNotification(LiveProcessLogLevelUpdatedNotification.type, logLevelUpdated)
context.subscriptions.push(
VSCode.commands.registerCommand('vscode-spring-boot.set.log-levels', () => {
Expand Down

0 comments on commit 59fc4ac

Please sign in to comment.