From c39ba74249738d84022ae8163caab5d5a2e9f0e8 Mon Sep 17 00:00:00 2001 From: "J.C. Jones" Date: Thu, 29 Feb 2024 12:54:50 -0700 Subject: [PATCH] Don't emit a Prometheus error for empty tables Fixes #79 Causes #81 --- partitionmanager/cli.py | 2 ++ partitionmanager/database_helpers_test.py | 11 +++++++++++ partitionmanager/table_append_partition.py | 2 +- partitionmanager/types.py | 4 ++++ pyproject.toml | 2 +- 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/partitionmanager/cli.py b/partitionmanager/cli.py index 723b650..c8f03e0 100644 --- a/partitionmanager/cli.py +++ b/partitionmanager/cli.py @@ -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) diff --git a/partitionmanager/database_helpers_test.py b/partitionmanager/database_helpers_test.py index 188752c..280ae17 100644 --- a/partitionmanager/database_helpers_test.py +++ b/partitionmanager/database_helpers_test.py @@ -9,6 +9,7 @@ SqlInput, SqlQuery, Table, + TableEmptyException, ) @@ -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}]) diff --git a/partitionmanager/table_append_partition.py b/partitionmanager/table_append_partition.py index 266bdd0..521b9f0 100644 --- a/partitionmanager/table_append_partition.py +++ b/partitionmanager/table_append_partition.py @@ -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] diff --git a/partitionmanager/types.py b/partitionmanager/types.py index cf1e3ca..d630cf7 100644 --- a/partitionmanager/types.py +++ b/partitionmanager/types.py @@ -596,6 +596,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.""" diff --git a/pyproject.toml b/pyproject.toml index ebb928b..29e78dd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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