Skip to content

Commit 892c34a

Browse files
committed
Improve logging and error handling
1 parent 261abff commit 892c34a

File tree

8 files changed

+75
-12
lines changed

8 files changed

+75
-12
lines changed

lib/logstash/outputs/elasticsearch/common.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,9 @@ def verify_ilm
403403
end
404404

405405
def verify_ilm_config
406-
# Default index name is logstash-XXXX and the default write alias is logstash - should we just change overwrite the default index name here?
407-
@index = @ilm_write_alias if @index == LogStash::Outputs::ElasticSearch::CommonConfigs::DEFAULT_INDEX_NAME
408-
raise LogStash::ConfigurationError, "ILM configuration error: The index name #{@index} does not match the write alias #{@ilm_write_alias}" if @ilm_write_alias != @index
409-
raise LogStash::ConfigurationError, "ILM configuration error: The template name #{@template_name} does not match the write alias #{@ilm_write_alias}" if @ilm_write_alias != @template_name
406+
# Overwrite the index with the write alias.
407+
@logger.warn "Overwriting index name with rollover alias #{@ilm_write_alias}" if @index != LogStash::Outputs::ElasticSearch::CommonConfigs::DEFAULT_INDEX_NAME
408+
@index = @ilm_write_alias
410409
verify_ilm_policy unless ilm_policy_default?
411410
end
412411

@@ -446,8 +445,6 @@ def write_alias_payload
446445
}
447446
end
448447

449-
450-
451448
def policy_payload
452449
policy_path = ::File.expand_path(ILM_POLICY_PATH, ::File.dirname(__FILE__))
453450
LogStash::Json.load(::IO.read(policy_path))

lib/logstash/outputs/elasticsearch/http_client.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,21 @@ def write_alias_exists?(name)
362362
# Create a new write alias
363363
def write_alias_put(alias_name, alias_definition)
364364
logger.info("Creating write alias #{alias_name}")
365-
@pool.put(CGI::escape(alias_name), nil, LogStash::Json.dump(alias_definition))
365+
begin
366+
@pool.put(CGI::escape(alias_name), nil, LogStash::Json.dump(alias_definition))
367+
# If the rollover alias already exists, ignore the error that comes back from Elasticsearch
368+
#
369+
rescue ::LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError => e
370+
if e.response_code == 400 && e.response_body
371+
error_body = LogStash::Json.load(e.response_bod)
372+
if error_body['error'] && error_body['error']['root_cause'] &&
373+
error_body['error']['root_cause'][0] && error_body['error']['root_cause'][0]['type'] == 'resource_already_exists_exception'
374+
logger.info("Write Alias #{alias_name} already exists. Skipping")
375+
return
376+
end
377+
end
378+
raise e
379+
end
366380
end
367381

368382
def get_xpack_info

lib/logstash/outputs/elasticsearch/template_manager.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ def self.install(client, template_name, template, template_overwrite)
2424

2525
def self.add_ilm_settings_to_template(plugin, template)
2626
# Include ilm settings in template:
27+
plugin.logger.info("Overwriting template name and pattern as ILM is enabled.")
2728
template['template'] = "#{plugin.ilm_write_alias}-*"
29+
if template['settings'] && (template['settings']['index.lifecycle.name'] || template['settings']['index.lifecycle.rollover_alias'])
30+
plugin.logger.info("Overwriting index lifecycle name and rollover alias as ILM is enabled.")
31+
end
2832
template['settings'].update({ 'index.lifecycle.name' => plugin.ilm_policy, 'index.lifecycle.rollover_alias' => plugin.ilm_write_alias})
2933
end
3034

spec/es_spec_helper.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'elasticsearch'
44
require_relative "support/elasticsearch/api/actions/delete_ilm_policy"
55
require_relative "support/elasticsearch/api/actions/get_alias"
6+
require_relative "support/elasticsearch/api/actions/put_alias"
67
require_relative "support/elasticsearch/api/actions/get_ilm_policy"
78
require_relative "support/elasticsearch/api/actions/put_ilm_policy"
89

