@@ -140,6 +140,12 @@ public class InternalEngine extends Engine {
140
140
private final AtomicLong maxUnsafeAutoIdTimestamp = new AtomicLong (-1 );
141
141
private final CounterMetric numVersionLookups = new CounterMetric ();
142
142
private final CounterMetric numIndexVersionsLookups = new CounterMetric ();
143
+ /**
144
+ * How many bytes we are currently moving to disk, via either IndexWriter.flush or refresh. IndexingMemoryController polls this
145
+ * across all shards to decide if throttling is necessary because moving bytes to disk is falling behind vs incoming documents
146
+ * being indexed/deleted.
147
+ */
148
+ private final AtomicLong writingBytes = new AtomicLong ();
143
149
144
150
@ Nullable
145
151
private final String historyUUID ;
@@ -409,6 +415,12 @@ public String getHistoryUUID() {
409
415
return historyUUID ;
410
416
}
411
417
418
+ /** Returns how many bytes we are currently moving from indexing buffer to segments on disk */
419
+ @ Override
420
+ public long getWritingBytes () {
421
+ return writingBytes .get ();
422
+ }
423
+
412
424
/**
413
425
* Reads the current stored translog ID from the IW commit data. If the id is not found, recommits the current
414
426
* translog id into lucene and returns null.
@@ -1217,21 +1229,26 @@ public void refresh(String source) throws EngineException {
1217
1229
}
1218
1230
1219
1231
final void refresh (String source , SearcherScope scope ) throws EngineException {
1232
+ long bytes = 0 ;
1220
1233
// we obtain a read lock here, since we don't want a flush to happen while we are refreshing
1221
1234
// since it flushes the index as well (though, in terms of concurrency, we are allowed to do it)
1222
1235
try (ReleasableLock lock = readLock .acquire ()) {
1223
1236
ensureOpen ();
1237
+ bytes = indexWriter .ramBytesUsed ();
1224
1238
switch (scope ) {
1225
1239
case EXTERNAL :
1226
1240
// even though we maintain 2 managers we really do the heavy-lifting only once.
1227
1241
// the second refresh will only do the extra work we have to do for warming caches etc.
1242
+ writingBytes .addAndGet (bytes );
1228
1243
externalSearcherManager .maybeRefreshBlocking ();
1229
1244
// the break here is intentional we never refresh both internal / external together
1230
1245
break ;
1231
1246
case INTERNAL :
1247
+ final long versionMapBytes = versionMap .ramBytesUsedForRefresh ();
1248
+ bytes += versionMapBytes ;
1249
+ writingBytes .addAndGet (bytes );
1232
1250
internalSearcherManager .maybeRefreshBlocking ();
1233
1251
break ;
1234
-
1235
1252
default :
1236
1253
throw new IllegalArgumentException ("unknown scope: " + scope );
1237
1254
}
@@ -1245,6 +1262,8 @@ final void refresh(String source, SearcherScope scope) throws EngineException {
1245
1262
e .addSuppressed (inner );
1246
1263
}
1247
1264
throw new RefreshFailedEngineException (shardId , e );
1265
+ } finally {
1266
+ writingBytes .addAndGet (-bytes );
1248
1267
}
1249
1268
1250
1269
// TODO: maybe we should just put a scheduled job in threadPool?
@@ -1258,24 +1277,7 @@ final void refresh(String source, SearcherScope scope) throws EngineException {
1258
1277
public void writeIndexingBuffer () throws EngineException {
1259
1278
// we obtain a read lock here, since we don't want a flush to happen while we are writing
1260
1279
// since it flushes the index as well (though, in terms of concurrency, we are allowed to do it)
1261
- try (ReleasableLock lock = readLock .acquire ()) {
1262
- ensureOpen ();
1263
- final long versionMapBytes = versionMap .ramBytesUsedForRefresh ();
1264
- final long indexingBufferBytes = indexWriter .ramBytesUsed ();
1265
- logger .debug ("use refresh to write indexing buffer (heap size=[{}]), to also clear version map (heap size=[{}])" ,
1266
- new ByteSizeValue (indexingBufferBytes ), new ByteSizeValue (versionMapBytes ));
1267
- refresh ("write indexing buffer" , SearcherScope .INTERNAL );
1268
- } catch (AlreadyClosedException e ) {
1269
- failOnTragicEvent (e );
1270
- throw e ;
1271
- } catch (Exception e ) {
1272
- try {
1273
- failEngine ("writeIndexingBuffer failed" , e );
1274
- } catch (Exception inner ) {
1275
- e .addSuppressed (inner );
1276
- }
1277
- throw new RefreshFailedEngineException (shardId , e );
1278
- }
1280
+ refresh ("write indexing buffer" , SearcherScope .INTERNAL );
1279
1281
}
1280
1282
1281
1283
@ Override
0 commit comments