Skip to content

Commit

Permalink
Don't emit a Prometheus error for empty tables (#80)
Browse files Browse the repository at this point in the history
Fixes #79

Causes #81
  • Loading branch information
jcjones authored Feb 29, 2024
1 parent 552118a commit 0f319f8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions partitionmanager/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ def do_partition(conf):
except partitionmanager.types.DatabaseCommandException as e:
log.warning("Failed to automatically handle %s: %s", table, e)
metrics.add("alter_errors", table.name, 1)
except partitionmanager.types.TableEmptyException:
log.warning("Table %s appears to be empty. Skipping.", table)
except (ValueError, Exception) as e:
log.warning("Failed to handle %s: %s", table, e)
metrics.add("alter_errors", table.name, 1)
Expand Down
11 changes: 11 additions & 0 deletions partitionmanager/database_helpers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
SqlInput,
SqlQuery,
Table,
TableEmptyException,
)


Expand Down Expand Up @@ -46,6 +47,16 @@ def test_position_of_table(self):
pos = get_position_of_table(db, table, data)
self.assertEqual(pos.as_list(), [90210])

def test_empty_table(self):
db = MockDatabase()
db.add_response("SELECT id FROM `burgers` ORDER BY", [])

table = Table("burgers")
data = {"range_cols": ["id"]}

with self.assertRaises(TableEmptyException):
get_position_of_table(db, table, data)

def test_exact_timestamp_no_query(self):
db = MockDatabase()
db.add_response("SELECT id FROM `burgers` ORDER BY", [{"id": 42}])
Expand Down
2 changes: 1 addition & 1 deletion partitionmanager/table_append_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_current_positions(database, table, columns):
f"Expected one result from {table.name}"
)
if not rows:
raise partitionmanager.types.TableInformationException(
raise partitionmanager.types.TableEmptyException(
f"Table {table.name} appears to be empty. (No results)"
)
positions[column] = rows[0][column]
Expand Down
4 changes: 4 additions & 0 deletions partitionmanager/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,10 @@ class TableInformationException(Exception):
"""Raised when the table's status doesn't include the information we need."""


class TableEmptyException(Exception):
"""Raised when the table is empty."""


class NoEmptyPartitionsAvailableException(Exception):
"""Raised if no empty partitions are available to safely modify."""

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@ max-complexity = 16 # default is 10
[tool.ruff.lint.pylint]
max-args = 7 # default is 5
max-branches = 15 # default is 12
max-statements = 52 # default is 50
max-statements = 54 # default is 50

0 comments on commit 0f319f8

Please sign in to comment.