Skip to content

Commit b5d6d1c

Browse files
Merge pull request #3539 from sebastian-miclea/PDB-5135
(PDB-5135) Notify when resource_event size limit is reached
2 parents 9d96611 + bbb579e commit b5d6d1c

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

puppet/lib/puppet/reports/puppetdb.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,19 @@ def build_events_list(events)
190190
# @api private
191191
def resource_status_to_hash(resource_status)
192192
defaulted_corrective_change = defined?(resource_status.corrective_change) ? resource_status.corrective_change : nil
193+
index_size = resource_event_index_size(resource_status)
194+
195+
if index_size > 2500 && index_size <= 2704
196+
Puppet.warning("resource_event on #{self.host} is very large. Values (resource timestamp + title + type) \
197+
are #{index_size} bytes and the hard limit is 2704. Caused by Resource type: \"#{resource_status.resource_type}\" \
198+
with title: \"#{resource_status.title.to_s}\" in #{resource_status.file}:#{resource_status.line}")
199+
end
200+
201+
if index_size > 2704
202+
Puppet.warning("resource_event on #{self.host} could not be saved. Values (resource timestamp + title + type) are larger \
203+
than index total size of 2704. Caused by Resource type: \"#{resource_status.resource_type}\" with title: \
204+
\"#{resource_status.title.to_s}\" in #{resource_status.file}:#{resource_status.line}")
205+
end
193206
{
194207
"skipped" => resource_status.skipped,
195208
"timestamp" => Puppet::Util::Puppetdb.to_wire_time(resource_status.time),
@@ -203,6 +216,17 @@ def resource_status_to_hash(resource_status)
203216
}
204217
end
205218

219+
# Get the size of the columns that will populate the resource_events_resource_timestamp_xxxxxz index.
220+
# resource_events_resource_timestamp_xxxxxz index is comprised of 3 columns (resource timestamp, title and type)
221+
# and it's maximum size is 2704 bytes. So the size of the 3 resource attributes combined, must not exceed 2704 bytes.
222+
#
223+
# @return Boolean
224+
# @api private
225+
def resource_event_index_size(resource_status)
226+
# 8 - represents the timestamp which has the same size all the time
227+
8 + resource_status.resource_type.bytesize + resource_status.title.to_s.bytesize
228+
end
229+
206230
# Helper method for accessing the puppetdb configuration
207231
#
208232
# @api private

puppet/spec/unit/reports/puppetdb_spec.rb

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
}
2121

2222
context "#process" do
23-
2423
let(:http) { mock "http" }
2524
let(:httpok) { Net::HTTPOK.new('1.1', 200, '') }
2625

@@ -367,7 +366,57 @@ def without_producer_timestamp(json_body)
367366
end
368367

369368
end
369+
end
370+
end
371+
372+
context '#resource_status_to_hash' do
373+
before do
374+
subject.host = 'localhost'
375+
end
376+
let(:resource) do
377+
stub('resource',
378+
{ pathbuilder: ['foo', 'bar', 'baz'],
379+
path: 'foo',
380+
file: 'foo',
381+
line: 1,
382+
tags: [],
383+
provider: 'foo',
384+
skipped: false,
385+
containment_path: '\\',
386+
events: [],
387+
time: DateTime.now,
388+
type: 'File',
389+
resource_type: 'File',
390+
title: huge_title,
391+
merge_into: nil })
392+
end
393+
394+
context 'large resources' do
395+
let(:huge_title) { (0...2501).map { ('a'..'z').to_a[rand(26)] }.join }
396+
let (:status) do
397+
Puppet::Resource::Status.new(resource)
398+
end
399+
400+
it 'large resources will warn about the index size' do
401+
Puppet.expects(:warning).with("resource_event on localhost is very large. Values (resource timestamp + title + type) \
402+
are 2513 bytes and the hard limit is 2704. Caused by Resource type: \"File\" with title: \"#{huge_title}\" in foo:1")
403+
subject.add_resource_status(status)
404+
subject.resource_status_to_hash(resource)
405+
end
406+
end
407+
408+
context "oversized resource" do
409+
let(:huge_title) { (0...2704).map { ('a'..'z').to_a[rand(26)] }.join }
410+
let (:status) do
411+
Puppet::Resource::Status.new(resource)
412+
end
370413

414+
it 'large resources will warn about the index size' do
415+
Puppet.expects(:warning).with("resource_event on localhost could not be saved. Values (resource timestamp + title + type) \
416+
are larger than index total size of 2704. Caused by Resource type: \"File\" with title: \"#{huge_title}\" in foo:1")
417+
subject.add_resource_status(status)
418+
subject.resource_status_to_hash(resource)
419+
end
371420
end
372421
end
373422
end

0 commit comments

Comments
 (0)