Skip to content

Commit 7013b8f

Browse files
committed
ENH 35488718 - [34329628->14.1.1.0.14] Add MBean Operation to Clear Cache (merge 14.1.1.0 -> ce/14.1.1.0 @ 101312)
[git-p4: depot-paths = "//dev/coherence-ce/release/coherence-ce-v14.1.1.0/": change = 101358]
1 parent 5f1ebc7 commit 7013b8f

File tree

11 files changed

+1037
-17
lines changed

11 files changed

+1037
-17
lines changed

prj/coherence-management/src/main/java/com/tangosol/coherence/management/internal/resources/AbstractManagementResource.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
* Copyright (c) 2000, 2022, Oracle and/or its affiliates.
2+
* Copyright (c) 2000, 2023, Oracle and/or its affiliates.
33
*
44
* Licensed under the Universal Permissive License v 1.0 as shown at
5-
* http://oss.oracle.com/licenses/upl.
5+
* https://oss.oracle.com/licenses/upl.
66
*/
77
package com.tangosol.coherence.management.internal.resources;
88

@@ -1850,6 +1850,11 @@ protected MBeanAccessor getMBeanAccessor()
18501850
*/
18511851
public static final String CWEB_APPLICATION_QUERY = ":type=*HttpSessionManager,appId=";
18521852

1853+
/**
1854+
* MBean query to filter out all StorageManager MBean objects in the cluster.
1855+
*/
1856+
public static final String STORAGE_MANAGERS_ALL_QUERY = ":type=StorageManager";
1857+
18531858
/**
18541859
* MBean query to filter out StorageManager MBean of a specific cache and service, running on a specific node.
18551860
*/
@@ -1926,6 +1931,9 @@ protected MBeanAccessor getMBeanAccessor()
19261931
public static final String TIER_BACK = "back";
19271932
public static final String TIER = "tier";
19281933
public static final String OPTIONS = "options";
1934+
public static final String CACHE = "cache";
1935+
public static final String TRUNCATE = "truncate";
1936+
public static final String CLEAR = "clear";
19291937

19301938
/**
19311939
* Map of URL to platform Mbean query.

prj/coherence-management/src/main/java/com/tangosol/coherence/management/internal/resources/ClusterResource.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2000, 2023, Oracle and/or its affiliates.
33
*
44
* Licensed under the Universal Permissive License v 1.0 as shown at
5-
* http://oss.oracle.com/licenses/upl.
5+
* https://oss.oracle.com/licenses/upl.
66
*/
77
package com.tangosol.coherence.management.internal.resources;
88

@@ -346,6 +346,17 @@ public Object getReportersResource()
346346
return new ReportersResource(this);
347347
}
348348

349+
/**
350+
* Sub resource for storage.
351+
*
352+
* @return the storage sub resource
353+
*/
354+
@Path(STORAGE)
355+
public Object getStorageManagersResource()
356+
{
357+
return new StorageManagersResource(this);
358+
}
359+
349360
/**
350361
* Call "diagnostic-cmd/jfrCmd" to perform JFR operation on ClusterMBean.
351362
*
@@ -498,5 +509,5 @@ protected QueryBuilder getManagementQuery()
498509
*/
499510
public static final String TRACING_RATIO = "tracingRatio";
500511

501-
public static final String[] CHILD_LINKS = {SERVICES, CACHES, MEMBERS, MANAGEMENT, JOURNAL, REPORTERS, WEB_APPS};
512+
public static final String[] CHILD_LINKS = {SERVICES, CACHES, MEMBERS, MANAGEMENT, JOURNAL, REPORTERS, WEB_APPS, STORAGE};
502513
}

prj/coherence-management/src/main/java/com/tangosol/coherence/management/internal/resources/ServiceResource.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2000, 2023, Oracle and/or its affiliates.
33
*
44
* Licensed under the Universal Permissive License v 1.0 as shown at
5-
* http://oss.oracle.com/licenses/upl.
5+
* https://oss.oracle.com/licenses/upl.
66
*/
77
package com.tangosol.coherence.management.internal.resources;
88

@@ -221,6 +221,17 @@ public Object getPersistenceResource()
221221
return new PersistenceResource(this);
222222
}
223223

224+
/**
225+
* Sub resource for storage.
226+
*
227+
* @return the storage child resource
228+
*/
229+
@Path(STORAGE)
230+
public Object getStorageManagersResource()
231+
{
232+
return new StorageManagersResource(this);
233+
}
234+
224235
// ----- AbstractManagementResource methods -------------------------------------------
225236

226237
@Override
@@ -296,5 +307,5 @@ protected QueryBuilder getPartitionAssignmentQuery(String sServiceName)
296307

