@@ -252,20 +252,45 @@ public SelectedPool selectWritePool(long preallocated)
252252 .toLowerCase ()));
253253 }
254254
255+ if (_zone .isPresent ()) {
256+ SelectedPool pool = filterWritePool (levels , true , preallocated );
257+ if (pool != null ) {
258+ return pool ;
259+ }
260+ }
261+
262+ SelectedPool pool = filterWritePool (levels , false , preallocated );
263+ if (pool != null ) {
264+ return pool ;
265+ }
266+
267+ throw new CacheException (CacheException .NO_POOL_ONLINE ,
268+ noOnlinePoolsErrorMessage (DirectionType .WRITE .name ()
269+ .toLowerCase ()));
270+ }
271+
272+ @ Nullable
273+ private SelectedPool filterWritePool (PoolPreferenceLevel [] levels ,
274+ boolean filterByZone ,
275+ long preallocated ) throws CacheException {
255276 CostException fallback = null ;
277+
256278 for (PoolPreferenceLevel level : levels ) {
257279 List <PoolInfo > pools =
258- level .getPoolList ().stream ()
259- .map (_costModule ::getPoolInfo )
260- .filter (Objects ::nonNull )
261- .collect (toList ());
262- pools = filterByZone (pools );
280+ level .getPoolList ().stream ()
281+ .map (_costModule ::getPoolInfo )
282+ .filter (Objects ::nonNull )
283+ .collect (toList ());
284+
285+ if (filterByZone ){
286+ pools = filterByZone (pools );
287+ }
263288
264289 if (!pools .isEmpty ()) {
265290 Partition partition = _partitionManager .getPartition (level .getTag ());
266291 try {
267292 return partition .selectWritePool (_costModule , pools , _fileAttributes ,
268- preallocated );
293+ preallocated );
269294 } catch (CostException e ) {
270295 if (!e .shouldFallBack ()) {
271296 throw e ;
@@ -275,16 +300,11 @@ public SelectedPool selectWritePool(long preallocated)
275300 }
276301 }
277302
278- /* We were asked to fall back, but all available links were
279- * exhausted. Let the caller deal with it.
280- */
281303 if (fallback != null ) {
282304 throw fallback ;
283305 }
284306
285- throw new CacheException (CacheException .NO_POOL_ONLINE ,
286- noOnlinePoolsErrorMessage (DirectionType .WRITE .name ()
287- .toLowerCase ()));
307+ return null ;
288308 }
289309
290310 @ Override
@@ -318,12 +338,37 @@ public SelectedPool selectReadPool()
318338 .toLowerCase ()));
319339 }
320340
341+
342+ if (_zone .isPresent ()) {
343+ SelectedPool pool = filterReadPool (level , onlinePoolsWithFile , true );
344+ if (pool != null ) {
345+ return pool ;
346+ }
347+ }
348+
349+ SelectedPool pool = filterReadPool (level , onlinePoolsWithFile , false );
350+ if (pool != null ) {
351+ return pool ;
352+ }
353+
354+ /* None of the pools we were allowed to read from were
355+ * online or had the file.
356+ */
357+ throw new PermissionDeniedCacheException (
358+ "File is online, but not in read-allowed pool" );
359+ }
360+
361+ @ Nullable
362+ private SelectedPool filterReadPool (PoolPreferenceLevel [] level ,
363+ Map <String , PoolInfo > onlinePoolsWithFile ,
364+ boolean filterByZone ) throws CacheException {
365+
321366 CostException costException = null ;
322367
323368 for (int prio = 0 ; prio < level .length ; prio ++) {
324369 List <String > poolsInCurrentLevel = level [prio ].getPoolList ();
325370 LOGGER .debug ("[read] Allowed pools at level {}: {}" ,
326- prio , poolsInCurrentLevel );
371+ prio , poolsInCurrentLevel );
327372
328373 if (poolsInCurrentLevel .isEmpty ()) {
329374 // No pools in this level....skip it.
@@ -341,9 +386,11 @@ public SelectedPool selectReadPool()
341386 }
342387 }
343388 LOGGER .debug ("[read] Available pools at level {}: {}" ,
344- prio , pools );
389+ prio , pools );
345390
346- pools = filterByZone (pools );
391+ if (filterByZone ) {
392+ pools = filterByZone (pools );
393+ }
347394
348395 /* If allowed, fallback to next link if current link doesn't point
349396 * to any pool holding the file.
@@ -360,14 +407,14 @@ public SelectedPool selectReadPool()
360407 * to select a pool.
361408 */
362409 _partition =
363- _partitionManager .getPartition (level [prio ].getTag ());
410+ _partitionManager .getPartition (level [prio ].getTag ());
364411
365412 /* The actual pool selection is delegated to the
366413 * Partition.
367414 */
368415 try {
369416 return _partition .selectReadPool (_costModule , pools ,
370- _fileAttributes );
417+ _fileAttributes );
371418 } catch (CostException e ) {
372419 costException = e ;
373420 if (!e .shouldFallBack ()) {
@@ -376,35 +423,21 @@ public SelectedPool selectReadPool()
376423 }
377424 }
378425
379- /* If we have a CostException where a pool was selected and
380- * shouldTryAlternatives not set then we return that pool anyway.
381- * REVISIT: consider updating partitions so they don't throw
382- * an exception in this case.
383- */
384426 if (costException != null ) {
385427 if (costException .getPool () != null
386- && !costException .shouldTryAlternatives ()) {
428+ && !costException .shouldTryAlternatives ()) {
387429 return costException .getPool ();
388430 }
389-
390431 throw costException ;
391432 }
392433
393- /* None of the pools we were allowed to read from were
394- * online or had the file.
395- */
396- throw new PermissionDeniedCacheException (
397- "File is online, but not in read-allowed pool" );
434+ return null ;
398435 }
399436
400437 private List <PoolInfo > filterByZone (List <PoolInfo > pools ) {
401- if (_zone .isPresent ()) {
402- List <PoolInfo > poolsZ =
403- pools .stream ()
438+ pools = pools .stream ()
404439 .filter (p -> Objects .equals (p .getTags ().get ("zone" ), _zone .get ()))
405440 .toList ();
406- pools = (poolsZ .isEmpty ()) ? pools : poolsZ ;
407- }
408441 return pools ;
409442 }
410443
0 commit comments