adutra commented on code in PR #1844: URL: https://github.com/apache/polaris/pull/1844#discussion_r2161573756
########## service/common/src/main/java/org/apache/polaris/service/events/listeners/FileBufferPolarisPersistenceEventListener.java: ########## @@ -0,0 +1,189 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.polaris.service.events.listeners; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Output; +import com.google.common.annotations.VisibleForTesting; +import io.smallrye.common.annotation.Identifier; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.time.Clock; +import java.util.HashMap; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiConsumer; +import java.util.function.Function; +import org.apache.polaris.core.config.FeatureConfiguration; +import org.apache.polaris.core.config.PolarisConfigurationStore; +import org.apache.polaris.core.context.CallContext; +import org.apache.polaris.core.context.RealmContext; +import org.apache.polaris.core.entity.PolarisEvent; +import org.apache.polaris.core.persistence.BasePersistence; +import org.apache.polaris.core.persistence.MetaStoreManagerFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** Event listener that buffers in memory and then dumps to persistence. */ +@ApplicationScoped +@Identifier("persistence-file-buffer") +public class FileBufferPolarisPersistenceEventListener extends PolarisPersistenceEventListener { + private static final Logger LOGGER = + LoggerFactory.getLogger(FileBufferPolarisPersistenceEventListener.class); + MetaStoreManagerFactory metaStoreManagerFactory; + PolarisConfigurationStore polarisConfigurationStore; + Clock clock; + + // Key: str - Realm + // Value: + // Key: int - shard number + // Value: BufferShard - an object representing the directory and file where events are + // persisted on the filesystem + private final HashMap<String, HashMap<Integer, BufferShard>> buffers = new HashMap<>(); + ConcurrentHashMap<String, Future> activeFlushFutures = new ConcurrentHashMap<>(); + + ScheduledExecutorService threadPool; + private static int shardCount; + private static int maxBufferSize; + private final Kryo kryo = new Kryo(); + private static final String BUFFER_SHARD_PREFIX = "polaris-event-buffer-shard-"; + + @Inject + public FileBufferPolarisPersistenceEventListener( + MetaStoreManagerFactory metaStoreManagerFactory, + PolarisConfigurationStore polarisConfigurationStore, + Clock clock) { + this.metaStoreManagerFactory = metaStoreManagerFactory; + this.polarisConfigurationStore = polarisConfigurationStore; + this.clock = clock; + shardCount = + polarisConfigurationStore.getConfiguration( + null, FeatureConfiguration.EVENT_BUFFER_NUM_SHARDS); + maxBufferSize = + polarisConfigurationStore.getConfiguration( + null, FeatureConfiguration.EVENT_BUFFER_MAX_SIZE); + int timeToFlush = + polarisConfigurationStore.getConfiguration( + null, FeatureConfiguration.EVENT_BUFFER_TIME_TO_FLUSH_IN_MS); + int numThreads = + polarisConfigurationStore.getConfiguration( + null, FeatureConfiguration.EVENT_BUFFER_NUM_THREADS); + threadPool = Executors.newScheduledThreadPool(numThreads); Review Comment: You cannot leave a thread pool unclosed. This is a serious resource leak. Depending on the thread pool, your application may never be able to halt. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@polaris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org