Skip to content

Commit bfa6e40

Browse files
committed
General tidy up
1 parent a226293 commit bfa6e40

File tree

8 files changed

+47
-68
lines changed

8 files changed

+47
-68
lines changed

ci/run.sh

-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ else
179179
start_es
180180
bundle exec rspec -fd $extra_tag_args --tag update_tests:painless --tag update_tests:groovy --tag es_version:$es_distribution_version $spec_path
181181
;;
182-
183182
5.*)
184183
setup_es https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}.tar.gz
185184
es_distribution_version=$(get_es_distribution_version)

lib/logstash/outputs/elasticsearch/common.rb

+19-35
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
require "logstash/outputs/elasticsearch/template_manager"
2-
require 'logstash/environment'
3-
42

53
module LogStash; module Outputs; class ElasticSearch;
64
module Common
7-
attr_reader :client, :hosts, :ilm_manager
5+
attr_reader :client, :hosts
86

97
# These codes apply to documents, not at the request level
108
DOC_DLQ_CODES = [400, 404]
@@ -28,7 +26,7 @@ def register
2826

2927
setup_hosts # properly sets @hosts
3028
build_client
31-
install_template_after_successful_connection
29+
setup_after_successful_connection
3230
check_action_validity
3331
@bulk_request_metrics = metric.namespace(:bulk_requests)
3432
@document_level_metrics = metric.namespace(:documents)
@@ -43,7 +41,7 @@ def multi_receive(events)
4341
retrying_submit(events.map {|e| event_action_tuple(e)})
4442
end
4543

46-
def install_template_after_successful_connection
44+
def setup_after_successful_connection
4745
@template_installer ||= Thread.new do
4846
sleep_interval = @retry_initial_interval
4947
until successful_connection? || @stopping.true?
@@ -52,9 +50,9 @@ def install_template_after_successful_connection
5250
sleep_interval = next_sleep_interval(sleep_interval)
5351
end
5452
if successful_connection?
55-
verify_ilm
53+
verify_ilm_readiness if ilm_enabled?
5654
install_template
57-
setup_ilm
55+
setup_ilm if ilm_enabled?
5856
end
5957
end
6058
end
@@ -123,7 +121,6 @@ def maximum_seen_major_version
123121
client.maximum_seen_major_version
124122
end
125123

126-
127124
def routing_field_name
128125
maximum_seen_major_version >= 6 ? :routing : :_routing
129126
end
@@ -374,61 +371,48 @@ def setup_ilm
374371
maybe_create_ilm_policy
375372
end
376373

377-
def verify_ilm
378-
return true unless ilm_enabled?
379-
begin
380-
verify_ilm_config
374+
def verify_ilm_readiness
375+
return unless ilm_enabled?
381376

382-
# For elasticsearch versions without ilm enablement, the _ilm endpoint will return a 400 bad request - the
383-
# endpoint is interpreted as an index, and will return a bad request error as that is an illegal format for
384-
# an index.
377+
unless ilm_policy_default? || client.ilm_policy_exists?(ilm_policy)
378+
raise LogStash::ConfigurationError, "The specified ILM policy #{ilm_policy} does not exist on your Elasticsearch instance"
379+
end
380+
381+
# Check the Elasticsearch instance for ILM readiness - this means that the version has to be a non-OSS release, with ILM feature
382+
# available and enabled.
383+
begin
385384
xpack = client.get_xpack_info
386385
features = xpack["features"]
387386
ilm = features["ilm"] unless features.nil?
388387
raise LogStash::ConfigurationError, "Index Lifecycle management is enabled in logstash, but not installed on your Elasticsearch cluster" if features.nil? || ilm.nil?
389388
raise LogStash::ConfigurationError, "Index Lifecycle management is enabled in logstash, but not available in your Elasticsearch cluster" unless ilm['available']
390389
raise LogStash::ConfigurationError, "Index Lifecycle management is enabled in logstash, but not enabled in your Elasticsearch cluster" unless ilm['enabled']
391-
true
392390
rescue ::LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError => e
393391
# Check xpack endpoint: If no xpack endpoint, then this version of Elasticsearch is not compatible
394392
if e.response_code == 404
395-
false
396393
raise LogStash::ConfigurationError, "Index Lifecycle management is enabled in logstash, but not installed on your Elasticsearch cluster"
397394
elsif e.response_code == 400
398-
false
399395
raise LogStash::ConfigurationError, "Index Lifecycle management is enabled in logstash, but not installed on your Elasticsearch cluster"
400396
else
401397
raise e
402398
end
403399
end
404400
end
405401

406-
def verify_ilm_config
407-
# Overwrite the index with the rollover alias.
408-
@logger.warn "Overwriting index name with rollover alias #{@ilm_rollover_alias}" if @index != LogStash::Outputs::ElasticSearch::CommonConfigs::DEFAULT_INDEX_NAME
409-
@index = @ilm_rollover_alias
410-
verify_ilm_policy unless ilm_policy_default?
411-
end
412-
413-
def ilm_policy_ok?
414-
415-
end
416-
417402
def ilm_policy_default?
418403
ilm_policy == DEFAULT_POLICY
419404
end
420405

