Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(QTDI-1188): call reset cache endpoint #989

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (C) 2006-2025 Talend Inc. - www.talend.com
*
* Licensed 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.talend.sdk.component.server.api;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;

@Path("cache")
@Tag(name = "Cache", description = "Endpoints related to caches management.")
public interface CacheResource {

@GET
@Path("clear")
@Operation(operationId = "clearCaches", description = "Clear all caches.")
@APIResponse(responseCode = "204", description = "Clear all caches.")
void clearCaches();
}
1 change: 1 addition & 0 deletions component-server-parent/component-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@
<endpointClass>org.talend.sdk.component.server.front.ConfigurationTypeResourceImpl</endpointClass>
<endpointClass>org.talend.sdk.component.server.front.DocumentationResourceImpl</endpointClass>
<endpointClass>org.talend.sdk.component.server.front.EnvironmentResourceImpl</endpointClass>
<endpointClass>org.talend.sdk.component.server.service.jcache.FrontCacheResolver</endpointClass>
</endpointClasses>
<info>
<version>1</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.geronimo.jcache.simple.cdi.CacheResolverImpl;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.talend.sdk.component.api.meta.Documentation;
import org.talend.sdk.component.server.api.CacheResource;
import org.talend.sdk.component.server.front.EnvironmentResourceImpl;
import org.talend.sdk.component.server.front.model.Environment;
import org.talend.sdk.components.vault.jcache.CacheConfigurationFactory;
Expand All @@ -45,7 +46,7 @@

@Slf4j
@ApplicationScoped
public class FrontCacheResolver implements CacheResolverFactory {
public class FrontCacheResolver implements CacheResolverFactory, CacheResource {

@Inject
private CacheManager cacheManager;
Expand Down Expand Up @@ -116,6 +117,7 @@ private void updateIfNeeded() {
}
}

@Override
public void clearCaches() {
StreamSupport
.stream(cacheManager.getCacheNames().spliterator(), false)
Expand All @@ -124,6 +126,19 @@ public void clearCaches() {
.forEach(r -> cacheManager.getCache(r).clear());
}

/**
* mainly used for testing purpose.
*
* @return active caches count
*/
public long countActiveCaches() {
return StreamSupport
.stream(cacheManager.getCacheNames().spliterator(), false)
.filter(name -> name.startsWith("org.talend.sdk.component.server.front."))
.filter(c -> cacheManager.getCache(c).iterator().hasNext())
.count();
}

@Override
public CacheResolver getCacheResolver(final CacheMethodDetails<? extends Annotation> cacheMethodDetails) {
return findCacheResolver(cacheMethodDetails.getCacheName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (C) 2006-2025 Talend Inc. - www.talend.com
*
* Licensed 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.talend.sdk.component.server.service.jcache;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
import static org.junit.jupiter.api.Assertions.assertEquals;

import javax.inject.Inject;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import org.apache.meecrowave.junit5.MonoMeecrowaveConfig;
import org.junit.jupiter.api.Test;
import org.talend.sdk.component.server.test.ComponentClient;

@MonoMeecrowaveConfig
class FrontCacheResolverTest {

@Inject
private ComponentClient client;

@Inject
private WebTarget base;

@Inject
private FrontCacheResolver cacheResolver;

@Test
void clearCaches() {
cacheResolver.clearCaches();
assertEquals(0, cacheResolver.countActiveCaches());
client.fetchIndex();
assertEquals(1, cacheResolver.countActiveCaches());
client.fetchConfigTypeNodes();
assertEquals(2, cacheResolver.countActiveCaches());
final Response resp = base
.path("cache/clear")
.request(APPLICATION_JSON_TYPE)
.get();
assertEquals(204, resp.getStatus());
assertEquals(0, cacheResolver.countActiveCaches());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

private static final String PATH_ENVIRONMENT = "/api/v1/environment";

private static final String PATH_CACHE_RESET = "/api/v1/cache/clear";

Check warning on line 53 in component-starter-server/src/main/java/org/talend/sdk/component/starter/server/front/ApiDemoEndpoints.java

View check run for this annotation

sonar-eks / Component Runtime Sonarqube Results

component-starter-server/src/main/java/org/talend/sdk/component/starter/server/front/ApiDemoEndpoints.java#L53

Refactor your code to get this URI from a customizable parameter.

private static final String PATH_ACTION_INDEX = "/api/v1/action/index";

private static final String PATH_ACTION_EXECUTE = "/api/v1/action/execute";
Expand Down Expand Up @@ -166,6 +168,12 @@
.build();
}

@GET
@Path(RES_VERSION + PATH_CACHE_RESET)
public Response clearCaches() {
return Response.noContent().build();
}

@GET
@Path(RES_VERSION + PATH_ACTION_INDEX)
@Produces({ APPLICATION_JSON })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

import org.talend.sdk.component.path.PathFactory;
import org.talend.sdk.component.server.api.ActionResource;
import org.talend.sdk.component.server.api.CacheResource;
import org.talend.sdk.component.server.api.ComponentResource;
import org.talend.sdk.component.server.api.ConfigurationTypeResource;
import org.talend.sdk.component.server.api.DocumentationResource;
Expand Down Expand Up @@ -175,6 +176,10 @@
.add(route("component_server_environment", "/api/v1/environment", MapBuilder.map().done(),
emptyMap(), emptyMap(), jsonb.toJson(environment.get())));

final CacheResource cache = container.select(CacheResource.class).get();

Check warning on line 179 in component-tools-webapp/src/main/java/org/talend/sdk/component/tools/webapp/standalone/generator/StaticResourceGenerator.java

View check run for this annotation

sonar-eks / Component Runtime Sonarqube Results

component-tools-webapp/src/main/java/org/talend/sdk/component/tools/webapp/standalone/generator/StaticResourceGenerator.java#L179

Remove this unused "cache" local variable.
routes.add(route("component_server_environment", "/api/v1/cache/clear", MapBuilder.map().done(),
emptyMap(), emptyMap(), null));

final ActionResource actions = container.select(ActionResource.class).get();
routes
.addAll(languages
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ public static void main(final String[] args) {
log.info("System is offline, skipping jira changelog and github contributor generation");
} else {
tasks.register(() -> generatedContributors(generatedDir, args[5], args[6]));
tasks.register(() -> generatedJira(generatedDir, args[1], args[2], version));
// was made in a jdk17 pom update then reverted. Reapply.
undx marked this conversation as resolved.
Show resolved Hide resolved
// tasks.register(() -> generatedJira(generatedDir, args[1], args[2], version));
}
}
}
Expand Down