-
Notifications
You must be signed in to change notification settings - Fork 123
Fix state consistency issues with ML Datafeed start and end times. #1563
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
Open
tobio
wants to merge
4
commits into
main
Choose a base branch
from
ml-start-time
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
edd7ee8
Add acceptance test reproducing the failing behaviour
tobio aec85c7
Set start to unknown if starting an existing datafeed and no explicit…
tobio ecacda9
Use the RFC3339 time string
tobio 88a5c1c
Persist the timezone from a user supplied time
tobio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,6 +74,7 @@ func GetSchema() schema.Schema { | |
| Computed: true, | ||
| PlanModifiers: []planmodifier.String{ | ||
| stringplanmodifier.UseStateForUnknown(), | ||
| SetUnknownIfStateHasChanges(), | ||
| }, | ||
| }, | ||
| "end": schema.StringAttribute{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we treat it similarly to “start”? (e.g. use |
||
|
|
||
51 changes: 51 additions & 0 deletions
51
internal/elasticsearch/ml/datafeed_state/set_unknown_if_state_has_changes.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package datafeed_state | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/elastic/terraform-provider-elasticstack/internal/utils" | ||
| "github.com/hashicorp/terraform-plugin-framework/path" | ||
| "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| ) | ||
|
|
||
| // SetUnknownIfStateHasChanges returns a plan modifier that sets the current attribute to unknown | ||
| // if the state attribute has changed between state and config. | ||
| func SetUnknownIfStateHasChanges() planmodifier.String { | ||
| return setUnknownIfStateHasChanges{} | ||
| } | ||
|
|
||
| type setUnknownIfStateHasChanges struct{} | ||
|
|
||
| func (s setUnknownIfStateHasChanges) Description(ctx context.Context) string { | ||
| return "Sets the attribute value to unknown if the state attribute has changed" | ||
| } | ||
|
|
||
| func (s setUnknownIfStateHasChanges) MarkdownDescription(ctx context.Context) string { | ||
| return s.Description(ctx) | ||
| } | ||
|
|
||
| func (s setUnknownIfStateHasChanges) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) { | ||
| // Only apply this modifier if we have both state and config | ||
| if req.State.Raw.IsNull() || req.Config.Raw.IsNull() { | ||
| return | ||
| } | ||
|
|
||
| // Continue using the config value if it's explicitly set | ||
| if utils.IsKnown(req.ConfigValue) { | ||
| return | ||
| } | ||
|
|
||
| // Get the state attribute from state and config to check if it has changed | ||
| var stateValue, configValue types.String | ||
| resp.Diagnostics.Append(req.State.GetAttribute(ctx, path.Root("state"), &stateValue)...) | ||
| resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("state"), &configValue)...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| // If the state attribute has changed between state and config, set the current attribute to Unknown | ||
| if !stateValue.Equal(configValue) { | ||
| resp.PlanValue = types.StringUnknown() | ||
| } | ||
| } |
121 changes: 121 additions & 0 deletions
121
...l/datafeed_state/testdata/TestAccResourceMLDatafeedState_multiStep/closed_stopped/main.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| variable "job_id" { | ||
| description = "The ML job ID" | ||
| type = string | ||
| } | ||
|
|
||
| variable "datafeed_id" { | ||
| description = "The ML datafeed ID" | ||
| type = string | ||
| } | ||
|
|
||
| variable "index_name" { | ||
| description = "The index name" | ||
| type = string | ||
| } | ||
|
|
||
| provider "elasticstack" { | ||
| elasticsearch {} | ||
| } | ||
|
|
||
| resource "elasticstack_elasticsearch_index" "test" { | ||
| name = var.index_name | ||
| deletion_protection = false | ||
| mappings = jsonencode({ | ||
| properties = { | ||
| "@timestamp" = { | ||
| type = "date" | ||
| } | ||
| nginx = { | ||
| properties = { | ||
| access = { | ||
| properties = { | ||
| body_sent = { | ||
| properties = { | ||
| bytes = { | ||
| type = "long" | ||
| } | ||
| } | ||
| } | ||
| geoip = { | ||
| properties = { | ||
| city_name = { | ||
| type = "keyword" | ||
| } | ||
| } | ||
| } | ||
| user_agent = { | ||
| properties = { | ||
| build = { | ||
| type = "keyword" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| resource "elasticstack_elasticsearch_ml_anomaly_detection_job" "nginx" { | ||
| job_id = var.job_id | ||
| description = "Anomaly detection for network traffic" | ||
| analysis_config = { | ||
| bucket_span = "15m" | ||
| detectors = [ | ||
| { | ||
| function = "count" | ||
| detector_description = "count" | ||
| }, | ||
| { | ||
| function = "mean" | ||
| field_name = "nginx.access.body_sent.bytes" | ||
| detector_description = "mean(\"nginx.access.body_sent.bytes\")" | ||
| } | ||
| ] | ||
| influencers = ["nginx.access.geoip.city_name", "nginx.access.user_agent.build"] | ||
| model_prune_window = "30d" | ||
| } | ||
| analysis_limits = { | ||
| model_memory_limit = "10MB" | ||
| categorization_examples_limit = 4 | ||
| } | ||
| data_description = { | ||
| time_field = "@timestamp" | ||
| time_format = "epoch_ms" | ||
| } | ||
| model_snapshot_retention_days = 10 | ||
| daily_model_snapshot_retention_after_days = 1 | ||
| } | ||
|
|
||
| resource "elasticstack_elasticsearch_ml_datafeed" "datafeed_nginx" { | ||
| datafeed_id = var.datafeed_id | ||
| job_id = elasticstack_elasticsearch_ml_anomaly_detection_job.nginx.job_id | ||
| query = jsonencode({ | ||
| bool = { | ||
| must = [ | ||
| { | ||
| match_all = {} | ||
| } | ||
| ] | ||
| } | ||
| }) | ||
| indices = [elasticstack_elasticsearch_index.test.name] | ||
| } | ||
|
|
||
| resource "elasticstack_elasticsearch_ml_job_state" "nginx" { | ||
| job_id = elasticstack_elasticsearch_ml_anomaly_detection_job.nginx.job_id | ||
| state = "closed" | ||
| } | ||
|
|
||
| resource "elasticstack_elasticsearch_ml_datafeed_state" "nginx" { | ||
| datafeed_id = elasticstack_elasticsearch_ml_datafeed.datafeed_nginx.datafeed_id | ||
| state = "stopped" | ||
| force = true | ||
|
|
||
| depends_on = [ | ||
| elasticstack_elasticsearch_ml_datafeed.datafeed_nginx, | ||
| elasticstack_elasticsearch_ml_job_state.nginx | ||
| ] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable
diagsis reused for both start and end conversions, shadowing the outerdiagsvariable declared at line 44. This makes it unclear which diagnostics are being accumulated. Use distinct variable names likestartDiagsandendDiagsto avoid confusion.