Class: Fluent::Plugin::RedisSentinelOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_redis_sentinel.rb

Constant Summary collapse

DEFAULT_BUFFER_TYPE =
"memory"

Instance Method Summary collapse

Constructor Details

#initializeRedisSentinelOutput

Returns a new instance of RedisSentinelOutput.



40
41
42
43
44
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 40

def initialize
  super
  require 'redis' unless defined?(Redis) == 'constant'
  require 'msgpack'
end

Instance Method Details

#configure(conf) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 46

def configure(conf)
  compat_parameters_convert(conf, :buffer)
  super

  if @key_path == nil and @key == nil
    raise Fluent::ConfigError, "either key_path or key is required"
  end
end

#format(tag, time, record) ⇒ Object



86
87
88
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 86

def format(tag, time, record)
  [tag, time.to_f, record].to_msgpack
end

#formatted_to_msgpack_binary?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 90

def formatted_to_msgpack_binary?
  true
end

#generate_ltrim_script(key, maxlen, order) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 218

def generate_ltrim_script(key, maxlen, order)
  script  = "local key = '" + key.to_s + "'\n"
  script += "local maxlen = " + maxlen.to_s + "\n"
  script += "local order ='" + order.to_s + "'\n"
  script += "local len = tonumber(redis.call('LLEN', key))\n"
  script += "if len > maxlen then\n"
  script += "    if order == 'asc' then\n"
  script += "        local l = len - maxlen\n"
  script += "        return redis.call('LTRIM', key, l, -1)\n"
  script += "    else\n"
  script += "        return redis.call('LTRIM', key, 0, maxlen - 1)\n"
  script += "    end\n"
  script += "end\n"
  return script
end

#generate_zremrangebyrank_script(key, maxlen, order) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 200

def generate_zremrangebyrank_script(key, maxlen, order)
  script  = "local key = '" + key.to_s + "'\n"
  script += "local maxlen = " + maxlen.to_s + "\n"
  script += "local order ='" + order.to_s + "'\n"
  script += "local len = tonumber(redis.call('ZCOUNT', key, '-inf', '+inf'))\n"
  script += "if len > maxlen then\n"
  script += "    if order == 'asc' then\n"
  script += "       local l = len - maxlen\n"
  script += "       if l >= 0 then\n"
  script += "           return redis.call('ZREMRANGEBYRANK', key, 0, l)\n"
  script += "       end\n"
  script += "    else\n"
  script += "       return redis.call('ZREMRANGEBYRANK', key, maxlen, -1)\n"
  script += "    end\n"
  script += "end\n"
  return script
end

#get_key_from(record) ⇒ Object

Raises:

  • (Fluent::ConfigError)


246
247
248
249
250
251
252
253
254
255
256
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 246

def get_key_from(record)
  if @key
    k = @key
  else
    k = traverse(record, @key_path).to_s
  end
  key = @key_prefix + k + @key_suffix

  raise Fluent::ConfigError, "key is empty" if key == ''
  key
end

#get_score_from(record, time) ⇒ Object



270
271
272
273
274
275
276
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 270

def get_score_from(record, time)
  if @score_path
    traverse(record, @score_path)
  else
    time
  end
end

#get_value_from(record) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 258

def get_value_from(record)
  value = traverse(record, @value_path)
  case @format_type
  when 'json'
    value.to_json
  when 'msgpack'
    value.to_msgpack
  else
    value
  end
end

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 94

def multi_workers_ready?
  true
end

#operation_for_list(record) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 170

def operation_for_list(record)
  key = get_key_from(record)
  value = get_value_from(record)

  if @order == 'asc'
    @redis.rpush key, value
  else
    @redis.lpush key, value
  end
  set_key_expire key
  if 0 < @value_length
    script = generate_ltrim_script(key, @value_length, @order)
    @redis.eval script
  end
end

#operation_for_publish(record) ⇒ Object



194
195
196
197
198
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 194

def operation_for_publish(record)
  key = get_key_from(record)
  value = get_value_from(record)
  @redis.publish key, value
end

#operation_for_set(record) ⇒ Object



163
164
165
166
167
168
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 163

def operation_for_set(record)
  key = get_key_from(record)
  value = get_value_from(record)
  @redis.sadd key, value
  set_key_expire key
end

#operation_for_string(record) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 186

def operation_for_string(record)
  key = get_key_from(record)
  value = get_value_from(record)
  @redis.set key, value

  set_key_expire key
end

#operation_for_zset(record, time) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 138

def operation_for_zset(record, time)
  key = get_key_from(record)
  value = get_value_from(record)
  score = get_score_from(record, time)
  if @collision_policy
    if @collision_policy == 'NX'
      @redis.zadd(key, score, value, :nx => true)
    elsif @collision_policy == 'XX'
      @redis.zadd(key, score, value, :xx => true)
    end
  else
    @redis.zadd(key, score, value)
  end

  set_key_expire key
  if 0 < @value_expire
    now = Time.now.to_i
    @redis.zremrangebyscore key , '-inf' , (now - @value_expire)
  end
  if 0 < @value_length
    script = generate_zremrangebyrank_script(key, @value_length, @order)
    @redis.eval script
  end
end

#set_key_expire(key) ⇒ Object



278
279
280
281
282
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 278

def set_key_expire(key)
  if 0 < @key_expire
    @redis.expire key, @key_expire
  end
end

#shutdownObject



81
82
83
84
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 81

def shutdown
  @redis.quit
  super
end

#startObject



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
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 55

def start
  super
  
  host=@sentinel_host
  hosts =  host.split(",")

  # Initialize Sentinel Empty Array
  sentinels = []

  # Loop through each hosts and add it to sentinel array
  if hosts.instance_of? Array
     hosts.each do |host|
      sentinels << {"host":host,"port":@port}
      end
  else
    sentinels << {"host":hosts,"port":@port}
  end

  # Printing Sentinel Server List
  $stdout.puts "Sentinel Server List #{sentinels}"
  
  
  @redis = Redis.new(name:@group_name,sentinels:sentinels,role: :master, timeout: @timeout)
  
end

#traverse(data, key) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 234

def traverse(data, key)
  val = data
  key.split('.').each{ |k|
    if val.has_key?(k)
      val = val[k]
    else
      return nil
    end
  }
  return val
end

#write(chunk) ⇒ Object



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
# File 'lib/fluent/plugin/out_redis_sentinel.rb', line 98

def write(chunk)
  @redis.pipelined {
    chunk.open { |io|
      begin
        MessagePack::Unpacker.new(io).each { |message|
          begin
            (_, time, record) = message
            case @store_type
            when 'zset'
              operation_for_zset(record, time)
            when 'set'
              operation_for_set(record)
            when 'list'
              operation_for_list(record)
            when 'string'
              operation_for_string(record)
            when 'publish'
              operation_for_publish(record)
            end
          rescue NoMethodError => e
            puts e
          rescue Encoding::UndefinedConversionError => e
            log.error "Plugin error: " + e.to_s
            log.error "Original record: " + record.to_s
            puts e
          rescue Redis::CannotConnectError => e
            log.error "Connection Error: #{e.message}"
            log.error "Original record: " + record.to_s
            log.info @redis
            log.info "Retrying Redis Connection"
            @redis = Redis.new(name:@group_name,sentinels:sentinels,role: :master, timeout: @timeout)
          end
        }
      rescue EOFError
        # EOFError always occured when reached end of chunk.
      end
    }
  }
end