Skip to content

Commit f2c2b02

Browse files
authored
Add clear and truncate cache operations to VisualVM (#68)
* Add clear and truncate cache operations to VisualVM
1 parent 1cc348e commit f2c2b02

File tree

5 files changed

+116
-16
lines changed

5 files changed

+116
-16
lines changed

coherence-visualvm-plugin/src/main/java/com/oracle/coherence/plugin/visualvm/helper/HttpRequestSender.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2023 Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,6 +32,7 @@
3232
import com.fasterxml.jackson.databind.node.ObjectNode;
3333

3434
import com.oracle.coherence.plugin.visualvm.GlobalPreferences;
35+
import com.oracle.coherence.plugin.visualvm.panel.CoherenceCachePanel;
3536
import com.oracle.coherence.plugin.visualvm.panel.CoherencePersistencePanel;
3637

3738
import java.io.IOException;
@@ -273,6 +274,20 @@ public Set<ObjectName> getAllCacheMembers()
273274
return setObjectNames;
274275
}
275276

277+
@Override
278+
public void invokeStorageManagerOperation(String sService, String sCacheName, String sOperation)
279+
throws Exception
280+
{
281+
URLBuilder urlBuilder = getBasePath()
282+
.addPathSegment(SERVICES)
283+
.addPathSegment(encodeServiceName(sService))
284+
.addPathSegment("storage")
285+
.addPathSegment(encodeServiceName(sCacheName))
286+
.addPathSegment(sOperation.equals(CoherenceCachePanel.CLEAR) ? "clear" : "truncate");
287+
288+
sendPostRequest(urlBuilder);
289+
}
290+
276291
@Override
277292
public Set<ObjectName> getAllJournalMembers(String sJournalType)
278293
throws Exception

coherence-visualvm-plugin/src/main/java/com/oracle/coherence/plugin/visualvm/helper/JMXRequestSender.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2023 Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -113,6 +113,18 @@ public Set<ObjectName> getAllCacheMembers()
113113
return f_connection.queryNames(new ObjectName("Coherence:type=Cache,*"), null);
114114
}
115115

116+
@Override
117+
public void invokeStorageManagerOperation(String sService, String sCacheName, String sOperation)
118+
throws Exception
119+
{
120+
ObjectName objectName = new ObjectName("Coherence:type=StorageManager,service=" + sService + ",cache=" + sCacheName + ",*");
121+
122+
Set<ObjectName> setResult = getCompleteObjectName(objectName);
123+
String sFQN = getFirstResult(setResult);
124+
125+
invoke(new ObjectName(sFQN), sOperation, new Object[]{}, new String[]{});
126+
}
127+
116128
@Override
117129
public Set<ObjectName> getAllJournalMembers(String sJournalType)
118130
throws Exception

coherence-visualvm-plugin/src/main/java/com/oracle/coherence/plugin/visualvm/helper/RequestSender.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ AttributeList getAttributes(ObjectName objectName, String[] asAttribute)
9090
Set<ObjectName> getAllCacheMembers()
9191
throws Exception;
9292

