_session.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #
  2. # Copyright 2019 The FATE 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 traceback
  17. import pymysql
  18. from fate_arch.storage import StorageSessionBase, StorageEngine, MySQLStoreType
  19. from fate_arch.abc import AddressABC
  20. from fate_arch.common.address import MysqlAddress
  21. class StorageSession(StorageSessionBase):
  22. def __init__(self, session_id, options=None):
  23. super(StorageSession, self).__init__(session_id=session_id, engine=StorageEngine.MYSQL)
  24. self._db_con = {}
  25. def table(self, name, namespace, address: AddressABC, partitions,
  26. store_type: MySQLStoreType = MySQLStoreType.InnoDB, options=None, **kwargs):
  27. if isinstance(address, MysqlAddress):
  28. from fate_arch.storage.mysql._table import StorageTable
  29. address_key = MysqlAddress(user=None,
  30. passwd=None,
  31. host=address.host,
  32. port=address.port,
  33. db=address.db,
  34. name=None)
  35. if address_key in self._db_con:
  36. con, cur = self._db_con[address_key]
  37. else:
  38. self._create_db_if_not_exists(address)
  39. con = pymysql.connect(host=address.host,
  40. user=address.user,
  41. passwd=address.passwd,
  42. port=address.port,
  43. db=address.db)
  44. cur = con.cursor()
  45. self._db_con[address_key] = (con, cur)
  46. return StorageTable(cur=cur, con=con, address=address, name=name, namespace=namespace,
  47. store_type=store_type, partitions=partitions, options=options)
  48. raise NotImplementedError(f"address type {type(address)} not supported with eggroll storage")
  49. def cleanup(self, name, namespace):
  50. pass
  51. def stop(self):
  52. try:
  53. for key, val in self._db_con.items():
  54. con = val[0]
  55. cur = val[1]
  56. cur.close()
  57. con.close()
  58. except Exception as e:
  59. traceback.print_exc()
  60. def kill(self):
  61. return self.stop()
  62. def _create_db_if_not_exists(self, address):
  63. connection = pymysql.connect(host=address.host,
  64. user=address.user,
  65. password=address.passwd,
  66. port=address.port)
  67. with connection:
  68. with connection.cursor() as cursor:
  69. cursor.execute("create database if not exists {}".format(address.db))
  70. print('create db {} success'.format(address.db))
  71. connection.commit()