td-logger 0.3.9 → 0.3.10

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,7 +1,12 @@
1
1
 
2
- == 2012-03-05 version 0.3.9
2
+ == 2012-03-05 version 0.3.10
3
3
 
4
4
  * Fixed 'ConditionVariable' is not defined problem
5
+ * Compress buffer chunk in memory
6
+
7
+
8
+ == 2012-03-05 version 0.3.9
9
+
5
10
  * Fixed Event.use feature
6
11
 
7
12
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.9
1
+ 0.3.10
@@ -1,12 +1,6 @@
1
1
  module TreasureData
2
2
  module Logger
3
3
 
4
- module Event
5
- def self.use(mod)
6
- send(:include, mod)
7
- end
8
- end
9
-
10
4
  module EventPreset
11
5
  def action(name, record, uid=TD.event.attribute[:uid])
12
6
  unless uid
@@ -37,9 +31,7 @@ module Logger
37
31
  end
38
32
  end
39
33
 
40
- Event.use EventPreset
41
-
42
- class EventCollector
34
+ class Event
43
35
  def initialize
44
36
  @attribute = {}
45
37
  end
@@ -50,11 +42,15 @@ module Logger
50
42
  TreasureData::Logger.post(action, @attribute.merge(record))
51
43
  end
52
44
 
53
- include Event
45
+ def self.use(mod)
46
+ send(:include, mod)
47
+ end
54
48
  end
55
49
 
50
+ Event.use EventPreset
51
+
56
52
  def self.event
57
- Thread.current[:td_event_collector] ||= EventCollector.new
53
+ Thread.current[:td_event] ||= Event.new
58
54
  end
59
55
 
60
56
  end
@@ -92,10 +92,11 @@ class TreasureDataLogger < Fluent::Logger::LoggerBase
92
92
  @upload_thread.join if @upload_thread
93
93
 
94
94
  @map.each {|(db,table),buffer|
95
- upload(db, table, buffer)
95
+ data = buffer.flush!
96
+ upload(db, table, data)
96
97
  }
