Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update replicas for downsample index only when necessary #110467

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,22 @@ protected void masterOperation(
delegate.onFailure(e);
return;
}

/*
* When creating the downsample index, we copy the index.number_of_shards from source index,
* and we set the index.number_of_replicas to 0, to avoid replicating the index being built.
* Also, we set the index.refresh_interval to -1.
* We will set the correct number of replicas and refresh the index later.
*
* We should note that there is a risk of losing a node during the downsample process. In this
* case downsample will fail.
*/
int minNumReplicas = clusterService.getSettings().getAsInt(Downsample.DOWNSAMPLE_MIN_NUMBER_OF_REPLICAS_NAME, 0);

// 3. Create downsample index
createDownsampleIndex(
clusterService.getSettings(),
downsampleIndexName,
minNumReplicas,
sourceIndexMetadata,
mapping,
request,
Expand All @@ -353,6 +365,7 @@ protected void masterOperation(
performShardDownsampling(
request,
delegate,
minNumReplicas,
sourceIndexMetadata,
downsampleIndexName,
parentTask,
Expand Down Expand Up @@ -382,6 +395,7 @@ protected void masterOperation(
performShardDownsampling(
request,
delegate,
minNumReplicas,
sourceIndexMetadata,
downsampleIndexName,
parentTask,
Expand Down Expand Up @@ -451,6 +465,7 @@ private boolean canShortCircuit(
private void performShardDownsampling(
DownsampleAction.Request request,
ActionListener<AcknowledgedResponse> listener,
int minNumReplicas,
IndexMetadata sourceIndexMetadata,
String downsampleIndexName,
TaskId parentTask,
Expand Down Expand Up @@ -509,7 +524,15 @@ public void onResponse(PersistentTasksCustomMetadata.PersistentTask<PersistentTa
logger.info("Downsampling task [" + persistentTaskId + " completed for shard " + params.shardId());
if (countDown.decrementAndGet() == 0) {
logger.info("All downsampling tasks completed [" + numberOfShards + "]");
updateTargetIndexSettingStep(request, listener, sourceIndexMetadata, downsampleIndexName, parentTask, startTime);
updateTargetIndexSettingStep(
request,
listener,
minNumReplicas,
sourceIndexMetadata,
downsampleIndexName,
parentTask,
startTime
);
}
}

Expand Down Expand Up @@ -556,6 +579,7 @@ public void onFailure(Exception e) {
private void updateTargetIndexSettingStep(
final DownsampleAction.Request request,
final ActionListener<AcknowledgedResponse> listener,
int minNumReplicas,
final IndexMetadata sourceIndexMetadata,
final String downsampleIndexName,
final TaskId parentTask,
Expand All @@ -564,7 +588,7 @@ private void updateTargetIndexSettingStep(
// 4. Make downsample index read-only and set the correct number of replicas
final Settings.Builder settings = Settings.builder().put(IndexMetadata.SETTING_BLOCKS_WRITE, true);
// Number of replicas had been previously set to 0 to speed up index population
if (sourceIndexMetadata.getNumberOfReplicas() > 0) {
if (sourceIndexMetadata.getNumberOfReplicas() > 0 && minNumReplicas == 0) {
settings.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, sourceIndexMetadata.getNumberOfReplicas());
}
// Setting index.hidden has been initially set to true. We revert this to the value of the
Expand Down Expand Up @@ -842,28 +866,18 @@ private static void addDynamicTemplates(final XContentBuilder builder) throws IO
}

private void createDownsampleIndex(
Settings settings,
String downsampleIndexName,
int minNumReplicas,
IndexMetadata sourceIndexMetadata,
String mapping,
DownsampleAction.Request request,
ActionListener<AcknowledgedResponse> listener
) {
/*
* When creating the downsample index, we copy the index.number_of_shards from source index,
* and we set the index.number_of_replicas to 0, to avoid replicating the index being built.
* Also, we set the index.refresh_interval to -1.
* We will set the correct number of replicas and refresh the index later.
*
* We should note that there is a risk of losing a node during the downsample process. In this
* case downsample will fail.
*/
int numberOfReplicas = settings.getAsInt(Downsample.DOWNSAMPLE_MIN_NUMBER_OF_REPLICAS_NAME, 0);
var downsampleInterval = request.getDownsampleConfig().getInterval().toString();
Settings.Builder builder = Settings.builder()
.put(IndexMetadata.SETTING_INDEX_HIDDEN, true)
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, sourceIndexMetadata.getNumberOfShards())
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, String.valueOf(numberOfReplicas))
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, minNumReplicas)
.put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), "-1")
.put(IndexMetadata.INDEX_DOWNSAMPLE_STATUS.getKey(), DownsampleTaskStatus.STARTED)
.put(IndexMetadata.INDEX_DOWNSAMPLE_INTERVAL.getKey(), downsampleInterval)
Expand Down