Skip to content

Commit 3fe30ec

Browse files
Add size of project ID to key size
1 parent 7759a1e commit 3fe30ec

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpCache.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,12 @@ private record CacheKey(ProjectId projectId, String ip, String databasePath) {
173173
private static final long BASE_BYTES = RamUsageEstimator.shallowSizeOfInstance(CacheKey.class);
174174

175175
private long sizeInBytes() {
176-
return keySizeInBytes(ip, databasePath);
176+
return keySizeInBytes(projectId, ip, databasePath);
177177
}
178178
}
179179

180180
// visible for testing
181-
static long keySizeInBytes(String ip, String databasePath) {
182-
// TODO(pete): Add ProjectID size
183-
return CacheKey.BASE_BYTES + RamUsageEstimator.sizeOf(ip) + RamUsageEstimator.sizeOf(databasePath);
181+
static long keySizeInBytes(ProjectId projectId, String ip, String databasePath) {
182+
return CacheKey.BASE_BYTES + projectId.sizeInBytes() + RamUsageEstimator.sizeOf(ip) + RamUsageEstimator.sizeOf(databasePath);
184183
}
185184
}

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpCacheTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public void testCachesAndEvictsResults_maxBytes() {
5454
String ip2 = "127.0.0.2";
5555
String databasePath2 = "path/to/db";
5656
FakeResponse response2 = new FakeResponse(222);
57-
long totalSize = GeoIpCache.keySizeInBytes(ip1, databasePath1) + GeoIpCache.keySizeInBytes(ip2, databasePath2) + response1
58-
.sizeInBytes() + response2.sizeInBytes();
57+
long totalSize = GeoIpCache.keySizeInBytes(projectId, ip1, databasePath1) + GeoIpCache.keySizeInBytes(projectId, ip2, databasePath2)
58+
+ response1.sizeInBytes() + response2.sizeInBytes();
5959

6060
GeoIpCache justBigEnoughCache = GeoIpCache.createGeoIpCacheWithMaxBytes(ByteSizeValue.ofBytes(totalSize));
6161
justBigEnoughCache.putIfAbsent(projectId, ip1, databasePath1, ip -> response1);

server/src/main/java/org/elasticsearch/cluster/metadata/ProjectId.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.cluster.metadata;
1111

12+
import org.apache.lucene.util.RamUsageEstimator;
1213
import org.elasticsearch.common.Strings;
1314
import org.elasticsearch.common.io.stream.StreamInput;
1415
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -27,6 +28,7 @@ public class ProjectId implements Writeable, ToXContent {
2728
public static final ProjectId DEFAULT = new ProjectId(DEFAULT_STRING);
2829
public static final Reader<ProjectId> READER = ProjectId::readFrom;
2930
private static final int MAX_LENGTH = 128;
31+
private static final long BASE_BYTES_SIZE = RamUsageEstimator.shallowSizeOfInstance(ProjectId.class);
3032

3133
private final String id;
3234

@@ -113,4 +115,8 @@ public boolean equals(Object o) {
113115
public int hashCode() {
114116
return Objects.hashCode(id);
115117
}
118+
119+
public long sizeInBytes() {
120+
return BASE_BYTES_SIZE + RamUsageEstimator.sizeOf(id);
121+
}
116122
}

0 commit comments

Comments
 (0)