Class: Statsig::SpecStore

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network, options, error_callback, diagnostics, error_boundary, logger, secret_key) ⇒ SpecStore

Returns a new instance of SpecStore.



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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/spec_store.rb', line 16

def initialize(network, options, error_callback, diagnostics, error_boundary, logger, secret_key)
  @init_reason = EvaluationReason::UNINITIALIZED
  @network = network
  @options = options
  @error_callback = error_callback
  @last_config_sync_time = 0
  @initial_config_sync_time = 0
  @rulesets_sync_interval = options.rulesets_sync_interval
  @id_lists_sync_interval = options.idlists_sync_interval
  @rules_updated_callback = options.rules_updated_callback
  @specs = {
    :gates => {},
    :configs => {},
    :layers => {},
    :id_lists => {},
    :experiment_to_layer => {},
    :sdk_keys_to_app_ids => {},
    :hashed_sdk_keys_to_app_ids => {}
  }
  @diagnostics = diagnostics
  @error_boundary = error_boundary
  @logger = logger
  @secret_key = secret_key

  @id_list_thread_pool = Concurrent::FixedThreadPool.new(
    options.idlist_threadpool_size,
    name: 'statsig-idlist',
    max_queue: 100,
    fallback_policy: :discard,
  )

  unless @options.bootstrap_values.nil?
    if !@options.data_store.nil?
      puts 'data_store gets priority over bootstrap_values. bootstrap_values will be ignored'
    else
      tracker = @diagnostics.track('initialize','bootstrap', 'process')
      begin
        if process_specs(options.bootstrap_values)
          @init_reason = EvaluationReason::BOOTSTRAP
        end
      rescue
        puts 'the provided bootstrapValues is not a valid JSON string'
      ensure
        tracker.end(success: @init_reason == EvaluationReason::BOOTSTRAP)
      end
    end
  end

  unless @options.data_store.nil?
    @options.data_store.init
    load_config_specs_from_storage_adapter('initialize')
  end

  if @init_reason == EvaluationReason::UNINITIALIZED
    download_config_specs('initialize')
  end

  @initial_config_sync_time = @last_config_sync_time == 0 ? -1 : @last_config_sync_time
  if !@options.data_store.nil?
    get_id_lists_from_adapter('initialize')
  else
    get_id_lists_from_network('initialize')
  end

  @config_sync_thread = spawn_sync_config_specs_thread
  @id_lists_sync_thread = spawn_sync_id_lists_thread
end

Instance Attribute Details

#init_reasonObject

Returns the value of attribute init_reason.



14
15
16
# File 'lib/spec_store.rb', line 14

def init_reason
  @init_reason
end

#initial_config_sync_timeObject

Returns the value of attribute initial_config_sync_time.



13
14
15
# File 'lib/spec_store.rb', line 13

def initial_config_sync_time
  @initial_config_sync_time
end

#last_config_sync_timeObject

Returns the value of attribute last_config_sync_time.



12
13
14
# File 'lib/spec_store.rb', line 12

def last_config_sync_time
  @last_config_sync_time
end

Instance Method Details

#configsObject



129
130
131
# File 'lib/spec_store.rb', line 129

def configs
  @specs[:configs]
end

#gatesObject



125
126
127
# File 'lib/spec_store.rb', line 125

def gates
  @specs[:gates]
end

#get_app_id_for_sdk_key(sdk_key) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/spec_store.rb', line 149

def get_app_id_for_sdk_key(sdk_key)
  if sdk_key.nil?
    return nil
  end
  hashed_sdk_key = Statsig::HashUtils.djb2(sdk_key)
  if has_hashed_sdk_key?(hashed_sdk_key)
    return @specs[:hashed_sdk_keys_to_app_ids][hashed_sdk_key]
  end
  return nil unless has_sdk_key?(sdk_key)
  @specs[:sdk_keys_to_app_ids][sdk_key]
end

