-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
112 lines (93 loc) · 2.64 KB
/
main.go
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
package main
import (
"errors"
"flag"
"io/ioutil"
"log"
"os"
"runtime"
"strings"
"time"
"github.com/go-chef/kasoku/kasoku"
"github.com/golang/groupcache"
"bazil.org/fuse"
"bazil.org/fuse/fs"
)
var (
me = flag.String("me", "127.0.0.1:710", "The bind address for the groupcache endpoint")
peerlist = flag.String("peerlist", "", "The remote peers to attempt to form Voltron and activate incredible speed power")
mountpoint = flag.String("mountpoint", "/kasoku", "Where to mount the FUSE groupcache storage")
groupName = flag.String("groupName", "kasoku", "The name of the groupcache cluster")
cacheLimit = flag.Int64("cacheLimit", int64(1e6<<20), "Default maximum key size limit, in bytes")
target = flag.String("target", "/var/cache/cookbooks", "The source from which groupcache fetnches from")
)
// TODO(fujin): Refactor and extract.
func main() {
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU())
peers := strings.Split(*peerlist, ",")
NewHTTPPool(
*me,
peers,
)
group := NewGroup(
*groupName,
*cacheLimit,
)
if err := os.MkdirAll(*mountpoint, 0777); err != nil {
log.Fatal("could not create directory: ", *mountpoint, err)
}
conn, err := NewConn(*mountpoint)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
// TODO(fujin): Consider a better way to give the groupcache to FUSE.
td := kasoku.NewTargetDir(
*target,
group,
)
fs.Serve(conn, td)
}
// NewHTTPPool sets up a new grooupcache pool (maybe with friends)
func NewHTTPPool(me string, peerlist []string) *groupcache.HTTPPool {
pool := groupcache.NewHTTPPool(me)
// Voltron mode activate
if peerlist != nil && len(peerlist) > 0 {
pool.Set(peerlist...)
}
return pool
}
// TODO(fujin): This will likely need to understand/use go-chef/chef.
func getter(ctx groupcache.Context, key string, dest groupcache.Sink) error {
contents, err := ioutil.ReadFile(key)
dest.SetBytes(contents)
return err
}
// NewGroup compositely constructs a groupcache Group
func NewGroup(name string, cacheBytes int64) *groupcache.Group {
return groupcache.NewGroup(
name,
cacheBytes,
groupcache.GetterFunc(getter),
)
}
// NewConn returns a connection to a (named) FUSE mount.
// It's not safe to use immediately; it must be checked if it is ready
// Via the conn's Ready chan.
func NewConn(mountpoint string) (*fuse.Conn, error) {
c, err := fuse.Mount(mountpoint)
if err != nil {
log.Fatal(err)
}
maybeErr := make(chan error, 1)
go func(*fuse.Conn, chan error) {
select {
case <-c.Ready:
break
case <-time.After(time.Duration(60) * time.Millisecond):
maybeErr <- errors.New("timeout waiting for fuse to become ready")
}
}(c, maybeErr)
return c, err
}