Skip to content

Commit

Permalink
update cohort_sync_config fields: include polling and remove request …
Browse files Browse the repository at this point in the history
…delay, use enum for serverzone, update tests accordingly
  • Loading branch information
tyiuhc committed Aug 6, 2024
1 parent 3a3d313 commit 87c0ebc
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 22 deletions.
6 changes: 3 additions & 3 deletions lib/experiment/cohort/cohort_download_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ module AmplitudeExperiment
# CohortDownloadApi
class CohortDownloadApi
COHORT_REQUEST_TIMEOUT_MILLIS = 5000
COHORT_REQUEST_RETRY_DELAY_MILLIS = 100
def get_cohort(cohort_id, cohort = nil)
raise NotImplementedError
end
end

# DirectCohortDownloadApi
class DirectCohortDownloadApi < CohortDownloadApi
def initialize(api_key, secret_key, max_cohort_size, cohort_request_delay_millis, server_url, logger)
def initialize(api_key, secret_key, max_cohort_size, server_url, logger)
super()
@api_key = api_key
@secret_key = secret_key
@max_cohort_size = max_cohort_size
@cohort_request_delay_millis = cohort_request_delay_millis
@server_url = server_url
@logger = logger
end
Expand Down Expand Up @@ -61,7 +61,7 @@ def get_cohort(cohort_id, cohort = nil)
raise e if errors >= 3 || e.is_a?(CohortTooLargeError)
end

sleep(@cohort_request_delay_millis / 1000.0)
sleep(COHORT_REQUEST_RETRY_DELAY_MILLIS / 1000.0)
end
end

Expand Down
10 changes: 5 additions & 5 deletions lib/experiment/cohort/cohort_sync_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ class CohortSyncConfig
# api_key (str): The project API Key
# secret_key (str): The project Secret Key
# max_cohort_size (int): The maximum cohort size that can be downloaded
# cohort_request_delay_millis (int): The delay in milliseconds between each cohort download request,
# applied upon failure
# cohort_polling_interval_millis (int): The interval in milliseconds to poll for cohorts, the minimum value is 60000
# cohort_server_url (str): The server endpoint from which to request cohorts

attr_accessor :api_key, :secret_key, :max_cohort_size, :cohort_request_delay_millis, :cohort_server_url
attr_accessor :api_key, :secret_key, :max_cohort_size, :cohort_polling_interval_millis, :cohort_server_url

def initialize(api_key, secret_key, max_cohort_size: 2_147_483_647, cohort_request_delay_millis: 5000, cohort_server_url: DEFAULT_COHORT_SYNC_URL)
def initialize(api_key, secret_key, max_cohort_size: 2_147_483_647, cohort_polling_interval_millis: 60_000,
cohort_server_url: DEFAULT_COHORT_SYNC_URL)
@api_key = api_key
@secret_key = secret_key
@max_cohort_size = max_cohort_size
@cohort_request_delay_millis = cohort_request_delay_millis
@cohort_polling_interval_millis = [cohort_polling_interval_millis, 60_000].max
@cohort_server_url = cohort_server_url
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/experiment/deployment/deployment_runner.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'set'

