Skip to content

Commit b2b5cc7

Browse files
authored
Merge pull request #2212 from lonvia/check-date-on-replication-init
Replication: make sure that the computed start state is valid
2 parents f654c89 + b13126a commit b2b5cc7

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed

scripts/osm2pgsql-replication

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,12 +449,22 @@ def init(props, args):
449449
raise DBError(1, f"Cannot reach the configured replication service '{base_url}'.\n"
450450
"Does the URL point to a directory containing OSM update data?")
451451

452+
state = repl.get_state_info(seq)
453+
if state is None:
454+
raise DBError(1, f"Cannot load state information for {seq} from replication service {base_url}.\n"
455+
+ (f"The server may not have diffs going as far back as {date}."
456+
if date is not None else
457+
"Does the URL point to a directory containing OSM update data?"))
458+
452459
if date is None:
453-
state = repl.get_state_info(seq)
454-
if state is None:
455-
raise DBError(1, f"Cannot reach the configured replication service '{base_url}'.\n"
456-
"Does the URL point to a directory containing OSM update data?")
457460
date = from_osm_date(state.timestamp)
461+
else:
462+
# Sanity check. Is the sequence in line with the date requested?
463+
if state.timestamp > date:
464+
raise DBError(1, "The replication service does not have diff files for the requested date.\n"
465+
f"Replication service used: {base_url}\n"
466+
f"Most recent diffs available start at: {state.timestamp}\n"
467+
f"Database date: {date}")
458468

459469
props.write_replication_state(base_url, seq, date)
460470

tests/bdd/command-line/replication.feature

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Feature: Tests for the osm2pgsql-replication script with property table
66
n34 Tamenity=restaurant x77 y45.3
77
"""
88
And the replication service at http://example.com/europe/liechtenstein-updates
9+
| sequence | timestamp |
10+
| 9999999 | 2013-08-03T19:00:02Z |
911
When running osm2pgsql pgsql with parameters
1012
| --slim |
1113

@@ -22,6 +24,8 @@ Feature: Tests for the osm2pgsql-replication script with property table
2224
Scenario: Replication will be initialised from the information of the import file
2325
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
2426
And the replication service at http://example.com/europe/liechtenstein-updates
27+
| sequence | timestamp |
28+
| 9999999 | 2013-08-03T19:00:02Z |
2529
When running osm2pgsql pgsql with parameters
2630
| --slim |
2731

@@ -65,6 +69,8 @@ Feature: Tests for the osm2pgsql-replication script with property table
6569
Given the database schema foobar
6670
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
6771
And the replication service at http://example.com/europe/liechtenstein-updates
72+
| sequence | timestamp |
73+
| 9999999 | 2013-08-03T19:00:02Z |
6874
When running osm2pgsql pgsql with parameters
6975
| --slim | --middle-schema=foobar |
7076

@@ -82,6 +88,8 @@ Feature: Tests for the osm2pgsql-replication script with property table
8288
Given the database schema foobar
8389
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
8490
And the replication service at http://example.com/europe/liechtenstein-updates
91+
| sequence | timestamp |
92+
| 9999999 | 2013-08-03T19:00:02Z |
8593
When running osm2pgsql pgsql with parameters
8694
| --slim | --schema=foobar |
8795

@@ -100,6 +108,8 @@ Feature: Tests for the osm2pgsql-replication script with property table
100108
Given the database schema baz
101109
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
102110
And the replication service at http://example.com/europe/liechtenstein-updates
111+
| sequence | timestamp |
112+
| 9999999 | 2013-08-03T19:00:02Z |
103113
When running osm2pgsql pgsql with parameters
104114
| --slim | --middle-schema=foobar | --schema=baz |
105115

@@ -220,9 +230,9 @@ Feature: Tests for the osm2pgsql-replication script with property table
220230
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
221231
And the replication service at http://example.com/europe/liechtenstein-updates
222232
| sequence | timestamp |
223-
| 345 | 2020-10-04T01:00:00Z |
224-
| 346 | 2020-10-04T02:00:00Z |
225-
| 347 | 2020-10-04T03:00:00Z |
233+
| 344 | 2013-08-03T13:00:02Z |
234+
| 345 | 2013-08-03T14:00:02Z |
235+
| 346 | 2013-08-03T15:00:02Z |
226236
When running osm2pgsql pgsql with parameters
227237
| --slim |
228238

@@ -370,3 +380,37 @@ Feature: Tests for the osm2pgsql-replication script with property table
370380
"server": {"base_url": "http://example.com/europe/liechtenstein-updates", "sequence": 10000001, "timestamp": "2013-10-01T01:00:00Z"
371381
"local": {"sequence": 9999999, "timestamp": "2013-08-03T19:00:02Z"
372382
"""
383+
384+
385+
Scenario: Replication initialisation will fail when diffs are not old enough
386+
Given the database schema foobar
387+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
388+
And the replication service at http://example.com/europe/liechtenstein-updates
389+
| sequence | timestamp |
390+
| 10000000 | 2020-01-01T01:00:02Z |
391+
When running osm2pgsql pgsql with parameters
392+
| --slim |
393+
394+
Then running osm2pgsql-replication fails with returncode 1
395+
| init | --server | http://example.com/europe/liechtenstein-updates |
396+
And the error output contains
397+
"""
398+
The replication service does not have diff files for the requested date.
399+
"""
400+
401+
402+
Scenario: Replication initialisation will fail when diffs are not old enough
403+
Given the database schema foobar
404+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
405+
And the replication service at http://example.com/europe/liechtenstein-updates
406+
| sequence | timestamp |
407+
| 10000000 | 2020-01-01T01:00:02Z |
408+
When running osm2pgsql pgsql with parameters
409+
| --slim |
410+
411+
Then running osm2pgsql-replication fails with returncode 1
412+
| init |
413+
And the error output contains
414+
"""
415+
Cannot load state information for 9999999 from replication service http://example.com/europe/liechtenstein-updates.
416+
"""

tests/bdd/command-line/replication_legacy.feature

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Feature: Tests for the osm2pgsql-replication script without property table
1010

1111
Scenario: Replication can be initialised with a osm file
1212
Given the replication service at http://example.com/europe/liechtenstein-updates
13+
| sequence | timestamp |
14+
| 9999999 | 2013-08-03T19:00:02Z |
1315
When running osm2pgsql pgsql with parameters
1416
| --slim |
1517
And deleting table osm2pgsql_properties
@@ -38,6 +40,8 @@ Feature: Tests for the osm2pgsql-replication script without property table
3840

3941
Scenario: Replication can be initialised in different schema
4042
Given the replication service at http://example.com/europe/liechtenstein-updates
43+
| sequence | timestamp |
44+
| 9999999 | 2013-08-03T19:00:02Z |
4145
Given the database schema foobar
4246
When running osm2pgsql pgsql with parameters
4347
| --slim | --middle-schema=foobar |

tests/bdd/steps/replication_server_mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def get_state_info(self, seq=None, retries=2):
2828
if info.sequence == seq:
2929
return info
3030

31-
assert False, f"No sequence information for sequence ID {seq}."
31+
return None
3232

3333
def timestamp_to_sequence(self, timestamp, balanced_search=False):
3434
assert self.state_infos, 'Replication mock not properly set up'

0 commit comments

Comments
 (0)