93+
/**
94+
* Invoke a Storage Manager operation.
95+
*
96+
* @param sService the name of the service
97+
* @param sCacheName the name of the cache
98+
* @param sOperation the name of the operation
99+
*
100+
* @throws Exception in case of errors
101+
*/
102+
void invokeStorageManagerOperation(String sService, String sCacheName, String sOperation)
103+
throws Exception;
104+
93105
/**
94106
* Return the list of all journal member ObjectNames in the cluster.
95107
*

coherence-visualvm-plugin/src/main/java/com/oracle/coherence/plugin/visualvm/panel/CoherenceCachePanel.java

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525

2626
package com.oracle.coherence.plugin.visualvm.panel;
2727

28-
import com.oracle.coherence.plugin.visualvm.GlobalPreferences;
2928
import com.oracle.coherence.plugin.visualvm.Localization;
3029
import com.oracle.coherence.plugin.visualvm.helper.DialogHelper;
3130
import com.oracle.coherence.plugin.visualvm.helper.GraphHelper;
3231
import com.oracle.coherence.plugin.visualvm.helper.HttpRequestSender;
3332
import com.oracle.coherence.plugin.visualvm.helper.RenderHelper;
3433
import com.oracle.coherence.plugin.visualvm.helper.RequestSender;
3534
import com.oracle.coherence.plugin.visualvm.panel.util.MenuOption;
35+
import com.oracle.coherence.plugin.visualvm.panel.util.SeparatorMenuOption;
3636
import com.oracle.coherence.plugin.visualvm.tablemodel.CacheDetailTableModel;
3737
import com.oracle.coherence.plugin.visualvm.tablemodel.CacheStorageManagerTableModel;
3838
import com.oracle.coherence.plugin.visualvm.tablemodel.CacheTableModel;
@@ -206,16 +206,14 @@ public CoherenceCachePanel(VisualVMModel model)
206206
table.setIntercellSpacing(new Dimension(6, 3));
207207
table.setRowHeight(table.getRowHeight() + 4);
208208

209-
// experimental heat map
210-
if (GlobalPreferences.sharedInstance().isHeatMapEnabled())
211-
{
212-
table.setMenuOptions(
213-
new MenuOption[]{new ShowHeatMapMenuOption(model, m_requestSender,
214-
table,
215-
ShowHeatMapMenuOption.TYPE_SIZE),
216-
new ShowHeatMapMenuOption(model, m_requestSender, table,
217-
ShowHeatMapMenuOption.TYPE_MEMORY)});
218-
}
209+
table.setMenuOptions(
210+
new MenuOption[]{
211+
new ShowHeatMapMenuOption(model, m_requestSender, table, ShowHeatMapMenuOption.TYPE_SIZE),
212+
new ShowHeatMapMenuOption(model, m_requestSender, table, ShowHeatMapMenuOption.TYPE_MEMORY),
213+
new SeparatorMenuOption(model, m_requestSender, table),
214+
new InvokeCacheOperationMenuOpen(model, m_requestSender, table, TRUNCATE),
215+
new InvokeCacheOperationMenuOpen(model, m_requestSender, table, CLEAR)
216+
});
219217

220218
setTablePadding(f_tableDetail);
221219
f_tableDetail.setMenuOptions(new MenuOption[] {new ShowDetailMenuOption(model, f_tableDetail, SELECTED_CACHE)});
@@ -673,14 +671,69 @@ public void actionPerformed(ActionEvent e)
673671
* Menu option description.
674672
*/
675673
private final String f_sMenuItem;
674+
}
675+
676+
/**
677+
* Menu option to invoke clear or truncate against a cache. Only available in most recent versions of Coherence.
678+
*/
679+
protected class InvokeCacheOperationMenuOpen
680+
extends AbstractMenuOption
681+
{
682+
// ----- constructors -----------------------------------------------
683+
684+
public InvokeCacheOperationMenuOpen(VisualVMModel model, RequestSender requestSender,
685+
ExportableJTable jtable, String sOperation)
686+
{
687+
super(model, requestSender, jtable);
688+
f_sOperation = sOperation;
689+
}
690+
691+
@Override
692+
public String getMenuItem()
693+
{
694+
return getLocalizedText(f_sOperation.equals(TRUNCATE) ? "LBL_truncate" : "LBL_clear");
695+
}
696+
697+
@Override
698+
public void actionPerformed(ActionEvent e)
699+
{
700+
int nRow = getSelectedRow();
676701

702+
if (nRow == -1)
703+
{
704+
DialogHelper.showInfoDialog(Localization.getLocalText("LBL_must_select_row"));
705+
}
706+
else
707+
{
708+
Pair<String, String> selectedCache = f_model.getSelectedCache();
709+
String sQuestion = Localization.getLocalText("LBL_confirm_cache_operation", f_sOperation, selectedCache.toString());
677710

711+
if (!DialogHelper.showConfirmDialog(sQuestion))
712+
{
713+
return;
714+
}
715+
try
716+
{
717+
m_requestSender.invokeStorageManagerOperation(selectedCache.getX(), selectedCache.getY(), f_sOperation);
718+
showMessageDialog(Localization.getLocalText("LBL_result"), Localization.getLocalText("LBL_cache_operation_completed", f_sOperation),
719+
JOptionPane.INFORMATION_MESSAGE);
720+
}
721+
catch (Exception ee)
722+
{
723+
showMessageDialog(Localization.getLocalText("ERR_cannot_run_cache_operation", selectedCache.toString()),
724+
ee.getMessage() + "\n" + ee.getCause(), JOptionPane.ERROR_MESSAGE);
725+
}
726+
}
727+
}
728+
729+
// ----- data members ------------------------------------------------
730+
731+
private final String f_sOperation;
678732
}
679733

680734
/**
681-
* An experimental menu option to display a heat map for the cache sizes or
682-
* primary storage used. To enable, set the following system property<br>
683-
* coherence.jvisualvm.heatmap.enabled=true
735+
* A menu option to display a heat map for the cache sizes or
736+
* primary storage used.
684737
*/
685738
protected class ShowHeatMapMenuOption
686739
extends AbstractMenuOption
@@ -1072,6 +1125,9 @@ private Color getColour()
10721125

10731126
private static final long serialVersionUID = -7612569043492412496L;
10741127

1128+
public static final String TRUNCATE = "truncateCache";
1129+
public static final String CLEAR = "clearCache";
1130+
10751131
// ----- data members ---------------------------------------------------
10761132

10771133
/**

coherence-visualvm-plugin/src/main/resources/com/oracle/coherence/plugin/visualvm/Bundle.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ LBL_title_memory_heat_map=Primary Memory Heat Map for All Caches
9393
LBL_no_caches=No caches are defined or all caches have zero size. Unable to create Heat Map.
9494
LBL_no_data=You have selected a new row, please wait for the data to be refreshed and try viewing the heat map again.
9595
LBL_index_info=Index information
96+
LBL_truncate=Truncate Cache
97+
LBL_clear=Clear Cache
98+
LBL_confirm_cache_operation=WARNING: This operation will remove the cache contents.\nAre you sure you want to perform operation {0} against cache {1}?
99+
ERR_cannot_run_cache_operation=Unable to run this operation against the cache {0}
100+
LBL_cache_operation_completed=Operation {0} completed
96101

97102
# CoherenceTopicPanel
98103
LBL_total_topics=Total Topics

0 commit comments

Comments
 (0)