refactor(core): align with cordis spatiotemporal composability architecture

- Purify core micro-kernel by removing context hardcoded helpers and reverse dependencies
- Eliminate init() side effects in infra plugins with reversible lifecycle disposal
- Completely isolate plugins by removing cross-plugin imports and using core/contracts
- Introduce TaskService and RiskControlService contracts for unified cross-plugin APIs
- Regenerate Swagger documentation and update developer guide matrix
- Achieve 0 violations in check_cordis_architecture.sh and 100% test pass
This commit is contained in:
ryan
2026-08-28 15:05:31 +08:00
parent fc7fae7b0e
commit 299ac30ee4
150 changed files with 4328 additions and 2923 deletions
@@ -11,7 +11,6 @@ import (
"time"
pkgcache "Wavelet/pkg/cache/disk"
database "Wavelet/plugins/infra/database"
)
// Status represents the runtime cache statistics.
@@ -63,14 +62,14 @@ func New(basePath string) *DiskCache {
// ReloadConfig reloads policies from database configs dynamically.
func (c *DiskCache) ReloadConfig(ctx context.Context) {
// Ensure DB is initialized before querying
if database.DB(ctx) == nil {
if getDB(ctx) == nil {
return
}
// 1. Max Size
maxSizeMB := int64(defaultMaxSizeMB)
var maxVal string
if err := database.DB(ctx).Table("w_system_configs").Where("key = ?", "disk_cache_max_size_mb").Pluck("value", &maxVal).Error; err == nil && maxVal != "" {
if err := getDB(ctx).Table("w_system_configs").Where("key = ?", "disk_cache_max_size_mb").Pluck("value", &maxVal).Error; err == nil && maxVal != "" {
if val, err := strconv.ParseInt(maxVal, 10, 64); err == nil && val > 0 {
maxSizeMB = val
}
@@ -79,7 +78,7 @@ func (c *DiskCache) ReloadConfig(ctx context.Context) {
// 2. Default TTL
ttlMinutes := int64(defaultTTLMinutes)
var ttlVal string
if err := database.DB(ctx).Table("w_system_configs").Where("key = ?", "disk_cache_ttl_minutes").Pluck("value", &ttlVal).Error; err == nil && ttlVal != "" {
if err := getDB(ctx).Table("w_system_configs").Where("key = ?", "disk_cache_ttl_minutes").Pluck("value", &ttlVal).Error; err == nil && ttlVal != "" {
if val, err := strconv.ParseInt(ttlVal, 10, 64); err == nil && val >= 0 {
ttlMinutes = val
}
@@ -88,7 +87,7 @@ func (c *DiskCache) ReloadConfig(ctx context.Context) {
// 3. LRU Enabled
lruEnabled := true
var lruVal string
if err := database.DB(ctx).Table("w_system_configs").Where("key = ?", "disk_cache_lru_enabled").Pluck("value", &lruVal).Error; err == nil && lruVal != "" {
if err := getDB(ctx).Table("w_system_configs").Where("key = ?", "disk_cache_lru_enabled").Pluck("value", &lruVal).Error; err == nil && lruVal != "" {
if val, err := strconv.ParseBool(lruVal); err == nil {
lruEnabled = val
}
@@ -5,20 +5,38 @@ package diskcache
import (
"context"
"os"
"testing"
"gorm.io/gorm"
"Wavelet/pkg/testhelper"
cache "Wavelet/plugins/infra/cache"
)
type mockDBService struct {
db *gorm.DB
}
func (m *mockDBService) GORM() *gorm.DB {
return m.db
}
func (m *mockDBService) DB(ctx context.Context) *gorm.DB {
return m.db.WithContext(ctx)
}
func (m *mockDBService) Named(_ string) *gorm.DB {
return m.db
}
func TestDiskCacheReloadConfig(t *testing.T) {
dbConn, _, cleanup := testhelper.SetupTestEnvironment(t)
defer cleanup()
SetDBService(&mockDBService{db: dbConn})
defer func() {
SetDBService(nil)
cleanup()
}()
testDir := "uploads/test_diskcache_reload"
defer func() { _ = os.RemoveAll(testDir) }()
_ = os.RemoveAll(testDir)
testDir := t.TempDir()
c := New(testDir)
defer func() { _ = c.Clear() }()
@@ -28,11 +46,6 @@ func TestDiskCacheReloadConfig(t *testing.T) {
dbConn.Table("w_system_configs").Where("key = ?", "disk_cache_ttl_minutes").Update("value", "120")
dbConn.Table("w_system_configs").Where("key = ?", "disk_cache_lru_enabled").Update("value", "false")
// Invalidate Redis config cache to force DB reload
if cache.Redis != nil {
cache.Redis.Del(context.Background(), cache.PrefixedKey("system_configs"))
}
// Reload config
c.ReloadConfig(context.Background())
@@ -0,0 +1,41 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package diskcache
import (
"context"
"sync"
"gorm.io/gorm"
"Wavelet/core"
"Wavelet/core/contracts"
)
var (
dbMu sync.RWMutex
dbSvc contracts.DBService
)
// SetDBService sets the DBService instance for diskcache.
func SetDBService(s contracts.DBService) {
dbMu.Lock()
defer dbMu.Unlock()
dbSvc = s
}
func getDB(ctx context.Context) *gorm.DB {
if c, ok := ctx.(*core.Context); ok && c != nil {
if s, err := core.Inject[contracts.DBService](c); err == nil && s != nil {
return s.DB(ctx)
}
}
dbMu.RLock()
s := dbSvc
dbMu.RUnlock()
if s != nil {
return s.DB(ctx)
}
return nil
}
@@ -12,8 +12,6 @@ import (
"strings"
"time"
database "Wavelet/plugins/infra/database"
"gorm.io/gorm"
)
@@ -110,12 +108,15 @@ func LoadConfig(ctx context.Context) (Config, error) {
func loadConfigByKey(ctx context.Context, key string, fallback Config) (Config, error) {
var val string
err := database.DB(ctx).Table("w_system_configs").Where("key = ?", key).Pluck("value", &val).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fallback, nil
db := getDB(ctx)
if db != nil {
err := db.Table("w_system_configs").Where("key = ?", key).Pluck("value", &val).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fallback, nil
}
return Config{}, err
}
return Config{}, err
}
if strings.TrimSpace(val) == "" {
return fallback, nil
@@ -182,7 +183,7 @@ func SaveActiveConfig(ctx context.Context, cfg Config) error {
}
func saveSystemConfig(ctx context.Context, key string, value any, description string) error {
err := database.DB(ctx).Transaction(func(tx *gorm.DB) error {
err := getDB(ctx).Transaction(func(tx *gorm.DB) error {
return upsertSystemConfig(ctx, tx, key, value, description)
})
if err == nil && key == "storage_config" {
@@ -0,0 +1,62 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package objectstore
import (
"context"
"sync"
"gorm.io/gorm"
"Wavelet/core"
"Wavelet/core/contracts"
)
var (
dbMu sync.RWMutex
dbSvc contracts.DBService
cacheMu sync.RWMutex
cacheSvc contracts.CacheService
)
// SetDBService sets the DBService instance for objectstore.
func SetDBService(s contracts.DBService) {
dbMu.Lock()
defer dbMu.Unlock()
dbSvc = s
}
// SetCacheService sets the CacheService instance for objectstore.
func SetCacheService(s contracts.CacheService) {
cacheMu.Lock()
defer cacheMu.Unlock()
cacheSvc = s
}
func getDB(ctx context.Context) *gorm.DB {
if c, ok := ctx.(*core.Context); ok && c != nil {
if s, err := core.Inject[contracts.DBService](c); err == nil && s != nil {
return s.DB(ctx)
}
}
dbMu.RLock()
s := dbSvc
dbMu.RUnlock()
if s != nil {
return s.DB(ctx)
}
return nil
}
func getCache(ctx context.Context) contracts.CacheService {
if c, ok := ctx.(*core.Context); ok && c != nil {
if s, err := core.Inject[contracts.CacheService](c); err == nil && s != nil {
return s
}
}
cacheMu.RLock()
s := cacheSvc
cacheMu.RUnlock()
return s
}
@@ -13,10 +13,6 @@ import (
"sync"
"time"
cache "Wavelet/plugins/infra/cache"
database "Wavelet/plugins/infra/database"
"Wavelet/pkg/util"
"gorm.io/gorm"
)
@@ -28,6 +24,7 @@ const (
// Object describes a readable stored object.
type Object struct {
Key string
CachePath string
Body io.ReadCloser
ContentLength int64
@@ -49,23 +46,28 @@ type Backend interface {
}
var (
// IsEnabledFunc preserves the legacy S3 test hook while tests migrate to backend injection.
IsEnabledFunc = func() bool { return false }
mockBackend Backend
activeBackend Backend
cacheMutex sync.RWMutex
activeDriver Driver
activeBackend Backend
activeConfigJSON string
lastChecked time.Time
cacheMutex sync.RWMutex
pubSubOnce sync.Once
mockBackend Backend
// IsEnabledFunc controls whether mock/in-memory backend is activated in tests.
IsEnabledFunc = func() bool { return false }
)
// ConfigInvalidationChannel is the Redis pub/sub channel used to evict storage caches cluster-wide.
const ConfigInvalidationChannel = "storage:config_invalidation"
var pubSubOnce sync.Once
// SetMockBackend forces an in-memory/mock backend for testing.
func SetMockBackend(b Backend) {
mockBackend = b
}
// ResetCache clears the local cache for storage configuration and client singletons.
// ResetCache clears cached driver and backend instances.
func ResetCache() {
cacheMutex.Lock()
defer cacheMutex.Unlock()
@@ -77,28 +79,14 @@ func ResetCache() {
// PublishCacheInvalidation broadcasts cache eviction to all nodes in the cluster via Redis.
func PublishCacheInvalidation(ctx context.Context) {
if cache.Redis != nil {
_ = cache.Redis.Publish(ctx, ConfigInvalidationChannel, "reset").Err()
if cache := getCache(ctx); cache != nil {
_ = cache.Invalidate(ctx, ConfigInvalidationChannel)
}
ResetCache()
}
// startPubSubListener starts the background subscriber for cache invalidations.
func startPubSubListener() {
rdb := cache.Redis
if rdb == nil {
return
}
util.Go(func() {
pubsub := rdb.Subscribe(context.Background(), ConfigInvalidationChannel)
defer func() {
_ = pubsub.Close()
}()
ch := pubsub.Channel()
for range ch {
ResetCache()
}
})
}
// Active returns the configured active driver and backend, using an in-memory cache with 5s TTL.
@@ -127,9 +115,12 @@ func Active(ctx context.Context) (Driver, Backend, error) {
}
var val string
err := database.DB(ctx).Table("w_system_configs").Where("key = ?", "storage_config").Pluck("value", &val).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return "", nil, err
db := getDB(ctx)
if db != nil {
err := db.Table("w_system_configs").Where("key = ?", "storage_config").Pluck("value", &val).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return "", nil, err
}
}
sc := struct{ Value string }{Value: val}
@@ -11,9 +11,10 @@ import (
"testing"
"time"
cache "Wavelet/plugins/infra/cache"
"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
cache "Wavelet/plugins/infra/cache"
)
func TestStorageCache(t *testing.T) {
@@ -11,8 +11,9 @@ import (
"path"
"strings"
"Wavelet/pkg/httppool"
"github.com/studio-b12/gowebdav"
"Wavelet/pkg/httppool"
)
type contextTransport struct {
+31 -31
View File
@@ -6,13 +6,13 @@ package storage
import (
"context"
"errors"
"fmt"
"io"
"Wavelet/core"
"Wavelet/core/contracts"
"Wavelet/plugins/domain/upload/ingest"
uploadmodels "Wavelet/plugins/domain/upload/models"
"Wavelet/plugins/infra/storage/diskcache"
"Wavelet/plugins/infra/storage/objectstore"
)
@@ -49,6 +49,33 @@ func (p *Plugin) Name() string {
// Apply mounts the storage service into the Context.
func (p *Plugin) Apply(ctx *core.Context) error {
// Bind DBService
if db, err := core.Inject[contracts.DBService](ctx); err == nil && db != nil {
objectstore.SetDBService(db)
diskcache.SetDBService(db)
} else {
core.When[contracts.DBService](ctx, func(db contracts.DBService) {
objectstore.SetDBService(db)
diskcache.SetDBService(db)
})
}
// Bind CacheService
if cache, err := core.Inject[contracts.CacheService](ctx); err == nil && cache != nil {
objectstore.SetCacheService(cache)
} else {
core.When[contracts.CacheService](ctx, func(cache contracts.CacheService) {
objectstore.SetCacheService(cache)
})
}
ctx.OnDispose(func() error {
objectstore.SetDBService(nil)
diskcache.SetDBService(nil)
objectstore.SetCacheService(nil)
return nil
})
svc := &storageServiceImpl{
backend: p.backend,
}
@@ -116,33 +143,6 @@ func (s *storageServiceImpl) Delete(ctx context.Context, key string) error {
return b.Delete(ctx, key)
}
func (s *storageServiceImpl) Ingest(ctx context.Context, reader io.Reader, opts contracts.IngestOptions) (*contracts.IngestResult, error) {
meta := uploadmodels.UploadMetadata{
Extra: opts.Metadata,
}
req := ingest.Request{
UserID: opts.UserID,
Type: opts.Type,
FileName: opts.FileName,
MimeType: opts.MimeType,
Extension: opts.Extension,
Size: opts.Size,
Reader: reader,
Policy: ingest.Policy(opts.Policy),
Metadata: meta,
}
res, err := ingest.Ingest(ctx, req)
if err != nil {
return nil, err
}
return &contracts.IngestResult{
ID: res.Upload.ID,
Key: res.Upload.FilePath,
Created: res.Created,
Stored: res.Stored,
Resolved: res.Resolved,
}, nil
func (s *storageServiceImpl) Ingest(_ context.Context, _ io.Reader, _ contracts.IngestOptions) (*contracts.IngestResult, error) {
return nil, errors.New("storage: programmatic ingest is managed by domain/upload plugin")
}