97
- @queue.each {|tuple|
98
- upload(*tuple)
98
+ @queue.each {|db,table,data|
99
+ upload(ddb, data, table)
99
100
  }
100
101
  end
101
102
  end
@@ -149,6 +150,46 @@ class TreasureDataLogger < Fluent::Logger::LoggerBase
149
150
  end
150
151
 
151
152
  private
153
+ MAX_KEY_CARDINALITY = 512
154
+ WARN_KEY_CARDINALITY = 256
155
+
156
+ class Buffer
157
+ def initialize
158
+ @key_set = {}
159
+ @data = StringIO.new
160
+ @gz = Zlib::GzipWriter.new(@data)
161
+ end
162
+
163
+ def key_set_size
164
+ @key_set.size
165
+ end
166
+
167
+ def update_key_set(record)
168
+ record.each_key {|key|
169
+ @key_set[key] = true
170
+ }
171
+ @key_set.size
172
+ end
173
+
174
+ def append(data)
175
+ @gz << data
176
+ map = MessagePack.unpack(data)
177
+ end
178
+
179
+ def size
180
+ @data.size
181
+ end
182
+
183
+ def flush!
184
+ close
185
+ @data.string
186
+ end
187
+
188
+ def close
189
+ @gz.close unless @gz.closed?
190
+ end
191
+ end
192
+
152
193
  def to_msgpack(msg)
153
194
  begin
154
195
  msg.to_msgpack
@@ -186,12 +227,30 @@ class TreasureDataLogger < Fluent::Logger::LoggerBase
186
227
  return false
187
228
  end
188
229
 
189
- buffer = (@map[key] ||= '')
230
+ buffer = (@map[key] ||= Buffer.new)
231
+
232
+ record = MessagePack.unpack(data)
233
+ unless record.is_a?(Hash)
234
+ @logger.error("TreasureDataLogger: record must be a Hash: #{msg.inspect}")
235
+ return false
236
+ end
237
+
238
+ before = buffer.key_set_size
239
+ after = buffer.update_key_set(record)
240
+ if after > MAX_KEY_CARDINALITY
241
+ @logger.error("TreasureDataLogger: kind of keys in a buffer exceeds #{MAX_KEY_CARDINALITY}.")
242
+ @map.delete(key)
243
+ return false
244
+ end
245
+ if before <= WARN_KEY_CARDINALITY && after > WARN_KEY_CARDINALITY
246
+ @logger.warn("TreasureDataLogger: kind of keys in a buffer exceeds #{WARN_KEY_CARDINALITY} which is too large. please check the schema design.")
247
+ end
190
248
 
191
- buffer << data
249
+ buffer.append(data)
192
250
 
193
251
  if buffer.size > @chunk_limit
194
- @queue << [db, table, buffer]
252
+ data = buffer.flush!
253
+ @queue << [db, table, data]
195
254
  @map.delete(key)
196
255
  @cond.signal
197
256
  end
@@ -209,7 +268,8 @@ class TreasureDataLogger < Fluent::Logger::LoggerBase
209
268
  @mutex.synchronize do
210
269
  if @queue.empty?
211
270
  @map.reject! {|(db,table),buffer|
212
- @queue << [db, table, buffer]
271
+ data = buffer.flush!
272
+ @queue << [db, table, data]
213
273
  }
214
274
  end
215
275
  end
@@ -217,10 +277,10 @@ class TreasureDataLogger < Fluent::Logger::LoggerBase
217
277
  flushed = false
218
278
 
219
279
  until @queue.empty?
220
- tuple = @queue.first
280
+ db, table, data = @queue.first
221
281
 
222
282
  begin
223
- upload(*tuple)
283
+ upload(db, table, data)
224
284
  @queue.shift
225
285
  @error_count = 0
226
286
  flushed = true
@@ -243,11 +303,9 @@ class TreasureDataLogger < Fluent::Logger::LoggerBase
243
303
  flushed
244
304
  end
245
305
 
246
- def upload(db, table, buffer)
306
+ def upload(db, table, data)
247
307
  begin
248
- out = StringIO.new
249
- Zlib::GzipWriter.wrap(out) {|gz| gz.write buffer }
250
- stream = StringIO.new(out.string)
308
+ stream = StringIO.new(data)
251
309
 
252
310
  @logger.debug "Uploading event logs to #{db}.#{table} table on Treasure Data (#{stream.size} bytes)"
253
311
 
@@ -1,7 +1,7 @@
1
1
  module TreasureData
2
2
  module Logger
3
3
 
4
- VERSION = '0.3.9'
4
+ VERSION = '0.3.10'
5
5
 
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: td-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.9
4
+ version: 0.3.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-05 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: msgpack
16
- requirement: &70209063366040 !ruby/object:Gem::Requirement
16
+ requirement: &70312343520780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.4.4
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70209063366040
24
+ version_requirements: *70312343520780
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: td-client
27
- requirement: &70209063365440 !ruby/object:Gem::Requirement
27
+ requirement: &70312343518300 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.8.4
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70209063365440
35
+ version_requirements: *70312343518300
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: fluent-logger
38
- requirement: &70209063364980 !ruby/object:Gem::Requirement
38
+ requirement: &70312343515480 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.4.1
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70209063364980
46
+ version_requirements: *70312343515480
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &70209063364500 !ruby/object:Gem::Requirement
49
+ requirement: &70312343514500 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.9.2
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70209063364500
57
+ version_requirements: *70312343514500
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rspec
60
- requirement: &70209063363940 !ruby/object:Gem::Requirement
60
+ requirement: &70312343511440 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: 2.7.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70209063363940
68
+ version_requirements: *70312343511440
69
69
  description: Treasure Data logging library for Rails
70
70
  email:
71
71
  executables: []