Skip to content

Commit

Permalink
Populate location in polaris database with fallback if not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
insyncoss committed Jan 27, 2022
1 parent 2ad270c commit 7fb2477
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.server.connectors.ConnectorContext;
import com.netflix.metacat.common.server.connectors.ConnectorDatabaseService;
import com.netflix.metacat.common.server.connectors.ConnectorRequestContext;
import com.netflix.metacat.common.server.connectors.ConnectorUtils;
Expand All @@ -28,17 +29,23 @@
*/
@Slf4j
public class PolarisConnectorDatabaseService implements ConnectorDatabaseService {
private static final String DEFAULT_LOCATION_SUFFIX = ".db";
private static final String DB_DEFAULT_LOCATION = "polaris.db-default-location";
private final String defaultLocationPrefix;
private final PolarisStoreService polarisStoreService;

/**
* Constructor.
*
* @param polarisStoreService polaris connector
* @param connectorContext connector context
*/
public PolarisConnectorDatabaseService(
final PolarisStoreService polarisStoreService
final PolarisStoreService polarisStoreService,
final ConnectorContext connectorContext
) {
this.polarisStoreService = polarisStoreService;
this.defaultLocationPrefix = connectorContext.getConfiguration().getOrDefault(DB_DEFAULT_LOCATION, "");
}

/**
Expand All @@ -52,7 +59,9 @@ public void create(final ConnectorRequestContext context, final DatabaseInfo dat
throw new DatabaseAlreadyExistsException(name);
}
try {
this.polarisStoreService.createDatabase(name.getDatabaseName(), null /* TODO: add default */);
final String location = databaseInfo.getUri() == null
? this.defaultLocationPrefix + name.getDatabaseName() + DEFAULT_LOCATION_SUFFIX : databaseInfo.getUri();
this.polarisStoreService.createDatabase(name.getDatabaseName(), location);
} catch (DataIntegrityViolationException exception) {
throw new InvalidMetaException(name, exception);
} catch (Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ public class PolarisConnectorConfig {
/**
* Create polaris connector database service.
*
* @param polarisStoreService polaris store service.
* @param polarisStoreService polaris store service
* @param connectorContext connector context
* @return PolarisConnectorDatabaseService
*/
@Bean
@ConditionalOnMissingBean(PolarisConnectorDatabaseService.class)
public PolarisConnectorDatabaseService polarisDatabaseService(
final PolarisStoreService polarisStoreService
final PolarisStoreService polarisStoreService,
final ConnectorContext connectorContext
) {
return new PolarisConnectorDatabaseService(polarisStoreService);
return new PolarisConnectorDatabaseService(polarisStoreService, connectorContext);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public PolarisDatabaseMapper(final String catalogName) {
public DatabaseInfo toInfo(final PolarisDatabaseEntity entity) {
final DatabaseInfo databaseInfo = DatabaseInfo.builder()
.name(QualifiedName.ofDatabase(catalogName, entity.getDbName()))
.uri(entity.getLocation())
.build();
return databaseInfo;
}
Expand All @@ -40,6 +41,7 @@ public DatabaseInfo toInfo(final PolarisDatabaseEntity entity) {
public PolarisDatabaseEntity toEntity(final DatabaseInfo info) {
final PolarisDatabaseEntity databaseEntity = PolarisDatabaseEntity.builder()
.dbName(info.getName().getDatabaseName())
.location(info.getUri())
.build();
return databaseEntity;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@

package com.netflix.metacat.connector.polaris;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.netflix.metacat.common.QualifiedName;
import com.netflix.metacat.common.server.connectors.ConnectorContext;
import com.netflix.metacat.common.server.connectors.ConnectorRequestContext;
import com.netflix.metacat.common.server.connectors.exception.DatabaseAlreadyExistsException;
import com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException;
import com.netflix.metacat.common.server.connectors.model.DatabaseInfo;
import com.netflix.metacat.common.server.properties.DefaultConfigImpl;
import com.netflix.metacat.common.server.properties.MetacatProperties;
import com.netflix.metacat.connector.polaris.configs.PolarisPersistenceConfig;
import com.netflix.metacat.connector.polaris.store.PolarisStoreService;
import com.netflix.spectator.api.NoopRegistry;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -43,6 +48,9 @@ public class PolarisConnectorDatabaseServiceTest {
@Autowired
private PolarisStoreService polarisStoreService;

@Shared
private ConnectorContext connectorContext;

@Shared
private ConnectorRequestContext requestContext = new ConnectorRequestContext();

Expand All @@ -54,7 +62,9 @@ public class PolarisConnectorDatabaseServiceTest {
*/
@BeforeEach
public void init() {
polarisDBService = new PolarisConnectorDatabaseService(polarisStoreService);
connectorContext = new ConnectorContext(CATALOG_NAME, CATALOG_NAME, "polaris",
new DefaultConfigImpl(new MetacatProperties()), new NoopRegistry(), null, Maps.newHashMap());
polarisDBService = new PolarisConnectorDatabaseService(polarisStoreService, connectorContext);
}

/**
Expand All @@ -72,7 +82,7 @@ public void testCreateDb() {
*/
@Test
public void testGetDb() {
final DatabaseInfo info = DatabaseInfo.builder().name(DB1_QUALIFIED_NAME).build();
final DatabaseInfo info = DatabaseInfo.builder().name(DB1_QUALIFIED_NAME).uri("uri").build();
polarisDBService.create(requestContext, info);
final DatabaseInfo result = polarisDBService.get(requestContext, DB1_QUALIFIED_NAME);
Assert.assertEquals(info, result);
Expand All @@ -99,12 +109,25 @@ public void testCreateDbAlreadyExists() {
() -> polarisDBService.create(requestContext, info));
}

/**
* Test create database with no uri set should fallback to default uri.
*/
@Test
public void testCreateDbDefaultUri() {
final DatabaseInfo info = DatabaseInfo.builder().name(DB1_QUALIFIED_NAME).build();
polarisDBService.create(requestContext, info);
final DatabaseInfo infoExpected = DatabaseInfo.builder()
.name(DB1_QUALIFIED_NAME).uri("db1_name.db").build();
final DatabaseInfo result = polarisDBService.get(requestContext, DB1_QUALIFIED_NAME);
Assert.assertEquals(infoExpected, result);
}

/**
* Test update database.
*/
@Test
public void testUpdateDb() {
final DatabaseInfo info = DatabaseInfo.builder().name(DB1_QUALIFIED_NAME).build();
final DatabaseInfo info = DatabaseInfo.builder().name(DB1_QUALIFIED_NAME).uri("uri").build();
polarisDBService.create(requestContext, info);
Assert.assertTrue(polarisDBService.exists(requestContext, DB1_QUALIFIED_NAME));
polarisDBService.update(requestContext, info);
Expand All @@ -129,8 +152,8 @@ public void testDeleteDb() {
*/
@Test
public void testListDb() {
final DatabaseInfo db1 = DatabaseInfo.builder().name(DB1_QUALIFIED_NAME).build();
final DatabaseInfo db2 = DatabaseInfo.builder().name(DB2_QUALIFIED_NAME).build();
final DatabaseInfo db1 = DatabaseInfo.builder().name(DB1_QUALIFIED_NAME).uri("uri1").build();
final DatabaseInfo db2 = DatabaseInfo.builder().name(DB2_QUALIFIED_NAME).uri("uri2").build();
polarisDBService.create(requestContext, db1);
polarisDBService.create(requestContext, db2);
Assert.assertTrue(polarisDBService.exists(requestContext, DB1_QUALIFIED_NAME));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class PolarisConnectorTableServiceTest {
public void init() {
connectorContext = new ConnectorContext(CATALOG_NAME, CATALOG_NAME, "polaris",
new DefaultConfigImpl(new MetacatProperties()), new NoopRegistry(), null, Maps.newHashMap());
polarisDBService = new PolarisConnectorDatabaseService(polarisStoreService);
polarisDBService = new PolarisConnectorDatabaseService(polarisStoreService, connectorContext);
polarisTableService = new PolarisConnectorTableService(
polarisStoreService,
CATALOG_NAME,
Expand Down

0 comments on commit 7fb2477

Please sign in to comment.