1
+ package ossfs2
2
+
3
+ import (
4
+ "io"
5
+ "io/fs"
6
+ "os"
7
+ "path/filepath"
8
+ "k8s.io/klog/v2"
9
+ "sync"
10
+ )
11
+
12
+ // Read files in 1GB chunks to trigger caching without excessive memory usage
13
+ const readChunkSize = 1024 * 1024 * 1024
14
+ var totalBytesRead int64
15
+
16
+
17
+ func worker (id int , entryChan <- chan string , bytesReadChan chan <- int64 , wg * sync.WaitGroup ) {
18
+ defer wg .Done ()
19
+
20
+ // Function to read a single file (used if the entry is a file)
21
+ readSingleFile := func (filePath string , workerBytesRead * int64 ) error {
22
+ klog .Infof ("Worker %d: Reading single file: %s" , id , filePath )
23
+ file , err := os .Open (filePath )
24
+ if err != nil {
25
+ klog .Errorf ("Worker %d: Error opening file %s: %v" , id , filePath , err )
26
+ return err
27
+ }
28
+ defer file .Close ()
29
+
30
+ buffer := make ([]byte , readChunkSize )
31
+ for {
32
+ n , readErr := file .Read (buffer )
33
+ if n > 0 {
34
+ * workerBytesRead += int64 (n )
35
+ }
36
+ if readErr == io .EOF {
37
+ break
38
+ }
39
+ if readErr != nil {
40
+ klog .Errorf ("Worker %d: Error reading file %s: %v" , id , filePath , readErr )
41
+ return readErr
42
+ }
43
+ }
44
+ return nil
45
+ }
46
+
47
+
48
+ walkAndRead := func (startPath string , workerBytesRead * int64 ) error {
49
+ return filepath .WalkDir (startPath , func (path string , d fs.DirEntry , err error ) error {
50
+ if err != nil {
51
+ klog .Errorf ("Worker %d: Error accessing path %s: %v" , id , path , err )
52
+ return nil
53
+ }
54
+
55
+ // Skip the starting directory itself, as we're processing its contents
56
+ if path == startPath && d .IsDir () {
57
+ return nil
58
+ }
59
+
60
+ if ! d .IsDir () {
61
+ return readSingleFile (path , workerBytesRead )
62
+ } else {
63
+ klog .Infof ("Worker %d: Visiting directory: %s" , id , path )
64
+ }
65
+ return nil
66
+ })
67
+ }
68
+
69
+ for entryPath := range entryChan {
70
+ klog .Infof ("Worker %d received task for: %s" , id , entryPath )
71
+ var workerBytesRead int64
72
+
73
+ info , err := os .Stat (entryPath )
74
+ if err != nil {
75
+ klog .Errorf ("Worker %d: Error stating entry %s: %v" , id , entryPath , err )
76
+ // Report 0 bytes for this failed entry and continue
77
+ bytesReadChan <- 0
78
+ continue
79
+ }
80
+
81
+ if info .IsDir () {
82
+ // Process directory recursively
83
+ err = walkAndRead (entryPath , & workerBytesRead )
84
+ if err != nil {
85
+ klog .Errorf ("Worker %d: Error during recursive walk for %s: %v" , id , entryPath , err )
86
+ }
87
+ klog .Infof ("Worker %d finished directory task: %s (Read %d MiB)" , id , entryPath , workerBytesRead / (1024 * 1024 ))
88
+
89
+ } else {
90
+ // Process single file
91
+ err = readSingleFile (entryPath , & workerBytesRead )
92
+ if err != nil {
93
+ klog .Errorf ("Worker %d: Error reading single file %s: %v" , id , entryPath , err )
94
+ }
95
+ klog .Infof ("Worker %d finished file task: %s (Read %d MiB)" , id , entryPath , workerBytesRead / (1024 * 1024 ))
96
+ }
97
+
98
+ // Report bytes read for this task (either recursive walk or single file read)
99
+ bytesReadChan <- workerBytesRead
100
+ }
101
+
102
+ klog .Infof ("Worker %d shutting down." , id )
103
+ }
0 commit comments