Skip to content

Commit a9ed268

Browse files
committed
fix: count buckets built while finishing a half-built branch
A run that finds a branch an earlier run left detached completes its bucket set and attaches it. Those buckets were reported nowhere. `created_count` is right to stay 0 -- the partition already existed -- but the work happens under CREATE, before the reconcile stage that would otherwise count it, and by the time reconcile runs the branch is complete, so it counts 0 too. A run that built four buckets and attached a branch could report `created_count=0, repaired_count=0`. They belong in `repaired_count`, which is documented as subpartitions created inside pre-existing branches to close gaps in their child sets -- exactly this. The same call was dropping `findings` outright. A branch whose shape the planner refuses is reported by the later reconcile only if reconcile still sees the problem; a branch completed here looks fine by then, so the finding was lost. Both now travel out through a collector the lifecycle service passes down, the same shape `materialize` already uses.
1 parent 105a518 commit a9ed268

6 files changed

Lines changed: 246 additions & 22 deletions

File tree

‎pg_partsmith/aio/service.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,14 @@ def _record_issue(step: MaintenanceIssueStep, exc: Exception) -> None:
371371
# Optimization: fetch all partitions once
372372
all_partitions = await self._metadata.list_partitions(qualified_parent)
373373

374+
# Finishing a branch an earlier run left half-built happens during
375+
# CREATE, before the reconcile stage that would otherwise count it.
376+
converged: list[SubpartitionReconcileResult] = []
377+
374378
if not skip_create:
375379
try:
376380
created = await self._require_periods().create_future_partitions(
377-
config, existing_partitions=all_partitions
381+
config, existing_partitions=all_partitions, converged=converged
378382
)
379383
except (asyncio.CancelledError, KeyboardInterrupt, SystemExit):
380384
raise
@@ -387,6 +391,16 @@ def _record_issue(step: MaintenanceIssueStep, exc: Exception) -> None:
387391
if created:
388392
all_partitions.extend(created)
389393

394+
# Buckets built while completing a half-built branch are repairs of
395+
# a pre-existing branch, which is exactly what repaired_count means.
396+
repaired_count += sum(result.created_count for result in converged)
397+
issues.extend(
398+
to_maintenance_issue(finding)
399+
for result in converged
400+
for finding in result.findings
401+
if finding.is_actionable
402+
)
403+
390404
try:
391405
partitions_to_prune = await self._require_pruning().identify_partitions_to_prune(config, all_partitions)
392406
except (asyncio.CancelledError, KeyboardInterrupt, SystemExit):
@@ -415,7 +429,7 @@ def _record_issue(step: MaintenanceIssueStep, exc: Exception) -> None:
415429
raise
416430
_record_issue(MaintenanceIssueStep.RECONCILE, e)
417431
else:
418-
repaired_count = reconciled.created_count
432+
repaired_count += reconciled.created_count
419433
issues.extend(to_maintenance_issue(f) for f in reconciled.findings if f.is_actionable)
420434

421435
if not partitions_to_prune:

‎pg_partsmith/aio/services/creation.py‎

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from pg_partsmith.aio.services.subpartitions import PartitionSubpartitionService
3232
from pg_partsmith.entities import TablePartitionConfig
3333
from pg_partsmith.protocols import PeriodCalculator
34+
from pg_partsmith.subpartition_plan import SubpartitionReconcileResult
3435

3536
logger = logging.getLogger(__name__)
3637

