Skip to content

Commit 674559b

Browse files
committed
Add test for timeouts on ML job state
1 parent 0151a26 commit 674559b

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed

internal/elasticsearch/ml/datafeed_state/create.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package datafeed_state
22

33
import (
44
"context"
5+
"fmt"
56
"time"
67

8+
"github.com/elastic/terraform-provider-elasticstack/internal/diagutil"
79
"github.com/hashicorp/terraform-plugin-framework/resource"
810
)
911

@@ -23,5 +25,9 @@ func (r *mlDatafeedStateResource) Create(ctx context.Context, req resource.Creat
2325
}
2426

2527
diags = r.update(ctx, req.Plan, &resp.State, createTimeout)
28+
if diags.Contains(diagutil.FrameworkDiagFromError(context.DeadlineExceeded)[0]) {
29+
diags.AddError("Operation timed out", fmt.Sprintf("The operation to create the ML datafeed state timed out after %s. You may need to allocate more free memory within ML nodes by either closing other jobs, or increasing the overall ML memory. You may retry the operation.", createTimeout))
30+
}
31+
2632
resp.Diagnostics.Append(diags...)
2733
}

internal/elasticsearch/ml/datafeed_state/update.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
99
"github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch"
10+
"github.com/elastic/terraform-provider-elasticstack/internal/diagutil"
1011
"github.com/elastic/terraform-provider-elasticstack/internal/elasticsearch/ml/datafeed"
1112
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
1213
"github.com/hashicorp/terraform-plugin-framework/diag"
@@ -32,6 +33,10 @@ func (r *mlDatafeedStateResource) Update(ctx context.Context, req resource.Updat
3233
}
3334

3435
diags = r.update(ctx, req.Plan, &resp.State, updateTimeout)
36+
if diags.Contains(diagutil.FrameworkDiagFromError(context.DeadlineExceeded)[0]) {
37+
diags.AddError("Operation timed out", fmt.Sprintf("The operation to update the ML datafeed state timed out after %s. You may need to allocate more free memory within ML nodes by either closing other jobs, or increasing the overall ML memory. You may retry the operation.", updateTimeout))
38+
}
39+
3540
resp.Diagnostics.Append(diags...)
3641
if resp.Diagnostics.HasError() {
3742
return

internal/elasticsearch/ml/job_state/acc_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,23 @@ func TestAccResourceMLJobStateImport(t *testing.T) {
115115
},
116116
})
117117
}
118+
119+
func TestAccResourceMLJobState_timeouts(t *testing.T) {
120+
jobID := fmt.Sprintf("test-job-%s", sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum))
121+
indexName := fmt.Sprintf("test-datafeed-index-%s", sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum))
122+
123+
resource.Test(t, resource.TestCase{
124+
PreCheck: func() { acctest.PreCheck(t) },
125+
Steps: []resource.TestStep{
126+
{
127+
ProtoV6ProviderFactories: acctest.Providers,
128+
ConfigDirectory: acctest.NamedTestCaseDirectory("timeouts"),
129+
ConfigVariables: config.Variables{
130+
"job_id": config.StringVariable(jobID),
131+
"index_name": config.StringVariable(indexName),
132+
},
133+
ExpectError: regexp.MustCompile("Operation timed out"),
134+
},
135+
},
136+
})
137+
}

internal/elasticsearch/ml/job_state/create.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package job_state
22

33
import (
44
"context"
5+
"fmt"
56
"time"
67

8+
"github.com/elastic/terraform-provider-elasticstack/internal/diagutil"
79
"github.com/hashicorp/terraform-plugin-framework/resource"
810
)
911

@@ -23,5 +25,9 @@ func (r *mlJobStateResource) Create(ctx context.Context, req resource.CreateRequ
2325
}
2426

2527
diags = r.update(ctx, req.Plan, &resp.State, createTimeout)
28+
if diags.Contains(diagutil.FrameworkDiagFromError(context.DeadlineExceeded)[0]) {
29+
diags.AddError("Operation timed out", fmt.Sprintf("The operation to create the ML job state timed out after %s. You may need to allocate more free memory within ML nodes by either closing other jobs, or increasing the overall ML memory. You may retry the operation.", createTimeout))
30+
}
31+
2632
resp.Diagnostics.Append(diags...)
2733
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
variable "job_id" {
2+
description = "The ML job ID"
3+
type = string
4+
}
5+
6+
variable "index_name" {
7+
description = "The index name"
8+
type = string
9+
}
10+
11+
provider "elasticstack" {
12+
elasticsearch {}
13+
}
14+
15+
resource "elasticstack_elasticsearch_index" "test" {
16+
name = var.index_name
17+
deletion_protection = false
18+
mappings = jsonencode({
19+
properties = {
20+
"@timestamp" = {
21+
type = "date"
22+
}
23+
value = {
24+
type = "double"
25+
}
26+
}
27+
})
28+
}
29+
30+
resource "elasticstack_elasticsearch_ml_anomaly_detection_job" "test" {
31+
job_id = var.job_id
32+
description = "Test job for datafeed state timeout testing with large memory"
33+
analysis_config = {
34+
bucket_span = "1h"
35+
detectors = [{
36+
function = "count"
37+
detector_description = "count"
38+
}]
39+
}
40+
data_description = {
41+
time_field = "@timestamp"
42+
time_format = "epoch_ms"
43+
}
44+
analysis_limits = {
45+
model_memory_limit = "784mb" # Large memory requirement close to cluster limit
46+
}
47+
allow_lazy_open = true # This should cause datafeed to wait for available node
48+
}
49+
50+
resource "elasticstack_elasticsearch_ml_job_state" "test" {
51+
job_id = elasticstack_elasticsearch_ml_anomaly_detection_job.test.job_id
52+
state = "opened"
53+
54+
timeouts = {
55+
create = "10s"
56+
update = "10s"
57+
}
58+
}

internal/elasticsearch/ml/job_state/update.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
99
"github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch"
10+
"github.com/elastic/terraform-provider-elasticstack/internal/diagutil"
1011
"github.com/hashicorp/terraform-plugin-framework/diag"
1112
"github.com/hashicorp/terraform-plugin-framework/resource"
1213
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
@@ -30,6 +31,10 @@ func (r *mlJobStateResource) Update(ctx context.Context, req resource.UpdateRequ
3031
}
3132

3233
diags = r.update(ctx, req.Plan, &resp.State, updateTimeout)
34+
if diags.Contains(diagutil.FrameworkDiagFromError(context.DeadlineExceeded)[0]) {
35+
diags.AddError("Operation timed out", fmt.Sprintf("The operation to update the ML job state timed out after %s. You may need to allocate more free memory within ML nodes by either closing other jobs, or increasing the overall ML memory. You may retry the operation.", updateTimeout))
36+
}
37+
3338
resp.Diagnostics.Append(diags...)
3439
if resp.Diagnostics.HasError() {
3540
return

0 commit comments

Comments
 (0)