297308
public static final String SCHEDULED_DISTRIBUTIONS = "scheduledDistributions";
298309

299-
public static String[] CHILD_LINKS = {CACHES, MEMBERS, PARTITION};
310+
public static String[] CHILD_LINKS = {CACHES, MEMBERS, PARTITION, STORAGE};
300311
}
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Universal Permissive License v 1.0 as shown at
5+
* https://oss.oracle.com/licenses/upl.
6+
*/
7+
package com.tangosol.coherence.management.internal.resources;
8+
9+
import com.tangosol.coherence.management.internal.Converter;
10+
import com.tangosol.coherence.management.internal.EntityMBeanResponse;
11+
12+
import com.tangosol.net.CacheFactory;
13+
import com.tangosol.net.management.MBeanAccessor.QueryBuilder;
14+
15+
import com.tangosol.util.Filter;
16+
import com.tangosol.util.filter.AlwaysFilter;
17+
18+
import java.net.URI;
19+
20+
import java.util.HashSet;
21+
import java.util.LinkedHashMap;
22+
import java.util.Map;
23+
import java.util.Objects;
24+
import java.util.Set;
25+
26+
import javax.management.MalformedObjectNameException;
27+
import javax.management.ObjectName;
28+
29+
import javax.ws.rs.GET;
30+
import javax.ws.rs.POST;
31+
import javax.ws.rs.Path;
32+
import javax.ws.rs.PathParam;
33+
import javax.ws.rs.Produces;
34+
import javax.ws.rs.QueryParam;
35+
import javax.ws.rs.core.Response;
36+
37+
import static java.lang.String.format;
38+
39+
/**
40+
* Handles management API requests for a storage manager in a service.
41+
*/
42+
public class StorageManagerResource
43+
extends AbstractManagementResource
44+
{
45+
// ----- constructors ---------------------------------------------------
46+
47+
/**
48+
* Construct a StorageManagerResource.
49+
*
50+
* @param resource the {@link AbstractManagementResource} to be used to initialize the context
51+
*/
52+
public StorageManagerResource(AbstractManagementResource resource)
53+
{
54+
super(resource);
55+
}
56+
57+
// ----- GET API --------------------------------------------------------
58+
59+
/**
60+
* Return the aggregated metrics of StorageManagerMBean's for a single cache belonging to a Service.
61+
*
62+
* @param sCacheName the cache name
63+
* @param sRoleName either a regex to be applied against node ids or a role name
64+
* @param sCollector the collector to use instead of the default
65+
*
66+
* @return the response object
67+
*/
68+
@GET
69+
@Produces(MEDIA_TYPES)
70+
public Response get(@PathParam(CACHE_NAME) String sCacheName,
71+
@QueryParam(ROLE_NAME) String sRoleName,
72+
@QueryParam(COLLECTOR) String sCollector)
73+
{
74+
QueryBuilder queryBuilder = getQuery(sCacheName);
75+
76+
// collect attributes from the ObjectNames
77+
Set<String> setObjectNames = getMBeanAccessor().queryKeys(queryBuilder.build());
78+
79+
if (setObjectNames == null || setObjectNames.isEmpty())
80+
{
81+
return Response.status(Response.Status.NOT_FOUND).build();
82+
}
83+
84+
EntityMBeanResponse responseEntity = createResponse(getParentUri(), getCurrentUri(), getLinksFilter());
85+
addObjectNamesToResponse(setObjectNames, responseEntity);
86+
87+
Map<String, Object> mapResponse = responseEntity.toJson();
88+
89+
// aggregate cache and storage metrics into the response, storage manage metrics is always sent along with cache
90+
addAggregatedMetricsToResponseMap(sRoleName, sCollector, queryBuilder, mapResponse);
91+
return response(mapResponse);
92+
}
93+
94+
// ----- POST API -------------------------------------------------------
95+
96+
/**
97+
* Call "resetStatistics" operation on StorageManagerMBean for all members.
98+
*
99+
* @param sCacheName the cache name
100+
*
101+
* @return the response object
102+
*/
103+
@POST
104+
@Produces(MEDIA_TYPES)
105+
public Response resetStatistics(@PathParam(CACHE_NAME) String sCacheName)
106+
{
107+
QueryBuilder bldrQuery = getQuery(sCacheName);
108+
return executeMBeanOperation(bldrQuery, RESET_STATS, null, null);
109+
}
110+
111+
/**
112+
* Call "clearCache" operation on StorageManagerMBean for all members.
113+
*
114+
* @param sCacheName the cache name
115+
*
116+
* @return the response object
117+
*/
118+
@POST
119+
@Path(CLEAR)
120+
@Produces(MEDIA_TYPES)
121+
public Response clearCache(@PathParam(CACHE_NAME) String sCacheName)
122+
{
123+
QueryBuilder bldrQuery = getQuery(sCacheName);
124+
return executeMBeanOperation(bldrQuery, "clearCache", null, null);
125+
}
126+
127+
/**
128+
* Call "truncateCache" operation on StorageManagerMBean for all members.
129+
*
130+
* @param sCacheName the cache name
131+
*
132+
* @return the response object
133+
*/
134+
@POST
135+
@Path(TRUNCATE)
136+
@Produces(MEDIA_TYPES)
137+
public Response truncateCache(@PathParam(CACHE_NAME) String sCacheName)
138+
{
139+
QueryBuilder bldrQuery = getQuery(sCacheName);
140+
return executeMBeanOperation(bldrQuery, "truncateCache", null, null);
141+
}
142+
143+
// ----- AbstractManagementResource methods -------------------------------------------
144+
145+
@Override
146+
protected EntityMBeanResponse getQueryResult(Map mapQuery, Map<String, String> mapArguments, URI uriParent)
147+
{
148+
String sCacheName = mapArguments.get(CACHE);
149+
150+
// collect attributes from the ObjectNames
151+
URI uriSelf = getSubUri(uriParent, sCacheName);
152+
Filter<String> filterLinks = getLinksFilter(mapQuery);
153+
QueryBuilder bldrQuery = getQuery(sCacheName);
154+
Set<String> setObjectNames = getMBeanAccessor().queryKeys(bldrQuery.build());
155+
156+
if (setObjectNames == null || setObjectNames.isEmpty())
157+
{
158+
return null;
159+
}
160+
161+
EntityMBeanResponse responseEntity = createResponse(uriParent, uriSelf, filterLinks);
162+
addObjectNamesToResponse(setObjectNames, responseEntity);
163+
164+
Map<String, Object> mapEntity = responseEntity.getEntity();
165+
addAggregatedMetricsToResponseMap("*", null, bldrQuery, mapEntity);
166+
167+
return responseEntity;
168+
}
169+
170+
// ----- StorageManagerResource methods-------------------------------------------
171+
172+
/**
173+
* MBean query to retrieve StorageManagerMBeans for the provided cache.
174+
*
175+
* @param sCacheName the cache name
176+
*
177+
* @return the MBean query
178+
*/
179+
protected QueryBuilder getQuery(String sCacheName)
180+
{
181+
return createQueryBuilder()
182+
.withBaseQuery(format(STORAGE_MANAGERS_QUERY, sCacheName))
183+
.withService(getService());
184+
}
185+
186+
/**
187+
* Add attributes from the ObjectNames to the given EntityMBeanResponse.
188+
*
189+
* @param setObjectNames the set of ObjectNames from which to add to the response
190+
* @param responseEntity the EntityMBeanResponse to add attributes to
191+
*/
192+
protected void addObjectNamesToResponse(Set<String> setObjectNames, EntityMBeanResponse responseEntity)
193+
{
194+
Filter<String> filterAttributes = getAttributesFilter();
195+
Map<String, Object> mapAttributes = new LinkedHashMap<>();
196+
197+
// return name, service, and node_id if no field is specified
198+
if (filterAttributes instanceof AlwaysFilter)
199+
{
200+
filterAttributes = getAttributesFilter(String.join(",", CACHE, SERVICE, NODE_ID), null);
201+
}
202+
203+
for (String sName : setObjectNames)
204+
{
205+
try
206+
{
207+
ObjectName objectName = new ObjectName(sName);
208+
for (String sKey : objectName.getKeyPropertyList().keySet())
209+
{
210+
if (filterAttributes.evaluate(sKey))
211+
{
212+
Object oValue = Converter.convert(objectName.getKeyProperty(sKey));
213+
Object oCurrent = mapAttributes.get(sKey);
214+
215+
if (oCurrent == null)
216+
{
217+
mapAttributes.put(sKey, oValue);
218+
}
219+
else if (oCurrent instanceof Set)
220+
{
221+
((Set) oCurrent).add(oValue);
222+
}
223+
else if (!Objects.equals(oCurrent, oValue))
224+
{
225+
Set values = new HashSet<>();
226+
values.add(oCurrent);
227+
values.add(oValue);
228+
mapAttributes.put(sKey, values);
229+
}
230+
}
231+
}
232+
}
233+
catch (MalformedObjectNameException e)
234+
{
235+
CacheFactory.log("Exception occurred while creating an ObjectName " +
236+
sName + "\n" + CacheFactory.getStackTrace(e));
237+
}
238+
}
239+
240+
responseEntity.setEntity(mapAttributes);
241+
}
242+
}

0 commit comments

Comments
 (0)