Skip to content

Search: Use content-hash-versioned enrich policy names#3105

Merged
reakaleek merged 1 commit intomainfrom
mango-journey
Apr 14, 2026
Merged

Search: Use content-hash-versioned enrich policy names#3105
reakaleek merged 1 commit intomainfrom
mango-journey

Conversation

@reakaleek
Copy link
Copy Markdown
Member

What

Version enrich policy names with a SHA256 hash of their definition so re-runs don't fail on resource_already_exists_exception.

Why

Elasticsearch enrich policies can't be updated or deleted while referenced by an ingest pipeline. On repeated CI runs, PutEnrichPolicyAsync would try to create a policy that already exists and fail with a 400 error.

How

  • Policy name now includes an 8-char content hash suffix (e.g., ...-policy-a1b2c3d4)
  • Same definition across runs → same hash → "already exists" is handled gracefully
  • Changed definition → new hash → new policy created, pipeline updated, old policy cleaned up via CleanupOldPoliciesAsync

Test plan

  • Run the assembler twice against the same cluster — second run should succeed
  • Change an enrich field in code and run again — new policy should be created, old one cleaned up

Notes

Cleanup of old policies is best-effort — if deletion fails (e.g., still referenced by another pipeline), it logs a warning and continues.

🤖 Generated with Claude Code

Enrich policies can't be updated or deleted while referenced by a
pipeline. Version the policy name with a SHA256 hash of its definition
so re-runs reuse the existing policy, and definition changes create a
new one alongside the old. After the pipeline is updated to reference
the new policy, old policies are cleaned up.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@reakaleek reakaleek requested a review from a team as a code owner April 14, 2026 12:02
@reakaleek reakaleek requested a review from cotti April 14, 2026 12:02
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 14, 2026

📝 Walkthrough

Walkthrough

The ContentDateEnrichment.cs file has been modified to improve how Elasticsearch enrich policies are managed. The policy naming strategy now appends a hash of the policy JSON to the base name, allowing content-based differentiation. PutEnrichPolicyAsync now gracefully handles cases where a policy already exists by reusing it. A new CleanupOldPoliciesAsync method removes superseded policies after initialization. Helper methods BuildPolicyBody and ComputePolicyHash support the new policy construction and hashing logic. The initialization flow now includes the cleanup step.

Suggested labels

enhancement

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: versioning enrich policy names with content hashes to handle resource-already-exists errors on repeated CI runs.
Description check ✅ Passed The description is directly related to the changeset, clearly explaining the what, why, and how of the implementation, along with test plans and notes about error handling.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch mango-journey

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/Elastic.Markdown/Exporters/Elasticsearch/ContentDateEnrichment.cs`:
- Around line 203-221: The current 8-char suffix from ComputePolicyHash is too
short and can cause silent collisions; change ComputePolicyHash to return a
longer, safer digest (e.g., full hex or at least 16 chars) and, in the
enrich-policy creation flow where you inspect response.Body / errorType and
handle "resource_already_exists_exception", fetch the existing policy definition
(GET _enrich/policy/{PolicyName}) and compare its full JSON to
BuildPolicyBody().ToJsonString(); only treat the error as a no-op if the
definitions match, otherwise throw a clear InvalidOperationException mentioning
PolicyName and the mismatch. Ensure you reference ComputePolicyHash,
BuildPolicyBody, PolicyName and the existing response/errorType handling when
making these changes.
- Around line 253-255: The current filter (in ContentDateEnrichment.cs around
the block referencing PolicyBaseName and PolicyName) uses
name.StartsWith(PolicyBaseName) which is too broad; change the check so it only
matches the exact versioned naming scheme used by your code (e.g., require the
PolicyBaseName + delimiter and a version token/hash), for example by validating
the suffix with a strict pattern or regex (e.g., ensure
name.StartsWith(PolicyBaseName + "-") and the remainder matches the expected
hash/ GUID/length/hex format) and keep the existing exclusions for null and
PolicyName; update the condition that currently reads name == null || name ==
PolicyName || !name.StartsWith(PolicyBaseName, ...) to instead reject names that
do not conform to the versioned-name pattern.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: c4c86446-a970-4cc6-a834-1ba83305827e

📥 Commits

Reviewing files that changed from the base of the PR and between 7832e4c and ed4480d.

📒 Files selected for processing (1)
  • src/Elastic.Markdown/Exporters/Elasticsearch/ContentDateEnrichment.cs

@reakaleek reakaleek merged commit 0ce3494 into main Apr 14, 2026
28 of 29 checks passed
@reakaleek reakaleek deleted the mango-journey branch April 14, 2026 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant