betr-io / terraform-provider-mssql

Terraform provider for Microsoft SQL Server
https://registry.terraform.io/providers/betr-io/mssql/latest
MIT License
35 stars 28 forks source link

Implement configurable db connection timeout #16

Closed ghost closed 2 years ago

ghost commented 2 years ago

It would be great if the provider configuration allowed for a configurable timeout for connecting to the DB. Currently using Azure SQL serverless which goes to sleep after 1 hour...it usually takes it 45-60 seconds to wake up and I get:

Error: unable to read user [xxx].[xxx]: db connection failed after 30s timeout with mssql_user.xxx, on xxx.database.tf line 54, in resource "mssql_user" "xxx": 54: resource "mssql_user" "xxx" {

magne commented 2 years ago

The provider should respect normal resource Operation Timeouts.

greggbjensen commented 2 years ago

@magne can you give some more details on how this should work? I am running into the same issue with timeouts at 30 seconds. We tried adding the Operation Timeout to both azurerm_mssql_server and azurerm_sql_database and it does not seem to change it. We are using a SQL login.

wtassi commented 2 years ago

It would be great if the provider configuration allowed for a configurable timeout for connecting to the DB. Currently using Azure SQL serverless which goes to sleep after 1 hour...it usually takes it 45-60 seconds to wake up and I get:

Error: unable to read user [xxx].[xxx]: db connection failed after 30s timeout with mssql_user.xxx, on xxx.database.tf line 54, in resource "mssql_user" "xxx": 54: resource "mssql_user" "xxx" {

This is because the resource tries to connect to the instance to do the validation, and when we created the azure_mssql_server we didn't insert our output ip in the Firewall.

If you add your outgoing ip on the sql server firewall, it will run successfully.

dhoss1979 commented 2 years ago

I am able to connect to the database locally and I am still getting this message:

Error: unable to read user [Communication].[Communication]: db connection failed after 30s timeout
│
│   with module.cluster.module._api_communication.mssql_user._api_db,
│   on ..\modules\cluster\modules\_api_with_db\main.tf line 67, in resource "mssql_user" "_api_db":
│   67: resource "mssql_user" "_api_db"
waltervos commented 2 years ago

For anyone unsure of how to use the "normal resource Operation Timeouts" that magne mentions, here's a snippet:

resource "mssql_user" "users" {
  server {
    host = azurerm_mssql_server.main.fully_qualified_domain_name
    azure_login {
      client_id     = null
      client_secret = null
      tenant_id     = null
    }
  }
  database = azurerm_mssql_database.my_database.name
  username = "..."
  roles    = ["db_owner"]
  timeouts {
    default = "5m"
  }
}

Note the timeouts block (which supports only the property default).

pisethdanh commented 2 years ago

For anyone unsure of how to use the "normal resource Operation Timeouts" that magne mentions, here's a snippet:

resource "mssql_user" "users" {
  server {
    host = azurerm_mssql_server.main.fully_qualified_domain_name
    azure_login {
      client_id     = null
      client_secret = null
      tenant_id     = null
    }
  }
  database = azurerm_mssql_database.my_database.name
  username = "..."
  roles    = ["db_owner"]
  timeouts {
    default = "5m"
  }
}

Note the timeouts block (which supports only the property default).

I have the following:

resource "mssql_login" "example" {

  # omit for brevity ...

  timeouts {
    default = "5m"
  }
}

and I'm still getting:

Error: unable to read login [login_name]: db connection failed after 30s timeout

@magne, is this the correct way to use the Resource Timeouts mentioned above? There doesn't seem to be any documentation for this timeouts property.

Note: I'm using the latest version (v0.2.5)

waltervos commented 2 years ago

@pisethdanh I think you'll find you'll keep getting that error message for 5 minutes. That's all that a resource operation timeout does.

pisethdanh commented 2 years ago

@waltervos , thank you for your response. The error message says it's timing out after 30 seconds, which doesn't respect the 5 minutes override that I specified. Unless I'm misconstruing how this works?

waltervos commented 2 years ago

@pisethdanh As far as I know this timeout value just refers to how long terraform will keep trying: https://www.terraform.io/language/resources/syntax#operation-timeouts

magne commented 2 years ago

@pisethdanh, could you try change the timeouts block to

timeouts {
    read = "5m"
}

If this works, I've misunderstood the terraform internals, assuming that the struct would automatically return the default if no specific timeout was set.

pisethdanh commented 2 years ago

@magne , thank you for your response. When setting create, read, update, or delete, I get this error:

image

It appears those attributes are not supported.

Speculation here, but Azure SQL MI may be throttling the read requests resulting in transient errors. Are there any plans to implement retry logic in this provider?

Apologies for resurrecting a closed issue. Is it more appropriate to create a new issue?

nazirakz commented 1 year ago

It would be great if the provider configuration allowed for a configurable timeout for connecting to the DB. Currently using Azure SQL serverless which goes to sleep after 1 hour...it usually takes it 45-60 seconds to wake up and I get:

Error: unable to read user [xxx].[xxx]: db connection failed after 30s timeout with mssql_user.xxx, on xxx.database.tf line 54, in resource "mssql_user" "xxx": 54: resource "mssql_user" "xxx" {

Hello! I also faced this error. How did you resolve it?

ghost commented 1 year ago

@nazirakz , no real resolution...I just re-run terraform and by the second time the DB is already awake