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

feat(impl):[210] added cache mechanism to ConnectorEndpointsService #623

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions charts/irs-helm/templates/configmap-spring-app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ data:
operator: {{ .operator | quote }}
rightOperand: {{ .rightOperand | quote }}
{{- end }}
connectorEndpointService:
cacheTTL: {{ tpl(.Values.edc.connectorEndpointService.cacheTTL) }}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cacheTTL: {{ tpl(.Values.edc.connectorEndpointService.cacheTTL) }}
cacheTTL: {{ .Values.edc.connectorEndpointService.cacheTTL | int64 }}

ess:
localBpn: {{ tpl (.Values.bpn | default "") . | quote }}
localEdcEndpoint: {{ tpl (.Values.edc.provider.host | default "") . | quote }}
Expand Down
3 changes: 2 additions & 1 deletion charts/irs-helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ edc:
- leftOperand: "Membership"
operator: "eq"
rightOperand: "active"

connectorEndpointService:
cacheTTL: 86400000
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to iso format
add comment

discovery:
oAuthClientId: portal # ID of the OAuth2 client registration to use, see config spring.security.oauth2.client

Expand Down
2 changes: 2 additions & 0 deletions irs-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ irs-edc-client:
- leftOperand: "Membership"
operator: "eq"
rightOperand: "active"
connectorEndpointService:
cacheTTL: 86400000

digitalTwinRegistry:
type: ${DIGITALTWINREGISTRY_TYPE:decentral} # The type of DTR. This can be either "central" or "decentral". If "decentral", descriptorEndpoint, shellLookupEndpoint and oAuthClientId is not required.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/********************************************************************************
* Copyright (c) 2021,2022,2023
* 2022: ZF Friedrichshafen AG
* 2022: ISTOS GmbH
* 2022,2023: Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* 2022,2023: BOSCH AG
* Copyright (c) 2021,2022,2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
package org.eclipse.tractusx.irs;

import org.assertj.core.api.Assertions;
import org.eclipse.tractusx.irs.registryclient.discovery.ConnectorEndpointsService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = { "digitalTwinRegistry.type=central" })
@ActiveProfiles(profiles = "test")
@Import(TestConfig.class)
@ExtendWith({ MockitoExtension.class,
SpringExtension.class
})
public class ConnectorEndpointServiceTest {

@Autowired
ConnectorEndpointsService connectorEndpointsService;

@Autowired
CacheManager cacheManager;

@Test
public void shouldGenerateCacheRecordWhenFetchConnectorEndpointsCalled() {
// given
final String bpnRecord = "";

// when
connectorEndpointsService.fetchConnectorEndpoints(bpnRecord);

// then
final Cache connectorEndpointServiceCache = cacheManager.getCache("connector_endpoint_service_cache");
Assertions.assertThat(connectorEndpointServiceCache.get(bpnRecord)).isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Scheduled;

/**
* Connector Endpoints service to find connectors in Discovery Finder
Expand All @@ -39,7 +42,9 @@
public class ConnectorEndpointsService {

private final DiscoveryFinderClient discoveryFinderClient;
private static final String CONNECTOR_ENDPOINT_SERVICE_CACHE_NAME = "connector_endpoint_service_cache";

@Cacheable(CONNECTOR_ENDPOINT_SERVICE_CACHE_NAME)
public List<String> fetchConnectorEndpoints(final String bpn) {
if (StringUtils.isBlank(bpn)) {
log.warn("BPN was null, cannot search for any connector endpoints. Returning empty list.");
Expand All @@ -65,4 +70,9 @@ public List<String> fetchConnectorEndpoints(final String bpn) {
return endpoints;
}

@CacheEvict(value = CONNECTOR_ENDPOINT_SERVICE_CACHE_NAME, allEntries = true)
@Scheduled(fixedRateString = "${irs-edc-client.connectorEndpointService.cacheTTL}")
public void evictCachesValues() {
log.debug("Clearing \"{}\" cache.", CONNECTOR_ENDPOINT_SERVICE_CACHE_NAME);
}
}