Skip to content

Fix downsampling with disabled subobjects#138715

Merged
gmarouli merged 12 commits intoelastic:mainfrom
gmarouli:fix-downsampling-with-disabled-subobjects
Dec 5, 2025
Merged

Fix downsampling with disabled subobjects#138715
gmarouli merged 12 commits intoelastic:mainfrom
gmarouli:fix-downsampling-with-disabled-subobjects

Conversation

@gmarouli
Copy link
Contributor

The downsampling operation is editing the source mapping to replace gauge metrics with an aggregate type which contains the pre-aggregated data.

However, the process does not respect the disabled subobjects configured by parent fields, for example, a passthrough field and gets conflicts as it tried to reconstruct the updated mapping.

For example, the following valid mapping section:

     "metrics": {
        "type": "passthrough",
        "priority": 10,
        "dynamic": "true",
        "properties": {
          "k8s.volume.inodes": {
            "type": "long",
            "time_series_metric": "gauge"
          },
          "k8s.volume.inodes.free": {
            "type": "long",
            "time_series_metric": "gauge"
          },
          "k8s.volume.inodes.used": {
            "type": "long",
            "time_series_metric": "gauge"
          }
        }
      }

gets converted to:

          "metrics.k8s.volume.inodes": {
            "type": "aggregate_metric_double",
            "time_series_metric": "gauge",
            ....
          },
          "metrics.k8s.volume.inodes.free": {
            "type": "aggregate_metric_double",
            "time_series_metric": "gauge",
          ...
          },
          "metrics.k8s.volume.inodes.used": {
            "type": "aggregate_metric_double",
            "time_series_metric": "gauge",
            ...
          }

Which is not valid because the disabled subobjects is not set anymore.

This PR (re)proposes to use the visitor pattern to update the source mapping in place. Last time we proposed that there was a better solution; however, now we see that the original structure of the mapping is important for inherited properties.

Fixes: #138572

@gmarouli gmarouli requested a review from kkrik-es November 27, 2025 09:29
@gmarouli gmarouli added >bug auto-backport Automatically create backport pull requests when merged :StorageEngine/Downsampling Downsampling (replacement for rollups) - Turn fine-grained time-based data into coarser-grained data branch:9.2 labels Nov 27, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-storage-engine (Team:StorageEngine)

@elasticsearchmachine
Copy link
Collaborator

Hi @gmarouli, I've created a changelog YAML for you.

Map<String, Object> downsampledMapping = new HashMap<>();
for (Map.Entry<String, Object> entry : sourceIndexMappings.entrySet()) {
if (entry.getKey().equals(PROPERTIES) == false) {
downsampledMapping.put(entry.getKey(), entry.getValue());
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this? Maybe add a comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a comment and a test to ensure this will not be accidentally removed.

);

HashMap<String, Object> result = new HashMap<>();
MappingVisitor.visitAndCopyMapping(expectedProperties, result, (ignored, source, dest) -> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we also add a test that creates random mappings and checks that the copied one is a clone of the source?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea!

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add this separately.

gmarouli and others added 2 commits November 28, 2025 09:37
…ats/MappingVisitorTests.java

Co-authored-by: Kostas Krikellas <131142368+kkrik-es@users.noreply.github.com>
@gmarouli
Copy link
Contributor Author

Update:

I am currently working on the ci failures. The problem is that we need to be able to distringuish and handle differently a multi field from a subobjects false field :) Working on it.

@gmarouli
Copy link
Contributor Author

gmarouli commented Dec 2, 2025

Waiting for #138869

@gmarouli gmarouli requested a review from kkrik-es December 5, 2025 14:48
Comment on lines +148 to +160
@SuppressWarnings("unchecked")
public void testVisitAndCopy() {
DataGeneratorSpecification specification = DataGeneratorSpecification.buildDefault();
var template = new TemplateGenerator(specification).generate();
MappingGenerator mappingGenerator = new MappingGenerator(specification);
for (int i = 0; i < MAPPING_GENERATION_ROUNDS; i++) {
var mapping = mappingGenerator.generate(template).raw();
var properties = (Map<String, Object>) mapping.get("_doc");
var updatedMapping = new HashMap<String, Object>();
MappingVisitor.visitPropertiesAndCopyMapping(properties, updatedMapping, (f, source, dest) -> dest.put(f, source));
assertThat(updatedMapping, equalTo(updatedMapping));
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kkrik-es oh I did add this one, is this not what you had in mind?

@gmarouli gmarouli merged commit c2e6a67 into elastic:main Dec 5, 2025
34 checks passed
@gmarouli gmarouli deleted the fix-downsampling-with-disabled-subobjects branch December 5, 2025 15:12
@elasticsearchmachine
Copy link
Collaborator

💔 Backport failed

Status Branch Result
9.2 Commit could not be cherrypicked due to conflicts

You can use sqren/backport to manually backport by running backport --upstream elastic/elasticsearch --pr 138715

@gmarouli
Copy link
Contributor Author

gmarouli commented Dec 5, 2025

💚 All backports created successfully

Status Branch Result
9.2

Questions ?

Please refer to the Backport tool documentation

gmarouli added a commit to gmarouli/elasticsearch that referenced this pull request Dec 5, 2025
We change the way the downsampled mapping gets created. Instead of creating a new mapping with the metric fields and then merging it with the original, we use a visitor to copy and change the original.

(cherry picked from commit c2e6a67)

# Conflicts:
#	x-pack/plugin/downsample/src/main/java/org/elasticsearch/xpack/downsample/TransportDownsampleAction.java
#	x-pack/plugin/downsample/src/test/java/org/elasticsearch/xpack/downsample/TransportDownsampleActionTests.java
mamazzol pushed a commit to mamazzol/elasticsearch that referenced this pull request Dec 5, 2025
We change the way the downsampled mapping gets created. Instead of creating a new mapping with the metric fields and then merging it with the original, we use a visitor to copy and change the original.
gmarouli added a commit that referenced this pull request Dec 5, 2025
We change the way the downsampled mapping gets created. Instead of creating a new mapping with the metric fields and then merging it with the original, we use a visitor to copy and change the original.

(cherry picked from commit c2e6a67)

# Conflicts:
#	x-pack/plugin/downsample/src/main/java/org/elasticsearch/xpack/downsample/TransportDownsampleAction.java
#	x-pack/plugin/downsample/src/test/java/org/elasticsearch/xpack/downsample/TransportDownsampleActionTests.java
elasticsearchmachine pushed a commit that referenced this pull request Feb 3, 2026
We change the way the downsampled mapping gets created. Instead of creating a new mapping with the metric fields and then merging it with the original, we use a visitor to copy and change the original.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-backport Automatically create backport pull requests when merged backport pending >bug :StorageEngine/Downsampling Downsampling (replacement for rollups) - Turn fine-grained time-based data into coarser-grained data Team:StorageEngine v9.2.3 v9.3.0

3 participants