@@ -160,117 +160,103 @@ public void run() {
160160 }
161161
162162 public void runWithoutExceptionHandling () throws InterruptedException {
163- SplitChange change = _splitChangeFetcher .fetch (_changeNumber .get ());
163+ while (true ) {
164+ SplitChange change = _splitChangeFetcher .fetch (_changeNumber .get ());
164165
165- if (change == null ) {
166- throw new IllegalStateException ("SplitChange was null" );
167- }
168-
169- if (change .till == _changeNumber .get ()) {
170- // no change.
171- return ;
172- }
173-
174- if (change .since != _changeNumber .get ()
175- || change .till < _changeNumber .get ()) {
176- // some other thread may have updated the shared state. exit
177- return ;
178- }
166+ if (change == null ) {
167+ throw new IllegalStateException ("SplitChange was null" );
168+ }
179169
180- if (change .splits .isEmpty ()) {
181- // there are no changes. weird!
182- _changeNumber .set (change .till );
183- return ;
184- }
170+ if (change .till == _changeNumber .get ()) {
171+ // no change.
172+ break ;
173+ }
185174
186- synchronized (_lock ) {
187- // check state one more time.
188- if (change .since != _changeNumber .get ()
189- || change .till < _changeNumber .get ()) {
175+ if (change .since != _changeNumber .get () || change .till < _changeNumber .get ()) {
190176 // some other thread may have updated the shared state. exit
191- return ;
177+ break ;
192178 }
193179
194- Set <String > toRemove = Sets .newHashSet ();
195- Map <String , ParsedSplit > toAdd = Maps .newHashMap ();
196- List <String > trafficTypeNamesToRemove = Lists .newArrayList ();
197- List <String > trafficTypeNamesToAdd = Lists .newArrayList ();
180+ if (change .splits .isEmpty ()) {
181+ // there are no changes. weird!
182+ _changeNumber .set (change .till );
183+ break ;
184+ }
198185
199- for (Split split : change .splits ) {
200- if (Thread .currentThread ().isInterrupted ()) {
201- throw new InterruptedException ();
186+ synchronized (_lock ) {
187+ // check state one more time.
188+ if (change .since != _changeNumber .get ()
189+ || change .till < _changeNumber .get ()) {
190+ // some other thread may have updated the shared state. exit
191+ break ;
202192 }
203193
204- if (split .status != Status .ACTIVE ) {
205- // archive.
206- toRemove .add (split .name );
207- if (split .trafficTypeName != null ) {
208- trafficTypeNamesToRemove .add (split .trafficTypeName );
209- }
210- continue ;
211- }
194+ Set <String > toRemove = Sets .newHashSet ();
195+ Map <String , ParsedSplit > toAdd = Maps .newHashMap ();
196+ List <String > trafficTypeNamesToRemove = Lists .newArrayList ();
197+ List <String > trafficTypeNamesToAdd = Lists .newArrayList ();
212198
213- ParsedSplit parsedSplit = _parser .parse (split );
214- if (parsedSplit == null ) {
215- _log .info ("We could not parse the experiment definition for: " + split .name + " so we are removing it completely to be careful" );
216- toRemove .add (split .name );
217- if (split .trafficTypeName != null ) {
218- trafficTypeNamesToRemove .add (split .trafficTypeName );
199+ for (Split split : change .splits ) {
200+ if (Thread .currentThread ().isInterrupted ()) {
201+ throw new InterruptedException ();
219202 }
220- continue ;
221- }
222-
223- toAdd .put (split .name , parsedSplit );
224-
225- // If the split already exists, this is either an update, or the split has been
226- // deleted and recreated (possibly with a different traffic type).
227- // If it's an update, the traffic type should NOT be increased.
228- // If it's deleted & recreated, the old one should be decreased and the new one increased.
229- // To handle both cases, we simply delete the old one if the split is present.
230- // The new one is always increased.
231- ParsedSplit current = _concurrentMap .get (split .name );
232- if (current != null && current .trafficTypeName () != null ) {
233- trafficTypeNamesToRemove .add (current .trafficTypeName ());
234- }
235203
236- if (split .trafficTypeName != null ) {
237- trafficTypeNamesToAdd .add (split .trafficTypeName );
238- }
239- }
204+ if (split .status != Status .ACTIVE ) {
205+ // archive.
206+ toRemove .add (split .name );
207+ if (split .trafficTypeName != null ) {
208+ trafficTypeNamesToRemove .add (split .trafficTypeName );
209+ }
210+ continue ;
211+ }
240212
241- _concurrentMap .putAll (toAdd );
242- _concurrentTrafficTypeNameSet .addAll (trafficTypeNamesToAdd );
243- //removeAll does not work here, since it wont remove all the occurrences, just one
244- Multisets .removeOccurrences (_concurrentTrafficTypeNameSet , trafficTypeNamesToRemove );
213+ ParsedSplit parsedSplit = _parser .parse (split );
214+ if (parsedSplit == null ) {
215+ _log .info ("We could not parse the experiment definition for: " + split .name + " so we are removing it completely to be careful" );
216+ toRemove .add (split .name );
217+ if (split .trafficTypeName != null ) {
218+ trafficTypeNamesToRemove .add (split .trafficTypeName );
219+ }
220+ continue ;
221+ }
245222
246- for (String remove : toRemove ) {
247- _concurrentMap .remove (remove );
248- }
223+ toAdd .put (split .name , parsedSplit );
224+
225+ // If the split already exists, this is either an update, or the split has been
226+ // deleted and recreated (possibly with a different traffic type).
227+ // If it's an update, the traffic type should NOT be increased.
228+ // If it's deleted & recreated, the old one should be decreased and the new one increased.
229+ // To handle both cases, we simply delete the old one if the split is present.
230+ // The new one is always increased.
231+ ParsedSplit current = _concurrentMap .get (split .name );
232+ if (current != null && current .trafficTypeName () != null ) {
233+ trafficTypeNamesToRemove .add (current .trafficTypeName ());
234+ }
249235
250- if (!toAdd .isEmpty ()) {
251- _log .debug ("Updated features: " + toAdd .keySet ());
252- }
236+ if (split .trafficTypeName != null ) {
237+ trafficTypeNamesToAdd .add (split .trafficTypeName );
238+ }
239+ }
253240
254- if (!toRemove .isEmpty ()) {
255- _log .debug ("Deleted features: " + toRemove );
256- }
241+ _concurrentMap .putAll (toAdd );
242+ _concurrentTrafficTypeNameSet .addAll (trafficTypeNamesToAdd );
243+ //removeAll does not work here, since it wont remove all the occurrences, just one
244+ Multisets .removeOccurrences (_concurrentTrafficTypeNameSet , trafficTypeNamesToRemove );
257245
258- _changeNumber .set (change .till );
259- }
246+ for (String remove : toRemove ) {
247+ _concurrentMap .remove (remove );
248+ }
260249
261- }
250+ if (!toAdd .isEmpty ()) {
251+ _log .debug ("Updated features: " + toAdd .keySet ());
252+ }
262253
263- private List <String > collectSegmentsInUse (Split split ) {
264- List <String > result = Lists .newArrayList ();
265- for (Condition condition : split .conditions ) {
266- for (Matcher matcher : condition .matcherGroup .matchers ) {
267- if (matcher .matcherType == MatcherType .IN_SEGMENT ) {
268- if (matcher .userDefinedSegmentMatcherData != null && matcher .userDefinedSegmentMatcherData .segmentName != null ) {
269- result .add (matcher .userDefinedSegmentMatcherData .segmentName );
270- }
254+ if (!toRemove .isEmpty ()) {
255+ _log .debug ("Deleted features: " + toRemove );
271256 }
257+
258+ _changeNumber .set (change .till );
272259 }
273260 }
274- return result ;
275261 }
276262}
0 commit comments