Module: Fluent::NorikraPlugin::OutputMixin

Included in:
Fluent::NorikraFilterOutput, Fluent::NorikraOutput
Defined in:
lib/fluent/plugin/norikra/output.rb

Instance Method Summary collapse

Instance Method Details

#fetch_event_registration(query) ⇒ Object



67
68
69
70
71
# File 'lib/fluent/plugin/norikra/output.rb', line 67

def fetch_event_registration(query)
  return if query.tag.nil? || query.tag.empty?
  req = FetchRequest.new(:event, query.name, query.interval, 'string', query.tag, nil)
  insert_fetch_queue(req)
end

#format_stream(tag, es) ⇒ Object



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
# File 'lib/fluent/plugin/norikra/output.rb', line 155

def format_stream(tag, es)
  tobe_registered_target_names = []

  out = ''

  es.each do |time,record|
    target = @target_generator.call(tag, record)

    tgt = @target_mutex.synchronize do
      t = @target_map[target]
      unless t
        unless tobe_registered_target_names.include?(target)
          conf = @config_targets[target]
          unless conf
            @config_targets.values.each do |c|
              if c.target_matcher.match(target)
                conf = c
                break
              end
            end
          end
          t = Target.new(target, @default_target + conf)
          @registered_targets[target] = t
          @register_queue.push(t)
          tobe_registered_target_names.push(target)
        end
        t = @registered_targets[target]
      end
      t
    end

    event = tgt.filter(time, record)

    out << [tgt.escaped_name,event].to_msgpack
  end

  out
end

#prepare_target(client, target) ⇒ Object



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
# File 'lib/fluent/plugin/norikra/output.rb', line 106

def prepare_target(client, target)
  # target open and reserve fields
  log.debug "Going to prepare about target"
  begin
    unless client.targets.include?(target.escaped_name)
      log.debug "opening target #{target.escaped_name}"
      client.open(target.escaped_name, target.reserve_fields, target.auto_field)
      log.debug "opening target #{target.escaped_name}, done."
    end

    reserving = target.reserve_fields
    reserved = []
    client.fields(target.escaped_name).each do |field|
      if reserving[field['name']]
        reserved.push(field['name'])
        if reserving[field['name']] != field['type']
          log.warn "field type mismatch, reserving:#{reserving[field['name']]} but reserved:#{field['type']}"
        end
      end
    end

    reserving.each do |fieldname,type|
      client.reserve(target.escaped_name, fieldname, type) unless reserved.include?(fieldname)
    end
  rescue => e
    log.error "failed to prepare target:#{target.escaped_name}", :norikra => "#{@host}:#{@port}", :error => e.class, :message => e.message
    return false
  end

  # query registration
  begin
    registered = Hash[client.queries.map{|q| [q['name'], q['expression']]}]
    target.queries.each do |query|
      if registered.has_key?(query.name) # query already registered
        if registered[query.name] != query.expression
          log.warn "query name and expression mismatch, check norikra server status. target query name:#{query.name}"
        end
        next
      end
      client.register(query.name, query.group, query.expression)

      @query_map[query.name] = query
      fetch_event_registration(query)
    end
  rescue => e
    log.warn "failed to register query", :norikra => "#{@host}:#{@port}", :error => e.class, :message => e.message
  end
end

#prepared?(target_names) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/fluent/plugin/norikra/output.rb', line 63

def prepared?(target_names)
  fetchable? && target_names.reduce(true){|r,t| r && @target_map.values.any?{|target| target.escaped_name == t}}
end

#register_workerObject



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
# File 'lib/fluent/plugin/norikra/output.rb', line 73

def register_worker
  while sleep(0.25)
    break unless @register_worker_running
    next unless fetchable?

    c = client()

    targets = @register_queue.shift(10)
    targets.each do |t|
      next if @target_map[t.name]

      log.debug "Preparing norikra target #{t.name} on #{@host}:#{@port}"
      if prepare_target(c, t)
        log.debug "success to prepare target #{t.name} on #{@host}:#{@port}"

        if @enable_auto_query
          raise "bug" unless self.respond_to?(:insert_fetch_queue)

          t.queries.each do |query|
            @query_map[query.name] = query
            fetch_event_registration(query)
          end
        end
        @target_map[t.name] = t
        @registered_targets.delete(t.name)
      else
        log.error "Failed to prepare norikra data for target:#{t.name}"
        @register_queue.push(t)
      end
    end
  end
end

#setup_output(conf, enable_auto_query) ⇒ Object



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
# File 'lib/fluent/plugin/norikra/output.rb', line 11

def setup_output(conf, enable_auto_query)
  @enable_auto_query = enable_auto_query

  @target_generator = case
                      when @target_string
                        lambda {|tag,record| @target_string}
                      when @target_map_key
                        lambda {|tag,record| record[@target_map_key]}
                      when @target_map_tag
                        lambda {|tag,record| tag.gsub(/^#{@remove_tag_prefix}(\.)?/, '')}
                      else
                        raise Fluent::ConfigError, "no one way specified to decide target"
                      end

  # target map already prepared (opened, and related queries registered)
  @target_map = {} # 'target' => instance of Fluent::NorikraPlugin::Target

  # for conversion from query_name to tag
  @query_map = {} # 'query_name' => instance of Fluent::NorikraPlugin::Query

  @default_target = ConfigSection.new(Fluent::Config::Element.new('default', nil, {}, []), @enable_auto_query)
  @config_targets = {}

  conf.elements.each do |element|
    case element.name
    when 'default'
      @default_target = ConfigSection.new(element, @enable_auto_query)
    when 'target'
      c = ConfigSection.new(element, @enable_auto_query)
      @config_targets[c.target] = c
    end
  end

  @target_mutex = Mutex.new
end

#shutdown_outputObject



58
59
60
61
# File 'lib/fluent/plugin/norikra/output.rb', line 58

def shutdown_output
  # @register_thread.kill
  @register_thread.join
end

#start_outputObject



47
48
49
50
51
52
# File 'lib/fluent/plugin/norikra/output.rb', line 47

def start_output
  @register_worker_running = true
  @register_queue = []
  @registered_targets = {}
  @register_thread = Thread.new(&method(:register_worker))
end

#stop_outputObject



54
55
56
# File 'lib/fluent/plugin/norikra/output.rb', line 54

def stop_output
  @register_worker_running = false
end

#write(chunk) ⇒ Object



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
# File 'lib/fluent/plugin/norikra/output.rb', line 194

def write(chunk)
  events_map = {} # target => [event]
  chunk.msgpack_each do |target, event|
    events_map[target] ||= []
    events_map[target].push(event)
  end

  unless prepared?(events_map.keys)
    raise RuntimeError, "norikra server is not ready for this targets: #{events_map.keys.join(',')}"
  end

  c = client()

  events_map.each do |target, events|
    begin
      c.send(target, events)
    rescue Norikra::RPC::ClientError => e
      raise unless @drop_error_record
      log.warn "Norikra server reports ClientError, and dropped", target: target, message: e.message
    rescue Norikra::RPC::ServerError => e
      raise unless @drop_server_error_record
      log.warn "Norikra server reports ServerError, and dropped", target: target, message: e.message
    rescue Norikra::RPC::ServiceUnavailableError => e
      raise unless @drop_when_shutoff
      log.warn "Norikra server is now in Shutoff mode, and dropped", target: target, message: e.message
    end
  end
end