module AmplitudeExperiment
COHORT_POLLING_INTERVAL_MILLIS = 60_000
# DeploymentRunner
class DeploymentRunner
def initialize(
Expand Down Expand Up @@ -35,7 +34,7 @@ def start
@flag_poller.start
if @cohort_loader
@cohort_poller = Poller.new(
COHORT_POLLING_INTERVAL_MILLIS / 1000.0,
@config.cohort_sync_config.cohort_polling_interval_millis / 1000.0,
method(:update_cohorts)
)
@cohort_poller.start
Expand Down
1 change: 0 additions & 1 deletion lib/experiment/local/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def initialize(api_key, config = nil)
@cohort_download_api = DirectCohortDownloadApi.new(@config.cohort_sync_config.api_key,
@config.cohort_sync_config.secret_key,
@config.cohort_sync_config.max_cohort_size,
@config.cohort_sync_config.cohort_request_delay_millis,
@config.cohort_sync_config.cohort_server_url,
@logger)
@cohort_loader = CohortLoader.new(@cohort_download_api, @cohort_storage)
Expand Down
11 changes: 8 additions & 3 deletions lib/experiment/local/config.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
module AmplitudeExperiment
module ServerZone
US = 'US'.freeze
EU = 'EU'.freeze
end

# LocalEvaluationConfig
class LocalEvaluationConfig
# Default server url
Expand Down Expand Up @@ -36,14 +41,14 @@ class LocalEvaluationConfig
# @param [long] flag_config_polling_interval_millis The value of flag config polling interval in million seconds.
# @param [AssignmentConfig] assignment_config Configuration for automatically tracking assignment events after an evaluation.
# @param [CohortSyncConfig] cohort_sync_config Configuration for downloading cohorts required for flag evaluation
def initialize(server_url: DEFAULT_SERVER_URL, server_zone: 'us', bootstrap: {},
def initialize(server_url: DEFAULT_SERVER_URL, server_zone: ServerZone::US, bootstrap: {},
flag_config_polling_interval_millis: 30_000, debug: false, assignment_config: nil,
cohort_sync_config: nil)
@debug = debug || false
@server_url = server_url
@server_zone = server_zone.downcase
@server_zone = server_zone
@cohort_sync_config = cohort_sync_config
if server_url == DEFAULT_SERVER_URL && @server_zone == 'eu'
if server_url == DEFAULT_SERVER_URL && @server_zone == ServerZone::EU
@server_url = EU_SERVER_URL
@cohort_sync_config.cohort_server_url = EU_COHORT_SYNC_URL if @cohort_sync_config && @cohort_sync_config.cohort_server_url == DEFAULT_COHORT_SYNC_URL
end
Expand Down
2 changes: 1 addition & 1 deletion spec/experiment/cohort/cohort_download_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module AmplitudeExperiment
let(:server_url) { 'https://example.amplitude.com' }
let(:max_cohort_size) { 15_000 }
let(:cohort_request_delay_millis) { 100 }
let(:api) { AmplitudeExperiment::DirectCohortDownloadApi.new(api_key, secret_key, max_cohort_size, cohort_request_delay_millis, server_url, logger) }
let(:api) { AmplitudeExperiment::DirectCohortDownloadApi.new(api_key, secret_key, max_cohort_size, server_url, logger) }

def response(code, body = nil)
{ status: code, body: body.nil? ? '' : JSON.dump(body), headers: { 'Content-Type' => 'application/json' } }
Expand Down
4 changes: 2 additions & 2 deletions spec/experiment/deployment/deployment_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module AmplitudeExperiment
cohort_loader = CohortLoader.new(cohort_download_api, cohort_storage)
logger = Logger.new($stdout)
runner = DeploymentRunner.new(
LocalEvaluationConfig.new,
LocalEvaluationConfig.new(cohort_sync_config: CohortSyncConfig.new('api_key', 'secret_key')),
flag_fetcher,
flag_config_storage,
cohort_storage,
Expand All @@ -52,7 +52,7 @@ module AmplitudeExperiment
cohort_loader = CohortLoader.new(cohort_download_api, cohort_storage)
logger = Logger.new($stdout)
runner = DeploymentRunner.new(
LocalEvaluationConfig.new,
LocalEvaluationConfig.new(cohort_sync_config: CohortSyncConfig.new('api_key', 'secret_key')),
flag_fetcher,
flag_config_storage,
cohort_storage,
Expand Down
8 changes: 3 additions & 5 deletions spec/experiment/local/client_cohort_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ module AmplitudeExperiment
let(:api_key_eu) { 'server-Qlp7XiSu6JtP2S3JzA95PnP27duZgQCF' }
cohort_sync_config = CohortSyncConfig.new(
ENV['API_KEY'],
ENV['SECRET_KEY'],
cohort_request_delay_millis: 100
ENV['SECRET_KEY']
)
cohort_sync_config_eu = CohortSyncConfig.new(
ENV['EU_API_KEY'],
ENV['EU_SECRET_KEY'],
cohort_request_delay_millis: 100
ENV['EU_SECRET_KEY']
)
let(:config) { LocalEvaluationConfig.new(cohort_sync_config: cohort_sync_config) }
let(:config_eu) { LocalEvaluationConfig.new(cohort_sync_config: cohort_sync_config_eu, server_zone: 'eu') }
let(:config_eu) { LocalEvaluationConfig.new(cohort_sync_config: cohort_sync_config_eu, server_zone: ServerZone::EU) }

before(:each) do
WebMock.allow_net_connect!
Expand Down

0 comments on commit 87c0ebc

Please sign in to comment.