@@ -86,32 +86,80 @@ def fetch_splits(target_change_number)
8686
8787 if result [ :success ]
8888 @segment_fetcher . fetch_segments_if_not_exists ( result [ :segment_names ] , true ) unless result [ :segment_names ] . empty?
89- @config . logger . debug ( "Refresh completed bypassing the CDN in #{ attempts } attempts." )
89+ @config . logger . debug ( "Refresh completed bypassing the CDN in #{ attempts } attempts." ) if @config . debug_enabled
9090 else
91- @config . logger . debug ( "No changes fetched after #{ attempts } attempts with CDN bypassed." )
91+ @config . logger . debug ( "No changes fetched after #{ attempts } attempts with CDN bypassed." ) if @config . debug_enabled
9292 end
9393 rescue StandardError => error
9494 @config . log_found_exception ( __method__ . to_s , error )
9595 end
9696
97- def fetch_segment ( name )
97+ def fetch_segment ( name , target_change_number )
98+ return if target_change_number <= @segments_repository . get_change_number ( name ) . to_i
99+
98100 fetch_options = { cache_control_headers : true , till : nil }
99- @segment_fetcher . fetch_segment ( name , fetch_options )
101+ result = attempt_segment_sync ( name ,
102+ target_change_number ,
103+ fetch_options ,
104+ @config . on_demand_fetch_max_retries ,
105+ @config . on_demand_fetch_retry_delay_seconds ,
106+ false )
107+
108+ attempts = @config . on_demand_fetch_max_retries - result [ :remaining_attempts ]
109+ if result [ :success ]
110+ @config . logger . debug ( "Segment #{ name } refresh completed in #{ attempts } attempts." ) if @config . debug_enabled
111+
112+ return
113+ end
114+
115+ fetch_options = { cache_control_headers : true , till : target_change_number }
116+ result = attempt_segment_sync ( name ,
117+ target_change_number ,
118+ fetch_options ,
119+ ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES ,
120+ nil ,
121+ true )
122+
123+ attempts = @config . on_demand_fetch_max_retries - result [ :remaining_attempts ]
124+ if result [ :success ]
125+ @config . logger . debug ( "Segment #{ name } refresh completed bypassing the CDN in #{ attempts } attempts." ) if @config . debug_enabled
126+ else
127+ @config . logger . debug ( "No changes fetched for segment #{ name } after #{ attempts } attempts with CDN bypassed." ) if @config . debug_enabled
128+ end
129+ rescue StandardError => error
130+ @config . log_found_exception ( __method__ . to_s , error )
100131 end
101132
102133 private
103134
135+ def attempt_segment_sync ( name , target_cn , fetch_options , max_retries , retry_delay_seconds , with_backoff )
136+ remaining_attempts = max_retries
137+ backoff = Engine ::BackOff . new ( ON_DEMAND_FETCH_BACKOFF_BASE_SECONDS , 0 , ON_DEMAND_FETCH_BACKOFF_MAX_WAIT_SECONDS ) if with_backoff
138+
139+ loop do
140+ remaining_attempts -= 1
141+
142+ @segment_fetcher . fetch_segment ( name , fetch_options )
143+
144+ return sync_result ( true , remaining_attempts ) if target_cn <= @segments_repository . get_change_number ( name ) . to_i
145+ return sync_result ( false , remaining_attempts ) if remaining_attempts <= 0
146+
147+ delay = with_backoff ? backoff . interval : retry_delay_seconds
148+ sleep ( delay )
149+ end
150+ end
151+
104152 def attempt_splits_sync ( target_cn , fetch_options , max_retries , retry_delay_seconds , with_backoff )
105153 remaining_attempts = max_retries
106- backoff = SSE :: EventSource ::BackOff . new ( ON_DEMAND_FETCH_BACKOFF_BASE_SECONDS , 0 , ON_DEMAND_FETCH_BACKOFF_MAX_WAIT_SECONDS ) if with_backoff
154+ backoff = Engine ::BackOff . new ( ON_DEMAND_FETCH_BACKOFF_BASE_SECONDS , 0 , ON_DEMAND_FETCH_BACKOFF_MAX_WAIT_SECONDS ) if with_backoff
107155
108156 loop do
109157 remaining_attempts -= 1
110158
111159 segment_names = @split_fetcher . fetch_splits ( fetch_options )
112160
113- return split_sync_result ( true , remaining_attempts , segment_names ) if target_cn <= @splits_repository . get_change_number
114- return split_sync_result ( false , remaining_attempts , segment_names ) if remaining_attempts <= 0
161+ return sync_result ( true , remaining_attempts , segment_names ) if target_cn <= @splits_repository . get_change_number
162+ return sync_result ( false , remaining_attempts , segment_names ) if remaining_attempts <= 0
115163
116164 delay = with_backoff ? backoff . interval : retry_delay_seconds
117165 sleep ( delay )
@@ -141,7 +189,7 @@ def start_telemetry_sync_task
141189 Telemetry ::SyncTask . new ( @config , @telemetry_synchronizer ) . call
142190 end
143191
144- def split_sync_result ( success , remaining_attempts , segment_names )
192+ def sync_result ( success , remaining_attempts , segment_names = nil )
145193 { success : success , remaining_attempts : remaining_attempts , segment_names : segment_names }
146194 end
147195 end
0 commit comments