mirror of
https://github.com/Tencent/WeKnora.git
synced 2026-08-30 16:53:21 +08:00
perf(container): batch legacy storage alias lookup at startup
The startup migration probed every workspace/provider pair with First(), so each miss emitted a GORM "record not found" log line and a separate round trip. Load all aliases once and index them in memory instead.
This commit is contained in:
@@ -768,6 +768,28 @@ func migrateLegacyStorageBackends(db *gorm.DB) {
|
||||
logger.Warnf(context.Background(), "Failed to load workspaces for storage backend migration: %v", err)
|
||||
return
|
||||
}
|
||||
if len(tenants) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// Load every alias in a single query. Probing each tenant/provider pair with
|
||||
// First() makes GORM log "record not found" for every miss, which floods the
|
||||
// startup log with workspaces × providers lines on fresh installs.
|
||||
var aliases []*types.StorageBackend
|
||||
if err := db.Where("legacy_alias = ?", true).Find(&aliases).Error; err != nil {
|
||||
logger.Warnf(context.Background(), "Failed to load legacy storage aliases: %v", err)
|
||||
return
|
||||
}
|
||||
existingAliases := make(map[uint64]map[string]*types.StorageBackend, len(aliases))
|
||||
for _, alias := range aliases {
|
||||
byProvider := existingAliases[alias.TenantID]
|
||||
if byProvider == nil {
|
||||
byProvider = make(map[string]*types.StorageBackend)
|
||||
existingAliases[alias.TenantID] = byProvider
|
||||
}
|
||||
byProvider[alias.Provider] = alias
|
||||
}
|
||||
|
||||
for _, tenant := range tenants {
|
||||
legacy := tenant.StorageEngineConfig
|
||||
defaultProvider := ""
|
||||
@@ -783,9 +805,7 @@ func migrateLegacyStorageBackends(db *gorm.DB) {
|
||||
|
||||
backendIDs := make(map[string]string)
|
||||
for _, provider := range storageallowlist.Supported() {
|
||||
var existing types.StorageBackend
|
||||
err := db.Where("tenant_id = ? AND provider = ? AND legacy_alias = ?", tenant.ID, provider, true).First(&existing).Error
|
||||
if err == nil {
|
||||
if existing := existingAliases[tenant.ID][provider]; existing != nil {
|
||||
// Environment-backed aliases are snapshots, not user-owned config.
|
||||
// Refresh them at every startup so credential rotation does not
|
||||
// leave the persisted resolver on stale values. If the workspace
|
||||
|
||||
Reference in New Issue
Block a user