/*
 * 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.lucene.index;


import org.apache.lucene.index.DocumentsWriterPerThreadPool.ThreadState;

Default FlushPolicy implementation that flushes new segments based on RAM used and document count depending on the IndexWriter's IndexWriterConfig. It also applies pending deletes based on the number of buffered delete terms. All IndexWriterConfig settings are used to mark DocumentsWriterPerThread as flush pending during indexing with respect to their live updates.

If IndexWriterConfig.setRAMBufferSizeMB(double) is enabled, the largest ram consuming DocumentsWriterPerThread will be marked as pending iff the global active RAM consumption is >= the configured max RAM buffer.

/** * Default {@link FlushPolicy} implementation that flushes new segments based on * RAM used and document count depending on the IndexWriter's * {@link IndexWriterConfig}. It also applies pending deletes based on the * number of buffered delete terms. * * <ul> * <li> * {@link #onDelete(DocumentsWriterFlushControl, DocumentsWriterPerThreadPool.ThreadState)} * - applies pending delete operations based on the global number of buffered * delete terms if the consumed memory is greater than {@link IndexWriterConfig#getRAMBufferSizeMB()}</li>. * <li> * {@link #onInsert(DocumentsWriterFlushControl, DocumentsWriterPerThreadPool.ThreadState)} * - flushes either on the number of documents per * {@link DocumentsWriterPerThread} ( * {@link DocumentsWriterPerThread#getNumDocsInRAM()}) or on the global active * memory consumption in the current indexing session iff * {@link IndexWriterConfig#getMaxBufferedDocs()} or * {@link IndexWriterConfig#getRAMBufferSizeMB()} is enabled respectively</li> * <li> * {@link #onUpdate(DocumentsWriterFlushControl, DocumentsWriterPerThreadPool.ThreadState)} * - calls * {@link #onInsert(DocumentsWriterFlushControl, DocumentsWriterPerThreadPool.ThreadState)} * and * {@link #onDelete(DocumentsWriterFlushControl, DocumentsWriterPerThreadPool.ThreadState)} * in order</li> * </ul> * All {@link IndexWriterConfig} settings are used to mark * {@link DocumentsWriterPerThread} as flush pending during indexing with * respect to their live updates. * <p> * If {@link IndexWriterConfig#setRAMBufferSizeMB(double)} is enabled, the * largest ram consuming {@link DocumentsWriterPerThread} will be marked as * pending iff the global active RAM consumption is {@code >=} the configured max RAM * buffer. */
class FlushByRamOrCountsPolicy extends FlushPolicy { @Override public void onDelete(DocumentsWriterFlushControl control, ThreadState state) { if ((flushOnRAM() && control.getDeleteBytesUsed() > 1024*1024*indexWriterConfig.getRAMBufferSizeMB())) { control.setApplyAllDeletes(); if (infoStream.isEnabled("FP")) { infoStream.message("FP", "force apply deletes bytesUsed=" + control.getDeleteBytesUsed() + " vs ramBufferMB=" + indexWriterConfig.getRAMBufferSizeMB()); } } } @Override public void onInsert(DocumentsWriterFlushControl control, ThreadState state) { if (flushOnDocCount() && state.dwpt.getNumDocsInRAM() >= indexWriterConfig .getMaxBufferedDocs()) { // Flush this state by num docs control.setFlushPending(state); } else if (flushOnRAM()) {// flush by RAM final long limit = (long) (indexWriterConfig.getRAMBufferSizeMB() * 1024.d * 1024.d); final long totalRam = control.activeBytes() + control.getDeleteBytesUsed(); if (totalRam >= limit) { if (infoStream.isEnabled("FP")) { infoStream.message("FP", "trigger flush: activeBytes=" + control.activeBytes() + " deleteBytes=" + control.getDeleteBytesUsed() + " vs limit=" + limit); } markLargestWriterPending(control, state, totalRam); } } }
Marks the most ram consuming active DocumentsWriterPerThread flush pending
/** * Marks the most ram consuming active {@link DocumentsWriterPerThread} flush * pending */
protected void markLargestWriterPending(DocumentsWriterFlushControl control, ThreadState perThreadState, final long currentBytesPerThread) { ThreadState largestNonPendingWriter = findLargestNonPendingWriter(control, perThreadState); if (largestNonPendingWriter != null) { control.setFlushPending(largestNonPendingWriter); } }
Returns true if this FlushPolicy flushes on IndexWriterConfig.getMaxBufferedDocs(), otherwise false.
/** * Returns <code>true</code> if this {@link FlushPolicy} flushes on * {@link IndexWriterConfig#getMaxBufferedDocs()}, otherwise * <code>false</code>. */
protected boolean flushOnDocCount() { return indexWriterConfig.getMaxBufferedDocs() != IndexWriterConfig.DISABLE_AUTO_FLUSH; }
Returns true if this FlushPolicy flushes on IndexWriterConfig.getRAMBufferSizeMB(), otherwise false.
/** * Returns <code>true</code> if this {@link FlushPolicy} flushes on * {@link IndexWriterConfig#getRAMBufferSizeMB()}, otherwise * <code>false</code>. */
protected boolean flushOnRAM() { return indexWriterConfig.getRAMBufferSizeMB() != IndexWriterConfig.DISABLE_AUTO_FLUSH; } }