__init__.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. from synapse.storage.data_stores.main.events import PersistEventsStore
  17. from synapse.storage.data_stores.state import StateGroupDataStore
  18. from synapse.storage.database import Database, make_conn
  19. from synapse.storage.engines import create_engine
  20. from synapse.storage.prepare_database import prepare_database
  21. logger = logging.getLogger(__name__)
  22. class DataStores(object):
  23. """The various data stores.
  24. These are low level interfaces to physical databases.
  25. Attributes:
  26. main (DataStore)
  27. """
  28. def __init__(self, main_store_class, hs):
  29. # Note we pass in the main store class here as workers use a different main
  30. # store.
  31. self.databases = []
  32. self.main = None
  33. self.state = None
  34. self.persist_events = None
  35. for database_config in hs.config.database.databases:
  36. db_name = database_config.name
  37. engine = create_engine(database_config.config)
  38. with make_conn(database_config, engine) as db_conn:
  39. logger.info("Preparing database %r...", db_name)
  40. engine.check_database(db_conn)
  41. prepare_database(
  42. db_conn, engine, hs.config, data_stores=database_config.data_stores,
  43. )
  44. database = Database(hs, database_config, engine)
  45. if "main" in database_config.data_stores:
  46. logger.info("Starting 'main' data store")
  47. # Sanity check we don't try and configure the main store on
  48. # multiple databases.
  49. if self.main:
  50. raise Exception("'main' data store already configured")
  51. self.main = main_store_class(database, db_conn, hs)
  52. # If we're on a process that can persist events also
  53. # instantiate a `PersistEventsStore`
  54. if hs.config.worker.writers.events == hs.get_instance_name():
  55. self.persist_events = PersistEventsStore(
  56. hs, database, self.main
  57. )
  58. if "state" in database_config.data_stores:
  59. logger.info("Starting 'state' data store")
  60. # Sanity check we don't try and configure the state store on
  61. # multiple databases.
  62. if self.state:
  63. raise Exception("'state' data store already configured")
  64. self.state = StateGroupDataStore(database, db_conn, hs)
  65. db_conn.commit()
  66. self.databases.append(database)
  67. logger.info("Database %r prepared", db_name)
  68. # Sanity check that we have actually configured all the required stores.
  69. if not self.main:
  70. raise Exception("No 'main' data store configured")
  71. if not self.state:
  72. raise Exception("No 'main' data store configured")