From 5d199c91b1125c9db9c98f18354d8f452eda5cb2 Mon Sep 17 00:00:00 2001 From: Purujit Saha Date: Tue, 25 Jun 2024 17:59:48 +0000 Subject: [PATCH] Make memory info provider a singleton because we dont want multiple instances of it --- .../streaming/internal/MemoryInfoProviderFromRuntime.java | 8 +++++++- .../internal/SnowflakeStreamingIngestChannelInternal.java | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/snowflake/ingest/streaming/internal/MemoryInfoProviderFromRuntime.java b/src/main/java/net/snowflake/ingest/streaming/internal/MemoryInfoProviderFromRuntime.java index 463527a34..3a09b2a0a 100644 --- a/src/main/java/net/snowflake/ingest/streaming/internal/MemoryInfoProviderFromRuntime.java +++ b/src/main/java/net/snowflake/ingest/streaming/internal/MemoryInfoProviderFromRuntime.java @@ -13,8 +13,10 @@ public class MemoryInfoProviderFromRuntime implements MemoryInfoProvider { private final long maxMemory; private volatile long totalFreeMemory; private final ScheduledExecutorService executorService; + private static final long FREE_MEMORY_UPDATE_INTERVAL_MS = 1000; + private static final MemoryInfoProviderFromRuntime INSTANCE = new MemoryInfoProviderFromRuntime(FREE_MEMORY_UPDATE_INTERVAL_MS); - public MemoryInfoProviderFromRuntime(long freeMemoryUpdateIntervalMs) { + private MemoryInfoProviderFromRuntime(long freeMemoryUpdateIntervalMs) { maxMemory = Runtime.getRuntime().maxMemory(); totalFreeMemory = Runtime.getRuntime().freeMemory() + (maxMemory - Runtime.getRuntime().totalMemory()); @@ -35,6 +37,10 @@ private void updateFreeMemory() { Runtime.getRuntime().freeMemory() + (maxMemory - Runtime.getRuntime().totalMemory()); } + public static MemoryInfoProviderFromRuntime getInstance() { + return INSTANCE; + } + @Override public long getMaxMemory() { return maxMemory; diff --git a/src/main/java/net/snowflake/ingest/streaming/internal/SnowflakeStreamingIngestChannelInternal.java b/src/main/java/net/snowflake/ingest/streaming/internal/SnowflakeStreamingIngestChannelInternal.java index 6e8f6109b..0d89b3be8 100644 --- a/src/main/java/net/snowflake/ingest/streaming/internal/SnowflakeStreamingIngestChannelInternal.java +++ b/src/main/java/net/snowflake/ingest/streaming/internal/SnowflakeStreamingIngestChannelInternal.java @@ -138,7 +138,7 @@ class SnowflakeStreamingIngestChannelInternal implements SnowflakeStreamingIn this.maxMemoryLimitInBytes = this.owningClient.getParameterProvider().getMaxMemoryLimitInBytes(); - this.memoryInfoProvider = new MemoryInfoProviderFromRuntime(insertThrottleIntervalInMs); + this.memoryInfoProvider = MemoryInfoProviderFromRuntime.getInstance(); this.channelFlushContext = new ChannelFlushContext( name, dbName, schemaName, tableName, channelSequencer, encryptionKey, encryptionKeyId);