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

Detecting when a deserialized Map is immutable before changing it in IndexDiskUsageStats #77219

Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public IndexDiskUsageStats(long indexSizeInBytes) {
}

public IndexDiskUsageStats(StreamInput in) throws IOException {
this.fields = in.readMap(StreamInput::readString, PerFieldDiskUsage::new);
this.fields = new HashMap<>(in.readMap(StreamInput::readString, PerFieldDiskUsage::new));
this.indexSizeInBytes = in.readVLong();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.action.admin.indices.diskusage;

import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;

public class IndexDiskUsageStatsTests extends ESTestCase {

public void testEmptySerialization() throws IOException {
IndexDiskUsageStats emptyDndexDiskUsageStats = createEmptyDiskUsageStats();
try (BytesStreamOutput out = new BytesStreamOutput()) {
emptyDndexDiskUsageStats.writeTo(out);
try (StreamInput in = out.bytes().streamInput()) {
IndexDiskUsageStats deserializedNodeStats = new IndexDiskUsageStats(in);
assertEquals(emptyDndexDiskUsageStats.getIndexSizeInBytes(), deserializedNodeStats.getIndexSizeInBytes());
assertEquals(emptyDndexDiskUsageStats.getFields(), deserializedNodeStats.getFields());
// Now just making sure that an exception doesn't get thrown here:
deserializedNodeStats.addStoredField(randomAlphaOfLength(10), randomNonNegativeLong());
}
}
}

private IndexDiskUsageStats createEmptyDiskUsageStats() {
return new IndexDiskUsageStats(randomNonNegativeLong());
}

}