421-
def verify_ilm_policy
422-
raise LogStash::ConfigurationError, "The specified ILM policy does not exist" unless client.ilm_policy_exists?(ilm_policy)
423-
end
424-
425406
def maybe_create_ilm_policy
426407
if ilm_policy_default? && !client.ilm_policy_exists?(ilm_policy)
427408
client.ilm_policy_put(ilm_policy, policy_payload)
428409
end
429410
end
430411

431412
def maybe_create_rollover_alias
413+
@logger.warn "Overwriting supplied index name with rollover alias #{@ilm_rollover_alias}" if @index != LogStash::Outputs::ElasticSearch::CommonConfigs::DEFAULT_INDEX_NAME
414+
@index = @ilm_rollover_alias
415+
432416
client.rollover_alias_put(rollover_alias_target, rollover_alias_payload) unless client.rollover_alias_exists?(ilm_rollover_alias)
433417
end
434418

@@ -438,9 +422,9 @@ def rollover_alias_target
438422

439423
def rollover_alias_payload
440424
{
441-
"aliases" => {
425+
'aliases' => {
442426
ilm_rollover_alias =>{
443-
"is_write_index" => true
427+
'is_write_index' => true
444428
}
445429
}
446430
}

lib/logstash/outputs/elasticsearch/elasticsearch-template-es6x.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"template" : "logstash-*",
33
"version" : 60001,
44
"settings" : {
5-
"index.refresh_interval" : "5s",
6-
"number_of_shards": 1
5+
"index.refresh_interval" : "5s"
76
},
87
"mappings" : {
98
"_default_" : {

lib/logstash/outputs/elasticsearch/http_client.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,7 @@ def rollover_alias_put(alias_name, alias_definition)
364364
logger.info("Creating rollover alias #{alias_name}")
365365
begin
366366
@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-
#
367+
# If the rollover alias already exists, ignore the error that comes back from Elasticsearch
369368
rescue ::LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError => e
370369
if e.response_code == 400
371370
logger.info("Rollover Alias #{alias_name} already exists. Skipping")

lib/logstash/outputs/elasticsearch/template_manager.rb

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def self.install(client, template_name, template, template_overwrite)
2323
end
2424

2525
def self.add_ilm_settings_to_template(plugin, template)
26-
# Include ilm settings in template:
2726
plugin.logger.info("Overwriting index patterns, as ILM is enabled.")
2827
# Overwrite any index patterns, and use the rollover alias. Use 'index_patterns' rather than 'template' for pattern
2928
# definition - remove any existing definition of 'template'

spec/integration/outputs/ilm_spec.rb

-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,6 @@
442442
it 'should write the ILM settings into the template' do
443443
subject.register
444444
sleep(1)
445-
puts @es.indices.get_template(name: template_name)[template_name]
446445
expect(@es.indices.get_template(name: template_name)[template_name]["index_patterns"]).to eq(["#{ilm_rollover_alias}-*"])
447446
expect(@es.indices.get_template(name: template_name)[template_name]["settings"]['index']['lifecycle']['name']).to eq(ilm_policy_name)
448447
expect(@es.indices.get_template(name: template_name)[template_name]["settings"]['index']['lifecycle']['rollover_alias']).to eq(ilm_rollover_alias)

spec/integration/outputs/parent_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require "logstash/outputs/elasticsearch"
33

44
if ESHelper.es_version_satisfies?("<= 5.x")
5-
context "when using elasticsearch 5.x and before", :integration => "changethis" do
5+
context "when using elasticsearch 5.x and before", :integration => true do
66
shared_examples "a type based parent indexer" do
77
let(:index) { 10.times.collect { rand(10).to_s }.join("") }
88
let(:type) { 10.times.collect { rand(10).to_s }.join("") }

spec/integration/outputs/routing_spec.rb

+25-25
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,28 @@
3434
end
3535
end
3636

37-
# describe "(http protocol) index events with static routing", :integration => true do
38-
# it_behaves_like 'a routing indexer' do
39-
# let(:routing) { "test" }
40-
# let(:config) {
41-
# {
42-
# "hosts" => get_host_port,
43-
# "index" => index,
44-
# "routing" => routing
45-
# }
46-
# }
47-
# end
48-
# end
49-
#
50-
# describe "(http_protocol) index events with fieldref in routing value", :integration => true do
51-
# it_behaves_like 'a routing indexer' do
52-
# let(:routing) { "test" }
53-
# let(:config) {
54-
# {
55-
# "hosts" => get_host_port,
56-
# "index" => index,
57-
# "routing" => "%{message}"
58-
# }
59-
# }
60-
# end
61-
# end
37+
describe "(http protocol) index events with static routing", :integration => true do
38+
it_behaves_like 'a routing indexer' do
39+
let(:routing) { "test" }
40+
let(:config) {
41+
{
42+
"hosts" => get_host_port,
43+
"index" => index,
44+
"routing" => routing
45+
}
46+
}
47+
end
48+
end
49+
50+
describe "(http_protocol) index events with fieldref in routing value", :integration => true do
51+
it_behaves_like 'a routing indexer' do
52+
let(:routing) { "test" }
53+
let(:config) {
54+
{
55+
"hosts" => get_host_port,
56+
"index" => index,
57+
"routing" => "%{message}"
58+
}
59+
}
60+
end
61+
end

0 commit comments

Comments
 (0)