Skip to content

Commit

Permalink
Revert "add support for setting throughput on database creation"
Browse files Browse the repository at this point in the history
This reverts commit 4ddf06e.
  • Loading branch information
Blackbaud-MikeLueders committed Oct 1, 2021
1 parent 6387097 commit 2e51219
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package com.azure.spring.data.cosmos.core;

import com.azure.cosmos.CosmosAsyncClient;
import com.azure.cosmos.CosmosAsyncDatabase;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.CosmosException;
import com.azure.cosmos.implementation.ConflictException;
Expand Down Expand Up @@ -118,9 +117,18 @@ public class CosmosTemplateIT {
public void setUp() throws ClassNotFoundException {
if (cosmosTemplate == null) {
client = CosmosFactory.createCosmosAsyncClient(cosmosClientBuilder);
final CosmosFactory cosmosFactory = new CosmosFactory(client, TestConstants.DB_NAME);

final CosmosMappingContext mappingContext = new CosmosMappingContext();
personInfo = new CosmosEntityInformation<>(Person.class);
containerName = personInfo.getContainerName();
cosmosTemplate = createCosmosTemplate(cosmosConfig, TestConstants.DB_NAME);

mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class));

final MappingCosmosConverter cosmosConverter = new MappingCosmosConverter(mappingContext,
null);

cosmosTemplate = new CosmosTemplate(cosmosFactory, cosmosConfig, cosmosConverter);
}

collectionManager.ensureContainersCreatedAndEmpty(cosmosTemplate, Person.class,
Expand All @@ -129,14 +137,6 @@ public void setUp() throws ClassNotFoundException {
new PartitionKey(TEST_PERSON.getLastName()));
}

private CosmosTemplate createCosmosTemplate(CosmosConfig config, String dbName) throws ClassNotFoundException {
final CosmosFactory cosmosFactory = new CosmosFactory(client, dbName);
final CosmosMappingContext mappingContext = new CosmosMappingContext();
mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class));
final MappingCosmosConverter cosmosConverter = new MappingCosmosConverter(mappingContext, null);
return new CosmosTemplate(cosmosFactory, config, cosmosConverter);
}

private void insertPerson(Person person) {
cosmosTemplate.insert(person,
new PartitionKey(personInfo.getPartitionKeyFieldValue(person)));
Expand Down Expand Up @@ -660,34 +660,4 @@ public void createWithAutoscale() throws ClassNotFoundException {
assertEquals(Integer.parseInt(TestConstants.AUTOSCALE_MAX_THROUGHPUT),
throughput.getProperties().getAutoscaleMaxThroughput());
}

@Test
public void createDatabaseWithThroughput() throws ClassNotFoundException {
final String dbName = TestConstants.DB_NAME + "-other";
deleteDatabaseIfExists(dbName);

Integer expectedRequestUnits = 700;
final CosmosConfig config = CosmosConfig.builder()
.enableDatabaseThroughput(false, expectedRequestUnits)
.build();
cosmosTemplate = createCosmosTemplate(config, dbName);

final CosmosEntityInformation<Person, String> personInfo =
new CosmosEntityInformation<>(Person.class);
cosmosTemplate.createContainerIfNotExists(personInfo);

final CosmosAsyncDatabase database = client.getDatabase(dbName);
final ThroughputResponse response = database.readThroughput().block();
assertEquals(expectedRequestUnits, response.getProperties().getManualThroughput());
}

private void deleteDatabaseIfExists(String dbName) {
CosmosAsyncDatabase database = client.getDatabase(dbName);
try {
database.delete().block();
} catch (CosmosException ex) {
assertEquals(ex.getStatusCode(), 404);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.azure.core.credential.AzureKeyCredential;
import com.azure.cosmos.CosmosAsyncClient;
import com.azure.cosmos.CosmosAsyncDatabase;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.CosmosException;
import com.azure.cosmos.implementation.ConflictException;
Expand Down Expand Up @@ -120,9 +119,17 @@ public void setUp() throws ClassNotFoundException {
azureKeyCredential = new AzureKeyCredential(cosmosDbKey);
cosmosClientBuilder.credential(azureKeyCredential);
client = CosmosFactory.createCosmosAsyncClient(cosmosClientBuilder);
final CosmosFactory dbFactory = new CosmosFactory(client, TestConstants.DB_NAME);

final CosmosMappingContext mappingContext = new CosmosMappingContext();
personInfo = new CosmosEntityInformation<>(Person.class);
containerName = personInfo.getContainerName();
cosmosTemplate = createReactiveCosmosTemplate(cosmosConfig, TestConstants.DB_NAME);

mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class));

final MappingCosmosConverter dbConverter =
new MappingCosmosConverter(mappingContext, null);
cosmosTemplate = new ReactiveCosmosTemplate(dbFactory, cosmosConfig, dbConverter);
}

collectionManager.ensureContainersCreatedAndEmpty(cosmosTemplate, Person.class, GenIdEntity.class, AuditableEntity.class);
Expand All @@ -131,14 +138,6 @@ public void setUp() throws ClassNotFoundException {
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON))).block();
}

