Skip to content

Commit

Permalink
[th2-5232] Fixed the problem - page cache doesn't work correct when s…
Browse files Browse the repository at this point in the history
…torage has removed page(s)
  • Loading branch information
Nikita-Smirnov-Exactpro committed Nov 5, 2024
1 parent b87a6e9 commit 9ac67fc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 30 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Overview (5.4.3)
# Overview (5.4.4)

Cradle API is used to work with Cradle - the datalake where th2 stores its data.

Expand Down Expand Up @@ -209,6 +209,9 @@ Test events have mandatory parameters that are verified when storing an event. T

## Release notes

### 5.4.4
* Fixed the problem - page cache doesn't work correct when storage has removed page(s)

### 5.4.3
* Refactored the CassandraCradleStorage methods (doAddPages, doUpdatePageName, doUpdatePageComment) to execute insert and update operations atomically.
* Updated th2 gradle plugin: `0.1.3` (bom: `4.8.0`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Collection<PageInfo> loadPageInfo(BookId bookId, Instant start, Instant end, boo
}

public PageInfo loadLastPageInfo(BookId bookId, boolean loadRemoved) {
for (PageEntity pageEntity : operators.getPageOperator().getLast(
for (PageEntity pageEntity : operators.getPageOperator().getAllDesc(
bookId.getName(),
readAttrs
)) {
Expand All @@ -142,10 +142,7 @@ public PageInfo loadLastPageInfo(BookId bookId, boolean loadRemoved) {
}

public PageInfo loadFirstPageInfo(BookId bookId, boolean loadRemoved) {
for (PageEntity pageEntity : operators.getPageOperator().getFirst(
bookId.getName(),
readAttrs
)) {
for (PageEntity pageEntity : operators.getPageOperator().getAll(bookId.getName(), readAttrs)) {
if (loadRemoved || pageEntity.getRemoved() == null || pageEntity.getRemoved().equals(DEFAULT_PAGE_REMOVE_TIME)) {
return pageEntity.toPageInfo();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ public interface PageOperator {
@Select
PagingIterable<PageEntity> getAll(String book, Function<BoundStatementBuilder, BoundStatementBuilder> attributes);

/**
* Executes request to Cassandra to receive several pages in descending order
*
* @param book - book id
* @param attributes - request attributes
* @return iterator for all pages in descending order
*/
@Query("SELECT * FROM ${qualifiedTableId} " +
"WHERE " +
FIELD_BOOK +"=:book " +
"ORDER BY " +
FIELD_START_DATE + " DESC, " +
FIELD_START_TIME + " DESC")
PagingIterable<PageEntity> getAllDesc(String book, Function<BoundStatementBuilder, BoundStatementBuilder> attributes);

/**
* Executes request to Cassandra to receive several pages
* @param book - book id
Expand Down Expand Up @@ -73,24 +88,6 @@ PagingIterable<PageEntity> getAllDescBefore(String book,
LocalDate date, LocalTime time,
Function<BoundStatementBuilder, BoundStatementBuilder> attributes);

@Query("SELECT * FROM ${qualifiedTableId} " +
"WHERE " +
FIELD_BOOK +"=:book " +
"ORDER BY " +
FIELD_START_DATE + " DESC, " +
FIELD_START_TIME + " DESC " +
"LIMIT 1")
PagingIterable<PageEntity> getLast(String book, Function<BoundStatementBuilder, BoundStatementBuilder> attributes);

@Query("SELECT * FROM ${qualifiedTableId} " +
"WHERE " +
FIELD_BOOK +"=:book " +
"ORDER BY " +
FIELD_START_DATE + " ASC, " +
FIELD_START_TIME + " ASC " +
"LIMIT 1")
PagingIterable<PageEntity> getFirst(String book, Function<BoundStatementBuilder, BoundStatementBuilder> attributes);


@Update(nullSavingStrategy = NullSavingStrategy.SET_TO_NULL)
ResultSet update(PageEntity entity, Function<BoundStatementBuilder, BoundStatementBuilder> attributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.exactpro.cradle.Order;
import com.exactpro.cradle.PageId;
import com.exactpro.cradle.PageInfo;
import com.exactpro.cradle.PageToAdd;
import com.exactpro.cradle.cassandra.CassandraCradleStorage;
import com.exactpro.cradle.cassandra.integration.CassandraCradleHelper;
import com.exactpro.cradle.utils.CradleStorageException;
Expand All @@ -37,6 +38,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.exactpro.cradle.BookInfoMetrics.CacheName.HOT;
import static com.exactpro.cradle.BookInfoMetrics.CacheName.RANDOM;
Expand All @@ -54,6 +56,9 @@ public class BookInfoApiTest {
private static final Instant NOW = Instant.now();
private static final BookId BOOK_ID = new BookId("test_book");

private static final PageInfo PAGE_0 = new PageInfo( // -2 00:00 - -1 00:00
new PageId(BOOK_ID, NOW.minus(2, DAYS).truncatedTo(DAYS), "page-0"),
NOW.minus(1, DAYS).truncatedTo(DAYS), "");
private static final PageInfo PAGE_1 = new PageInfo( // -1 00:00 - 1 00:00
new PageId(BOOK_ID, NOW.minus(1, DAYS).truncatedTo(DAYS), "page-1"),
NOW.plus(1, DAYS).truncatedTo(DAYS), "");
Expand All @@ -77,9 +82,10 @@ public class BookInfoApiTest {
public void beforeClass() throws IOException, CradleStorageException {
storage = CassandraCradleHelper.getInstance().getStorage();
storage.addBook(new BookToAdd(BOOK_ID.getName(), NOW.minus(100, DAYS)));
for (PageInfo page : PAGES) {
storage.addPage(BOOK_ID, page.getName(), page.getStarted(), page.getComment());
}
storage.addPages(BOOK_ID, Stream.concat(Stream.of(PAGE_0), PAGES.stream())
.map(pageInfo -> new PageToAdd(pageInfo.getName(), pageInfo.getStarted(), pageInfo.getComment()))
.collect(Collectors.toList()));
storage.removePage(PAGE_0.getId());
}

@BeforeMethod
Expand Down Expand Up @@ -145,7 +151,7 @@ public void getPagesByTimeTest(Order order) {
assertFalse(bookInfo.getPages(NOW.minus(4, DAYS), NOW.minus(3, DAYS), order).hasNext());
assertLoadCount(hotCacheLoad, randomCacheLoad, "after get pages before first page");

assertLoadCount(hotCacheLoad, randomCacheLoad, "after get [1, 2] pages");
assertLoadCount(hotCacheLoad, randomCacheLoad, "before get [1, 2] pages");
assertEquals(
extractNames(bookInfo.getPages(
NOW.minus(1, DAYS).truncatedTo(DAYS),
Expand All @@ -156,7 +162,7 @@ public void getPagesByTimeTest(Order order) {
);
assertLoadCount(hotCacheLoad, randomCacheLoad += 1, "after get [1, 2] pages");

assertLoadCount(hotCacheLoad, randomCacheLoad, "after get [3, 4] pages");
assertLoadCount(hotCacheLoad, randomCacheLoad, "before get [3, 4] pages");
assertEquals(
extractNames(bookInfo.getPages(
NOW.plus(1, DAYS).truncatedTo(DAYS)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
release_version=5.4.3
release_version=5.4.4
description='Cradle API'
vcs_url=https://github.com/th2-net/cradleapi

0 comments on commit 9ac67fc

Please sign in to comment.