itschrispeck commented on code in PR #13503:
URL: https://github.com/apache/pinot/pull/13503#discussion_r1690224690


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/RealtimeLuceneIndexRefreshManager.java:
##########
@@ -0,0 +1,268 @@
+/**
+ * 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.pinot.segment.local.realtime.impl.invertedindex;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.stream.Collectors;
+import org.apache.lucene.search.SearcherManager;
+import org.apache.pinot.common.utils.ScalingThreadPoolExecutor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This class manages the refreshing of all realtime Lucene index readers. It 
uses an auto-scaling pool of threads,
+ * which expands up to a configurable size, to refresh the readers.
+ * <p>
+ * During instantiation of a RealtimeLuceneTextIndex the corresponding 
SearcherManager is registered with this class.
+ * When the RealtimeLuceneTextIndex is closed, the flag set by the 
RealtimeLuceneTextIndex is checked before attempting
+ * a refresh, and SearcherManagerHolder instance previously registered to this 
class is dropped from the queue of
+ * readers to be refreshed.
+ */
+public class RealtimeLuceneIndexRefreshManager {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(RealtimeLuceneIndexRefreshManager.class);
+  // max number of parallel refresh threads
+  private final int _maxParallelism;
+  // delay between refresh iterations
+  private int _delayMs;
+  // partitioned lists of SearcherManagerHolders, each gets its own thread for 
refreshing. SearcherManagerHolders
+  // are added to the list with the smallest size to roughly balance the load 
across threads
+  private final List<List<SearcherManagerHolder>> _partitionedListsOfSearchers;
+  private static RealtimeLuceneIndexRefreshManager _singletonInstance;
+  private static ExecutorService _executorService;
+
+  private RealtimeLuceneIndexRefreshManager(int maxParallelism, int delayMs) {
+    _maxParallelism = maxParallelism;
+    _delayMs = delayMs;
+    // Set min pool size to 0, scale up/down as needed. Set keep alive time to 
0, as threads are generally long-lived
+    _executorService = ScalingThreadPoolExecutor.newScalingThreadPool(0, 
_maxParallelism, 0L);
+    _partitionedListsOfSearchers = new ArrayList<>();
+  }
+
+  public static RealtimeLuceneIndexRefreshManager getInstance() {

Review Comment:
   It's not the traditional singleton that can follow that pattern since it 
accepts args (server configs). The `init(..)` method will be called first in 
the server starter, ensuring the instance is not null. The precondition below 
is added as a hint to others when writing UTs



-- 
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: commits-unsubscr...@pinot.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to