2022-01-03 18:23:58 +03:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
#
|
|
|
|
# This file is part of Nominatim. (https://nominatim.org)
|
|
|
|
#
|
|
|
|
# Copyright (C) 2022 by the Nominatim developer community.
|
|
|
|
# For a full list of authors see the git log.
|
2021-02-08 19:23:05 +03:00
|
|
|
"""
|
|
|
|
Implementation of the 'replication' sub-command.
|
|
|
|
"""
|
2022-07-17 19:31:51 +03:00
|
|
|
from typing import Optional
|
|
|
|
import argparse
|
2021-02-08 19:23:05 +03:00
|
|
|
import datetime as dt
|
|
|
|
import logging
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
|
2021-04-16 15:20:09 +03:00
|
|
|
from nominatim.db import status
|
|
|
|
from nominatim.db.connection import connect
|
|
|
|
from nominatim.errors import UsageError
|
2022-07-17 19:31:51 +03:00
|
|
|
from nominatim.clicmd.args import NominatimArgs
|
2021-02-08 19:23:05 +03:00
|
|
|
|
|
|
|
LOG = logging.getLogger()
|
|
|
|
|
|
|
|
# Do not repeat documentation of subcommand classes.
|
|
|
|
# pylint: disable=C0111
|
|
|
|
# Using non-top-level imports to make pyosmium optional for replication only.
|
2022-05-11 09:59:28 +03:00
|
|
|
# pylint: disable=C0415
|
2021-02-08 19:23:05 +03:00
|
|
|
|
|
|
|
class UpdateReplication:
|
|
|
|
"""\
|
|
|
|
Update the database using an online replication service.
|
2021-10-12 00:27:38 +03:00
|
|
|
|
|
|
|
An OSM replication service is an online service that provides regular
|
|
|
|
updates (OSM diff files) for the planet or update they provide. The OSMF
|
|
|
|
provides the primary replication service for the full planet at
|
|
|
|
https://planet.osm.org/replication/ but there are other providers of
|
|
|
|
extracts of OSM data who provide such a service as well.
|
|
|
|
|
|
|
|
This sub-command allows to set up such a replication service and download
|
|
|
|
and import updates at regular intervals. You need to call '--init' once to
|
|
|
|
set up the process or whenever you change the replication configuration
|
|
|
|
parameters. Without any arguments, the sub-command will go into a loop and
|
|
|
|
continuously apply updates as they become available. Giving `--once` just
|
|
|
|
downloads and imports the next batch of updates.
|
2021-02-08 19:23:05 +03:00
|
|
|
"""
|
|
|
|
|
2022-07-17 19:31:51 +03:00
|
|
|
def add_args(self, parser: argparse.ArgumentParser) -> None:
|
2021-02-08 19:23:05 +03:00
|
|
|
group = parser.add_argument_group('Arguments for initialisation')
|
|
|
|
group.add_argument('--init', action='store_true',
|
|
|
|
help='Initialise the update process')
|
|
|
|
group.add_argument('--no-update-functions', dest='update_functions',
|
|
|
|
action='store_false',
|
2021-10-20 23:05:15 +03:00
|
|
|
help="Do not update the trigger function to "
|
|
|
|
"support differential updates (EXPERT)")
|
2021-02-08 19:23:05 +03:00
|
|
|
group = parser.add_argument_group('Arguments for updates')
|
|
|
|
group.add_argument('--check-for-updates', action='store_true',
|
|
|
|
help='Check if new updates are available and exit')
|
|
|
|
group.add_argument('--once', action='store_true',
|
2021-10-20 23:05:15 +03:00
|
|
|
help="Download and apply updates only once. When "
|
|
|
|
"not set, updates are continuously applied")
|
|
|
|
group.add_argument('--catch-up', action='store_true',
|
|
|
|
help="Download and apply updates until no new "
|
|
|
|
"data is available on the server")
|
2021-02-08 19:23:05 +03:00
|
|
|
group.add_argument('--no-index', action='store_false', dest='do_index',
|
2021-10-12 00:27:38 +03:00
|
|
|
help=("Do not index the new data. Only usable "
|
2021-03-29 11:30:45 +03:00
|
|
|
"together with --once"))
|
2021-02-08 19:23:05 +03:00
|
|
|
group.add_argument('--osm2pgsql-cache', metavar='SIZE', type=int,
|
|
|
|
help='Size of cache to be used by osm2pgsql (in MB)')
|
|
|
|
group = parser.add_argument_group('Download parameters')
|
|
|
|
group.add_argument('--socket-timeout', dest='socket_timeout', type=int, default=60,
|
2021-10-12 00:27:38 +03:00
|
|
|
help='Set timeout for file downloads')
|
2021-02-08 19:23:05 +03:00
|
|
|
|
2022-07-17 19:31:51 +03:00
|
|
|
|
|
|
|
def _init_replication(self, args: NominatimArgs) -> int:
|
2021-02-08 19:23:05 +03:00
|
|
|
from ..tools import replication, refresh
|
|
|
|
|
|
|
|
LOG.warning("Initialising replication updates")
|
2021-02-23 12:11:21 +03:00
|
|
|
with connect(args.config.get_libpq_dsn()) as conn:
|
2022-11-09 00:24:19 +03:00
|
|
|
replication.init_replication(conn, base_url=args.config.REPLICATION_URL,
|
|
|
|
socket_timeout=args.socket_timeout)
|
2021-02-23 12:11:21 +03:00
|
|
|
if args.update_functions:
|
|
|
|
LOG.warning("Create functions")
|
2021-04-19 11:01:17 +03:00
|
|
|
refresh.create_functions(conn, args.config, True, False)
|
2021-02-08 19:23:05 +03:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2022-07-17 19:31:51 +03:00
|
|
|
def _check_for_updates(self, args: NominatimArgs) -> int:
|
2021-02-08 19:23:05 +03:00
|
|
|
from ..tools import replication
|
|
|
|
|
2021-02-23 12:11:21 +03:00
|
|
|
with connect(args.config.get_libpq_dsn()) as conn:
|
2022-11-09 00:24:19 +03:00
|
|
|
return replication.check_for_updates(conn, base_url=args.config.REPLICATION_URL,
|
|
|
|
socket_timeout=args.socket_timeout)
|
2021-02-08 19:23:05 +03:00
|
|
|
|
2022-07-17 19:31:51 +03:00
|
|
|
|
|
|
|
def _report_update(self, batchdate: dt.datetime,
|
|
|
|
start_import: dt.datetime,
|
|
|
|
start_index: Optional[dt.datetime]) -> None:
|
|
|
|
def round_time(delta: dt.timedelta) -> dt.timedelta:
|
2021-02-08 19:23:05 +03:00
|
|
|
return dt.timedelta(seconds=int(delta.total_seconds()))
|
|
|
|
|
|
|
|
end = dt.datetime.now(dt.timezone.utc)
|
|
|
|
LOG.warning("Update completed. Import: %s. %sTotal: %s. Remaining backlog: %s.",
|
|
|
|
round_time((start_index or end) - start_import),
|
2022-05-11 09:59:28 +03:00
|
|
|
f"Indexing: {round_time(end - start_index)} " if start_index else '',
|
2021-02-08 19:23:05 +03:00
|
|
|
round_time(end - start_import),
|
|
|
|
round_time(end - batchdate))
|
|
|
|
|
2021-10-20 23:05:15 +03:00
|
|
|
|
2022-07-17 19:31:51 +03:00
|
|
|
def _compute_update_interval(self, args: NominatimArgs) -> int:
|
2021-10-20 23:05:15 +03:00
|
|
|
if args.catch_up:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
update_interval = args.config.get_int('REPLICATION_UPDATE_INTERVAL')
|
|
|
|
# Sanity check to not overwhelm the Geofabrik servers.
|
|
|
|
if 'download.geofabrik.de' in args.config.REPLICATION_URL\
|
|
|
|
and update_interval < 86400:
|
|
|
|
LOG.fatal("Update interval too low for download.geofabrik.de.\n"
|
|
|
|
"Please check install documentation "
|
|
|
|
"(https://nominatim.org/release-docs/latest/admin/Import-and-Update#"
|
|
|
|
"setting-up-the-update-process).")
|
|
|
|
raise UsageError("Invalid replication update interval setting.")
|
|
|
|
|
|
|
|
return update_interval
|
|
|
|
|
|
|
|
|
2022-07-17 19:31:51 +03:00
|
|
|
def _update(self, args: NominatimArgs) -> None:
|
|
|
|
# pylint: disable=too-many-locals
|
2021-02-08 19:23:05 +03:00
|
|
|
from ..tools import replication
|
|
|
|
from ..indexer.indexer import Indexer
|
2021-04-24 12:25:47 +03:00
|
|
|
from ..tokenizer import factory as tokenizer_factory
|
2021-02-08 19:23:05 +03:00
|
|
|
|
2022-07-17 19:31:51 +03:00
|
|
|
update_interval = self._compute_update_interval(args)
|
2021-10-20 23:05:15 +03:00
|
|
|
|
2021-02-24 12:38:19 +03:00
|
|
|
params = args.osm2pgsql_options(default_cache=2000, default_threads=1)
|
2021-02-08 19:23:05 +03:00
|
|
|
params.update(base_url=args.config.REPLICATION_URL,
|
2021-10-20 23:05:15 +03:00
|
|
|
update_interval=update_interval,
|
2021-02-08 19:23:05 +03:00
|
|
|
import_file=args.project_dir / 'osmosischange.osc',
|
|
|
|
max_diff_size=args.config.get_int('REPLICATION_MAX_DIFF'),
|
|
|
|
indexed_only=not args.once)
|
|
|
|
|
|
|
|
if not args.once:
|
|
|
|
if not args.do_index:
|
|
|
|
LOG.fatal("Indexing cannot be disabled when running updates continuously.")
|
|
|
|
raise UsageError("Bad argument '--no-index'.")
|
|
|
|
recheck_interval = args.config.get_int('REPLICATION_RECHECK_INTERVAL')
|
|
|
|
|
2021-04-24 12:25:47 +03:00
|
|
|
tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
|
2021-11-19 16:47:00 +03:00
|
|
|
indexer = Indexer(args.config.get_libpq_dsn(), tokenizer, args.threads or 1)
|
2021-04-24 12:25:47 +03:00
|
|
|
|
2023-06-19 13:02:51 +03:00
|
|
|
dsn = args.config.get_libpq_dsn()
|
|
|
|
|
2021-02-08 19:23:05 +03:00
|
|
|
while True:
|
2023-06-19 13:02:51 +03:00
|
|
|
start = dt.datetime.now(dt.timezone.utc)
|
|
|
|
state = replication.update(dsn, params, socket_timeout=args.socket_timeout)
|
|
|
|
|
|
|
|
with connect(dsn) as conn:
|
2021-02-23 12:11:21 +03:00
|
|
|
if state is not replication.UpdateState.NO_CHANGES:
|
|
|
|
status.log_status(conn, start, 'import')
|
|
|
|
batchdate, _, _ = status.get_status(conn)
|
2021-05-26 12:47:08 +03:00
|
|
|
conn.commit()
|
2021-02-08 19:23:05 +03:00
|
|
|
|
|
|
|
if state is not replication.UpdateState.NO_CHANGES and args.do_index:
|
|
|
|
index_start = dt.datetime.now(dt.timezone.utc)
|
2021-10-20 23:05:15 +03:00
|
|
|
indexer.index_full(analyse=False)
|
2021-02-08 19:23:05 +03:00
|
|
|
|
2023-06-19 13:02:51 +03:00
|
|
|
with connect(dsn) as conn:
|
2021-02-23 12:11:21 +03:00
|
|
|
status.set_indexed(conn, True)
|
|
|
|
status.log_status(conn, index_start, 'index')
|
2021-05-26 12:47:08 +03:00
|
|
|
conn.commit()
|
2021-02-08 19:23:05 +03:00
|
|
|
else:
|
|
|
|
index_start = None
|
|
|
|
|
2021-10-20 23:05:15 +03:00
|
|
|
if state is replication.UpdateState.NO_CHANGES and \
|
|
|
|
args.catch_up or update_interval > 40*60:
|
|
|
|
while indexer.has_pending():
|
|
|
|
indexer.index_full(analyse=False)
|
|
|
|
|
2021-02-08 19:23:05 +03:00
|
|
|
if LOG.isEnabledFor(logging.WARNING):
|
2022-07-17 19:31:51 +03:00
|
|
|
assert batchdate is not None
|
|
|
|
self._report_update(batchdate, start, index_start)
|
2021-02-08 19:23:05 +03:00
|
|
|
|
2021-10-20 23:05:15 +03:00
|
|
|
if args.once or (args.catch_up and state is replication.UpdateState.NO_CHANGES):
|
2021-02-08 19:23:05 +03:00
|
|
|
break
|
|
|
|
|
|
|
|
if state is replication.UpdateState.NO_CHANGES:
|
|
|
|
LOG.warning("No new changes. Sleeping for %d sec.", recheck_interval)
|
|
|
|
time.sleep(recheck_interval)
|
|
|
|
|
|
|
|
|
2022-07-17 19:31:51 +03:00
|
|
|
def run(self, args: NominatimArgs) -> int:
|
2021-02-08 19:23:05 +03:00
|
|
|
socket.setdefaulttimeout(args.socket_timeout)
|
|
|
|
|
|
|
|
if args.init:
|
2022-07-17 19:31:51 +03:00
|
|
|
return self._init_replication(args)
|
2021-02-08 19:23:05 +03:00
|
|
|
|
|
|
|
if args.check_for_updates:
|
2022-07-17 19:31:51 +03:00
|
|
|
return self._check_for_updates(args)
|
2021-02-08 19:23:05 +03:00
|
|
|
|
2022-07-17 19:31:51 +03:00
|
|
|
self._update(args)
|
2021-02-11 12:01:31 +03:00
|
|
|
return 0
|