@@ -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,14 @@ public String getHistoryUUID() {
409
415
return historyUUID ;
410
416
}
411
417
418
+ /**
419
+ * Returns how many bytes we are currently moving from heap to disk
420
+ */
421
+ @ Override
422
+ public long getWritingBytes () {
423
+ return writingBytes .get ();
424
+ }
425
+
412
426
/**
413
427
* Reads the current stored translog ID from the IW commit data. If the id is not found, recommits the current
414
428
* translog id into lucene and returns null.
@@ -1217,21 +1231,26 @@ public void refresh(String source) throws EngineException {
1217
1231
}
1218
1232
1219
1233
final void refresh (String source , SearcherScope scope ) throws EngineException {
1234
+ long bytes = 0 ;
1220
1235
// we obtain a read lock here, since we don't want a flush to happen while we are refreshing
1221
1236
// since it flushes the index as well (though, in terms of concurrency, we are allowed to do it)
1222
1237
try (ReleasableLock lock = readLock .acquire ()) {
1223
1238
ensureOpen ();
1239
+ bytes = indexWriter .ramBytesUsed ();
1224
1240
switch (scope ) {
1225
1241
case EXTERNAL :
1226
1242
// even though we maintain 2 managers we really do the heavy-lifting only once.
1227
1243
// the second refresh will only do the extra work we have to do for warming caches etc.
1244
+ writingBytes .addAndGet (bytes );
1228
1245
externalSearcherManager .maybeRefreshBlocking ();
1229
1246
// the break here is intentional we never refresh both internal / external together
1230
1247
break ;
1231
1248
case INTERNAL :
1249
+ final long versionMapBytes = versionMap .ramBytesUsedForRefresh ();
1250
+ bytes += versionMapBytes ;
1251
+ writingBytes .addAndGet (bytes );
1232
1252
internalSearcherManager .maybeRefreshBlocking ();
1233
1253
break ;
1234
-
1235
1254
default :
1236
1255
throw new IllegalArgumentException ("unknown scope: " + scope );
1237
1256
}
@@ -1245,6 +1264,8 @@ final void refresh(String source, SearcherScope scope) throws EngineException {
1245
1264
e .addSuppressed (inner );
1246
1265
}
1247
1266
throw new RefreshFailedEngineException (shardId , e );
1267
+ } finally {
1268
+ writingBytes .addAndGet (-bytes );
1248
1269
}
1249
1270
1250
1271
// TODO: maybe we should just put a scheduled job in threadPool?
@@ -1258,24 +1279,7 @@ final void refresh(String source, SearcherScope scope) throws EngineException {
1258
1279
public void writeIndexingBuffer () throws EngineException {
1259
1280
// we obtain a read lock here, since we don't want a flush to happen while we are writing
1260
1281
// 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
- }
1282
+ refresh ("write indexing buffer" , SearcherScope .INTERNAL );
1279
1283
}
1280
1284
1281
1285
@ Override
0 commit comments