FINERACT-2241: New command processing - Cache Management (org.apache.fineract.infrastructure.cache)

This commit is contained in:
viktorpavlenko
2025-06-27 12:49:37 +02:00
committed by Aleksandar Vidakovic
parent ba06234f26
commit 0629bb2d48
12 changed files with 164 additions and 165 deletions
@@ -19,13 +19,8 @@
package org.apache.fineract.infrastructure.cache.api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.PUT;
@@ -33,16 +28,16 @@ import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import java.util.Collection;
import java.util.UUID;
import java.util.function.Supplier;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.commands.domain.CommandWrapper;
import org.apache.fineract.commands.service.CommandWrapperBuilder;
import org.apache.fineract.commands.service.PortfolioCommandSourceWritePlatformService;
import org.apache.fineract.command.core.CommandPipeline;
import org.apache.fineract.infrastructure.cache.command.CacheSwitchCommand;
import org.apache.fineract.infrastructure.cache.data.CacheData;
import org.apache.fineract.infrastructure.cache.data.request.CacheRequest;
import org.apache.fineract.infrastructure.cache.data.CacheSwitchRequest;
import org.apache.fineract.infrastructure.cache.data.CacheSwitchResponse;
import org.apache.fineract.infrastructure.cache.service.RuntimeDelegatingCacheManager;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@@ -50,37 +45,45 @@ import org.springframework.stereotype.Component;
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Component
@Tag(name = "Cache", description = "The following settings are possible for cache:\n" + "\n" + "No Caching: caching turned off\n"
+ "Single node: caching on for single instance deployments of platorm (works for multiple tenants but only one tomcat)\n"
+ "By default caching is set to No Caching. Switching between caches results in the cache been clear e.g. from Single node to No cache and back again would clear down the single node cache.")
@Tag(name = "Cache", description = """
The following settings are possible for cache:
No Caching: caching turned off
Single node: caching on for single instance deployments of platorm (works for multiple tenants but only one tomcat).
By default caching is set to No Caching. Switching between caches results in the cache been clear e.g. from single
node to no cache and back again would clear down the single node cache.
""")
@RequiredArgsConstructor
public class CacheApiResource {
private static final String RESOURCE_NAME_FOR_PERMISSIONS = "CACHE";
private final PlatformSecurityContext context;
private final DefaultToApiJsonSerializer<CacheData> toApiJsonSerializer;
private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService;
@Qualifier("runtimeDelegatingCacheManager")
private final RuntimeDelegatingCacheManager cacheService;
private final CommandPipeline commandPipeline;
@GET
@Operation(summary = "Retrieve Cache Types", description = "Returns the list of caches.\n" + "\n" + "Example Requests:\n" + "\n"
+ "caches")
@Operation(summary = "Retrieve Cache Types", description = """
Returns the list of caches.
Example Requests:
caches
""")
public Collection<CacheData> retrieveAll() {
this.context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSIONS);
return cacheService.retrieveAll();
}
@PUT
@Operation(summary = "Switch Cache", description = "Switches the cache to chosen one.")
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = CacheRequest.class)))
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CacheApiResourceSwagger.PutCachesResponse.class))) })
public CommandProcessingResult switchCache(@Parameter(hidden = true) CacheRequest cacheRequest) {
final CommandWrapper commandRequest = new CommandWrapperBuilder().updateCache()
.withJson(toApiJsonSerializer.serialize(cacheRequest)).build();
public CacheSwitchResponse switchCache(@Valid CacheSwitchRequest request) {
final var command = new CacheSwitchCommand();
return this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
command.setId(UUID.randomUUID());
command.setCreatedAt(DateUtils.getAuditOffsetDateTime());
command.setPayload(request);
final Supplier<CacheSwitchResponse> response = commandPipeline.send(command);
return response.get();
}
}
@@ -0,0 +1,28 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.cache.command;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.fineract.command.core.Command;
import org.apache.fineract.infrastructure.cache.data.CacheSwitchRequest;
@Data
@EqualsAndHashCode(callSuper = true)
public class CacheSwitchCommand extends Command<CacheSwitchRequest> {}
@@ -1,89 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.cache.command;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.commands.annotation.CommandType;
import org.apache.fineract.commands.handler.NewCommandSourceHandler;
import org.apache.fineract.infrastructure.cache.CacheApiConstants;
import org.apache.fineract.infrastructure.cache.domain.CacheType;
import org.apache.fineract.infrastructure.cache.service.CacheWritePlatformService;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.data.ApiParameterError;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder;
import org.apache.fineract.infrastructure.core.data.DataValidatorBuilder;
import org.apache.fineract.infrastructure.core.exception.InvalidJsonException;
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@CommandType(entity = "CACHE", action = "UPDATE")
public class UpdateCacheCommandHandler implements NewCommandSourceHandler {
private final CacheWritePlatformService cacheService;
private static final Set<String> REQUEST_DATA_PARAMETERS = new HashSet<>(Arrays.asList(CacheApiConstants.CACHE_TYPE_PARAMETER));
@Autowired
public UpdateCacheCommandHandler(final CacheWritePlatformService cacheService) {
this.cacheService = cacheService;
}
@Transactional
@Override
public CommandProcessingResult processCommand(final JsonCommand command) {
final String json = command.json();
if (StringUtils.isBlank(json)) {
throw new InvalidJsonException();
}
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
command.checkForUnsupportedParameters(typeOfMap, json, REQUEST_DATA_PARAMETERS);
final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors)
.resource(CacheApiConstants.RESOURCE_NAME.toLowerCase());
final int cacheTypeEnum = command.integerValueSansLocaleOfParameterNamed(CacheApiConstants.CACHE_TYPE_PARAMETER);
baseDataValidator.reset().parameter(CacheApiConstants.CACHE_TYPE_PARAMETER).value(Integer.valueOf(cacheTypeEnum)).notNull()
.isOneOfTheseValues(Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3));
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException(dataValidationErrors);
}
final CacheType cacheType = CacheType.fromInt(cacheTypeEnum);
final Map<String, Object> changes = this.cacheService.switchToCache(cacheType);
return new CommandProcessingResultBuilder().withCommandId(command.commandId()).with(changes).build();
}
}
@@ -18,23 +18,20 @@
*/
package org.apache.fineract.infrastructure.cache.data;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.fineract.infrastructure.core.data.EnumOptionData;
@Builder
@Data
@NoArgsConstructor
@Accessors(chain = true)
@AllArgsConstructor
public final class CacheData {
@SuppressWarnings("unused")
private EnumOptionData cacheType;
@SuppressWarnings("unused")
private boolean enabled;
public static CacheData instance(final EnumOptionData cacheType, final boolean enabled) {
return new CacheData().setCacheType(cacheType).setEnabled(enabled);
}
}
@@ -16,38 +16,25 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.cache.api;
package org.apache.fineract.infrastructure.cache.data;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import java.io.Serial;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Created by sanyam on 28/7/17.
*/
final class CacheApiResourceSwagger {
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CacheSwitchRequest implements Serializable {
private CacheApiResourceSwagger() {
@Serial
private static final long serialVersionUID = 1L;
}
@Schema(description = "PutCachesResponse")
public static final class PutCachesResponse {
private PutCachesResponse() {
}
public static final class PutCachechangesSwagger {
private PutCachechangesSwagger() {
}
@Schema(example = "2")
public Long cacheType;
}
public PutCachechangesSwagger cacheType;
}
@NotNull(message = "{org.apache.fineract.cache.cache-type.not-null}")
private Integer cacheType;
}
@@ -16,13 +16,25 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.cache.data.request;
package org.apache.fineract.infrastructure.cache.data;
import java.io.Serial;
import java.io.Serializable;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
public record CacheRequest(Long cacheType) implements Serializable {
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CacheSwitchResponse implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private Integer cacheType;
private Map<String, Object> changes;
}
@@ -0,0 +1,48 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.cache.handler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.command.core.Command;
import org.apache.fineract.command.core.CommandHandler;
import org.apache.fineract.infrastructure.cache.data.CacheSwitchRequest;
import org.apache.fineract.infrastructure.cache.data.CacheSwitchResponse;
import org.apache.fineract.infrastructure.cache.domain.CacheType;
import org.apache.fineract.infrastructure.cache.service.CacheWritePlatformService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Component
@RequiredArgsConstructor
public class CacheSwitchCommandHandler implements CommandHandler<CacheSwitchRequest, CacheSwitchResponse> {
private final CacheWritePlatformService cacheService;
@Transactional
@Override
public CacheSwitchResponse handle(final Command<CacheSwitchRequest> command) {
var request = command.getPayload();
var cacheType = CacheType.fromInt(request.getCacheType());
var changes = cacheService.switchToCache(cacheType);
return CacheSwitchResponse.builder().changes(changes).cacheType(request.getCacheType()).build();
}
}
@@ -78,8 +78,8 @@ public class RuntimeDelegatingCacheManager implements CacheManager, Initializing
final EnumOptionData noCacheType = CacheEnumerations.cacheType(CacheType.NO_CACHE);
final EnumOptionData singleNodeCacheType = CacheEnumerations.cacheType(CacheType.SINGLE_NODE);
final CacheData noCache = CacheData.instance(noCacheType, noCacheEnabled);
final CacheData singleNodeCache = CacheData.instance(singleNodeCacheType, ehCacheEnabled);
final CacheData noCache = CacheData.builder().cacheType(noCacheType).enabled(noCacheEnabled).build();
final CacheData singleNodeCache = CacheData.builder().cacheType(singleNodeCacheType).enabled(ehCacheEnabled).build();
return Arrays.asList(noCache, singleNodeCache);
}
@@ -19,18 +19,18 @@
package org.apache.fineract.infrastructure.event.external.handler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.command.core.Command;
import org.apache.fineract.command.core.CommandHandler;
import org.apache.fineract.commands.annotation.CommandType;
import org.apache.fineract.infrastructure.event.external.data.ExternalEventConfigurationUpdateRequest;
import org.apache.fineract.infrastructure.event.external.data.ExternalEventConfigurationUpdateResponse;
import org.apache.fineract.infrastructure.event.external.service.ExternalEventConfigurationWritePlatformService;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Component
@RequiredArgsConstructor
@Service
@CommandType(entity = "EXTERNAL_EVENT_CONFIGURATION", action = "UPDATE")
public class ExternalEventConfigurationUpdateHandler
implements CommandHandler<ExternalEventConfigurationUpdateRequest, ExternalEventConfigurationUpdateResponse> {
@@ -142,6 +142,11 @@ public class SecurityConfig {
.hasAnyAuthority("ALL_FUNCTIONS", "ALL_FUNCTIONS_READ", "READ_EXTERNAL_EVENT_CONFIGURATION")
.requestMatchers(antMatcher(HttpMethod.PUT, "/api/*/externalevents/configuration"))
.hasAnyAuthority("ALL_FUNCTIONS", "ALL_FUNCTIONS_WRITE", "UPDATE_EXTERNAL_EVENT_CONFIGURATION")
// cache
.requestMatchers(antMatcher(HttpMethod.GET, "/api/*/caches"))
.hasAnyAuthority("ALL_FUNCTIONS", "ALL_FUNCTIONS_READ", "READ_CACHE")
.requestMatchers(antMatcher(HttpMethod.PUT, "/api/*/caches"))
.hasAnyAuthority("ALL_FUNCTIONS", "ALL_FUNCTIONS_WRITE", "UPDATE_CACHE")
// ...
.requestMatchers(antMatcher(HttpMethod.POST, "/api/*/twofactor/validate")).fullyAuthenticated() //
.requestMatchers(antMatcher("/api/*/twofactor")).fullyAuthenticated() //
@@ -32,3 +32,7 @@ org.apache.fineract.businessdate.locale.not-blank=The parameter 'locale' is mand
# External Events
org.apache.fineract.externalevent.configurations.not-null=The parameter 'externalEventConfigurations' is mandatory: '${validatedValue}'.
# Cache
org.apache.fineract.cache.cache-type.not-null=The parameter 'cacheType' is mandatory.
@@ -32,3 +32,7 @@ org.apache.fineract.businessdate.locale.not-blank=The parameter 'locale' is mand
# External Events
org.apache.fineract.externalevent.configurations.not-null=The parameter 'externalEventConfigurations' is mandatory: '${validatedValue}'.
# Cache
org.apache.fineract.cache.cache-type.not-null=The parameter 'cacheType' is mandatory.