Skip to content

Commit

Permalink
[TH2-5165] corrected after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Smirnov-Exactpro committed Feb 19, 2024
1 parent 10c1056 commit ad5f947
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
52 changes: 21 additions & 31 deletions cradle-core/src/main/java/com/exactpro/cradle/BookInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import org.apache.commons.lang3.concurrent.AtomicInitializer;
import org.apache.commons.lang3.concurrent.ConcurrentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -32,6 +30,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicReference;

/**
* Information about a book
Expand All @@ -52,9 +51,10 @@ public class BookInfo
private final LoadingCache<Long, IPageInterval> randomAccessCache;

// AtomicInitializer call initialize method again if previous value is null
private final AtomicInitializer<PageInfo> firstPage;
private final AtomicReference<PageInfo> firstPage = new AtomicReference<>();
private final PagesLoader pagesLoader;
private final PageLoader lastPagesLoader;
private final PageLoader firstPageLoader;
private final PageLoader lastPageLoader;

public BookInfo(BookId id,
String fullName,
Expand All @@ -69,23 +69,16 @@ public BookInfo(BookId id,
this.desc = desc;
this.created = created;
this.pagesLoader = pagesLoader;
this.lastPagesLoader = lastPageLoader;
this.firstPageLoader = firstPageLoader;
this.lastPageLoader = lastPageLoader;

this.firstPage = new AtomicInitializer<>() {
@Override
protected PageInfo initialize() {
return firstPageLoader.load(id);
}
};
this.hotCache = Caffeine.newBuilder()
.maximumSize(HOT_CACHE_SIZE)
.build(this::createPageInterval);

this.randomAccessCache = Caffeine.newBuilder()
.maximumSize(cacheSize)
.build(this::createPageInterval);

initializeHotCache();
}

public BookId getId()
Expand Down Expand Up @@ -119,17 +112,22 @@ public Collection<PageInfo> getPages()

public @Nullable PageInfo getFirstPage()
{
try {
return firstPage.get();
} catch (ConcurrentException e) {
LOGGER.error("Unexpected exception during first page lazy initialization", e);
return null;
}
PageInfo result = firstPage.get();

if (result == null) {
result = firstPageLoader.load(id);
if (!firstPage.compareAndSet(null, result)) {
// another thread has initialized the reference
result = firstPage.get();
}
}

return result;
}

public @Nullable PageInfo getLastPage()
{
return lastPagesLoader.load(id);
return lastPageLoader.load(id);
}

public PageInfo getPage(PageId pageId)
Expand Down Expand Up @@ -161,7 +159,7 @@ public PageInfo getPreviousPage(Instant startTimestamp)
}

void invalidate() {
// FIXME: invalidate initializer
firstPage.set(null);
hotCache.invalidateAll();
randomAccessCache.invalidateAll();
}
Expand Down Expand Up @@ -191,14 +189,6 @@ private void invalidate(long epochDay) {
randomAccessCache.invalidate(epochDay);
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private void initializeHotCache() {
long currentEpochDay = currentEpochDay();
for (int shift = HOT_CACHE_SIZE - 1; shift >= 0; shift--) {
this.hotCache.get(currentEpochDay - shift);
}
}

private IPageInterval getPageInterval(long epochDate) {
long currentEpochDate = currentEpochDay();
IPageInterval pageInterval = currentEpochDate - epochDate < 2
Expand Down Expand Up @@ -300,13 +290,13 @@ public PageInfo find(Instant timestamp)

@Override
public PageInfo next(Instant startTimestamp) {
Entry<Instant, PageInfo> result = pageByInstant.ceilingEntry(startTimestamp.plus(1, ChronoUnit.NANOS));
Entry<Instant, PageInfo> result = pageByInstant.higherEntry(startTimestamp);
return result != null ? result.getValue() : null;
}

@Override
public PageInfo previous(Instant startTimestamp) {
Entry<Instant, PageInfo> result = pageByInstant.floorEntry(startTimestamp.minus(1, ChronoUnit.NANOS));
Entry<Instant, PageInfo> result = pageByInstant.lowerEntry(startTimestamp);
return result != null ? result.getValue() : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,6 @@ public PageInfo updatePageName(BookId bookId, String pageName, String newPageNam
}

public PageInfo updatePageName(BookId bookId, Instant pageStart, String pageName, String newPageName) throws CradleStorageException {
getBookCache().getBook(bookId);
PageInfo updatedPageInfo = doUpdatePageName(bookId, pageName, newPageName);

try {
Expand Down
10 changes: 6 additions & 4 deletions cradle-core/src/test/java/com/exactpro/cradle/BookInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@
public class BookInfoTest {
public static Random RANDOM = new Random();
public static final BookId BOOK_ID = new BookId("test-book");
public static final List<PageInfo> PAGES;
private static final List<PageInfo> PAGES;

static {
PAGES = new ArrayList<>();
List<PageInfo> pages = new ArrayList<>();
Instant start = Instant.now().minus(7, ChronoUnit.DAYS);
Instant end = Instant.now();
Instant current = start;
Instant previous;
do {
previous = current;
current = current.plus(1, ChronoUnit.HOURS);
PAGES.add(createPageInfo(previous, current));
pages.add(createPageInfo(previous, current));
} while (current.isBefore(end));
PAGES.add(createPageInfo(current, current));
pages.add(createPageInfo(current, current));

PAGES = Collections.unmodifiableList(pages);
}

@Test
Expand Down

0 comments on commit ad5f947

Please sign in to comment.