Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public Iterator<StorageLocation> getLocations()

private final int numStorageLocations = storageLocations.size();
private int remainingIterations = numStorageLocations;
// Each call to this methods starts with a different startIndex to avoid the same location being picked up over
// again. See https://github.com/apache/incubator-druid/issues/8614.
private int i = startIndex.getAndUpdate(n -> (n + 1) % numStorageLocations);

@Override
public boolean hasNext()
Expand All @@ -62,8 +65,10 @@ public StorageLocation next()
throw new NoSuchElementException();
}
remainingIterations--;
final StorageLocation nextLocation =
storageLocations.get(startIndex.getAndUpdate(n -> (n + 1) % numStorageLocations));
final StorageLocation nextLocation = storageLocations.get(i++);
if (i == numStorageLocations) {
i = 0;
}
return nextLocation;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,74 @@ public void testRoundRobinLocationSelectorStrategy() throws Exception

StorageLocationSelectorStrategy roundRobinStrategy = new RoundRobinStorageLocationSelectorStrategy(storageLocations);

iterateLocs(localStorageFolder1, localStorageFolder2, localStorageFolder3, roundRobinStrategy);
iterateLocs(localStorageFolder1, localStorageFolder2, localStorageFolder3, roundRobinStrategy);
iterateLocs(localStorageFolder1, localStorageFolder2, localStorageFolder3, roundRobinStrategy);
iterateLocs(localStorageFolder1, localStorageFolder2, localStorageFolder3, roundRobinStrategy);
iterateLocs(localStorageFolder1, localStorageFolder2, localStorageFolder3, roundRobinStrategy);
// First call to getLocations()
Iterator<StorageLocation> locations = roundRobinStrategy.getLocations();

StorageLocation loc1 = locations.next();
Assert.assertEquals("The next element of the iterator should point to path local_storage_folder_1",
localStorageFolder1, loc1.getPath());

StorageLocation loc2 = locations.next();
Assert.assertEquals("The next element of the iterator should point to path local_storage_folder_2",
localStorageFolder2, loc2.getPath());

StorageLocation loc3 = locations.next();
Assert.assertEquals("The next element of the iterator should point to path local_storage_folder_3",
localStorageFolder3, loc3.getPath());


// Second call to getLocations()
locations = roundRobinStrategy.getLocations();

loc2 = locations.next();
Assert.assertEquals("The next element of the iterator should point to path local_storage_folder_2",
localStorageFolder2, loc2.getPath());

loc3 = locations.next();
Assert.assertEquals("The next element of the iterator should point to path local_storage_folder_3",
localStorageFolder3, loc3.getPath());

loc1 = locations.next();
Assert.assertEquals("The next element of the iterator should point to path local_storage_folder_1",
localStorageFolder1, loc1.getPath());
}

private void iterateLocs(File localStorageFolder1, File localStorageFolder2, File localStorageFolder3,
StorageLocationSelectorStrategy roundRobinStrategy)
@Test
public void testRoundRobinLocationSelectorStrategyMultipleCallsToGetLocations() throws Exception
{
List<StorageLocation> storageLocations = new ArrayList<>();

final File localStorageFolder1 = tmpFolder.newFolder("local_storage_folder_1");
final File localStorageFolder2 = tmpFolder.newFolder("local_storage_folder_2");
final File localStorageFolder3 = tmpFolder.newFolder("local_storage_folder_3");

storageLocations.add(new StorageLocation(localStorageFolder1, 10000000000L, null));
storageLocations.add(new StorageLocation(localStorageFolder2, 10000000000L, null));
storageLocations.add(new StorageLocation(localStorageFolder3, 10000000000L, null));

StorageLocationSelectorStrategy roundRobinStrategy = new RoundRobinStorageLocationSelectorStrategy(storageLocations);

Iterator<StorageLocation> locations = roundRobinStrategy.getLocations();

StorageLocation loc1 = locations.next();
Assert.assertEquals("The next element of the iterator should point to path local_storage_folder_1",
localStorageFolder1, loc1.getPath());

locations = roundRobinStrategy.getLocations();

StorageLocation loc2 = locations.next();
Assert.assertEquals("The next element of the iterator should point to path local_storage_folder_2",
localStorageFolder2, loc2.getPath());

locations = roundRobinStrategy.getLocations();

StorageLocation loc3 = locations.next();
Assert.assertEquals("The next element of the iterator should point to path local_storage_folder_3",
localStorageFolder3, loc3.getPath());

loc1 = locations.next();
Assert.assertEquals("The next element of the iterator should point to path local_storage_folder_1",
localStorageFolder1, loc1.getPath());
}

}