Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi-tenant sample enhancements #716

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@
public class AppConfiguration extends AbstractCosmosConfiguration {
private static final Logger logger = LoggerFactory.getLogger(AppConfiguration.class);
private final CosmosProperties properties;
private final ApplicationContext applicationContext;
private final Environment env;

public AppConfiguration(CosmosProperties properties, Environment env, ApplicationContext applicationContext ){
public AppConfiguration(CosmosProperties properties, Environment env){
this.env = env;
this.properties = properties;
this.applicationContext = applicationContext;
}
private CosmosAsyncClient client;

@Bean
public CosmosClientBuilder cosmosClientBuilder() {
Expand Down Expand Up @@ -65,9 +62,6 @@ public MultiTenantContainerCosmosFactory cosmosFactory(CosmosAsyncClient cosmosA
protected String getDatabaseName() {
String databaseName;
databaseName = properties.getDatabaseName();
client = applicationContext.getBean(CosmosAsyncClient.class);
client.createDatabaseIfNotExists(databaseName, ThroughputProperties.createAutoscaledThroughput(4000));
logger.info("config databaseName result: "+databaseName);
return databaseName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
@Container(autoCreateContainer = false)
public class Order {
@Id
@PartitionKey
private String id;
private String orderDetail;
@PartitionKey
private String lastName;
private String type;
public Order() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public String overrideContainerName() {
String tenantId = TenantStorage.getCurrentTenant();
if (tenantId !=null){
this.tenantId = tenantId;
//first check if the tenant exists in a thread-safe list of tenant ids
//if it exists, no further action taken.
//If not, create the tenant container on the fly, using the default database as a model
tenantStorage.createTenantSpecificContainerIfNotExists(tenantId, properties.getPartitionKeyPath());
return tenantId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ spring:
uri: ${ACCOUNT_HOST}
key: ${ACCOUNT_KEY}
databaseName: tenants
partitionKeyPath: /lastName
partitionKeyPath: /id
queryMetricsEnabled: true
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ public class AppConfiguration extends AbstractCosmosConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(AppConfiguration.class);
private final CosmosProperties properties;
private final Environment env;
private final ApplicationContext applicationContext;

public AppConfiguration(CosmosProperties properties, Environment env, ApplicationContext applicationContext ){
public AppConfiguration(CosmosProperties properties, Environment env){
this.env = env;
this.properties = properties;
this.applicationContext = applicationContext;
}
private CosmosAsyncClient client;

@Bean
public CosmosClientBuilder cosmosClientBuilder() {
Expand Down Expand Up @@ -65,9 +62,6 @@ public CosmosConfig cosmosConfig() {
protected String getDatabaseName() {
String databaseName;
databaseName = properties.getDatabaseName();
client = applicationContext.getBean(CosmosAsyncClient.class);
client.createDatabaseIfNotExists(databaseName, ThroughputProperties.createAutoscaledThroughput(4000));
LOGGER.info("config databaseName result: "+databaseName);
return databaseName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
@Container(autoCreateContainer = true)
public class Order {
@Id
@PartitionKey
private String id;
private String orderDetail;
@PartitionKey
private String lastName;
public Order() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public OrderController(OrderRepository orderRepository) {
}

@PostMapping
public @ResponseBody String createUser(@RequestBody Order order) {
public @ResponseBody String createOrder(@RequestBody Order order) {
UUID uuid = UUID.randomUUID();
order.setId(String.valueOf(uuid));
orderRepository.save(order);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
@Container(autoCreateContainer = true)
public class User {
@Id
@PartitionKey
private String id;
private String firstName;
@PartitionKey
private String lastName;
public User() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@ public MultiTenantDBCosmosFactory(CosmosAsyncClient cosmosAsyncClient, String da
public String getDatabaseName() {
String tenantId = TenantStorage.getCurrentTenant();
if (tenantId !=null && !tenantId.equals(env.getProperty("spring.data.cosmos.databaseName"))){
//the getTenant method will first check if the tenant exists in a thread-safe list of tenant ids
//if it exists, it returns the id, and no further action taken.
//If not, it will create the tenant database resources on the fly, using the default database as a model
this.tenantId = tenantId;
//first check if the tenant exists in a thread-safe list of tenant ids
//if it exists, no further action taken.
//If not, create the tenant database resources on the fly, using the default database as a model
tenantStorage.createTenantSpecificDatabaseIfNotExists(tenantId);
return tenantId;
}
else {
this.client.createDatabaseIfNotExists(env.getProperty("spring.data.cosmos.databaseName"), ThroughputProperties.createAutoscaledThroughput(4000));
return this.tenantId;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ spring:
cosmos:
uri: ${ACCOUNT_HOST}
key: ${ACCOUNT_KEY}
databaseName: default
databaseName: TenantTemplateDB
queryMetricsEnabled: true
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@
public class AppConfiguration extends AbstractCosmosConfiguration {
private static final Logger logger = LoggerFactory.getLogger(AppConfiguration.class);
private final CosmosProperties properties;
private final ApplicationContext applicationContext;
private final Environment env;

public AppConfiguration(CosmosProperties properties, Environment env, ApplicationContext applicationContext ){
public AppConfiguration(CosmosProperties properties, Environment env){
this.env = env;
this.properties = properties;
this.applicationContext = applicationContext;
}
private CosmosAsyncClient client;

@Bean
public CosmosClientBuilder cosmosClientBuilder() {
Expand Down Expand Up @@ -65,9 +62,6 @@ public MultiTenantContainerCosmosFactory cosmosFactory(CosmosAsyncClient cosmosA
protected String getDatabaseName() {
String databaseName;
databaseName = properties.getDatabaseName();
client = applicationContext.getBean(CosmosAsyncClient.class);
client.createDatabaseIfNotExists(databaseName, ThroughputProperties.createAutoscaledThroughput(4000));
logger.info("config databaseName result: "+databaseName);
return databaseName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
@Container(autoCreateContainer = false)
public class Order {
@Id
@PartitionKey
private String id;
private String orderDetail;
@PartitionKey
private String lastName;
private String type;
public Order() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
@Container(autoCreateContainer = false)
public class User {
@Id
@PartitionKey
private String id;
private String firstName;
@PartitionKey
private String lastName;
private String type;
public User() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public String overrideContainerName() {
String tenantId = TenantStorage.getCurrentTenant();
if (tenantId !=null){
this.tenantId = tenantId;
//first check if the tenant exists in a thread-safe list of tenant ids
//if it exists, no further action taken.
//If not, create the tenant container on the fly, using the default database as a model
tenantStorage.createTenantSpecificContainerIfNotExists(tenantId, properties.getPartitionKeyPath());
return tenantId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ spring:
uri: ${ACCOUNT_HOST}
key: ${ACCOUNT_KEY}
databaseName: tenants
partitionKeyPath: /lastName
partitionKeyPath: /id
queryMetricsEnabled: true
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ public class AppConfiguration extends AbstractCosmosConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(AppConfiguration.class);
private final CosmosProperties properties;
private final Environment env;
private final ApplicationContext applicationContext;

public AppConfiguration(CosmosProperties properties, Environment env, ApplicationContext applicationContext ){
public AppConfiguration(CosmosProperties properties, Environment env){
this.env = env;
this.properties = properties;
this.applicationContext = applicationContext;
}
private CosmosAsyncClient client;

@Bean
public CosmosClientBuilder cosmosClientBuilder() {
Expand Down Expand Up @@ -65,9 +62,6 @@ public CosmosConfig cosmosConfig() {
protected String getDatabaseName() {
String databaseName;
databaseName = properties.getDatabaseName();
client = applicationContext.getBean(CosmosAsyncClient.class);
client.createDatabaseIfNotExists(databaseName, ThroughputProperties.createAutoscaledThroughput(4000));
LOGGER.info("config databaseName result: "+databaseName);
return databaseName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
@Container(autoCreateContainer = true)
public class Order {
@Id
@PartitionKey
private String id;
private String orderDetail;
@PartitionKey
private String lastName;
public Order() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public OrderController(OrderRepository orderRepository) {
}

@PostMapping
public @ResponseBody String createUser(@RequestBody Order order) {
public @ResponseBody String createOrder(@RequestBody Order order) {
UUID uuid = UUID.randomUUID();
order.setId(String.valueOf(uuid));
orderRepository.save(order);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
@Container(autoCreateContainer = true)
public class User {
@Id
@PartitionKey
private String id;
private String firstName;
@PartitionKey
private String lastName;
public User() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@ public MultiTenantDBCosmosFactory(CosmosAsyncClient cosmosAsyncClient, String da
public String getDatabaseName() {
String tenantId = TenantStorage.getCurrentTenant();
if (tenantId !=null && !tenantId.equals(env.getProperty("spring.data.cosmos.databaseName"))){
//the getTenant method will first check if the tenant exists in a thread-safe list of tenant ids
//if it exists, it returns the id, and no further action taken.
//If not, it will create the tenant database resources on the fly, using the default database as a model
this.tenantId = tenantId;
//first check if the tenant exists in a thread-safe list of tenant ids
//if it exists, no further action taken.
//If not, create the tenant database resources on the fly, using the default database as a model
tenantStorage.createTenantSpecificDatabaseIfNotExists(tenantId);
return tenantId;
}
else {
this.client.createDatabaseIfNotExists(env.getProperty("spring.data.cosmos.databaseName"), ThroughputProperties.createAutoscaledThroughput(4000));
return this.tenantId;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ spring:
cosmos:
uri: ${ACCOUNT_HOST}
key: ${ACCOUNT_KEY}
databaseName: default
databaseName: TenantTemplateDB
queryMetricsEnabled: true