Skip to content

Commit

Permalink
Merge e22983d into 4af58b8
Browse files Browse the repository at this point in the history
  • Loading branch information
beknazaresenbek authored Oct 24, 2023
2 parents 4af58b8 + e22983d commit de86368
Show file tree
Hide file tree
Showing 44 changed files with 592 additions and 223 deletions.
8 changes: 5 additions & 3 deletions agreement/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,13 @@
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</testResource>
</testResources>
<plugins>
<!-- generate api code -->

Expand Down
21 changes: 14 additions & 7 deletions agreement/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
micronaut:

caches:
entities:
maximumSize: 1000
# enough to not call twice in one call, but would prevent conflicting writes
expire-after-write: 2s
expire-after-access: 2s

server:
port: 8632

Expand Down Expand Up @@ -44,3 +37,17 @@ loggers:
general:
contextUrl: https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld
serverHost: http://localhost:8632

---
redis:
uri: redis://localhost:6379
caches:
entities:
# enough to not call twice in one call, but would prevent conflicting writes
expire-after-write: 2s
expire-after-access: 2s
value-serializer: io.github.wistefan.mapping.EntityVOSerializer
subscriptions:
expire-after-write: 14d
expire-after-access: 14d
value-serializer: io.github.wistefan.mapping.EntityVOSerializer
25 changes: 15 additions & 10 deletions agreement/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@ micronaut:
server:
port: 8632

caches:
entities:
enabled: false
# maximumSize: 1000
# enough to not call twice in one call, but would prevent conflicting writes
# expire-after-write: 2s
# expire-after-access: 2s


metrics:
enabled: false
export:
Expand Down Expand Up @@ -44,4 +35,18 @@ loggers:

---
general:
contextUrl: https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld
contextUrl: https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld

---
redis:
uri: redis://localhost:6379
caches:
entities:
# enough to not call twice in one call, but would prevent conflicting writes
expire-after-write: 2s
expire-after-access: 2s
value-serializer: io.github.wistefan.mapping.EntityVOSerializer
subscriptions:
expire-after-write: 10s
expire-after-access: 10s
value-serializer: io.github.wistefan.mapping.EntityVOSerializer
4 changes: 4 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
<artifactId>micronaut-cache-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.micronaut.redis</groupId>
<artifactId>micronaut-redis-lettuce</artifactId>
</dependency>

