You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db_services.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #
  2. # Copyright 2021 The RAG Flow Authors. All Rights Reserved.
  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. #
  16. import abc
  17. import json
  18. import time
  19. from functools import wraps
  20. from shortuuid import ShortUUID
  21. from web_server.versions import get_rag_version
  22. from web_server.errors.error_services import *
  23. from web_server.settings import (
  24. GRPC_PORT, HOST, HTTP_PORT,
  25. RANDOM_INSTANCE_ID, stat_logger,
  26. )
  27. instance_id = ShortUUID().random(length=8) if RANDOM_INSTANCE_ID else f'flow-{HOST}-{HTTP_PORT}'
  28. server_instance = (
  29. f'{HOST}:{GRPC_PORT}',
  30. json.dumps({
  31. 'instance_id': instance_id,
  32. 'timestamp': round(time.time() * 1000),
  33. 'version': get_rag_version() or '',
  34. 'host': HOST,
  35. 'grpc_port': GRPC_PORT,
  36. 'http_port': HTTP_PORT,
  37. }),
  38. )
  39. def check_service_supported(method):
  40. """Decorator to check if `service_name` is supported.
  41. The attribute `supported_services` MUST be defined in class.
  42. The first and second arguments of `method` MUST be `self` and `service_name`.
  43. :param Callable method: The class method.
  44. :return: The inner wrapper function.
  45. :rtype: Callable
  46. """
  47. @wraps(method)
  48. def magic(self, service_name, *args, **kwargs):
  49. if service_name not in self.supported_services:
  50. raise ServiceNotSupported(service_name=service_name)
  51. return method(self, service_name, *args, **kwargs)
  52. return magic
  53. class ServicesDB(abc.ABC):
  54. """Database for storage service urls.
  55. Abstract base class for the real backends.
  56. """
  57. @property
  58. @abc.abstractmethod
  59. def supported_services(self):
  60. """The names of supported services.
  61. The returned list SHOULD contain `ragflow` (model download) and `servings` (RAG-Serving).
  62. :return: The service names.
  63. :rtype: list
  64. """
  65. pass
  66. @abc.abstractmethod
  67. def _get_serving(self):
  68. pass
  69. def get_serving(self):
  70. try:
  71. return self._get_serving()
  72. except ServicesError as e:
  73. stat_logger.exception(e)
  74. return []
  75. @abc.abstractmethod
  76. def _insert(self, service_name, service_url, value=''):
  77. pass
  78. @check_service_supported
  79. def insert(self, service_name, service_url, value=''):
  80. """Insert a service url to database.
  81. :param str service_name: The service name.
  82. :param str service_url: The service url.
  83. :return: None
  84. """
  85. try:
  86. self._insert(service_name, service_url, value)
  87. except ServicesError as e:
  88. stat_logger.exception(e)
  89. @abc.abstractmethod
  90. def _delete(self, service_name, service_url):
  91. pass
  92. @check_service_supported
  93. def delete(self, service_name, service_url):
  94. """Delete a service url from database.
  95. :param str service_name: The service name.
  96. :param str service_url: The service url.
  97. :return: None
  98. """
  99. try:
  100. self._delete(service_name, service_url)
  101. except ServicesError as e:
  102. stat_logger.exception(e)
  103. def register_flow(self):
  104. """Call `self.insert` for insert the flow server address to databae.
  105. :return: None
  106. """
  107. self.insert('flow-server', *server_instance)
  108. def unregister_flow(self):
  109. """Call `self.delete` for delete the flow server address from databae.
  110. :return: None
  111. """
  112. self.delete('flow-server', server_instance[0])
  113. @abc.abstractmethod
  114. def _get_urls(self, service_name, with_values=False):
  115. pass
  116. @check_service_supported
  117. def get_urls(self, service_name, with_values=False):
  118. """Query service urls from database. The urls may belong to other nodes.
  119. Currently, only `ragflow` (model download) urls and `servings` (RAG-Serving) urls are supported.
  120. `ragflow` is a url containing scheme, host, port and path,
  121. while `servings` only contains host and port.
  122. :param str service_name: The service name.
  123. :return: The service urls.
  124. :rtype: list
  125. """
  126. try:
  127. return self._get_urls(service_name, with_values)
  128. except ServicesError as e:
  129. stat_logger.exception(e)
  130. return []