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

Shard raw_alerts table into two #78

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 5 additions & 1 deletion procedures_js/alert_queries_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ function exec(sqlText, binds = []) {
return retval
}

function randInt(n) {
return Math.floor(Math.random()*n)
}

function fillArray(value, len) {
const arr = []
for (var i = 0; i < len; i++) {
Expand All @@ -39,7 +43,7 @@ function defaultNullReference(columnAndType) {
}

const RUN_ID = Math.random().toString(36).substring(2).toUpperCase()
const RAW_ALERTS_TABLE = `${results_raw_alerts_table}`
const RAW_ALERTS_TABLE = `${results_raw_alerts_table}_${randInt(${alert_shards_count})}`

const CREATE_ALERTS_SQL = `INSERT INTO $${RAW_ALERTS_TABLE} (
run_id,
Expand Down
6 changes: 3 additions & 3 deletions tables.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
resource "snowflake_table" "raw_alerts" {
count = var.create_tables ? 1 : 0
resource "snowflake_table" "raw_alerts_tables" {
count = var.create_tables ? var.alert_shards_count : 0
provider = snowflake.alerting_role

database = local.snowalert_database_name
schema = local.results_schema
name = "RAW_ALERTS"
name = "RAW_ALERTS_${count.index}"
change_tracking = true

column {
Expand Down
5 changes: 5 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ variable "enable_multiple_grants" {
default = true
}

variable "alert_shards_count" {
type = number
default = 2
}

data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
data "aws_partition" "current" {}
Expand Down
20 changes: 20 additions & 0 deletions views.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ resource "snowflake_view" "alerts" {
]
}

resource "snowflake_view" "raw_alerts" {
provider = snowflake.alerting_role

database = local.snowalert_database_name
schema = local.results_schema
name = "RAW_ALERTS"
comment = "joins raw alert shards"

statement = join(
" UNION ALL ",
[for table in [
for i in range(var.alert_shards_count) : "raw_alerts_${i}"] :
"TABLE ${table}"]
)

depends_on = [
local.raw_alerts_tables
]
}

resource "snowflake_view" "violations" {
provider = snowflake.alerting_role

Expand Down