Skip to content
Open
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,12 @@ public boolean check(long timeInMillis, int location) {
dataLock.lock();
try {
long resolvedTime = resolution.convert(timeInMillis, TimeUnit.MILLISECONDS);
// if currentInstant > resolvedTime then it means that bitset is already cleared for that particular resolvedTime
// which increases the chances of getting duplicated id generation
if (currentInstant > resolvedTime) {
return false;
}

if (currentInstant != resolvedTime) {
currentInstant = resolvedTime;
bitSet.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.appform.ranger.discovery.bundle.id.constraints.impl.PartitionValidator;
import io.appform.ranger.discovery.bundle.id.formatter.IdFormatters;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.awaitility.Awaitility;
Expand All @@ -31,11 +32,8 @@
import org.junit.jupiter.api.Test;

import java.time.*;
import java.util.Collections;
import java.util.Date;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.*;
import java.util.concurrent.*;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert wildcard imports with individual imports. This would raise issues on sonar

import java.util.stream.IntStream;

/**
Expand Down Expand Up @@ -235,6 +233,26 @@ void testParseSuccessAfterGeneration() {
Assertions.assertEquals(parsedId.getGeneratedDate(), generatedId.getGeneratedDate());
}

@Test
@SneakyThrows
void testConcurrentIdGenerators() {
ExecutorService executorService = Executors.newFixedThreadPool(32);
ConcurrentSkipListSet<String> concurrentIds = new ConcurrentSkipListSet<>();
Long startTime = System.currentTimeMillis();
List<CompletableFuture> futures = new ArrayList<>();
for (int numThreads = 0; numThreads < 32; numThreads++) {
futures.add(CompletableFuture.runAsync(() -> {
for (int hits = 0; hits < 1000; hits++) {
Id id = IdGenerator.generate("0E");
concurrentIds.add(id.getId());
}
}, executorService));
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[32])).get();
Assertions.assertEquals(32000, concurrentIds.size());

}


@SuppressWarnings("SameParameterValue")
private Date generateDate(int year, int month, int day, int hour, int min, int sec, int ms, ZoneId zoneId) {
Expand Down