private ReactiveCosmosTemplate createReactiveCosmosTemplate(CosmosConfig config, String dbName) throws ClassNotFoundException {
final CosmosFactory cosmosFactory = new CosmosFactory(client, dbName);
final CosmosMappingContext mappingContext = new CosmosMappingContext();
mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class));
final MappingCosmosConverter cosmosConverter = new MappingCosmosConverter(mappingContext, null);
return new ReactiveCosmosTemplate(cosmosFactory, config, cosmosConverter);
}

@After
public void cleanup() {
// Reset master key
Expand Down Expand Up @@ -546,34 +545,4 @@ public void createWithAutoscale() {
assertEquals(Integer.parseInt(TestConstants.AUTOSCALE_MAX_THROUGHPUT),
throughput.getProperties().getAutoscaleMaxThroughput());
}

@Test
public void createDatabaseWithThroughput() throws ClassNotFoundException {
final String dbName = TestConstants.DB_NAME + "-other";
deleteDatabaseIfExists(dbName);

Integer expectedRequestUnits = 700;
final CosmosConfig config = CosmosConfig.builder()
.enableDatabaseThroughput(false, expectedRequestUnits)
.build();
cosmosTemplate = createReactiveCosmosTemplate(config, dbName);

final CosmosEntityInformation<Person, String> personInfo =
new CosmosEntityInformation<>(Person.class);
cosmosTemplate.createContainerIfNotExists(personInfo).block();

final CosmosAsyncDatabase database = client.getDatabase(dbName);
final ThroughputResponse response = database.readThroughput().block();
assertEquals(expectedRequestUnits, response.getProperties().getManualThroughput());
}

private void deleteDatabaseIfExists(String dbName) {
CosmosAsyncDatabase database = client.getDatabase(dbName);
try {
database.delete().block();
} catch (CosmosException ex) {
assertEquals(ex.getStatusCode(), 404);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.
package com.azure.spring.data.cosmos.config;

import com.azure.spring.data.cosmos.Constants;
import com.azure.spring.data.cosmos.core.ResponseDiagnosticsProcessor;

import java.beans.ConstructorProperties;
Expand All @@ -14,8 +13,6 @@ public class CosmosConfig {

private final ResponseDiagnosticsProcessor responseDiagnosticsProcessor;

private final DatabaseThroughputConfig databaseThroughputConfig;

private final boolean queryMetricsEnabled;

/**
Expand All @@ -27,22 +24,7 @@ public class CosmosConfig {
@ConstructorProperties({"responseDiagnosticsProcessor", "queryMetricsEnabled"})
public CosmosConfig(ResponseDiagnosticsProcessor responseDiagnosticsProcessor,
boolean queryMetricsEnabled) {
this(responseDiagnosticsProcessor, null, queryMetricsEnabled);
}

/**
* Initialization
*
* @param responseDiagnosticsProcessor must not be {@literal null}
* @param databaseThroughputConfig may be @{literal null}
* @param queryMetricsEnabled must not be {@literal null}
*/
@ConstructorProperties({"responseDiagnosticsProcessor", "databaseThroughputConfig", "queryMetricsEnabled"})
public CosmosConfig(ResponseDiagnosticsProcessor responseDiagnosticsProcessor,
DatabaseThroughputConfig databaseThroughputConfig,
boolean queryMetricsEnabled) {
this.responseDiagnosticsProcessor = responseDiagnosticsProcessor;
this.databaseThroughputConfig = databaseThroughputConfig;
this.queryMetricsEnabled = queryMetricsEnabled;
}

Expand All @@ -64,15 +46,6 @@ public boolean isQueryMetricsEnabled() {
return queryMetricsEnabled;
}

/**
* Gets the database throughput configuration.
*
* @return DatabaseThroughputConfig, or null if no database throughput is configured
*/
public DatabaseThroughputConfig getDatabaseThroughputConfig() {
return databaseThroughputConfig;
}

/**
* Create a CosmosConfigBuilder instance
*
Expand All @@ -87,7 +60,6 @@ public static CosmosConfigBuilder builder() {
*/
public static class CosmosConfigBuilder {
private ResponseDiagnosticsProcessor responseDiagnosticsProcessor;
private DatabaseThroughputConfig databaseThroughputConfig;
private boolean queryMetricsEnabled;
CosmosConfigBuilder() {
}
Expand Down Expand Up @@ -116,25 +88,19 @@ public CosmosConfigBuilder enableQueryMetrics(boolean queryMetricsEnabled) {
return this;
}

public CosmosConfigBuilder enableDatabaseThroughput(boolean autoscale, int requestUnits) {
this.databaseThroughputConfig = new DatabaseThroughputConfig(autoscale, requestUnits);
return this;
}

/**
* Build a CosmosConfig instance
*
* @return CosmosConfig
*/
public CosmosConfig build() {
return new CosmosConfig(this.responseDiagnosticsProcessor, this.databaseThroughputConfig, this.queryMetricsEnabled);
return new CosmosConfig(this.responseDiagnosticsProcessor, this.queryMetricsEnabled);
}

@Override
public String toString() {
return "CosmosConfigBuilder{"
+ "responseDiagnosticsProcessor=" + responseDiagnosticsProcessor
+ ", databaseThroughputConfig=" + databaseThroughputConfig
+ ", queryMetricsEnabled=" + queryMetricsEnabled
+ '}';
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.azure.cosmos.CosmosAsyncDatabase;
import com.azure.cosmos.models.CosmosContainerProperties;
import com.azure.cosmos.models.CosmosContainerResponse;
import com.azure.cosmos.models.CosmosDatabaseResponse;
import com.azure.cosmos.models.CosmosItemRequestOptions;
import com.azure.cosmos.models.CosmosItemResponse;
import com.azure.cosmos.models.CosmosQueryRequestOptions;
Expand All @@ -21,7 +20,6 @@
import com.azure.spring.data.cosmos.CosmosFactory;
import com.azure.spring.data.cosmos.common.CosmosUtils;
import com.azure.spring.data.cosmos.config.CosmosConfig;
import com.azure.spring.data.cosmos.config.DatabaseThroughputConfig;
import com.azure.spring.data.cosmos.core.convert.MappingCosmosConverter;
import com.azure.spring.data.cosmos.core.generator.CountQueryGenerator;
import com.azure.spring.data.cosmos.core.generator.FindQuerySpecGenerator;
Expand Down Expand Up @@ -77,7 +75,6 @@ public class CosmosTemplate implements CosmosOperations, ApplicationContextAware
private final ResponseDiagnosticsProcessor responseDiagnosticsProcessor;
private final boolean queryMetricsEnabled;
private final CosmosAsyncClient cosmosAsyncClient;
private final DatabaseThroughputConfig databaseThroughputConfig;

private ApplicationContext applicationContext;

Expand Down Expand Up @@ -129,7 +126,6 @@ public CosmosTemplate(CosmosFactory cosmosFactory,
this.databaseName = cosmosFactory.getDatabaseName();
this.responseDiagnosticsProcessor = cosmosConfig.getResponseDiagnosticsProcessor();
this.queryMetricsEnabled = cosmosConfig.isQueryMetricsEnabled();
this.databaseThroughputConfig = cosmosConfig.getDatabaseThroughputConfig();
}

/**
Expand Down Expand Up @@ -462,7 +458,8 @@ public String getContainerName(Class<?> domainType) {
@Override
public CosmosContainerProperties createContainerIfNotExists(CosmosEntityInformation<?, ?> information) {

final CosmosContainerResponse response = createDatabaseIfNotExists()
final CosmosContainerResponse response = cosmosAsyncClient
.createDatabaseIfNotExists(this.databaseName)
.publishOn(Schedulers.parallel())
.onErrorResume(throwable ->
CosmosExceptionUtils.exceptionHandler("Failed to create database", throwable))
Expand Down Expand Up @@ -504,19 +501,6 @@ public CosmosContainerProperties createContainerIfNotExists(CosmosEntityInformat
return response.getProperties();
}

private Mono<CosmosDatabaseResponse> createDatabaseIfNotExists() {
if (databaseThroughputConfig == null) {
return cosmosAsyncClient
.createDatabaseIfNotExists(this.databaseName);
} else {
ThroughputProperties throughputProperties = databaseThroughputConfig.isAutoScale()
? ThroughputProperties.createAutoscaledThroughput(databaseThroughputConfig.getRequestUnits())
: ThroughputProperties.createManualThroughput(databaseThroughputConfig.getRequestUnits());
return cosmosAsyncClient
.createDatabaseIfNotExists(this.databaseName, throughputProperties);
}
}

@Override
public CosmosContainerProperties getContainerProperties(String containerName) {
final CosmosContainerResponse response = cosmosAsyncClient.getDatabase(this.databaseName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.azure.cosmos.CosmosAsyncDatabase;
import com.azure.cosmos.models.CosmosContainerProperties;
import com.azure.cosmos.models.CosmosContainerResponse;
import com.azure.cosmos.models.CosmosDatabaseResponse;
import com.azure.cosmos.models.CosmosItemRequestOptions;
import com.azure.cosmos.models.CosmosQueryRequestOptions;
import com.azure.cosmos.models.FeedResponse;
Expand All @@ -19,7 +18,6 @@
import com.azure.spring.data.cosmos.CosmosFactory;
import com.azure.spring.data.cosmos.common.CosmosUtils;
import com.azure.spring.data.cosmos.config.CosmosConfig;
import com.azure.spring.data.cosmos.config.DatabaseThroughputConfig;
import com.azure.spring.data.cosmos.core.convert.MappingCosmosConverter;
import com.azure.spring.data.cosmos.core.generator.CountQueryGenerator;
import com.azure.spring.data.cosmos.core.generator.FindQuerySpecGenerator;
Expand Down Expand Up @@ -63,7 +61,6 @@ public class ReactiveCosmosTemplate implements ReactiveCosmosOperations, Applica
private final boolean queryMetricsEnabled;
private final CosmosAsyncClient cosmosAsyncClient;
private final IsNewAwareAuditingHandler cosmosAuditingHandler;
private final DatabaseThroughputConfig databaseThroughputConfig;

private ApplicationContext applicationContext;

Expand Down Expand Up @@ -117,7 +114,6 @@ public ReactiveCosmosTemplate(CosmosFactory cosmosFactory,
this.responseDiagnosticsProcessor = cosmosConfig.getResponseDiagnosticsProcessor();
this.queryMetricsEnabled = cosmosConfig.isQueryMetricsEnabled();
this.cosmosAuditingHandler = cosmosAuditingHandler;
this.databaseThroughputConfig = cosmosConfig.getDatabaseThroughputConfig();
}

/**
Expand Down Expand Up @@ -150,7 +146,8 @@ public void setApplicationContext(@NonNull ApplicationContext applicationContext
@Override
public Mono<CosmosContainerResponse> createContainerIfNotExists(CosmosEntityInformation<?, ?> information) {

return createDatabaseIfNotExists()
return cosmosAsyncClient
.createDatabaseIfNotExists(this.databaseName)
.publishOn(Schedulers.parallel())
.onErrorResume(throwable ->
CosmosExceptionUtils.exceptionHandler("Failed to create database", throwable))
Expand Down Expand Up @@ -191,19 +188,6 @@ public Mono<CosmosContainerResponse> createContainerIfNotExists(CosmosEntityInfo

}

private Mono<CosmosDatabaseResponse> createDatabaseIfNotExists() {
if (databaseThroughputConfig == null) {
return cosmosAsyncClient
.createDatabaseIfNotExists(this.databaseName);
} else {
ThroughputProperties throughputProperties = databaseThroughputConfig.isAutoScale()
? ThroughputProperties.createAutoscaledThroughput(databaseThroughputConfig.getRequestUnits())
: ThroughputProperties.createManualThroughput(databaseThroughputConfig.getRequestUnits());
return cosmosAsyncClient
.createDatabaseIfNotExists(this.databaseName, throughputProperties);
}
}

@Override
public Mono<CosmosContainerProperties> getContainerProperties(String containerName) {
return cosmosAsyncClient.getDatabase(this.databaseName)
Expand Down

0 comments on commit 2e51219

Please sign in to comment.