2024-08-16 00:28:26 +08:00
|
|
|
import time
|
|
|
|
|
|
|
|
import click
|
|
|
|
|
|
|
|
import app
|
|
|
|
from configs import dify_config
|
|
|
|
from core.rag.datasource.vdb.tidb_on_qdrant.tidb_service import TidbService
|
|
|
|
from extensions.ext_database import db
|
|
|
|
from models.dataset import TidbAuthBinding
|
|
|
|
|
|
|
|
|
2024-10-12 17:43:33 +08:00
|
|
|
@app.celery.task(queue="dataset")
|
2024-08-16 00:28:26 +08:00
|
|
|
def create_tidb_serverless_task():
|
2024-10-12 17:43:33 +08:00
|
|
|
click.echo(click.style("Start create tidb serverless task.", fg="green"))
|
2024-08-16 00:28:26 +08:00
|
|
|
tidb_serverless_number = dify_config.TIDB_SERVERLESS_NUMBER
|
|
|
|
start_at = time.perf_counter()
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
# check the number of idle tidb serverless
|
|
|
|
idle_tidb_serverless_number = TidbAuthBinding.query.filter(TidbAuthBinding.active == False).count()
|
|
|
|
if idle_tidb_serverless_number >= tidb_serverless_number:
|
|
|
|
break
|
|
|
|
# create tidb serverless
|
2024-09-30 14:44:35 +08:00
|
|
|
iterations_per_thread = 100
|
|
|
|
create_clusters(iterations_per_thread)
|
2024-08-16 00:28:26 +08:00
|
|
|
|
|
|
|
except Exception as e:
|
2024-10-12 17:43:33 +08:00
|
|
|
click.echo(click.style(f"Error: {e}", fg="red"))
|
2024-08-16 00:28:26 +08:00
|
|
|
break
|
|
|
|
|
|
|
|
end_at = time.perf_counter()
|
2024-10-12 17:43:33 +08:00
|
|
|
click.echo(click.style("Create tidb serverless task success latency: {}".format(end_at - start_at), fg="green"))
|
2024-08-16 00:28:26 +08:00
|
|
|
|
|
|
|
|
2024-09-27 19:01:55 +08:00
|
|
|
def create_clusters(batch_size):
|
|
|
|
try:
|
|
|
|
new_clusters = TidbService.batch_create_tidb_serverless_cluster(
|
|
|
|
batch_size,
|
|
|
|
dify_config.TIDB_PROJECT_ID,
|
|
|
|
dify_config.TIDB_API_URL,
|
|
|
|
dify_config.TIDB_IAM_API_URL,
|
|
|
|
dify_config.TIDB_PUBLIC_KEY,
|
|
|
|
dify_config.TIDB_PRIVATE_KEY,
|
2024-10-12 17:43:33 +08:00
|
|
|
dify_config.TIDB_REGION,
|
|
|
|
)
|
2024-09-27 19:01:55 +08:00
|
|
|
for new_cluster in new_clusters:
|
2024-10-12 17:43:33 +08:00
|
|
|
tidb_auth_binding = TidbAuthBinding(
|
|
|
|
cluster_id=new_cluster["cluster_id"],
|
|
|
|
cluster_name=new_cluster["cluster_name"],
|
|
|
|
account=new_cluster["account"],
|
|
|
|
password=new_cluster["password"],
|
|
|
|
)
|
2024-08-16 00:28:26 +08:00
|
|
|
db.session.add(tidb_auth_binding)
|
2024-09-27 19:01:55 +08:00
|
|
|
db.session.commit()
|
|
|
|
except Exception as e:
|
2024-10-12 17:43:33 +08:00
|
|
|
click.echo(click.style(f"Error: {e}", fg="red"))
|