@@ -57,9 +58,21 @@ async def create_future_partitions(
5758
config: TablePartitionConfig,
5859
*,
5960
existing_partitions: list[PartitionInfo] | None = None,
61+
converged: list[SubpartitionReconcileResult] | None = None,
6062
) -> list[PartitionInfo]:
6163
"""Create partitions for future periods.
6264
65+
Args:
66+
config: Table partitioning configuration.
67+
existing_partitions: Partitions already read from the catalog, to
68+
save a second listing.
69+
converged: Collector for subtrees completed while finishing a branch
70+
an earlier run left half-built. That work is real -- buckets get
71+
created and attached -- but it happens under CREATE, where the
72+
reconcile stage that normally counts it has not run yet. Pass a
73+
list and the counts and findings survive; omit it and they are
74+
only logged.
75+
6376
Returns:
6477
List of newly created and attached partitions.
6578
@@ -76,7 +89,7 @@ async def create_future_partitions(
7689
existing_by_period = self._map_partitions_to_periods(existing_partitions)
7790

7891
for period in periods:
79-
p_info = await self._ensure_partition_for_period(config, period, existing_by_period.get(period))
92+
p_info = await self._ensure_partition_for_period(config, period, existing_by_period.get(period), converged)
8093
if p_info:
8194
created.append(p_info)
8295

@@ -217,12 +230,13 @@ async def _ensure_partition_for_period(
217230
config: TablePartitionConfig,
218231
period: Period,
219232
existing: PartitionInfo | None,
233+
converged: list[SubpartitionReconcileResult] | None = None,
220234
) -> PartitionInfo | None:
221235
"""Ensure a partition exists and is attached for a specific period."""
222236
partition_name, from_value, to_value = self._get_partition_metadata(config, period)
223237

224238
if existing is not None:
225-
await self._handle_existing_partition(config, existing, from_value, to_value)
239+
await self._handle_existing_partition(config, existing, from_value, to_value, converged)
226240
return None
227241

228242
# Hooks: before create
@@ -233,7 +247,9 @@ async def _ensure_partition_for_period(
233247
)
234248

235249
# Creation and optional attachment
236-
partition_info = await self._create_and_attach_partition(config, partition_name, from_value, to_value)
250+
partition_info = await self._create_and_attach_partition(
251+
config, partition_name, from_value, to_value, converged
252+
)
237253

238254
if partition_info:
239255
# Hooks: after create
@@ -468,7 +484,12 @@ async def _restore_reconciled_rows(
468484
)
469485

470486
async def _handle_existing_partition(
471-
self, config: TablePartitionConfig, existing: PartitionInfo, from_value: str, to_value: str
487+
self,
488+
config: TablePartitionConfig,
489+
existing: PartitionInfo,
490+
from_value: str,
491+
to_value: str,
492+
converged: list[SubpartitionReconcileResult] | None = None,
472493
) -> None:
473494
"""Handle case where partition already exists in metadata."""
474495
if not (config.auto_attach_after_create and not existing.is_attached):
@@ -479,7 +500,7 @@ async def _handle_existing_partition(
479500
# be short of what the spec asks for -- and if the process stops in
480501
# that window, it stays live and rejecting. A detached branch is
481502
# introspectable on its own, so there is no reason to attach first.
482-
await self._converge_existing_branch(config, existing.name)
503+
await self._converge_existing_branch(config, existing.name, converged)
483504
await self._attach_tolerating_lost_race(config, existing.name, from_value, to_value)
484505

485506
async def _is_attach_conflict_benign(
@@ -497,7 +518,12 @@ async def _is_attach_conflict_benign(
497518
return await self._metadata.is_partition_attached(qualified_parent, partition_name)
498519

499520
async def _create_and_attach_partition(
500-
self, config: TablePartitionConfig, partition_name: str, from_value: str, to_value: str
521+
self,
522+
config: TablePartitionConfig,
523+
partition_name: str,
524+
from_value: str,
525+
to_value: str,
526+
converged: list[SubpartitionReconcileResult] | None = None,
501527
) -> PartitionInfo | None:
502528
"""Create the partition (or the whole branch) and attach it to the parent.
503529
@@ -512,7 +538,7 @@ async def _create_and_attach_partition(
512538
# is not attached: almost always a previous run interrupted between
513539
# creating a branch and attaching it. Finish that work rather than
514540
# leaving an invisible table behind forever.
515-
await self._converge_existing_branch(config, partition_name)
541+
await self._converge_existing_branch(config, partition_name, converged)
516542
if config.auto_attach_after_create:
517543
await self._attach_tolerating_lost_race(config, partition_name, from_value, to_value)
518544
return None
@@ -557,11 +583,24 @@ def _require_capabilities(self, config: TablePartitionConfig) -> None:
557583
f"Repository {type(self._repo).__name__}", "composite partition keys", "CompositeKeyRepository"
558584
)
559585

