2828import java .io .IOException ;
2929import java .util .ArrayList ;
3030import java .util .Collection ;
31- import java .util .HashMap ;
3231import java .util .LinkedHashMap ;
3332import java .util .List ;
3433import java .util .Map ;
3534import java .util .Objects ;
35+ import java .util .concurrent .ConcurrentLinkedQueue ;
36+ import java .util .concurrent .atomic .AtomicLong ;
3637
3738/**
3839 * A composite {@link IndexingExecutionEngine} that orchestrates indexing across
@@ -52,10 +53,11 @@ public class CompositeIndexingExecutionEngine implements IndexingExecutionEngine
5253
5354 private static final Logger logger = LogManager .getLogger (CompositeIndexingExecutionEngine .class );
5455
55- private final IndexingExecutionEngine <?, ?> primaryEngine ;
56- private final List <IndexingExecutionEngine <?, ?>> secondaryEngines ;
57- private final List <IndexingExecutionEngine <?, ?>> allEngines ;
56+ private final IndexingExecutionEngine <? extends DataFormat , ? extends DocumentInput <?>> primaryEngine ;
57+ private final List <IndexingExecutionEngine <? extends DataFormat , ? extends DocumentInput <?>>> secondaryEngines ;
5858 private final CompositeDataFormat compositeDataFormat ;
59+ private final CompositeDataFormatWriterPool dataFormatWriterPool ;
60+ private final AtomicLong writerGenerationCounter ;
5961
6062 /**
6163 * Constructs a CompositeIndexingExecutionEngine by reading index settings to
@@ -110,16 +112,18 @@ public CompositeIndexingExecutionEngine(
110112 }
111113 this .secondaryEngines = List .copyOf (secondaries );
112114
113- List <IndexingExecutionEngine <?, ?>> all = new ArrayList <>();
114- all .add (this .primaryEngine );
115- all .addAll (this .secondaryEngines );
116- this .allEngines = List .copyOf (all );
117-
118115 List <DataFormat > allFormats = new ArrayList <>();
119- for (IndexingExecutionEngine <?, ?> engine : this .allEngines ) {
116+ allFormats .add (primaryEngine .getDataFormat ());
117+ for (IndexingExecutionEngine <?, ?> engine : secondaryEngines ) {
120118 allFormats .add (engine .getDataFormat ());
121119 }
122120 this .compositeDataFormat = new CompositeDataFormat (allFormats );
121+ this .writerGenerationCounter = new AtomicLong (0 );
122+ this .dataFormatWriterPool = new CompositeDataFormatWriterPool (
123+ () -> new CompositeWriter (this , primaryEngine .getDataFormat (), writerGenerationCounter .getAndIncrement ()),
124+ ConcurrentLinkedQueue ::new ,
125+ Runtime .getRuntime ().availableProcessors ()
126+ );
123127 }
124128
125129 /**
@@ -160,12 +164,7 @@ static void validateFormatsRegistered(
160164
161165 @ Override
162166 public Writer <CompositeDocumentInput > createWriter (long writerGeneration ) {
163- Map <DataFormat , Writer <?>> writerMap = new LinkedHashMap <>();
164- for (IndexingExecutionEngine <?, ?> engine : allEngines ) {
165- Writer <?> writer = engine .createWriter (writerGeneration );
166- writerMap .put (engine .getDataFormat (), writer );
167- }
168- return new CompositeWriter (writerMap );
167+ return new CompositeWriter (this , primaryEngine .getDataFormat (), writerGeneration );
169168 }
170169
171170 @ Override
@@ -176,7 +175,9 @@ public Merger getMerger() {
176175 @ Override
177176 public RefreshResult refresh (RefreshInput refreshInput ) throws IOException {
178177 List <Segment > allSegments = new ArrayList <>();
179- for (IndexingExecutionEngine <?, ?> engine : allEngines ) {
178+ RefreshResult primaryResult = primaryEngine .refresh (refreshInput );
179+ allSegments .addAll (primaryResult .refreshedSegments ());
180+ for (IndexingExecutionEngine <?, ?> engine : secondaryEngines ) {
180181 RefreshResult result = engine .refresh (refreshInput );
181182 allSegments .addAll (result .refreshedSegments ());
182183 }
@@ -191,27 +192,55 @@ public CompositeDataFormat getDataFormat() {
191192
192193 @ Override
193194 public long getNativeBytesUsed () {
194- long total = 0 ;
195- for (IndexingExecutionEngine <?, ?> engine : allEngines ) {
195+ long total = primaryEngine . getNativeBytesUsed () ;
196+ for (IndexingExecutionEngine <?, ?> engine : secondaryEngines ) {
196197 total += engine .getNativeBytesUsed ();
197198 }
198199 return total ;
199200 }
200201
201202 @ Override
202203 public void deleteFiles (Map <String , Collection <String >> filesToDelete ) throws IOException {
203- for (IndexingExecutionEngine <?, ?> engine : allEngines ) {
204+ primaryEngine .deleteFiles (filesToDelete );
205+ for (IndexingExecutionEngine <?, ?> engine : secondaryEngines ) {
204206 engine .deleteFiles (filesToDelete );
205207 }
206208 }
207209
208210 @ Override
209211 public CompositeDocumentInput newDocumentInput () {
210212 DocumentInput <?> primaryInput = primaryEngine .newDocumentInput ();
211- Map <DataFormat , DocumentInput <?>> secondaryInputMap = new HashMap <>();
213+ Map <DataFormat , DocumentInput <?>> secondaryInputMap = new LinkedHashMap <>();
212214 for (IndexingExecutionEngine <?, ?> engine : secondaryEngines ) {
213215 secondaryInputMap .put (engine .getDataFormat (), engine .newDocumentInput ());
214216 }
215217 return new CompositeDocumentInput (primaryEngine .getDataFormat (), primaryInput , secondaryInputMap );
216218 }
219+
220+ /**
221+ * Returns the primary delegate engine.
222+ *
223+ * @return the primary engine
224+ */
225+ public IndexingExecutionEngine <?, ?> getPrimaryDelegate () {
226+ return primaryEngine ;
227+ }
228+
229+ /**
230+ * Returns the secondary delegate engines.
231+ *
232+ * @return the secondary engines
233+ */
234+ public List <IndexingExecutionEngine <? extends DataFormat , ? extends DocumentInput <?>>> getSecondaryDelegates () {
235+ return secondaryEngines ;
236+ }
237+
238+ /**
239+ * Returns the composite writer pool for this engine.
240+ *
241+ * @return the writer pool
242+ */
243+ public CompositeDataFormatWriterPool getDataFormatWriterPool () {
244+ return dataFormatWriterPool ;
245+ }
217246}
0 commit comments