Skip to content

Commit 80a797f

Browse files
committed
Update Settings: Allow to dynamically update index.translog settings, closes elastic#765.
1 parent c2a0e0b commit 80a797f

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/index/translog/TranslogService.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.index.engine.EngineClosedException;
2929
import org.elasticsearch.index.engine.FlushNotAllowedEngineException;
3030
import org.elasticsearch.index.settings.IndexSettings;
31+
import org.elasticsearch.index.settings.IndexSettingsService;
3132
import org.elasticsearch.index.shard.AbstractIndexShardComponent;
3233
import org.elasticsearch.index.shard.IndexShardState;
3334
import org.elasticsearch.index.shard.ShardId;
@@ -45,23 +46,28 @@ public class TranslogService extends AbstractIndexShardComponent {
4546

4647
private final ThreadPool threadPool;
4748

49+
private final IndexSettingsService indexSettingsService;
50+
4851
private final IndexShard indexShard;
4952

5053
private final Translog translog;
5154

52-
private final int flushThresholdOperations;
55+
private int flushThresholdOperations;
5356

54-
private final ByteSizeValue flushThresholdSize;
57+
private ByteSizeValue flushThresholdSize;
5558

56-
private final TimeValue flushThresholdPeriod;
59+
private TimeValue flushThresholdPeriod;
5760

5861
private final TimeValue interval;
5962

6063
private ScheduledFuture future;
6164

62-
@Inject public TranslogService(ShardId shardId, @IndexSettings Settings indexSettings, ThreadPool threadPool, IndexShard indexShard, Translog translog) {
65+
private final ApplySettings applySettings = new ApplySettings();
66+
67+
@Inject public TranslogService(ShardId shardId, @IndexSettings Settings indexSettings, IndexSettingsService indexSettingsService, ThreadPool threadPool, IndexShard indexShard, Translog translog) {
6368
super(shardId, indexSettings);
6469
this.threadPool = threadPool;
70+
this.indexSettingsService = indexSettingsService;
6571
this.indexShard = indexShard;
6672
this.translog = translog;
6773

@@ -73,13 +79,36 @@ public class TranslogService extends AbstractIndexShardComponent {
7379
logger.debug("interval [{}], flush_threshold_ops [{}], flush_threshold_size [{}], flush_threshold_period [{}]", interval, flushThresholdOperations, flushThresholdSize, flushThresholdPeriod);
7480

7581
this.future = threadPool.schedule(interval, ThreadPool.Names.SAME, new TranslogBasedFlush());
82+
83+
indexSettingsService.addListener(applySettings);
7684
}
7785

7886

7987
public void close() {
88+
indexSettingsService.removeListener(applySettings);
8089
this.future.cancel(true);
8190
}
8291

92+
class ApplySettings implements IndexSettingsService.Listener {
93+
@Override public void onRefreshSettings(Settings settings) {
94+
int flushThresholdOperations = settings.getAsInt("index.translog.flush_threshold_ops", TranslogService.this.flushThresholdOperations);
95+
if (flushThresholdOperations != TranslogService.this.flushThresholdOperations) {
96+
logger.info("updating flush_threshold_ops from [{}] to [{}]", TranslogService.this.flushThresholdOperations, flushThresholdOperations);
97+
TranslogService.this.flushThresholdOperations = flushThresholdOperations;
98+
}
99+
ByteSizeValue flushThresholdSize = settings.getAsBytesSize("index.translog.flush_threshold_size", TranslogService.this.flushThresholdSize);
100+
if (!flushThresholdSize.equals(TranslogService.this.flushThresholdSize)) {
101+
logger.info("updating flush_threshold_size from [{}] to [{}]", TranslogService.this.flushThresholdSize, flushThresholdSize);
102+
TranslogService.this.flushThresholdSize = flushThresholdSize;
103+
}
104+
TimeValue flushThresholdPeriod = settings.getAsTime("index.translog.flush_threshold_period", TranslogService.this.flushThresholdPeriod);
105+
if (!flushThresholdPeriod.equals(TranslogService.this.flushThresholdPeriod)) {
106+
logger.info("updating flush_threshold_period from [{}] to [{}]", TranslogService.this.flushThresholdPeriod, flushThresholdPeriod);
107+
TranslogService.this.flushThresholdPeriod = flushThresholdPeriod;
108+
}
109+
}
110+
}
111+
83112
private class TranslogBasedFlush implements Runnable {
84113

85114
private volatile long lastFlushTime = System.currentTimeMillis();

0 commit comments

Comments
 (0)