Class: TuneMyGc::Syncer

Inherits:
Object
  • Object
show all
Defined in:
lib/tunemygc/syncer.rb

Constant Summary collapse

ENVIRONMENT =
[ENV['RUBY_GC_TOKEN'], RUBY_VERSION, TuneMyGc.rails_version, ENV.select {|k,v| k =~ /RUBY_GC_/ }, TuneMyGc::VERSION, GC::OPTS, GC::INTERNAL_CONSTANTS].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = TuneMyGc::HOST) ⇒ Syncer

Returns a new instance of Syncer.



11
12
13
# File 'lib/tunemygc/syncer.rb', line 11

def initialize(host = TuneMyGc::HOST)
  @client = TuneMyGc.http_client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/tunemygc/syncer.rb', line 9

def client
  @client
end

Instance Method Details

#environment(snapshotter) ⇒ Object



64
65
66
# File 'lib/tunemygc/syncer.rb', line 64

def environment(snapshotter)
  ENVIRONMENT.dup.concat([snapshotter.stat_keys, TuneMyGc.spy_ids, Socket.gethostname, Process.ppid, Process.pid])
end

#sync(snapshotter) ⇒ Object



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
# File 'lib/tunemygc/syncer.rb', line 15

def sync(snapshotter)
  if sync_required?(snapshotter)
    snapshots = snapshotter.size
    TuneMyGc.log "Syncing #{snapshots} snapshots"
    payload = [environment(snapshotter)]
    debug = ENV["RUBY_GC_TUNE_DEBUG"]
    TuneMyGc.log "=== Snapshots ===" if debug
    while !snapshotter.empty?
      snapshot = snapshotter.deq
      TuneMyGc.log(snapshot) if debug
      payload << snapshot
    end
    data = ActiveSupport::JSON.encode(payload)
    begin
      3.times do |retries|
        Timeout.timeout(NETWORK_TIMEOUT + 1) do
          response = sync_with_tuner(data, snapshots)
          if response
            if response == :retryable
              TuneMyGc.log "Retrying in #{retries} seconds ..."
              sleep(retries + 1)
            else
              process_config_callback(response)
              return true
            end
          else
            return false
          end
        end
      end
      TuneMyGc.log "Sync failed after retries ..."
      false
    ensure
      payload.clear
    end
  else
    TuneMyGc.log "The TuneMyGC service requires an instrumented application to do at least one unit of work (an ActionController request / response cycle, processing a background job through ActiveJob etc.) in order to suggest a configuration."
    TuneMyGc.log "During the last instrumented run, the agent observed #{snapshotter.size} GC events of interest, but none of them happened within the context of a unit of work and is thus not representative of your application's GC profile."
    TuneMyGc.log "Therefore, we're discarding #{snapshotter.size} snapshots and will generate a configuration when there's more context available for the agent to get a representative sample size."
    false
  end
end

#sync_required?(snapshotter) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/tunemygc/syncer.rb', line 58

def sync_required?(snapshotter)
  return true if ENV['RUBY_GC_SYNC_ALWAYS']
  TuneMyGc.log "Sync required? #{snapshotter.unit_of_work}"
  snapshotter.unit_of_work
end