aboutsummaryrefslogtreecommitdiff
path: root/weed/mount/ml_integration.go
blob: c79882c82ae4424e208e01133e0b152ba1144b42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package mount

import (
	"time"

	"github.com/hanwen/go-fuse/v2/fuse"
	"github.com/seaweedfs/seaweedfs/weed/glog"
	"github.com/seaweedfs/seaweedfs/weed/mount/ml"
	"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
	"github.com/seaweedfs/seaweedfs/weed/util/chunk_cache"
	"github.com/seaweedfs/seaweedfs/weed/wdclient"
)

// MLIntegrationManager manages ML optimization integration for the main WFS
type MLIntegrationManager struct {
	mlOptimization *ml.MLOptimization
	fuseIntegration *ml.FUSEMLIntegration
	enabled        bool
}

// NewMLIntegrationManager creates a new ML integration manager
func NewMLIntegrationManager(chunkCache chunk_cache.ChunkCache, lookupFn wdclient.LookupFileIdFunctionType) *MLIntegrationManager {
	// Create ML optimization with default config
	config := ml.DefaultMLConfig()
	mlOpt := ml.NewMLOptimization(config, chunkCache, lookupFn)
	
	// Create FUSE integration
	fuseInt := ml.NewFUSEMLIntegration(mlOpt)
	
	manager := &MLIntegrationManager{
		mlOptimization:  mlOpt,
		fuseIntegration: fuseInt,
		enabled:         true,
	}
	
	glog.V(1).Infof("ML integration manager initialized")
	return manager
}

// EnableMLOptimization enables or disables ML optimization
func (mgr *MLIntegrationManager) EnableMLOptimization(enabled bool) {
	mgr.enabled = enabled
	
	if mgr.mlOptimization != nil {
		mgr.mlOptimization.Enable(enabled)
	}
	
	if mgr.fuseIntegration != nil {
		mgr.fuseIntegration.EnableMLOptimizations(enabled)
	}
	
	glog.V(1).Infof("ML optimization %s", map[bool]string{true: "enabled", false: "disabled"}[enabled])
}

// OnFileOpen should be called when a file is opened
func (mgr *MLIntegrationManager) OnFileOpen(inode uint64, entry *filer_pb.Entry, fullPath string, flags uint32, out *fuse.OpenOut) {
	if !mgr.enabled || mgr.fuseIntegration == nil {
		return
	}
	
	mgr.fuseIntegration.OnFileOpen(inode, entry, fullPath, flags, out)
}

// OnFileClose should be called when a file is closed
func (mgr *MLIntegrationManager) OnFileClose(inode uint64) {
	if !mgr.enabled || mgr.fuseIntegration == nil {
		return
	}
	
	mgr.fuseIntegration.OnFileClose(inode)
}

// OnFileRead should be called when a file is read
func (mgr *MLIntegrationManager) OnFileRead(inode uint64, offset int64, size int) {
	if !mgr.enabled || mgr.fuseIntegration == nil {
		return
	}
	
	mgr.fuseIntegration.OnFileRead(inode, offset, size)
}

// OnChunkAccess should be called when a chunk is accessed
func (mgr *MLIntegrationManager) OnChunkAccess(inode uint64, chunkIndex uint32, fileId string, cacheLevel int, isHit bool) {
	if !mgr.enabled || mgr.fuseIntegration == nil {
		return
	}
	
	mgr.fuseIntegration.OnChunkAccess(inode, chunkIndex, fileId, cacheLevel, isHit)
}

// OptimizeAttributes applies ML-specific attribute caching
func (mgr *MLIntegrationManager) OptimizeAttributes(inode uint64, out *fuse.AttrOut) {
	if !mgr.enabled || mgr.fuseIntegration == nil {
		return
	}
	
	mgr.fuseIntegration.OptimizeAttributes(inode, out)
}

// OptimizeEntryCache applies ML-specific entry caching
func (mgr *MLIntegrationManager) OptimizeEntryCache(inode uint64, entry *filer_pb.Entry, out *fuse.EntryOut) {
	if !mgr.enabled || mgr.fuseIntegration == nil {
		return
	}
	
	mgr.fuseIntegration.OptimizeEntryCache(inode, entry, out)
}

// ShouldEnableWriteback determines if writeback should be enabled for a file
func (mgr *MLIntegrationManager) ShouldEnableWriteback(inode uint64, entry *filer_pb.Entry) bool {
	if !mgr.enabled || mgr.fuseIntegration == nil {
		return false
	}
	
	return mgr.fuseIntegration.ShouldEnableWriteback(inode, entry)
}

// GetComprehensiveMetrics returns all ML optimization metrics
func (mgr *MLIntegrationManager) GetComprehensiveMetrics() *ml.FUSEMLMetrics {
	if !mgr.enabled || mgr.fuseIntegration == nil {
		return &ml.FUSEMLMetrics{}
	}
	
	metrics := mgr.fuseIntegration.GetOptimizationMetrics()
	return &metrics
}

// IsEnabled returns whether ML optimization is enabled
func (mgr *MLIntegrationManager) IsEnabled() bool {
	return mgr.enabled
}

// Shutdown gracefully shuts down the ML integration
func (mgr *MLIntegrationManager) Shutdown() {
	glog.V(1).Infof("Shutting down ML integration manager...")
	
	if mgr.fuseIntegration != nil {
		mgr.fuseIntegration.Shutdown()
	}
	
	glog.V(1).Infof("ML integration manager shutdown complete")
}