Spaces:
Running
Running
Commit ·
39c69dc
1
Parent(s): 56d1732
ok
Browse files- app/core/tidb_manager.py +9 -8
app/core/tidb_manager.py
CHANGED
|
@@ -85,7 +85,8 @@ class TiDBInstance:
|
|
| 85 |
self.pool = None
|
| 86 |
|
| 87 |
def __repr__(self) -> str:
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
class TiDBManager:
|
|
@@ -103,12 +104,12 @@ class TiDBManager:
|
|
| 103 |
return self._current_instance_index
|
| 104 |
|
| 105 |
async def initialize_pools(self, minsize: int = 1, maxsize: int = 10):
|
| 106 |
-
for inst in self.instances:
|
| 107 |
try:
|
| 108 |
await inst.create_pool(minsize=minsize, maxsize=maxsize)
|
| 109 |
-
logger.info("Connected to TiDB
|
| 110 |
except Exception as exc:
|
| 111 |
-
logger.error("Failed to connect to TiDB %
|
| 112 |
inst.is_available = False
|
| 113 |
|
| 114 |
async def close_all_pools(self):
|
|
@@ -116,7 +117,7 @@ class TiDBManager:
|
|
| 116 |
await inst.close_pool()
|
| 117 |
|
| 118 |
async def ensure_tables(self, table_ddls: Dict[str, str]):
|
| 119 |
-
for inst in self.instances:
|
| 120 |
if not inst.is_available or not inst.pool:
|
| 121 |
continue
|
| 122 |
async with inst.pool.acquire() as conn:
|
|
@@ -134,11 +135,11 @@ class TiDBManager:
|
|
| 134 |
stmt = statement.strip()
|
| 135 |
if stmt:
|
| 136 |
await cur.execute(stmt)
|
| 137 |
-
logger.info("Created table '%s'
|
| 138 |
except Exception as exc:
|
| 139 |
logger.error(
|
| 140 |
-
"Failed to create table '%s'
|
| 141 |
-
table_name,
|
| 142 |
)
|
| 143 |
await conn.commit()
|
| 144 |
|
|
|
|
| 85 |
self.pool = None
|
| 86 |
|
| 87 |
def __repr__(self) -> str:
|
| 88 |
+
user_prefix = self.user.split(".")[0] if "." in self.user else self.user
|
| 89 |
+
return f"TiDBInstance({user_prefix}@...)"
|
| 90 |
|
| 91 |
|
| 92 |
class TiDBManager:
|
|
|
|
| 104 |
return self._current_instance_index
|
| 105 |
|
| 106 |
async def initialize_pools(self, minsize: int = 1, maxsize: int = 10):
|
| 107 |
+
for i, inst in enumerate(self.instances, 1):
|
| 108 |
try:
|
| 109 |
await inst.create_pool(minsize=minsize, maxsize=maxsize)
|
| 110 |
+
logger.info("Connected to TiDB instance %d/%d", i, len(self.instances))
|
| 111 |
except Exception as exc:
|
| 112 |
+
logger.error("Failed to connect to TiDB instance %d/%d: %s", i, len(self.instances), exc)
|
| 113 |
inst.is_available = False
|
| 114 |
|
| 115 |
async def close_all_pools(self):
|
|
|
|
| 117 |
await inst.close_pool()
|
| 118 |
|
| 119 |
async def ensure_tables(self, table_ddls: Dict[str, str]):
|
| 120 |
+
for i, inst in enumerate(self.instances, 1):
|
| 121 |
if not inst.is_available or not inst.pool:
|
| 122 |
continue
|
| 123 |
async with inst.pool.acquire() as conn:
|
|
|
|
| 135 |
stmt = statement.strip()
|
| 136 |
if stmt:
|
| 137 |
await cur.execute(stmt)
|
| 138 |
+
logger.info("Created table '%s' (instance %d/%d)", table_name, i, len(self.instances))
|
| 139 |
except Exception as exc:
|
| 140 |
logger.error(
|
| 141 |
+
"Failed to create table '%s' (instance %d/%d): %s",
|
| 142 |
+
table_name, i, len(self.instances), exc,
|
| 143 |
)
|
| 144 |
await conn.commit()
|
| 145 |
|