diff --git a/service/datastore/internal/src/main/java/org/eclipse/kapua/service/datastore/internal/MetricInfoRegistryFacade.java b/service/datastore/internal/src/main/java/org/eclipse/kapua/service/datastore/internal/MetricInfoRegistryFacade.java index aaa0007f9eb..9847d7ba202 100644 --- a/service/datastore/internal/src/main/java/org/eclipse/kapua/service/datastore/internal/MetricInfoRegistryFacade.java +++ b/service/datastore/internal/src/main/java/org/eclipse/kapua/service/datastore/internal/MetricInfoRegistryFacade.java @@ -24,6 +24,8 @@ import org.eclipse.kapua.service.datastore.internal.schema.Metadata; import org.eclipse.kapua.service.datastore.internal.schema.MetricInfoSchema; import org.eclipse.kapua.service.datastore.internal.schema.SchemaUtil; +import org.eclipse.kapua.service.datastore.internal.setting.DatastoreSettings; +import org.eclipse.kapua.service.datastore.internal.setting.DatastoreSettingsKey; import org.eclipse.kapua.service.datastore.model.MetricInfo; import org.eclipse.kapua.service.datastore.model.MetricInfoListResult; import org.eclipse.kapua.service.datastore.model.query.MetricInfoQuery; @@ -131,16 +133,19 @@ public BulkUpdateResponse upstore(MetricInfo[] metricInfos) BulkUpdateRequest bulkRequest = new BulkUpdateRequest(); boolean performUpdate = false; + final boolean shouldFetchBeforeUpsertInCacheMiss = DatastoreSettings.getInstance().getBoolean(DatastoreSettingsKey.CONFIG_CACHE_METRICS_FETCH_FROM_SOURCE_BEFORE_UPSERT, true); // Create a bulk request for (MetricInfo metricInfo : metricInfos) { String metricInfoId = MetricInfoField.getOrDeriveId(metricInfo.getId(), metricInfo); // fix #REPLACE_ISSUE_NUMBER if (!DatastoreCacheManager.getInstance().getMetricsCache().get(metricInfoId)) { - StorableId storableId = STORABLE_ID_FACTORY.newStorableId(metricInfoId); - MetricInfo storedField = find(metricInfo.getScopeId(), storableId); - if (storedField != null) { - DatastoreCacheManager.getInstance().getMetricsCache().put(metricInfoId, true); - continue; + if (shouldFetchBeforeUpsertInCacheMiss) { + StorableId storableId = STORABLE_ID_FACTORY.newStorableId(metricInfoId); + MetricInfo storedField = find(metricInfo.getScopeId(), storableId); + if (storedField != null) { + DatastoreCacheManager.getInstance().getMetricsCache().put(metricInfoId, true); + continue; + } } performUpdate = true; Metadata metadata = mediator.getMetadata(metricInfo.getScopeId(), metricInfo.getFirstMessageOn().getTime()); diff --git a/service/datastore/internal/src/main/java/org/eclipse/kapua/service/datastore/internal/setting/DatastoreSettingsKey.java b/service/datastore/internal/src/main/java/org/eclipse/kapua/service/datastore/internal/setting/DatastoreSettingsKey.java index 3ced77d2d4b..86af793f988 100644 --- a/service/datastore/internal/src/main/java/org/eclipse/kapua/service/datastore/internal/setting/DatastoreSettingsKey.java +++ b/service/datastore/internal/src/main/java/org/eclipse/kapua/service/datastore/internal/setting/DatastoreSettingsKey.java @@ -70,6 +70,11 @@ public enum DatastoreSettingsKey implements SettingKey { * Metadata cache maximum size (default value is no specific cache value is defined) */ CONFIG_CACHE_METADATA_LOCAL_SIZE_MAXIMUM("datastore.cache.metadata.local.size.maximum"), + /** + * Metrics behaviour switch - when the cache does not know a specific metric, should the system ask ES about it (set to true, default) or just send an upsert for non-cached metrics (set to + * false)? + */ + CONFIG_CACHE_METRICS_FETCH_FROM_SOURCE_BEFORE_UPSERT("datastore.cache.metrics.fetchBeforeUpsert"), /** * Enable datastore timing profile */ diff --git a/service/datastore/internal/src/main/resources/kapua-datastore-settings.properties b/service/datastore/internal/src/main/resources/kapua-datastore-settings.properties index 4716f6ff132..c433260a040 100644 --- a/service/datastore/internal/src/main/resources/kapua-datastore-settings.properties +++ b/service/datastore/internal/src/main/resources/kapua-datastore-settings.properties @@ -39,6 +39,7 @@ datastore.cache.metadata.local.size.maximum=1000 #datastore.cache.metrics.local.size.maximum=1100 #datastore.cache.metrics.local.expire.after=120 #datastore.cache.metrics.local.expire.strategy=TOUCHED +#datastore.cache.metrics.fetchBeforeUpsert=false # # Datastore index prefix datastore.index.prefix=