Skip to content

Commit

Permalink
Replace CosmosDB with PostgreSQL in ACA Longhaul (#216)
Browse files Browse the repository at this point in the history
* Removed CosmoDB as dependency. Its transactional support isn't great and we were having issues with it:
   ```
    2024-01-10T23:08:04.892494542Z time="2024-01-10T23:08:04.892429945Z" level=error msg="Transaction failed due to operation 0 which failed with status code 400" app_id=hashtag-actor component="statestore (state.azure.cosmosdb/v1)" instance=hashtag-actor--vo723vq-56bf46647c-c7v55 scope=dapr.contrib type=log ver=1.11.6
    2024-01-10T23:08:04.892663298Z time="2024-01-10T23:08:04.892450433Z" level=debug msg="{ERR_ACTOR_STATE_TRANSACTION_SAVE error saving actor transaction state: transaction failed due to operation 0 which failed with status code 400}" app_id=hashtag-actor instance=hashtag-actor--vo723vq-56bf46647c-c7v55 scope=dapr.runtime.http type=log ver=1.11.6
   ```
* Simplified dependency lists
* Removed external network from applications -- there's no need to have them exposed to the public internet.
* Make resource-names more subscription-dependent, so we can run multiple instances of the same application in different resource groups in the same subscription.

Signed-off-by: Tiago Alves Macambira <[email protected]>
  • Loading branch information
tmacam authored Jan 12, 2024
1 parent 16a7158 commit 80ca515
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 36 deletions.
3 changes: 1 addition & 2 deletions deploy/aks/services/postgresql.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ param administratorLoginPassword string = 'pgpass-${uniqueString(resourceGroup()
@description('Azure database for PostgreSQL pricing tier')
@allowed([
'Burstable'
'Basic'
'GeneralPurpose'
'MemoryOptimized'
])
Expand Down Expand Up @@ -124,7 +123,7 @@ resource server 'Microsoft.DBforPostgreSQL/flexibleServers@2022-12-01' = {
// AAD support requires Dapr 1.12.0 or later
resource addAddUser 'Microsoft.DBforPostgreSQL/flexibleServers/administrators@2022-12-01' = {
parent: server
name: '${aadAdminObjectid}'
name: aadAdminObjectid
dependsOn: [
server
]
Expand Down
2 changes: 1 addition & 1 deletion deploy/containerapps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ az group create -n $RESOURCE_GROUP -l $LOCATION

### Deploy via Deployment Groups and Bicep
```bash
az deployment group create -g $RESOURCE_GROUP -f ./deploy/main.bicep
az deployment group create -g $RESOURCE_GROUP -f ./deploy/containerapps/main.bicep
```
2 changes: 1 addition & 1 deletion deploy/containerapps/apps/hashtag-actor.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ resource hashtagActor 'Microsoft.App/containerApps@2022-03-01' = {
}
configuration: {
ingress: {
external: true
external: false
targetPort: containerPort
}
dapr: {
Expand Down
2 changes: 1 addition & 1 deletion deploy/containerapps/apps/hashtag-counter.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ resource hashtagCounter 'Microsoft.App/containerApps@2022-03-01' = {
}
configuration: {
ingress: {
external: true
external: false
targetPort: containerPort
}
dapr: {
Expand Down
2 changes: 1 addition & 1 deletion deploy/containerapps/apps/message-analyzer.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ resource messageAnalyzer 'Microsoft.App/containerApps@2022-03-01' = {
}
configuration: {
ingress: {
external: true
external: false
targetPort: containerPort
}
dapr: {
Expand Down
2 changes: 1 addition & 1 deletion deploy/containerapps/apps/pubsub-workflow.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ resource pubsubWorkflow 'Microsoft.App/containerApps@2022-03-01' = {
}
configuration: {
ingress: {
external: true
external: false
targetPort: containerPort
}
dapr: {
Expand Down
2 changes: 1 addition & 1 deletion deploy/containerapps/apps/snapshot.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ resource snapshot 'Microsoft.App/containerApps@2022-03-01' = {
}
configuration: {
ingress: {
external: true
external: false
targetPort: containerPort
}
dapr: {
Expand Down
2 changes: 1 addition & 1 deletion deploy/containerapps/apps/validation-worker.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ resource validationWorkflow 'Microsoft.App/containerApps@2022-03-01' = {
}
configuration: {
ingress: {
external: true
external: false
targetPort: containerPort
}
dapr: {
Expand Down
2 changes: 1 addition & 1 deletion deploy/containerapps/azure/cosmos.bicep
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
param cosmosAccountName string = 'dapr-longhaul-cosmosdb'
param cosmosAccountName string = toLower('cosmosdb${uniqueString(resourceGroup().id)}')
param cosmosDatabaseName string = 'longhauldb'
param cosmosContainerName string = 'longhaulcontainer'
param location string
Expand Down
2 changes: 1 addition & 1 deletion deploy/containerapps/azure/environment.bicep
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
param environmentName string = 'dapr-longhaul-env'
param environmentName string = toLower('dapr-env-${uniqueString(resourceGroup().id)}')
param appInsightsName string = '${environmentName}-ai'
param logAnalyticsWorkspaceName string = '${environmentName}-la'
param location string
Expand Down
2 changes: 1 addition & 1 deletion deploy/containerapps/azure/servicebus.bicep
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
param serviceBusNamespace string = 'dapr-longhaul-capps-servicebus'
param serviceBusNamespace string = toLower('servicebus${uniqueString(resourceGroup().id)}')
param location string

resource servicebus 'Microsoft.ServiceBus/namespaces@2021-11-01' = {
Expand Down
2 changes: 1 addition & 1 deletion deploy/containerapps/azure/storage-services.bicep
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
param location string
param accountName string = 'testlonghaulstorage'
param accountName string = toLower('storage${uniqueString(resourceGroup().id)}')


// Storage account and associated services.
Expand Down
40 changes: 40 additions & 0 deletions deploy/containerapps/daprComponents/statestore-postgresql.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

param environmentName string

@secure()
param connectionString string

param componentName string = 'statestore'

resource environment 'Microsoft.App/managedEnvironments@2022-03-01' existing = {
name: environmentName
}

resource stateDaprComponent 'Microsoft.App/managedEnvironments/daprComponents@2022-03-01' = {
name: componentName
parent: environment
properties: {
componentType: 'state.postgresql'
version: 'v1'
secrets: [
{
name: 'secretconnectionstring'
value: connectionString
}
]
metadata: [
{
name: 'connectionString'
secretRef: 'secretconnectionstring'
}
{
name: 'actorStateStore'
value: 'true'
}
]
scopes: [
'hashtag-actor'
'snapshot'
]
}
}
2 changes: 1 addition & 1 deletion deploy/containerapps/daprComponents/statestore.bicep
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
param componentName string = 'statestore'
param componentName string = 'statestore-cosmos'
param environmentName string
param databaseName string
param collectionName string
Expand Down
47 changes: 25 additions & 22 deletions deploy/containerapps/main.bicep
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
// Global parameters
param location string = resourceGroup().location

@description('The unique discriminator of the solution. This is used to ensure that resource names are unique.')
@minLength(3)
@maxLength(16)
param solutionName string = uniqueString(resourceGroup().id)

param identityName string = '${solutionName}-identity'

// === Azure Setup ===

// Identity - Not a module so we can reference the resource below.
resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
name: identityName
location: location
}

// Container Apps Environment
module environment 'azure/environment.bicep' = {
name: '${deployment().name}--environment'
Expand All @@ -18,11 +32,15 @@ module storageServices 'azure/storage-services.bicep' = {
}
}

// CosmosDB
module cosmos 'azure/cosmos.bicep' = {
name: '${deployment().name}--cosmos'
// PostgreSQL
module postgresql '../aks/services/postgresql.bicep' = {
name: '${deployment().name}--postgresql'
params: {
solutionName: solutionName
location: location
allowAzureIPsFirewall: true
aadAdminName: managedIdentity.name
aadAdminObjectid: managedIdentity.properties.principalId
}
}

Expand All @@ -36,17 +54,15 @@ module servicebus 'azure/servicebus.bicep' = {

// === Component Setup ===
// Statestore (actors) component
module statestore 'daprComponents/statestore.bicep' = {
module statestore 'daprComponents/statestore-postgresql.bicep' = {
name: '${deployment().name}--statestore-component'
dependsOn: [
cosmos
postgresql
environment
]
params: {
environmentName: environment.outputs.environmentName
cosmosAccountName: cosmos.outputs.cosmosAccountName
databaseName: cosmos.outputs.cosmosDatabaseName
collectionName: cosmos.outputs.cosmosContainerName
connectionString: postgresql.outputs.connectionString
}
}

Expand Down Expand Up @@ -80,8 +96,6 @@ module binding 'daprComponents/storagebinding.bicep' = {
module pubsubWorkflow 'apps/pubsub-workflow.bicep' = {
name: '${deployment().name}--pubsub-workflow'
dependsOn: [
environment
servicebus
pubsub
]
params: {
Expand All @@ -93,8 +107,6 @@ module pubsubWorkflow 'apps/pubsub-workflow.bicep' = {
module feedGenerator 'apps/feed-generator.bicep' = {
name: '${deployment().name}--feed-generator'
dependsOn: [
environment
servicebus
pubsub
]
params: {
Expand All @@ -106,8 +118,6 @@ module feedGenerator 'apps/feed-generator.bicep' = {
module hashtagActor 'apps/hashtag-actor.bicep' = {
name: '${deployment().name}--hashtag-actor'
dependsOn: [
environment
cosmos
statestore
]
params: {
Expand All @@ -119,9 +129,8 @@ module hashtagActor 'apps/hashtag-actor.bicep' = {
module hashtagCounter 'apps/hashtag-counter.bicep' = {
name: '${deployment().name}--hashtag-counter'
dependsOn: [
environment
storageServices
binding
hashtagActor
]
params: {
environmentName: environment.outputs.environmentName
Expand All @@ -132,9 +141,6 @@ module hashtagCounter 'apps/hashtag-counter.bicep' = {
module messageAnalyzer 'apps/message-analyzer.bicep' = {
name: '${deployment().name}--message-analyzer'
dependsOn: [
environment
servicebus
storageServices
binding
pubsub
]
Expand All @@ -147,8 +153,6 @@ module messageAnalyzer 'apps/message-analyzer.bicep' = {
module snapshot 'apps/snapshot.bicep' = {
name: '${deployment().name}--snapshot'
dependsOn: [
environment
cosmos
statestore
]
params: {
Expand All @@ -160,7 +164,6 @@ module snapshot 'apps/snapshot.bicep' = {
module validationWorker 'apps/validation-worker.bicep' = {
name: '${deployment().name}--validation-worker'
dependsOn: [
environment
snapshot
]
params: {
Expand Down

0 comments on commit 80ca515

Please sign in to comment.