3636
3737import java .io .IOException ;
3838import java .util .HashMap ;
39+ import java .util .HashSet ;
3940import java .util .List ;
4041import java .util .Map ;
42+ import java .util .Set ;
4143import java .util .concurrent .Executor ;
4244import java .util .function .BiFunction ;
4345import java .util .stream .IntStream ;
4749
4850public class MetadataPersistentTasksTests extends ESTestCase {
4951
52+ private Set <Long > assignedAllocationIds ;
5053 private NamedWriteableRegistry namedWriteableRegistry ;
5154 private NamedWriteableRegistry namedWriteableRegistryBwc ;
5255
5356 @ Before
5457 public void initializeRegistries () {
58+ assignedAllocationIds = new HashSet <>();
5559 new PersistentTasksExecutorRegistry (
5660 List .of (
5761 new TestClusterPersistentTasksExecutor (TestClusterPersistentTasksParams .NAME , null ),
@@ -191,12 +195,18 @@ public void testPersistentTasksDiffSerializationBwc() throws IOException {
191195 );
192196 }
193197
194- private static <T extends PersistentTasks > T mutatePersistentTasks (
198+ private long randomUniqueAllocationIdBetween (long min , long max ) {
199+ final long allocationId = randomValueOtherThanMany (id -> assignedAllocationIds .contains (id ), () -> randomLongBetween (min , max ));
200+ assignedAllocationIds .add (allocationId );
201+ return allocationId ;
202+ }
203+
204+ private <T extends PersistentTasks > T mutatePersistentTasks (
195205 PersistentTasks persistentTasks ,
196206 BiFunction <String , Long , PersistentTask <?>> oneTaskFunc ,
197207 BiFunction <Long , Map <String , PersistentTask <?>>, T > tasksFunc
198208 ) {
199- final long updatedAllocationId = persistentTasks .getLastAllocationId () + randomLongBetween (100 , 1000 );
209+ final long updatedLastAllocationId = persistentTasks .getLastAllocationId () + randomLongBetween (100 , 1000 );
200210 final Map <String , PersistentTask <?>> tasks = persistentTasks .taskMap ();
201211
202212 final var updatedTasks = new HashMap <>(tasks );
@@ -206,7 +216,10 @@ private static <T extends PersistentTasks> T mutatePersistentTasks(
206216 final String taskId = randomTaskId ();
207217 updatedTasks .put (
208218 taskId ,
209- oneTaskFunc .apply (taskId , randomLongBetween (persistentTasks .getLastAllocationId () + i + 1 , updatedAllocationId ))
219+ oneTaskFunc .apply (
220+ taskId ,
221+ randomUniqueAllocationIdBetween (persistentTasks .getLastAllocationId () + i + 1 , updatedLastAllocationId )
222+ )
210223 );
211224 });
212225 } else {
@@ -215,14 +228,14 @@ private static <T extends PersistentTasks> T mutatePersistentTasks(
215228 updatedTasks .remove (randomFrom (updatedTasks .keySet ()));
216229 }
217230 }
218- return tasksFunc .apply (updatedAllocationId , updatedTasks );
231+ return tasksFunc .apply (updatedLastAllocationId , updatedTasks );
219232 }
220233
221234 private static String randomTaskId () {
222235 return randomAlphaOfLength (15 );
223236 }
224237
225- private static Metadata randomMetadataWithPersistentTasks () {
238+ private Metadata randomMetadataWithPersistentTasks () {
226239 final long lastAllocationIdCluster = randomLongBetween (0 , Long .MAX_VALUE / 2 );
227240 final var clusterPersistentTasksCustomMetadata = new ClusterPersistentTasksCustomMetadata (
228241 lastAllocationIdCluster ,
@@ -268,26 +281,26 @@ private static PersistentTask<?> oneProjectPersistentTask(String taskId, long al
268281 );
269282 }
270283
271- public static Map <String , PersistentTask <?>> randomClusterPersistentTasks (long allocationId ) {
284+ private Map <String , PersistentTask <?>> randomClusterPersistentTasks (long allocationId ) {
272285 return randomMap (0 , 5 , () -> {
273286 final String taskId = randomTaskId ();
274- return new Tuple <>(taskId , oneClusterPersistentTask (taskId , randomLongBetween (0 , allocationId )));
287+ return new Tuple <>(taskId , oneClusterPersistentTask (taskId , randomUniqueAllocationIdBetween (0 , allocationId )));
275288 });
276289 }
277290
278- public static Map <String , PersistentTask <?>> randomProjectPersistentTasks (long allocationId ) {
291+ private Map <String , PersistentTask <?>> randomProjectPersistentTasks (long allocationId ) {
279292 return randomMap (0 , 5 , () -> {
280293 final String taskId = randomTaskId ();
281- return new Tuple <>(taskId , oneProjectPersistentTask (taskId , randomLongBetween (0 , allocationId )));
294+ return new Tuple <>(taskId , oneProjectPersistentTask (taskId , randomUniqueAllocationIdBetween (0 , allocationId )));
282295 });
283296 }
284297
285- public static Tuple <Metadata , Metadata > randomMetadataAndUpdate () {
298+ private Tuple <Metadata , Metadata > randomMetadataAndUpdate () {
286299 final Metadata before = randomMetadataWithPersistentTasks ();
287300 final Metadata after = Metadata .builder (before )
288301 .putCustom (
289302 ClusterPersistentTasksCustomMetadata .TYPE ,
290- MetadataPersistentTasksTests .<ClusterPersistentTasksCustomMetadata >mutatePersistentTasks (
303+ this .<ClusterPersistentTasksCustomMetadata >mutatePersistentTasks (
291304 ClusterPersistentTasksCustomMetadata .get (before ),
292305 MetadataPersistentTasksTests ::oneClusterPersistentTask ,
293306 ClusterPersistentTasksCustomMetadata ::new
0 commit comments