|
| 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