Liking cljdoc? Tell your friends :D

Issue 12: Direct In-Progress Tracking - Complete Implementation Guide

Overview

This directory contains the complete plan to fix the race condition in durable-queue's in-progress statistics by tracking in-progress items directly instead of computing them.

Documents in This Implementation

1. race-condition-analysis.md (Read First!)

  • Explains the root cause of negative in-progress statistics
  • Shows the exact race condition timing scenario
  • Identifies all vulnerable code paths
  • Presents four solution options (1-4)
  • Start here if you're new to this issue

2. issue12-quick-reference.md (Quick Overview)

  • One-page visual summary of the problem and solution
  • Side-by-side comparison of old vs new code
  • Quick implementation steps
  • FAQ section
  • Read this for a quick understanding

3. issue12-implementation-plan.md (Detailed Guide)

  • Complete line-by-line code changes required
  • Explanation of each change and its rationale
  • State transition diagrams
  • Backward compatibility notes
  • Potential issues and mitigations
  • Read this before implementing

4. issue12-implementation-checklist.md (Step-by-Step Execution)

  • Checkbox-based implementation guide
  • Phase breakdown (Code Changes → Testing → Verification → Documentation)
  • Troubleshooting guide for common issues
  • Code review checklist
  • Follow this while implementing

Quick Start

For Understanding the Problem

  1. Read: race-condition-analysis.md (5 min)
  2. Review: issue12-quick-reference.md (3 min)

For Implementation

  1. Study: issue12-implementation-plan.md (10 min)
  2. Follow: issue12-implementation-checklist.md (2-4 hours execution)
  3. Run tests
  4. Submit for review

Implementation Summary

The Problem

immediate-stats reads three values non-atomically:
  1. cnt = queue.size()
  2. completed = completed.get()
  3. enqueued = enqueued.get()
  
Then computes: in-progress = (enqueued - completed) - cnt

Between reads, other threads can:
  - Remove items from queue (reduces cnt)
  - Mark items as complete (increases completed)
  
Result: in-progress = (larger - larger) - smaller = NEGATIVE ✗

The Solution

Track in-progress directly with an AtomicLong:
  - Increment when take!() dequeues an item
  - Decrement when mark-complete!() completes work
  - Decrement when mark-retry!() requeues work
  
No race window. Always correct. ✓

Code Changes Required (5 total)

LocationChangeLines
initial-statsAdd :in-progress (AtomicLong. 0)407-410
immediate-statsRead counter instead of computing412-419
mark-complete!Decrement in-progress649-653
mark-retry!Decrement in-progress643-647
take!Increment in-progress668-695

File Structure

docs/
├── race-condition-analysis.md              ← Problem analysis
├── issue12-quick-reference.md            ← One-page overview
├── issue12-implementation-plan.md        ← Detailed guide
├── issue12-implementation-checklist.md   ← Step-by-step checklist
└── ISSUE12_README.md                     ← This file

src/clj_commons/
└── durable_queue.clj                       ← 5 edits needed here

test/clj_commons/
└── durable_queue_test.clj                  ← Add tests here

Implementation Phases

Phase 1: Code Changes (30 minutes)

  • [ ] Add counter to initial-stats
  • [ ] Update immediate-stats formula
  • [ ] Add decrement to mark-complete!
  • [ ] Add decrement to mark-retry!
  • [ ] Add increment to take!

Phase 2: Testing (1-2 hours)

  • [ ] Write unit tests for counter behavior
  • [ ] Write concurrency/stress tests
  • [ ] Verify no regressions

Phase 3: Verification (30 minutes)

  • [ ] All existing tests pass
  • [ ] New tests pass
  • [ ] Manual smoke tests
  • [ ] Documentation updated

Phase 4: Review (1-2 hours)

  • [ ] Code review
  • [ ] Address feedback
  • [ ] Final verification
  • [ ] Merge

Success Criteria

✓ in-progress >= 0 (never negative) ✓ in-progress = items currently being processed ✓ All existing tests pass ✓ New tests verify race condition is fixed ✓ Stress tests with high concurrency are stable ✓ No performance degradation

Key Insights

  1. Root Cause: Non-atomic reads creating race window
  2. Fix Strategy: Replace computed value with direct counter
  3. Benefit: Simpler logic, faster reads, correct semantics
  4. Risk Level: Low (isolated to stats tracking)
  5. Backward Compatibility: Maintained (stats API unchanged)

Related Documents

  • Original issue: See GitHub issues for negative in-progress reports
  • Race condition article: race-condition-analysis.md
  • Alternative solutions: See race-condition-analysis.md Section 5

Questions?

Refer to the FAQ section in issue12-quick-reference.md


Last Updated: 2026-11-27 Status: Implementation Plan Complete, Ready for Execution Estimated Effort: 2-4 hours for experienced developer Risk Assessment: LOW

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close