560-
async def _converge_existing_branch(self, config: TablePartitionConfig, partition_name: str) -> None:
561-
"""Complete the subtree of a branch left half-built by an earlier run."""
586+
async def _converge_existing_branch(
587+
self,
588+
config: TablePartitionConfig,
589+
partition_name: str,
590+
converged: list[SubpartitionReconcileResult] | None = None,
591+
) -> None:
592+
"""Complete the subtree of a branch left half-built by an earlier run.
593+
594+
The result goes into ``converged`` when the caller is collecting. It has
595+
to: the reconcile stage runs after this and finds the branch already
596+
complete, so buckets created here are counted nowhere and the findings
597+
that came with them are reported nowhere.
598+
"""
562599
if config.subpartition is None:
563600
return
564601
result = await self._subpartition_service().converge_branch(config, partition_name)
602+
if converged is not None:
603+
converged.append(result)
565604
if result.created_count:
566605
logger.info(
567606
"Completed the subtree of a partition left behind by an earlier run",

‎pg_partsmith/sync/service.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,14 @@ def _record_issue(step: MaintenanceIssueStep, exc: Exception) -> None:
368368
# Optimization: fetch all partitions once
369369
all_partitions = self._metadata.list_partitions(qualified_parent)
370370

371+
# Finishing a branch an earlier run left half-built happens during
372+
# CREATE, before the reconcile stage that would otherwise count it.
373+
converged: list[SubpartitionReconcileResult] = []
374+
371375
if not skip_create:
372376
try:
373377
created = self._require_periods().create_future_partitions(
374-
config, existing_partitions=all_partitions
378+
config, existing_partitions=all_partitions, converged=converged
375379
)
376380
except (KeyboardInterrupt, SystemExit):
377381
raise
@@ -384,6 +388,16 @@ def _record_issue(step: MaintenanceIssueStep, exc: Exception) -> None:
384388
if created:
385389
all_partitions.extend(created)
386390

391+
# Buckets built while completing a half-built branch are repairs of
392+
# a pre-existing branch, which is exactly what repaired_count means.
393+
repaired_count += sum(result.created_count for result in converged)
394+
issues.extend(
395+
to_maintenance_issue(finding)
396+
for result in converged
397+
for finding in result.findings
398+
if finding.is_actionable
399+
)
400+
387401
try:
388402
partitions_to_prune = self._require_pruning().identify_partitions_to_prune(config, all_partitions)
389403
except (KeyboardInterrupt, SystemExit):
@@ -412,7 +426,7 @@ def _record_issue(step: MaintenanceIssueStep, exc: Exception) -> None:
412426
raise
413427
_record_issue(MaintenanceIssueStep.RECONCILE, e)
414428
else:
415-
repaired_count = reconciled.created_count
429+
repaired_count += reconciled.created_count
416430
issues.extend(to_maintenance_issue(f) for f in reconciled.findings if f.is_actionable)
417431

418432
if not partitions_to_prune:

‎pg_partsmith/sync/services/creation.py‎

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
from pg_partsmith.entities import TablePartitionConfig
2929
from pg_partsmith.protocols import PeriodCalculator
30+
from pg_partsmith.subpartition_plan import SubpartitionReconcileResult
3031
from pg_partsmith.sync.hooks import PartitionLifecycleHooks
3132
from pg_partsmith.sync.protocols import PartitionMetadataProvider, PartitionRepository
3233
from pg_partsmith.sync.services.subpartitions import PartitionSubpartitionService
@@ -56,9 +57,21 @@ def create_future_partitions(
5657
config: TablePartitionConfig,
5758
*,
5859
existing_partitions: list[PartitionInfo] | None = None,
60+
converged: list[SubpartitionReconcileResult] | None = None,
5961
) -> list[PartitionInfo]:
6062
"""Create partitions for future periods.
6163
64+
Args:
65+
config: Table partitioning configuration.
66+
existing_partitions: Partitions already read from the catalog, to
67+
save a second listing.
68+
converged: Collector for subtrees completed while finishing a branch
69+
an earlier run left half-built. That work is real -- buckets get
70+
created and attached -- but it happens under CREATE, where the
71+
reconcile stage that normally counts it has not run yet. Pass a
72+
list and the counts and findings survive; omit it and they are
73+
only logged.
74+
6275
Returns:
6376
List of newly created and attached partitions.
6477
@@ -75,7 +88,7 @@ def create_future_partitions(
7588
existing_by_period = self._map_partitions_to_periods(existing_partitions)
7689

7790
for period in periods:
78-
p_info = self._ensure_partition_for_period(config, period, existing_by_period.get(period))
91+
p_info = self._ensure_partition_for_period(config, period, existing_by_period.get(period), converged)
7992
if p_info:
8093
created.append(p_info)
8194

@@ -216,12 +229,13 @@ def _ensure_partition_for_period(
216229
config: TablePartitionConfig,
217230
period: Period,
218231
existing: PartitionInfo | None,
232+
converged: list[SubpartitionReconcileResult] | None = None,
219233
) -> PartitionInfo | None:
220234
"""Ensure a partition exists and is attached for a specific period."""
221235
partition_name, from_value, to_value = self._get_partition_metadata(config, period)
222236

223237
if existing is not None:
224-
self._handle_existing_partition(config, existing, from_value, to_value)
238+
self._handle_existing_partition(config, existing, from_value, to_value, converged)
225239
return None
226240

227241
# Hooks: before create
@@ -232,7 +246,7 @@ def _ensure_partition_for_period(
232246
)
233247

234248
# Creation and optional attachment
235-
partition_info = self._create_and_attach_partition(config, partition_name, from_value, to_value)
249+
partition_info = self._create_and_attach_partition(config, partition_name, from_value, to_value, converged)
236250

237251
if partition_info:
238252
# Hooks: after create
@@ -461,7 +475,12 @@ def _restore_reconciled_rows(
461475
)
462476

463477
def _handle_existing_partition(
464-
self, config: TablePartitionConfig, existing: PartitionInfo, from_value: str, to_value: str
478+
self,
479+
config: TablePartitionConfig,
480+
existing: PartitionInfo,
481+
from_value: str,
482+
to_value: str,
483+
converged: list[SubpartitionReconcileResult] | None = None,
465484
) -> None:
466485
"""Handle case where partition already exists in metadata."""
467486
if not (config.auto_attach_after_create and not existing.is_attached):
@@ -472,7 +491,7 @@ def _handle_existing_partition(
472491
# be short of what the spec asks for -- and if the process stops in
473492
# that window, it stays live and rejecting. A detached branch is
474493
# introspectable on its own, so there is no reason to attach first.
475-
self._converge_existing_branch(config, existing.name)
494+
self._converge_existing_branch(config, existing.name, converged)
476495
self._attach_tolerating_lost_race(config, existing.name, from_value, to_value)
477496

478497
def _is_attach_conflict_benign(self, qualified_parent: str, partition_name: str, exc: SQLAlchemyError) -> bool:
@@ -488,7 +507,12 @@ def _is_attach_conflict_benign(self, qualified_parent: str, partition_name: str,
488507
return self._metadata.is_partition_attached(qualified_parent, partition_name)
489508

490509
def _create_and_attach_partition(
491-
self, config: TablePartitionConfig, partition_name: str, from_value: str, to_value: str
510+
self,
511+
config: TablePartitionConfig,
512+
partition_name: str,
513+
from_value: str,
514+
to_value: str,
515+
converged: list[SubpartitionReconcileResult] | None = None,
492516
) -> PartitionInfo | None:
493517
"""Create the partition (or the whole branch) and attach it to the parent.
494518
@@ -503,7 +527,7 @@ def _create_and_attach_partition(
503527
# is not attached: almost always a previous run interrupted between
504528
# creating a branch and attaching it. Finish that work rather than
505529
# leaving an invisible table behind forever.
506-
self._converge_existing_branch(config, partition_name)
530+
self._converge_existing_branch(config, partition_name, converged)
507531
if config.auto_attach_after_create:
508532
self._attach_tolerating_lost_race(config, partition_name, from_value, to_value)
509533
return None
@@ -548,11 +572,24 @@ def _require_capabilities(self, config: TablePartitionConfig) -> None:
548572
f"Repository {type(self._repo).__name__}", "composite partition keys", "CompositeKeyRepository"
549573
)
550574

551-
def _converge_existing_branch(self, config: TablePartitionConfig, partition_name: str) -> None:
552-
"""Complete the subtree of a branch left half-built by an earlier run."""
575+
def _converge_existing_branch(
576+
self,
577+
config: TablePartitionConfig,
578+
partition_name: str,
579+
converged: list[SubpartitionReconcileResult] | None = None,
580+
) -> None:
581+
"""Complete the subtree of a branch left half-built by an earlier run.
582+
583+
The result goes into ``converged`` when the caller is collecting. It has
584+
to: the reconcile stage runs after this and finds the branch already
585+
complete, so buckets created here are counted nowhere and the findings
586+
that came with them are reported nowhere.
587+
"""
553588
if config.subpartition is None:
554589
return
555590
result = self._subpartition_service().converge_branch(config, partition_name)
591+
if converged is not None:
592+
converged.append(result)
556593
if result.created_count:
557594
logger.info(
558595
"Completed the subtree of a partition left behind by an earlier run",

0 commit comments

Comments
 (0)