Skip to content

Commit 60e052e

Browse files
committed
Fix serverless compatibility: nil params in pool requests and unsupported template settings
Backport of fix-serverless-incompatible-issues to 11.x branch. - Guard against nil params in perform_request_to_url with params ||= {} - Replace nil with {} in http_client pool.put calls (template_put, rollover_alias_put, ilm_policy_put) to prevent NoMethodError on serverless when merging EAV headers - Strip serverless-incompatible template settings (number_of_shards, number_of_replicas) before installing templates on serverless ES
1 parent 642f7a4 commit 60e052e

5 files changed

Lines changed: 93 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 11.22.16
2+
- Fix serverless compatibility: guard against nil params in pool requests and strip unsupported template settings (number_of_shards, number_of_replicas) on serverless
3+
14
## 11.22.15
25
- Improves the logging experience when DLQ used [#1258](https://github.com/logstash-plugins/logstash-output-elasticsearch/pull/1258)
36

lib/logstash/outputs/elasticsearch/http_client.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def template_exists?(template_endpoint, name)
433433
def template_put(template_endpoint, name, template)
434434
path = "#{template_endpoint}/#{name}"
435435
logger.info("Installing Elasticsearch template", name: name)
436-
@pool.put(path, nil, LogStash::Json.dump(template))
436+
@pool.put(path, {}, LogStash::Json.dump(template))
437437
rescue ::LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError => e
438438
raise e unless e.response_code == 404
439439
end
@@ -447,7 +447,7 @@ def rollover_alias_exists?(name)
447447

448448
# Create a new rollover alias
449449
def rollover_alias_put(alias_name, alias_definition)
450-
@pool.put(CGI::escape(alias_name), nil, LogStash::Json.dump(alias_definition))
450+
@pool.put(CGI::escape(alias_name), {}, LogStash::Json.dump(alias_definition))
451451
logger.info("Created rollover alias", name: alias_name)
452452
# If the rollover alias already exists, ignore the error that comes back from Elasticsearch
453453
rescue ::LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError => e
@@ -473,7 +473,7 @@ def ilm_policy_exists?(name)
473473
def ilm_policy_put(name, policy)
474474
path = "_ilm/policy/#{name}"
475475
logger.info("Installing ILM policy #{policy}", name: name)
476-
@pool.put(path, nil, LogStash::Json.dump(policy))
476+
@pool.put(path, {}, LogStash::Json.dump(policy))
477477
end
478478

479479

lib/logstash/outputs/elasticsearch/template_manager.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def self.install_template(plugin)
2828
end
2929

3030
add_ilm_settings_to_template(plugin, template) if plugin.ilm_in_use?
31+
strip_serverless_incompatible_settings(template) if plugin.serverless?
3132
plugin.logger.debug("Attempting to install template", template: template)
3233
install(plugin.client, template_endpoint(plugin), template_name(plugin), template, plugin.template_overwrite)
3334
end
@@ -129,5 +130,19 @@ def self.index_template_api?(plugin)
129130
end
130131
end
131132

133+
SERVERLESS_INCOMPATIBLE_SETTINGS = %w[
134+
number_of_shards
135+
index.number_of_shards
136+
number_of_replicas
137+
index.number_of_replicas
138+
].freeze
139+
140+
def self.strip_serverless_incompatible_settings(template)
141+
settings = template.dig('template', 'settings') || template['settings']
142+
return unless settings
143+
144+
SERVERLESS_INCOMPATIBLE_SETTINGS.each { |s| settings.delete(s) }
145+
end
146+
132147
end
133148
end end end

spec/unit/outputs/elasticsearch/template_manager_spec.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,75 @@
186186

187187
end
188188
end
189+
190+
describe ".strip_serverless_incompatible_settings" do
191+
context "with composable template (template.settings)" do
192+
let(:template) do
193+
{
194+
"template" => {
195+
"settings" => {
196+
"index.refresh_interval" => "5s",
197+
"number_of_shards" => 1,
198+
"number_of_replicas" => 0
199+
}
200+
}
201+
}
202+
end
203+
204+
it "removes number_of_shards and number_of_replicas" do
205+
described_class.strip_serverless_incompatible_settings(template)
206+
expect(template["template"]["settings"]).not_to have_key("number_of_shards")
207+
expect(template["template"]["settings"]).not_to have_key("number_of_replicas")
208+
end
209+
210+
it "preserves compatible settings" do
211+
described_class.strip_serverless_incompatible_settings(template)
212+
expect(template["template"]["settings"]["index.refresh_interval"]).to eq("5s")
213+
end
214+
end
215+
216+
context "with index-prefixed setting keys" do
217+
let(:template) do
218+
{
219+
"template" => {
220+
"settings" => {
221+
"index.number_of_shards" => 1,
222+
"index.number_of_replicas" => 0
223+
}
224+
}
225+
}
226+
end
227+
228+
it "removes index-prefixed variants" do
229+
described_class.strip_serverless_incompatible_settings(template)
230+
expect(template["template"]["settings"]).not_to have_key("index.number_of_shards")
231+
expect(template["template"]["settings"]).not_to have_key("index.number_of_replicas")
232+
end
233+
end
234+
235+
context "with legacy template (top-level settings)" do
236+
let(:template) do
237+
{
238+
"settings" => {
239+
"index.refresh_interval" => "5s",
240+
"number_of_shards" => 1
241+
}
242+
}
243+
end
244+
245+
it "removes incompatible settings from top-level settings" do
246+
described_class.strip_serverless_incompatible_settings(template)
247+
expect(template["settings"]).not_to have_key("number_of_shards")
248+
expect(template["settings"]["index.refresh_interval"]).to eq("5s")
249+
end
250+
end
251+
252+
context "with no settings" do
253+
let(:template) { { "mappings" => {} } }
254+
255+
it "does not raise" do
256+
expect { described_class.strip_serverless_incompatible_settings(template) }.not_to raise_error
257+
end
258+
end
259+
end
189260
end

version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
11.22.15
1+
11.22.16

0 commit comments

Comments
 (0)