-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use as of system time when listing tables to improve performance (#565)
* use as of system time when listing tables to improve performance * address comments --------- Co-authored-by: Yingjian Wu <[email protected]>
- Loading branch information
1 parent
2c0f438
commit b4f73aa
Showing
12 changed files
with
272 additions
and
68 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...ava/com/netflix/metacat/connector/polaris/PolarisConnectorTableServiceFunctionalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.netflix.metacat.connector.polaris; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.netflix.metacat.common.QualifiedName; | ||
import com.netflix.metacat.common.dto.Pageable; | ||
import com.netflix.metacat.common.dto.Sort; | ||
import com.netflix.metacat.common.dto.SortOrder; | ||
import com.netflix.metacat.common.server.connectors.model.TableInfo; | ||
import com.netflix.metacat.connector.polaris.configs.PolarisPersistenceConfig; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.Assert; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureDataJpa; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Test PolarisConnectorTableService in functional test. | ||
* Some of the tests cannot be run in unit test as it uses h2 database, which does not support all | ||
* functionalities in crdb so include those tests here. | ||
*/ | ||
@Slf4j | ||
@ExtendWith(SpringExtension.class) | ||
@SpringBootTest(classes = {PolarisPersistenceConfig.class}) | ||
@ActiveProfiles(profiles = {"polaris_functional_test"}) | ||
@AutoConfigureDataJpa | ||
public class PolarisConnectorTableServiceFunctionalTest extends PolarisConnectorTableServiceTest { | ||
/** | ||
* Test table list. | ||
*/ | ||
@Test | ||
public void testList() { | ||
final QualifiedName name1 = QualifiedName.ofTable(CATALOG_NAME, DB_NAME, "table1"); | ||
final TableInfo tableInfo1 = TableInfo.builder() | ||
.name(name1) | ||
.metadata(ImmutableMap.of("table_type", "ICEBERG", "metadata_location", "loc1")) | ||
.build(); | ||
this.getPolarisTableService().create(this.getRequestContext(), tableInfo1); | ||
final QualifiedName name2 = QualifiedName.ofTable(CATALOG_NAME, DB_NAME, "table2"); | ||
final TableInfo tableInfo2 = TableInfo.builder() | ||
.name(name2) | ||
.metadata(ImmutableMap.of("table_type", "ICEBERG", "metadata_location", "loc2")) | ||
.build(); | ||
this.getPolarisTableService().create(this.getRequestContext(), tableInfo2); | ||
|
||
|
||
final QualifiedName qualifiedName = QualifiedName.ofTable(CATALOG_NAME, DB_NAME, ""); | ||
|
||
try { | ||
// pause execution for 10000 milliseconds (10 seconds) | ||
Thread.sleep(10000); | ||
} catch (InterruptedException e) { | ||
log.debug("Sleep was interrupted"); | ||
} | ||
|
||
List<TableInfo> tables = this.getPolarisTableService().list( | ||
this.getRequestContext(), DB_QUALIFIED_NAME, qualifiedName, new Sort(null, SortOrder.ASC), | ||
new Pageable(2, 0)); | ||
Assert.assertEquals(tables.size(), 2); | ||
Assert.assertEquals(tables.stream().map(TableInfo::getName).collect(Collectors.toSet()), | ||
ImmutableSet.of(name1, name2)); | ||
|
||
// Create a 3rd table, but this time does not sleep so this table should not be included | ||
final QualifiedName name3 = QualifiedName.ofTable(CATALOG_NAME, DB_NAME, "table3"); | ||
final TableInfo tableInfo3 = TableInfo.builder() | ||
.name(name3) | ||
.metadata(ImmutableMap.of("table_type", "ICEBERG", "metadata_location", "loc2")) | ||
.build(); | ||
this.getPolarisTableService().create(this.getRequestContext(), tableInfo3); | ||
|
||
tables = this.getPolarisTableService().list( | ||
this.getRequestContext(), DB_QUALIFIED_NAME, qualifiedName, new Sort(null, SortOrder.ASC), | ||
new Pageable(3, 0)); | ||
Assert.assertEquals(tables.size(), 2); | ||
Assert.assertEquals(tables.stream().map(TableInfo::getName).collect(Collectors.toSet()), | ||
ImmutableSet.of(name1, name2)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
.../java/com/netflix/metacat/connector/polaris/store/repos/PolarisTableCustomRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.netflix.metacat.connector.polaris.store.repos; | ||
|
||
import com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity; | ||
import java.util.List; | ||
|
||
/** | ||
* Custom JPA repository implementation for storing PolarisTableEntity. | ||
*/ | ||
public interface PolarisTableCustomRepository { | ||
/** | ||
* Fetch table entities for given database using AS OF SYSTEM TIME follower_read_timestamp(). | ||
* @param dbName database name | ||
* @param tableNamePrefix table name prefix. can be empty. | ||
* @param pageSize target size for each page | ||
* @return table entities in the database. | ||
*/ | ||
List<PolarisTableEntity> findAllTablesByDbNameAndTablePrefix( | ||
String dbName, String tableNamePrefix, int pageSize); | ||
} |
78 changes: 78 additions & 0 deletions
78
...a/com/netflix/metacat/connector/polaris/store/repos/PolarisTableCustomRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.netflix.metacat.connector.polaris.store.repos; | ||
|
||
import javax.persistence.PersistenceContext; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.Query; | ||
|
||
import com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.data.domain.SliceImpl; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Sort; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Implementation for Custom JPA repository implementation for storing PolarisTableEntity. | ||
*/ | ||
@Repository | ||
public class PolarisTableCustomRepositoryImpl implements PolarisTableCustomRepository { | ||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
private Slice<PolarisTableEntity> findAllTablesByDbNameAndTablePrefixForCurrentPage( | ||
final String dbName, final String tableNamePrefix, final Pageable page) { | ||
|
||
// Generate ORDER BY clause | ||
String orderBy = ""; | ||
if (page.getSort().isSorted()) { | ||
orderBy = page.getSort().stream() | ||
.map(order -> order.getProperty() + " " + order.getDirection()) | ||
.collect(Collectors.joining(", ")); | ||
orderBy = " ORDER BY " + orderBy; | ||
} | ||
|
||
final String sql = "SELECT t.* FROM TBLS t " | ||
+ "WHERE t.db_name = :dbName AND t.tbl_name LIKE :tableNamePrefix" + orderBy; | ||
final Query query = entityManager.createNativeQuery(sql, PolarisTableEntity.class); | ||
query.setParameter("dbName", dbName); | ||
query.setParameter("tableNamePrefix", tableNamePrefix + "%"); | ||
query.setFirstResult(page.getPageNumber() * page.getPageSize()); | ||
query.setMaxResults(page.getPageSize() + 1); // Fetch one extra result to determine if there is a next page | ||
final List<PolarisTableEntity> resultList = query.getResultList(); | ||
// Check if there is a next page | ||
final boolean hasNext = resultList.size() > page.getPageSize(); | ||
// If there is a next page, remove the last item from the list | ||
if (hasNext) { | ||
resultList.remove(resultList.size() - 1); | ||
} | ||
return new SliceImpl<>(resultList, page, hasNext); | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public List<PolarisTableEntity> findAllTablesByDbNameAndTablePrefix( | ||
final String dbName, final String tableNamePrefix, final int pageFetchSize) { | ||
Pageable page = PageRequest.of(0, pageFetchSize, Sort.by("tbl_name").ascending()); | ||
entityManager.createNativeQuery("SET TRANSACTION AS OF SYSTEM TIME follower_read_timestamp()") | ||
.executeUpdate(); | ||
final List<PolarisTableEntity> retval = new ArrayList<>(); | ||
final String tblPrefix = tableNamePrefix == null ? "" : tableNamePrefix; | ||
Slice<PolarisTableEntity> tbls; | ||
boolean hasNext; | ||
do { | ||
tbls = findAllTablesByDbNameAndTablePrefixForCurrentPage(dbName, tblPrefix, page); | ||
retval.addAll(tbls.toList()); | ||
hasNext = tbls.hasNext(); | ||
if (hasNext) { | ||
page = tbls.nextPageable(); | ||
} | ||
} while (hasNext); | ||
return retval; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.