fourtrack 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +54 -0
- data/Rakefile +6 -0
- data/fourtrack.gemspec +33 -0
- data/lib/fourtrack.rb +6 -0
- data/lib/fourtrack/player.rb +26 -0
- data/lib/fourtrack/recorder.rb +80 -0
- data/lib/fourtrack/version.rb +3 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6a33b2a5e13cf6c134db5b352a27e3c096b4893f
|
4
|
+
data.tar.gz: 710204e5a058e83d29b47d41224393fc8363a1c3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3ef58accda8208ff7b24efff6201675235a456a8b90e48ed5d24ba93b3ff15ada3a08eb26dfe99785846d508228c9373ad01406020d5f18061d23d102d476897
|
7
|
+
data.tar.gz: a0205fbaab1f55f2a882bbc9d5685406e1aec00fbbac9597589cc9946b9f824de1bc08760b323835af4454cbfb8fadbaae63aec87960bcedf2b880e75590531a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 WeTransfer
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Fourtrack
|
2
|
+
|
3
|
+
A gem for massive, parallel recording of streaming event logs for later replay. You know when you need one.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
To record **all of your SQL:**
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
recorder = Fourtrack::Recorder.new(output_path: Rails.root.join('log/sql.jsonlines.gz'), flush_after: 1024)
|
11
|
+
ActiveSupport::Notifications.subscribe('sql.active_record') do |_, started, finished, id, payload|
|
12
|
+
recorder << JSON.dump(payload)
|
13
|
+
end
|
14
|
+
```
|
15
|
+
|
16
|
+
and to replay later:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
File.open(Rails.root.join('log/sql.jsonlines.gz')) do |f|
|
20
|
+
player = Fourtrac::Player.new(f)
|
21
|
+
player.each_line do |line|
|
22
|
+
puts JSON.load(line)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
## Installation
|
28
|
+
|
29
|
+
Add this line to your application's Gemfile:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
gem 'fourtrack'
|
33
|
+
```
|
34
|
+
|
35
|
+
And then execute:
|
36
|
+
|
37
|
+
$ bundle
|
38
|
+
|
39
|
+
Or install it yourself as:
|
40
|
+
|
41
|
+
$ gem install fourtrack
|
42
|
+
|
43
|
+
## Development
|
44
|
+
|
45
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/WeTransfer/fourtrack.
|
50
|
+
|
51
|
+
## License
|
52
|
+
|
53
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
54
|
+
|
data/Rakefile
ADDED
data/fourtrack.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fourtrack/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fourtrack"
|
8
|
+
spec.version = Fourtrack::VERSION
|
9
|
+
spec.authors = ["Julik Tarkhanov"]
|
10
|
+
spec.email = ["me@julik.nl"]
|
11
|
+
|
12
|
+
spec.summary = %q{Store massive ordered log records into a single .gz file.}
|
13
|
+
spec.description = %q{and replay them one by one}
|
14
|
+
spec.homepage = "https://github.com/WeTransfer/fourtrack"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
end
|
data/lib/fourtrack.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
class Fourtrack::Player
|
2
|
+
def initialize(io)
|
3
|
+
@io = io
|
4
|
+
end
|
5
|
+
|
6
|
+
def readlines
|
7
|
+
[].tap {|o| each_line{|line| o << line }}
|
8
|
+
end
|
9
|
+
|
10
|
+
def each_line(&blk)
|
11
|
+
# https://github.com/exAspArk/multiple_files_gzip_reader
|
12
|
+
# https://bugs.ruby-lang.org/issues/9790
|
13
|
+
loop do
|
14
|
+
break if @io.eof?
|
15
|
+
zr = Zlib::GzipReader.new(@io)
|
16
|
+
zr.each_line(&blk)
|
17
|
+
unused_bytestr = zr.unused
|
18
|
+
zr.finish
|
19
|
+
if unused_bytestr && unused_bytestr.bytesize.nonzero?
|
20
|
+
@io.pos -= unused_bytestr.bytesize
|
21
|
+
else
|
22
|
+
break
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# A class used as a destination for periodically writing out batch-written
|
2
|
+
# JSON formattable payloads. Can be used for stats logs, SQL replay logs and the like.
|
3
|
+
# Is thread safe and uses gzip compression. Writes are performed using a binary UNIX
|
4
|
+
# append, which with a small-ish record size should guarantee atomic append.
|
5
|
+
class Fourtrack::Recorder
|
6
|
+
require 'logger'
|
7
|
+
NULL_LOGGER = Logger.new(nil)
|
8
|
+
def initialize(output_path:, flush_after_n:, logger: NULL_LOGGER)
|
9
|
+
@output_path = File.expand_path(output_path)
|
10
|
+
@pid_at_create = Process.pid
|
11
|
+
@logger = logger
|
12
|
+
@buf = []
|
13
|
+
@mux = Mutex.new
|
14
|
+
@flush_every = flush_after_n
|
15
|
+
# Attempt to open the file for writing,
|
16
|
+
# which will raise an exception outright if we do not have access
|
17
|
+
File.open(@output_path, 'a') {}
|
18
|
+
# and once we know we were able to open it, install an at_exit block for ourselves
|
19
|
+
install_at_exit_hook!
|
20
|
+
end
|
21
|
+
|
22
|
+
def pending?
|
23
|
+
len = @mux.synchronize { @buf.length }
|
24
|
+
len.nonzero?
|
25
|
+
end
|
26
|
+
|
27
|
+
def <<(payload)
|
28
|
+
# Get the current PID.
|
29
|
+
mypid = Process.pid
|
30
|
+
len_so_far = @mux.synchronize {
|
31
|
+
# If the current PID doesn't match the one
|
32
|
+
# that was set at instantiation, it means the process was
|
33
|
+
# forked and we now possible also hold records for the parent
|
34
|
+
# process, which we have to discard (it is the responsibility
|
35
|
+
# of the parent to flush it's records, not ours!).
|
36
|
+
if mypid != @pid_at_create
|
37
|
+
@pid_at_create = mypid
|
38
|
+
@buf.clear
|
39
|
+
end
|
40
|
+
@buf << payload
|
41
|
+
@buf.length
|
42
|
+
}
|
43
|
+
flush! if len_so_far > @flush_every
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def flush!
|
48
|
+
# Refuse to flush and empty the buffer if flush! is called
|
49
|
+
# within a child and the object still has records pending
|
50
|
+
mypid = Process.pid
|
51
|
+
if mypid != @pid_at_create
|
52
|
+
@logger.debug { "%s: Flush requested child PID %d, will inhibit flush and empty the record log first" % mypid }
|
53
|
+
# Do not flush since we are in the child now
|
54
|
+
@mux.synchronize { @buf.clear }
|
55
|
+
return
|
56
|
+
end
|
57
|
+
|
58
|
+
io_buf = StringIO.new
|
59
|
+
|
60
|
+
@mux.synchronize do
|
61
|
+
@logger.debug { "%s: Compressing %d records from PID %d" % [self, @buf.length, Process.pid] }
|
62
|
+
z = Zlib::GzipWriter.new(io_buf)
|
63
|
+
@buf.each {|record| z.puts(JSON.dump(record) + "\n") }
|
64
|
+
z.finish
|
65
|
+
@buf.clear
|
66
|
+
end
|
67
|
+
|
68
|
+
@logger.debug { "%s: Flushing to %s, size before flush %d" % [self, @output_path, File.size(@output_path)] }
|
69
|
+
File.open(@output_path, 'ab') { |f| f << io_buf.string }
|
70
|
+
@logger.debug { "%s: After flush to %s size %d" % [self, @output_path, File.size(@output_path)] }
|
71
|
+
|
72
|
+
io_buf.truncate(0)
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def install_at_exit_hook!
|
78
|
+
at_exit { flush! if pending? }
|
79
|
+
end
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fourtrack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julik Tarkhanov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: and replay them one by one
|
56
|
+
email:
|
57
|
+
- me@julik.nl
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- fourtrack.gemspec
|
70
|
+
- lib/fourtrack.rb
|
71
|
+
- lib/fourtrack/player.rb
|
72
|
+
- lib/fourtrack/recorder.rb
|
73
|
+
- lib/fourtrack/version.rb
|
74
|
+
homepage: https://github.com/WeTransfer/fourtrack
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata:
|
78
|
+
allowed_push_host: https://rubygems.org
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.4.5.1
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Store massive ordered log records into a single .gz file.
|
99
|
+
test_files: []
|