forked from fluent/fluentd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.rb
More file actions
349 lines (296 loc) · 9.52 KB
/
storage.rb
File metadata and controls
349 lines (296 loc) · 9.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#
# Fluentd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'forwardable'
require 'fluent/plugin'
require 'fluent/plugin/storage'
require 'fluent/plugin_helper/timer'
require 'fluent/config/element'
require 'fluent/configurable'
module Fluent
module PluginHelper
module Storage
include Fluent::PluginHelper::Timer
StorageState = Struct.new(:storage, :running)
def storage_create(usage: '', type: nil, conf: nil, default_type: nil)
if conf && conf.respond_to?(:arg) && !conf.arg.empty?
usage = conf.arg
end
if !usage.empty? && usage !~ /^[a-zA-Z][-_.a-zA-Z0-9]*$/
raise Fluent::ConfigError, "Argument in <storage ARG> uses invalid characters: '#{usage}'"
end
s = @_storages[usage]
if s&.running
return s.storage
elsif s
# storage is already created, but not loaded / started
else # !s
type = if type
type
elsif conf && conf.respond_to?(:[])
raise Fluent::ConfigError, "@type is required in <storage>" unless conf['@type']
conf['@type']
elsif default_type
default_type
else
raise ArgumentError, "BUG: both type and conf are not specified"
end
storage = Plugin.new_storage(type, parent: self)
config = case conf
when Fluent::Config::Element
conf
when Hash
# in code, programmer may use symbols as keys, but Element needs strings
conf = Hash[conf.map{|k,v| [k.to_s, v]}]
Fluent::Config::Element.new('storage', usage, conf, [])
when nil
Fluent::Config::Element.new('storage', usage, {'@type' => type}, [])
else
raise ArgumentError, "BUG: conf must be a Element, Hash (or unspecified), but '#{conf.class}'"
end
storage.configure(config)
if @_storages_started
storage.start
end
s = @_storages[usage] = StorageState.new(wrap_instance(storage), false)
end
s.storage
end
module StorageParams
include Fluent::Configurable
# minimum section definition to instantiate storage plugin instances
config_section :storage, required: false, multi: true, param_name: :storage_configs, init: true do
config_argument :usage, :string, default: ''
config_param :@type, :string, default: Fluent::Plugin::Storage::DEFAULT_TYPE
end
end
def self.included(mod)
mod.include StorageParams
end
attr_reader :_storages # for tests
def initialize
super
@_storages_started = false
@_storages = {} # usage => storage_state
end
def configure(conf)
super
@storage_configs.each do |section|
if !section.usage.empty? && section.usage !~ /^[a-zA-Z][-_.a-zA-Z0-9]*$/
raise Fluent::ConfigError, "Argument in <storage ARG> uses invalid characters: '#{section.usage}'"
end
if @_storages[section.usage]
raise Fluent::ConfigError, "duplicated storages configured: #{section.usage}"
end
storage = Plugin.new_storage(section[:@type], parent: self)
storage.configure(section.corresponding_config_element)
@_storages[section.usage] = StorageState.new(wrap_instance(storage), false)
end
end
def start
super
@_storages_started = true
@_storages.each_pair do |usage, s|
s.storage.start
s.storage.load
if s.storage.autosave && !s.storage.persistent
timer_execute(:storage_autosave, s.storage.autosave_interval, repeat: true) do
begin
s.storage.save
rescue => e
log.error "plugin storage failed to save its data", usage: usage, type: type, error: e
end
end
end
s.running = true
end
end
def storage_operate(method_name, &block)
@_storages.each_pair do |usage, s|
begin
block.call(s) if block_given?
s.storage.__send__(method_name)
rescue => e
log.error "unexpected error while #{method_name}", usage: usage, storage: s.storage, error: e
end
end
end
def stop
super
# timer stops automatically in super
storage_operate(:stop)
end
def before_shutdown
storage_operate(:before_shutdown)
super
end
def shutdown
storage_operate(:shutdown) do |s|
s.storage.save if s.storage.save_at_shutdown
end
super
end
def after_shutdown
storage_operate(:after_shutdown)
super
end
def close
storage_operate(:close){|s| s.running = false }
super
end
def terminate
storage_operate(:terminate)
@_storages = {}
super
end
def wrap_instance(storage)
if storage.persistent && storage.persistent_always?
storage
elsif storage.persistent
PersistentWrapper.new(storage)
elsif !storage.synchronized?
SynchronizeWrapper.new(storage)
else
storage
end
end
class PersistentWrapper
# PersistentWrapper always provides synchronized operations
extend Forwardable
def initialize(storage)
@storage = storage
@monitor = Monitor.new
end
def_delegators :@storage, :autosave_interval, :save_at_shutdown
def_delegators :@storage, :start, :stop, :before_shutdown, :shutdown, :after_shutdown, :close, :terminate
def_delegators :@storage, :started?, :stopped?, :before_shutdown?, :shutdown?, :after_shutdown?, :closed?, :terminated?
def method_missing(name, *args)
@monitor.synchronize{ @storage.__send__(name, *args) }
end
def persistent_always?
true
end
def persistent
true
end
def autosave
false
end
def synchronized?
true
end
def implementation
@storage
end
def load
@monitor.synchronize do
@storage.load
end
end
def save
@monitor.synchronize do
@storage.save
end
end
def get(key)
@monitor.synchronize do
@storage.load
@storage.get(key)
end
end
def fetch(key, defval)
@monitor.synchronize do
@storage.load
@storage.fetch(key, defval)
end
end
def put(key, value)
@monitor.synchronize do
@storage.load
@storage.put(key, value)
@storage.save
value
end
end
def delete(key)
@monitor.synchronize do
@storage.load
val = @storage.delete(key)
@storage.save
val
end
end
def update(key, &block)
@monitor.synchronize do
@storage.load
v = block.call(@storage.get(key))
@storage.put(key, v)
@storage.save
v
end
end
end
class SynchronizeWrapper
extend Forwardable
def initialize(storage)
@storage = storage
@monitor = Monitor.new
end
def_delegators :@storage, :persistent, :autosave, :autosave_interval, :save_at_shutdown
def_delegators :@storage, :persistent_always?
def_delegators :@storage, :start, :stop, :before_shutdown, :shutdown, :after_shutdown, :close, :terminate
def_delegators :@storage, :started?, :stopped?, :before_shutdown?, :shutdown?, :after_shutdown?, :closed?, :terminated?
def method_missing(name, *args)
@monitor.synchronize{ @storage.__send__(name, *args) }
end
def synchronized?
true
end
def implementation
@storage
end
def load
@monitor.synchronize do
@storage.load
end
end
def save
@monitor.synchronize do
@storage.save
end
end
def get(key)
@monitor.synchronize{ @storage.get(key) }
end
def fetch(key, defval)
@monitor.synchronize{ @storage.fetch(key, defval) }
end
def put(key, value)
@monitor.synchronize{ @storage.put(key, value) }
end
def delete(key)
@monitor.synchronize{ @storage.delete(key) }
end
def update(key, &block)
@monitor.synchronize do
v = block.call(@storage.get(key))
@storage.put(key, v)
v
end
end
end
end
end
end