-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathutil.go
More file actions
123 lines (103 loc) · 3.12 KB
/
util.go
File metadata and controls
123 lines (103 loc) · 3.12 KB
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
package mapreduce
import (
"fmt"
"os"
"path/filepath"
"strconv"
"sync"
"github.com/BWbwchen/MapReduce/master"
"github.com/BWbwchen/MapReduce/worker"
"github.com/spf13/cobra"
)
func ParseArg() ([]string, string, int, int, bool) {
var files []string
var nReducer int64
var nWorker int64
var plugin string
var inRAM bool
var port int64
var rootCmd = &cobra.Command{
Use: "mapreduce",
Short: "MapReduce is an easy-to-use parallel framework by Bo-Wei Chen(BWbwchen)",
Long: `MapReudce is an easy-to-use Map Reduce Go parallel-computing framework inspired by 2021 6.824 lab1.
It supports multiple workers threads on a single machine and multiple processes on a single machine right now.`,
Run: func(cmd *cobra.Command, args []string) {
tempFiles := []string{}
for _, f := range files {
// expand the file path
expandFiles, err := filepath.Glob(f)
if err != nil {
panic(err)
}
tempFiles = append(tempFiles, expandFiles...)
}
pluginFiles, err := filepath.Glob(plugin)
if err != nil {
panic(err)
} else if len(pluginFiles) == 0 {
panic("No such file")
}
plugin = pluginFiles[0]
files = tempFiles
MasterIP = ":" + strconv.Itoa(int(port))
},
}
rootCmd.PersistentFlags().StringSliceVarP(&files, "input", "i", []string{}, "Input files")
rootCmd.MarkPersistentFlagRequired("input")
rootCmd.PersistentFlags().StringVarP(&plugin, "plugin", "p", "", "Plugin .so file")
rootCmd.MarkPersistentFlagRequired("plugin")
rootCmd.PersistentFlags().Int64VarP(&nReducer, "reduce", "r", 1, "Number of Reducers")
rootCmd.PersistentFlags().Int64VarP(&nWorker, "worker", "w", 4, "Number of Workers(for master node)\nID of worker(for worker node)")
rootCmd.PersistentFlags().Int64Var(&port, "port", 10000, "Port number")
rootCmd.PersistentFlags().BoolVarP(&inRAM, "inRAM", "m", true, "Whether write the intermediate file in RAM")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
return files, plugin, int(nReducer), int(nWorker), inRAM
}
func startSingleMachineWorker(plugin string, nWorker int, nReducer int, storeInRAM bool) {
if nWorker < nReducer {
panic("Need more worker!")
}
pluginFile, _ := filepath.Abs(plugin)
var wg sync.WaitGroup
worker.Init(MasterIP)
// Start Worker
for i := 0; i < nWorker; i++ {
wg.Add(1)
go func(i0 int) {
worker.StartWorker(pluginFile, nReducer, fmt.Sprintf(":1000%v", i0+1), storeInRAM)
wg.Done()
}(i)
}
wg.Wait()
}
func startMaster(input []string, nWorker int, nReducer int) {
inputFiles := []string{}
for _, s := range input {
f, _ := filepath.Abs(s)
inputFiles = append(inputFiles, f)
}
var wg sync.WaitGroup
// Start master
// master.StartMaster(os.Args[1:], nReducer, MasterIP)
wg.Add(1)
go func() {
master.StartMaster(inputFiles, nWorker, nReducer, MasterIP)
wg.Done()
}()
wg.Wait()
}
func startWorker(plugin string, id int, nReducer int, storeInRAM bool) {
pluginFile, _ := filepath.Abs(plugin)
var wg sync.WaitGroup
worker.Init(MasterIP)
// Start Worker
wg.Add(1)
go func() {
worker.StartWorker(pluginFile, nReducer, fmt.Sprintf(":1000%v", id+1), storeInRAM)
wg.Done()
}()
wg.Wait()
}