#get_config(config_name) ⇒ Object



115
116
117
118
# File 'lib/spec_store.rb', line 115

def get_config(config_name)
  return nil unless has_config?(config_name)
  @specs[:configs][config_name]
end

#get_gate(gate_name) ⇒ Object



110
111
112
113
# File 'lib/spec_store.rb', line 110

def get_gate(gate_name)
  return nil unless has_gate?(gate_name)
  @specs[:gates][gate_name]
end

#get_id_list(list_name) ⇒ Object



137
138
139
# File 'lib/spec_store.rb', line 137

def get_id_list(list_name)
  @specs[:id_lists][list_name]
end

#get_layer(layer_name) ⇒ Object



120
121
122
123
# File 'lib/spec_store.rb', line 120

def get_layer(layer_name)
  return nil unless has_layer?(layer_name)
  @specs[:layers][layer_name]
end

#get_raw_specsObject



161
162
163
# File 'lib/spec_store.rb', line 161

def get_raw_specs
  @specs
end

#has_config?(config_name) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/spec_store.rb', line 102

def has_config?(config_name)
  @specs[:configs].key?(config_name)
end

#has_gate?(gate_name) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/spec_store.rb', line 98

def has_gate?(gate_name)
  @specs[:gates].key?(gate_name)
end

#has_hashed_sdk_key?(hashed_sdk_key) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/spec_store.rb', line 145

def has_hashed_sdk_key?(hashed_sdk_key)
  @specs[:hashed_sdk_keys_to_app_ids].key?(hashed_sdk_key)
end

#has_layer?(layer_name) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/spec_store.rb', line 106

def has_layer?(layer_name)
  @specs[:layers].key?(layer_name)
end

#has_sdk_key?(sdk_key) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/spec_store.rb', line 141

def has_sdk_key?(sdk_key)
  @specs[:sdk_keys_to_app_ids].key?(sdk_key)
end

#is_ready_for_checksObject



84
85
86
# File 'lib/spec_store.rb', line 84

def is_ready_for_checks
  @last_config_sync_time != 0
end

#layersObject



133
134
135
# File 'lib/spec_store.rb', line 133

def layers
  @specs[:layers]
end

#maybe_restart_background_threadsObject



165
166
167
168
169
170
171
172
# File 'lib/spec_store.rb', line 165

def maybe_restart_background_threads
  if @config_sync_thread.nil? || !@config_sync_thread.alive?
    @config_sync_thread = sync_config_specs
  end
  if @id_lists_sync_thread.nil? || !@id_lists_sync_thread.alive?
    @id_lists_sync_thread = sync_id_lists
  end
end

#shutdownObject



88
89
90
91
92
93
94
95
96
# File 'lib/spec_store.rb', line 88

def shutdown
  @config_sync_thread&.exit
  @id_lists_sync_thread&.exit
  @id_list_thread_pool.shutdown
  @id_list_thread_pool.wait_for_termination(timeout = 3)
  unless @options.data_store.nil?
    @options.data_store.shutdown
  end
end

#sync_config_specsObject



174
175
176
177
178
179
180
181
# File 'lib/spec_store.rb', line 174

def sync_config_specs
  if @options.data_store&.should_be_used_for_querying_updates(Interfaces::IDataStore::CONFIG_SPECS_KEY)
    load_config_specs_from_storage_adapter('config_sync')
  else
    download_config_specs('config_sync')
  end
  @logger.log_diagnostics_event(@diagnostics, 'config_sync')
end

#sync_id_listsObject



183
184
185
186
187
188
189
190
# File 'lib/spec_store.rb', line 183

def sync_id_lists
  if @options.data_store&.should_be_used_for_querying_updates(Interfaces::IDataStore::ID_LISTS_KEY)
    get_id_lists_from_adapter('config_sync')
  else
    get_id_lists_from_network('config_sync')
  end
  @logger.log_diagnostics_event(@diagnostics, 'config_sync')
end