Fixing Location History Refresh Issues
This guide explains how to fix issues with the location history refresher process not properly updating data after new tracker reports are fetched.
Problem Description
The system has the following components:
- tracker-report-fetcher: Fetches reports from trackers and stores them in the database
- location-history-refresher: Listens for PostgreSQL notifications to refresh the materialized views
The issue occurs when:
- The fetcher successfully retrieves and stores reports
- The refresher receives notifications and refreshes the
location_historymaterialized view - However, the continuous aggregates (
location_history_hourlyandlocation_history_daily) are not properly refreshed - Data doesn't appear to be updated until manually running refresh commands
Root Causes
- Database Connection Issues: If the refresher experiences database connection issues, it might miss notifications
- Continuous Aggregate Refresh Timing: The continuous aggregates are only refreshed periodically (hourly) and not when notifications are received
- Data Retention Concerns: Raw data in
location_reportsis only kept for 2 days, which might be too short if there are issues with the aggregation process
Solution
We've implemented the following improvements:
- Enhanced Notification Handling: When a notification is received, both the materialized view and continuous aggregates are refreshed
- Timestamp-based Polling Fallback: A new mechanism periodically checks for unprocessed data based on timestamps
- Extended Data Retention: The retention period for raw data in
location_reportshas been increased from 2 days to 7 days
Applying the Changes
1. Update the Location History Refresher
The location_history_refresher.py file has been updated with the following improvements:
- When a notification is received, both the materialized view and continuous aggregates are refreshed
- A new
check_for_missed_data()function periodically checks for unprocessed data - Timestamps are tracked in Redis to ensure data is not missed
To apply these changes:
-
Stop the location-history-refresher service:
docker-compose -f fetcher/compose.yml stop location-history-refresher -
The updated
location_history_refresher.pyfile is already in place atfetcher/build/location_history_refresher.py -
Restart the location-history-refresher service:
docker-compose -f fetcher/compose.yml up -d location-history-refresher
2. Update the Data Retention Policy
The retention period for raw data in location_reports has been increased from 2 days to 7 days to ensure data is not deleted before it's properly aggregated.
To apply this change:
- Run the update_retention_policy.py script:
python scripts/update_retention_policy.py
Verifying the Changes
After applying the changes, you can verify that they're working correctly:
-
Check the logs of the location-history-refresher service:
docker-compose -f fetcher/compose.yml logs -f location-history-refresherLook for messages like:
- "Also refreshing continuous aggregates..."
- "Hourly aggregate refresh complete"
- "Daily aggregate refresh complete"
-
Verify that new tracker reports are properly reflected in the location history:
- Wait for new reports to be fetched
- Check that the data appears in the location history without manual intervention
-
Verify the updated retention policy:
SELECT * FROM _timescaledb_config.bgw_job
WHERE proc_name = 'policy_retention'
AND hypertable_name = 'location_reports';The
configcolumn should show a 7-day interval.
Troubleshooting
If you still experience issues:
-
Manual Refresh: You can still manually refresh the continuous aggregates and materialized view:
CALL refresh_continuous_aggregate('location_history_hourly', NULL, NULL);
CALL refresh_continuous_aggregate('location_history_daily', NULL, NULL);
REFRESH MATERIALIZED VIEW CONCURRENTLY location_history; -
Check Redis: Verify that Redis is working correctly:
docker-compose -f fetcher/compose.yml exec location-history-refresher redis-cli pingIt should respond with "PONG".
-
Check Database Connectivity: Verify that the refresher can connect to the database:
docker-compose -f fetcher/compose.yml logs location-history-refresher | grep "Connected to database" -
Restart Services: If all else fails, try restarting both services:
docker-compose -f fetcher/compose.yml restart