@@ -84,6 +85,17 @@ def put_policy(client, policy_name, policy)
8485
client.put_ilm_policy({:name => policy_name, :body=> policy})
8586
end
8687

88+
def put_alias(client, the_alias, index)
89+
body = {
90+
"aliases" => {
91+
index => {
92+
"is_write_index"=> true
93+
}
94+
}
95+
}
96+
client.put_alias({name: the_alias, body: body})
97+
end
98+
8799
def clean_ilm(client)
88100
client.get_ilm_policy.each_key {|key| client.delete_ilm_policy(name: key)}
89101
end

spec/fixtures/template-with-policy-es7x.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"version" : 60001,
44
"settings" : {
55
"index.refresh_interval" : "1s",
6-
"number_of_shards": 1,
7-
"index.lifecycle.name": "custom-policy",
8-
"index.lifecycle.rollover_alias": "custom"
6+
"number_of_shards": 1
97
},
108
"mappings" : {
119
"_doc" : {

spec/integration/outputs/ilm_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
}
193193
let (:policy) { small_max_doc_policy }
194194

195+
195196
let (:small_max_doc_policy) {
196197
{"policy" => {
197198
"phases"=> {
@@ -416,6 +417,7 @@
416417
before :each do
417418
put_policy(@es,ilm_policy_name, policy)
418419
end
420+
419421
it 'should create the write alias' do
420422
expect(@es.indices.exists_alias(index: ilm_write_alias)).to be_falsey
421423
subject.register
@@ -424,6 +426,18 @@
424426
expect(@es.get_alias(name: ilm_write_alias)).to include("#{ilm_write_alias}-#{todays_date}-000001")
425427
end
426428

429+
context 'when the custom write alias already exists' do
430+
it 'should ignore the already exists error' do
431+
expect(@es.indices.exists_alias(index: ilm_write_alias)).to be_falsey
432+
put_alias(@es, "#{ilm_write_alias}-#{todays_date}-000001", ilm_write_alias)
433+
expect(@es.indices.exists_alias(index: ilm_write_alias)).to be_truthy
434+
subject.register
435+
sleep(1)
436+
expect(@es.get_alias(name: ilm_write_alias)).to include("#{ilm_write_alias}-#{todays_date}-000001")
437+
end
438+
439+
end
440+
427441
it 'should write the ILM settings into the template' do
428442
subject.register
429443
sleep(1)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
# or more contributor license agreements. Licensed under the Elastic License;
3+
# you may not use this file except in compliance with the Elastic License.
4+
5+
module Elasticsearch
6+
module API
7+
module Actions
8+
9+
# @option arguments [String] :name The name of the alias (*Required*)
10+
# @option arguments [Hash] :The alias definition(*Required*)
11+
12+
def put_alias(arguments={})
13+
raise ArgumentError, "Required argument 'name' missing" unless arguments[:name]
14+
raise ArgumentError, "Required argument 'body' missing" unless arguments[:body]
15+
method = HTTP_PUT
16+
path = Utils.__pathify Utils.__escape(arguments[:name])
17+
18+
params = Utils.__validate_and_extract_params arguments
19+
body = arguments[:body]
20+
perform_request(method, path, params, body.to_json).body
21+
end
22+
end
23+
end
24+
end

spec/support/elasticsearch/api/actions/put_ilm_policy.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module Elasticsearch
66
module API
77
module Actions
88

9-
# @option arguments [String] :name The name of the template (*Required*)
10-
# @option arguments [Hash] :body The template definition (*Required*)
9+
# @option arguments [String] :name The name of the policy (*Required*)
10+
# @option arguments [Hash] :body The policy definition (*Required*)
1111

1212
def put_ilm_policy(arguments={})
1313
raise ArgumentError, "Required argument 'name' missing" unless arguments[:name]

0 commit comments

Comments
 (0)