2 Implementation of the 'replication' sub-command.
9 from nominatim.db import status
10 from nominatim.db.connection import connect
11 from nominatim.errors import UsageError
13 LOG = logging.getLogger()
15 # Do not repeat documentation of subcommand classes.
16 # pylint: disable=C0111
17 # Using non-top-level imports to make pyosmium optional for replication only.
18 # pylint: disable=E0012,C0415
20 class UpdateReplication:
22 Update the database using an online replication service.
27 group = parser.add_argument_group('Arguments for initialisation')
28 group.add_argument('--init', action='store_true',
29 help='Initialise the update process')
30 group.add_argument('--no-update-functions', dest='update_functions',
32 help=("Do not update the trigger function to "
33 "support differential updates."))
34 group = parser.add_argument_group('Arguments for updates')
35 group.add_argument('--check-for-updates', action='store_true',
36 help='Check if new updates are available and exit')
37 group.add_argument('--once', action='store_true',
38 help=("Download and apply updates only once. When "
39 "not set, updates are continuously applied"))
40 group.add_argument('--no-index', action='store_false', dest='do_index',
41 help=("Do not index the new data. Only applicable "
42 "together with --once"))
43 group.add_argument('--osm2pgsql-cache', metavar='SIZE', type=int,
44 help='Size of cache to be used by osm2pgsql (in MB)')
45 group = parser.add_argument_group('Download parameters')
46 group.add_argument('--socket-timeout', dest='socket_timeout', type=int, default=60,
47 help='Set timeout for file downloads.')
50 def _init_replication(args):
51 from ..tools import replication, refresh
53 LOG.warning("Initialising replication updates")
54 with connect(args.config.get_libpq_dsn()) as conn:
55 replication.init_replication(conn, base_url=args.config.REPLICATION_URL)
56 if args.update_functions:
57 LOG.warning("Create functions")
58 refresh.create_functions(conn, args.config, True, False)
63 def _check_for_updates(args):
64 from ..tools import replication
66 with connect(args.config.get_libpq_dsn()) as conn:
67 return replication.check_for_updates(conn, base_url=args.config.REPLICATION_URL)
70 def _report_update(batchdate, start_import, start_index):
71 def round_time(delta):
72 return dt.timedelta(seconds=int(delta.total_seconds()))
74 end = dt.datetime.now(dt.timezone.utc)
75 LOG.warning("Update completed. Import: %s. %sTotal: %s. Remaining backlog: %s.",
76 round_time((start_index or end) - start_import),
77 "Indexing: {} ".format(round_time(end - start_index))
78 if start_index else '',
79 round_time(end - start_import),
80 round_time(end - batchdate))
84 from ..tools import replication
85 from ..indexer.indexer import Indexer
87 params = args.osm2pgsql_options(default_cache=2000, default_threads=1)
88 params.update(base_url=args.config.REPLICATION_URL,
89 update_interval=args.config.get_int('REPLICATION_UPDATE_INTERVAL'),
90 import_file=args.project_dir / 'osmosischange.osc',
91 max_diff_size=args.config.get_int('REPLICATION_MAX_DIFF'),
92 indexed_only=not args.once)
94 # Sanity check to not overwhelm the Geofabrik servers.
95 if 'download.geofabrik.de'in params['base_url']\
96 and params['update_interval'] < 86400:
97 LOG.fatal("Update interval too low for download.geofabrik.de.\n"
98 "Please check install documentation "
99 "(https://nominatim.org/release-docs/latest/admin/Import-and-Update#"
100 "setting-up-the-update-process).")
101 raise UsageError("Invalid replication update interval setting.")
104 if not args.do_index:
105 LOG.fatal("Indexing cannot be disabled when running updates continuously.")
106 raise UsageError("Bad argument '--no-index'.")
107 recheck_interval = args.config.get_int('REPLICATION_RECHECK_INTERVAL')
110 with connect(args.config.get_libpq_dsn()) as conn:
111 start = dt.datetime.now(dt.timezone.utc)
112 state = replication.update(conn, params)
113 if state is not replication.UpdateState.NO_CHANGES:
114 status.log_status(conn, start, 'import')
115 batchdate, _, _ = status.get_status(conn)
117 if state is not replication.UpdateState.NO_CHANGES and args.do_index:
118 index_start = dt.datetime.now(dt.timezone.utc)
119 indexer = Indexer(args.config.get_libpq_dsn(),
121 indexer.index_boundaries(0, 30)
122 indexer.index_by_rank(0, 30)
124 with connect(args.config.get_libpq_dsn()) as conn:
125 status.set_indexed(conn, True)
126 status.log_status(conn, index_start, 'index')
130 if LOG.isEnabledFor(logging.WARNING):
131 UpdateReplication._report_update(batchdate, start, index_start)
136 if state is replication.UpdateState.NO_CHANGES:
137 LOG.warning("No new changes. Sleeping for %d sec.", recheck_interval)
138 time.sleep(recheck_interval)
143 socket.setdefaulttimeout(args.socket_timeout)
146 return UpdateReplication._init_replication(args)
148 if args.check_for_updates:
149 return UpdateReplication._check_for_updates(args)
151 UpdateReplication._update(args)