<dependency>
<groupId>io.micronaut.micrometer</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,14 @@ private CommonConstants() {
*/
public static final String ID_TEMPLATE = "urn:ngsi-ld:%s:%s";

/**
* Name for the entities cache
*/
public static final String ENTITIES_CACHE_NAME = "entities";

/**
* Name for the subscriptions cache
*/
public static final String SUBSCRIPTIONS_CACHE_NAME = "subscriptions";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.fiware.tmforum.common.caching;

import io.lettuce.core.TrackingArgs;
import io.lettuce.core.api.StatefulConnection;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.api.sync.RedisKeyCommands;
import io.micronaut.configuration.lettuce.cache.DefaultRedisCacheConfiguration;
import io.micronaut.configuration.lettuce.cache.RedisCache;
import io.micronaut.configuration.lettuce.cache.RedisCacheConfiguration;
import io.micronaut.context.BeanLocator;
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.EachBean;
import io.micronaut.context.annotation.Replaces;
import io.micronaut.context.exceptions.ConfigurationException;
import io.micronaut.core.convert.ConversionService;
import jakarta.inject.Singleton;
import org.fiware.tmforum.common.CommonConstants;

@Singleton
@Bean
@EachBean(RedisCacheConfiguration.class)
@Replaces(RedisCache.class)
public class ClientTrackingRedisCache extends RedisCache {
public ClientTrackingRedisCache(DefaultRedisCacheConfiguration defaultRedisCacheConfiguration,
RedisCacheConfiguration redisCacheConfiguration,
ConversionService<?> conversionService, BeanLocator beanLocator) {
super(defaultRedisCacheConfiguration, redisCacheConfiguration, conversionService, beanLocator);
}

@Override
protected RedisKeyCommands<byte[], byte[]> getRedisKeyCommands(StatefulConnection<byte[], byte[]> connection) {
RedisKeyCommands<byte[], byte[]> commands;
if (connection instanceof StatefulRedisConnection) {
RedisCommands<byte[], byte[]> sync = ((StatefulRedisConnection<byte[], byte[]>) connection).sync();
sync.clientTracking(TrackingArgs.Builder.enabled()
.bcast()
.prefixes(CommonConstants.SUBSCRIPTIONS_CACHE_NAME)
.prefixes(CommonConstants.ENTITIES_CACHE_NAME)
.noloop());
commands = sync;
} else {
throw new ConfigurationException(INVALID_REDIS_CONNECTION_MESSAGE);
}
return commands;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.fiware.tmforum.common.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.github.wistefan.mapping.annotations.AttributeGetter;
import io.github.wistefan.mapping.annotations.AttributeSetter;
import io.github.wistefan.mapping.annotations.AttributeType;
Expand Down Expand Up @@ -66,6 +67,7 @@ protected EntityWithId(String type, String id) {
@Nullable
String atType;

@JsonIgnore
public String getEntityState() {
return "default";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public class Subscription extends EntityWithId {
@Setter(onMethod = @__({@AttributeSetter(value = AttributeType.PROPERTY_LIST, targetName = "fields", targetClass = String.class) }))
private List<String> fields;

/**
* Empty constructor for cache serialization and deserialization
*/
public Subscription() {
super(TYPE_SUBSCRIPTION, null);
}

public Subscription(String id) {
super(TYPE_SUBSCRIPTION, id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.micronaut.context.annotation.Bean;
import lombok.RequiredArgsConstructor;
import org.fiware.ngsi.model.EntityVO;
import org.fiware.tmforum.common.CommonConstants;
import org.fiware.tmforum.common.domain.subscription.Event;
import org.fiware.tmforum.common.domain.subscription.Subscription;
import org.fiware.tmforum.common.exception.EventHandlingException;
Expand All @@ -25,15 +26,14 @@
@Bean
@RequiredArgsConstructor
public class EventHandler {
public static final String SUBSCRIPTIONS_CACHE_NAME = "subscriptions";

private final QueryParser queryParser;
private final TmForumRepository repository;
private final NotificationSender notificationSender;
private final SubscriptionQueryResolver subscriptionQueryResolver;
private final EntityVOMapper entityVOMapper;

@Cacheable(SUBSCRIPTIONS_CACHE_NAME)
@Cacheable(CommonConstants.SUBSCRIPTIONS_CACHE_NAME)
public Mono<List<Subscription>> getSubscriptions(String entityType, String eventType) {
return repository.findEntities(
DEFAULT_OFFSET,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.fiware.ngsi.api.EntitiesApiClient;
import org.fiware.ngsi.model.EntityFragmentVO;
import org.fiware.ngsi.model.EntityVO;
import org.fiware.tmforum.common.CommonConstants;
import org.fiware.tmforum.common.caching.EntityIdKeyGenerator;
import org.fiware.tmforum.common.configuration.GeneralProperties;
import org.fiware.tmforum.common.exception.DeletionException;
Expand All @@ -31,11 +32,6 @@
@RequiredArgsConstructor
public abstract class NgsiLdBaseRepository {

/**
* Name for the entities cache
*/
private static final String ENTITIES_CACHE_NAME = "entities";

protected final GeneralProperties generalProperties;
protected final EntitiesApiClient entitiesApi;
protected final JavaObjectMapper javaObjectMapper;
Expand All @@ -54,7 +50,7 @@ protected String getLinkHeader() {
* @param ngsiLDTenant - tenant the entity belongs to
* @return completable with the result
*/
@CachePut(value = ENTITIES_CACHE_NAME, keyGenerator = EntityIdKeyGenerator.class)
@CachePut(value = CommonConstants.ENTITIES_CACHE_NAME, keyGenerator = EntityIdKeyGenerator.class)
public Mono<Void> createEntity(EntityVO entityVO, String ngsiLDTenant) {
return entitiesApi.createEntity(entityVO, ngsiLDTenant);
}
Expand All @@ -65,7 +61,7 @@ public Mono<Void> createEntity(EntityVO entityVO, String ngsiLDTenant) {
* @param entityId id of the entity
* @return the entity
*/
@Cacheable(ENTITIES_CACHE_NAME)
@Cacheable(CommonConstants.ENTITIES_CACHE_NAME)
public Mono<EntityVO> retrieveEntityById(URI entityId) {
return asyncRetrieveEntityById(entityId, generalProperties.getTenant(), null, null, null, getLinkHeader());
}
Expand All @@ -77,7 +73,7 @@ public Mono<EntityVO> retrieveEntityById(URI entityId) {
* @param entityFragmentVO the entity elements to be updated
* @return an empty mono
*/
@CacheInvalidate(value = ENTITIES_CACHE_NAME, keyGenerator = EntityIdKeyGenerator.class)
@CacheInvalidate(value = CommonConstants.ENTITIES_CACHE_NAME, keyGenerator = EntityIdKeyGenerator.class)
public Mono<Void> patchEntity(URI entityId, EntityFragmentVO entityFragmentVO) {
return entitiesApi.updateEntity(entityId, entityFragmentVO, generalProperties.getTenant(), null);
}
Expand Down Expand Up @@ -111,7 +107,7 @@ public <T> Mono<Void> updateDomainEntity(String id, T domainEntity) {
* @param id id of the entity to be deleted
* @return an empty mono
*/
@CacheInvalidate(value = ENTITIES_CACHE_NAME, keyGenerator = EntityIdKeyGenerator.class)
@CacheInvalidate(value = CommonConstants.ENTITIES_CACHE_NAME, keyGenerator = EntityIdKeyGenerator.class)
public Mono<Void> deleteDomainEntity(URI id) {
return entitiesApi
.removeEntityById(id, generalProperties.getTenant(), null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.micronaut.cache.annotation.CacheInvalidate;
import io.micronaut.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.fiware.tmforum.common.CommonConstants;
import org.fiware.tmforum.common.domain.subscription.Subscription;
import org.fiware.tmforum.common.exception.TmForumException;
import org.fiware.tmforum.common.exception.TmForumExceptionReason;
Expand Down Expand Up @@ -31,7 +32,7 @@ public AbstractSubscriptionApiController(QueryParser queryParser, ReferenceValid
this.eventGroupToEntityNameMapping = eventGroupToEntityNameMapping;
}

@CacheInvalidate(value = EventHandler.SUBSCRIPTIONS_CACHE_NAME, all = true)
@CacheInvalidate(value = CommonConstants.SUBSCRIPTIONS_CACHE_NAME, all = true)
protected Mono<Subscription> create(Subscription subscription) {
return findExistingSubscription(subscription)
.switchIfEmpty(create(Mono.just(subscription), Subscription.class));
Expand Down Expand Up @@ -71,7 +72,7 @@ protected Subscription buildSubscription(String callback, String query, List<Str
}

@Override
@CacheInvalidate(value = EventHandler.SUBSCRIPTIONS_CACHE_NAME, all = true)
@CacheInvalidate(value = CommonConstants.SUBSCRIPTIONS_CACHE_NAME, all = true)
protected Mono<HttpResponse<Object>> delete(String id) {
return super.delete(id);
}
Expand Down
8 changes: 5 additions & 3 deletions customer-bill-management/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,13 @@
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</testResource>
</testResources>
<plugins>
<!-- generate api code -->
<plugin>
Expand Down
21 changes: 14 additions & 7 deletions customer-bill-management/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
micronaut:

caches:
entities:
maximumSize: 1000
# enough to not call twice in one call, but would prevent conflicting writes
expire-after-write: 2s
expire-after-access: 2s

server:
port: 8632

Expand Down Expand Up @@ -44,3 +37,17 @@ loggers:
general:
contextUrl: https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld
serverHost: http://localhost:8632

---
redis:
uri: redis://localhost:6379
caches:
entities:
# enough to not call twice in one call, but would prevent conflicting writes
expire-after-write: 2s
expire-after-access: 2s
value-serializer: io.github.wistefan.mapping.EntityVOSerializer
subscriptions:
expire-after-write: 14d
expire-after-access: 14d
value-serializer: io.github.wistefan.mapping.EntityVOSerializer
24 changes: 15 additions & 9 deletions customer-bill-management/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ micronaut:
server:
port: 8632

caches:
entities:
enabled: false
# maximumSize: 1000
# enough to not call twice in one call, but would prevent conflicting writes
# expire-after-write: 2s
# expire-after-access: 2s

metrics:
enabled: false
export:
Expand Down Expand Up @@ -43,4 +35,18 @@ loggers:

---
general:
contextUrl: https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld
contextUrl: https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld

---
redis:
uri: redis://localhost:6379
caches:
entities:
# enough to not call twice in one call, but would prevent conflicting writes
expire-after-write: 2s
expire-after-access: 2s
value-serializer: io.github.wistefan.mapping.EntityVOSerializer
subscriptions:
expire-after-write: 10s
expire-after-access: 10s
value-serializer: io.github.wistefan.mapping.EntityVOSerializer
8 changes: 5 additions & 3 deletions customer-management/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</testResource>
</testResources>
<plugins>
<!-- generate api code -->
<plugin>
Expand Down
Loading

0 comments on commit de86368

Please sign in to comment.