33import logging
44import re
55import itertools
6+ from numpy import append
7+ from pyparsing import Each
68import yaml
79import time
810
@@ -42,9 +44,8 @@ def __init__(self, split_api, split_storage):
4244 self ._backoff = Backoff (
4345 _ON_DEMAND_FETCH_BACKOFF_BASE ,
4446 _ON_DEMAND_FETCH_BACKOFF_MAX_WAIT )
45- self .segment_list = []
4647
47- def _fetch_until (self , fetch_options , till = None ):
48+ def _fetch_until (self , fetch_options , till = None , segment_sync = None ):
4849 """
4950 Hit endpoint, update storage and return when since==till.
5051
@@ -57,13 +58,14 @@ def _fetch_until(self, fetch_options, till=None):
5758 :return: last change number
5859 :rtype: int
5960 """
61+ segment_list = []
6062 while True : # Fetch until since==till
6163 change_number = self ._split_storage .get_change_number ()
6264 if change_number is None :
6365 change_number = - 1
6466 if till is not None and till < change_number :
6567 # the passed till is less than change_number, no need to perform updates
66- return change_number
68+ return change_number , segment_list
6769
6870 try :
6971 split_changes = self ._api .fetch_splits (change_number , fetch_options )
@@ -74,17 +76,21 @@ def _fetch_until(self, fetch_options, till=None):
7476
7577 for split in split_changes .get ('splits' , []):
7678 if split ['status' ] == splits .Status .ACTIVE .value :
77- # _LOGGER.debug('split details: '+str(split))
79+ _LOGGER .debug ('split details: ' + str (split ))
7880 self ._split_storage .put (splits .from_raw (split ))
7981 else :
8082 self ._split_storage .remove (split ['name' ])
81- self .segment_list = self ._split_storage .get_segment_names ()
83+ for segment in self ._split_storage .get_segment_names ():
84+ _LOGGER .debug ('Found segment: %s' , segment )
85+ if not segment_sync .segment_exist_in_storage (segment ):
86+ _LOGGER .debug ('Segment %s does not exist, syncing.' , segment )
87+ segment_list .append (segment )
8288
8389 self ._split_storage .set_change_number (split_changes ['till' ])
8490 if split_changes ['till' ] == split_changes ['since' ]:
85- return split_changes ['till' ]
91+ return split_changes ['till' ], segment_list
8692
87- def _attempt_split_sync (self , fetch_options , till = None ):
93+ def _attempt_split_sync (self , fetch_options , till = None , segment_sync = None ):
8894 """
8995 Hit endpoint, update storage and return True if sync is complete.
9096
@@ -101,35 +107,35 @@ def _attempt_split_sync(self, fetch_options, till=None):
101107 remaining_attempts = _ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES
102108 while True :
103109 remaining_attempts -= 1
104- change_number = self ._fetch_until (fetch_options , till )
110+ change_number , segment_list = self ._fetch_until (fetch_options , till , segment_sync )
105111 if till is None or till <= change_number :
106- return True , remaining_attempts , change_number
112+ return True , remaining_attempts , change_number , segment_list
107113 elif remaining_attempts <= 0 :
108- return False , remaining_attempts , change_number
114+ return False , remaining_attempts , change_number , segment_list
109115 how_long = self ._backoff .get ()
110116 time .sleep (how_long )
111117
112- def synchronize_splits (self , till = None ):
118+ def synchronize_splits (self , till = None , segment_sync = None ):
113119 """
114120 Hit endpoint, update storage and return True if sync is complete.
115121
116122 :param till: Passed till from Streaming.
117123 :type till: int
118124 """
119125 fetch_options = FetchOptions (True ) # Set Cache-Control to no-cache
120- successful_sync , remaining_attempts , change_number = self ._attempt_split_sync (fetch_options ,
121- till )
126+ successful_sync , remaining_attempts , change_number , segment_list = self ._attempt_split_sync (fetch_options ,
127+ till , segment_sync )
122128 attempts = _ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES - remaining_attempts
123129 if successful_sync : # succedeed sync
124130 _LOGGER .debug ('Refresh completed in %d attempts.' , attempts )
125- return self . segment_list
131+ return segment_list
126132 with_cdn_bypass = FetchOptions (True , change_number ) # Set flag for bypassing CDN
127- without_cdn_successful_sync , remaining_attempts , change_number = self ._attempt_split_sync (with_cdn_bypass , till )
133+ without_cdn_successful_sync , remaining_attempts , change_number , segment_list = self ._attempt_split_sync (with_cdn_bypass , till , segment_sync )
128134 without_cdn_attempts = _ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES - remaining_attempts
129135 if without_cdn_successful_sync :
130136 _LOGGER .debug ('Refresh completed bypassing the CDN in %d attempts.' ,
131137 without_cdn_attempts )
132- return self . segment_list
138+ return segment_list
133139 else :
134140 _LOGGER .debug ('No changes fetched after %d attempts with CDN bypassed.' ,
135141 without_cdn_attempts )
0 commit comments