forked from tomheon/kafkafs
/
kafkafsmount.go
72 lines (56 loc) · 1.84 KB
/
kafkafsmount.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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
"time"
"github.com/Shopify/sarama"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
"github.com/tomheon/kafkafs/kafkafs"
)
func unmountOnInt(c chan os.Signal, server *fuse.Server, client *sarama.Client) {
s := <-c
fmt.Println("Got signal:", s)
server.Unmount()
client.Close()
}
func main() {
var commandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
var kafkaAddrs = commandLine.String("kafkaAddrs", "localhost:9092",
"Kafka server addresses host1:port1[;host2:port2...]")
var metadataRetries = commandLine.Int("metadataRetries", 10,
"Max times to attempt metadata refresh from Kafka before failing")
var waitForElectionMs = commandLine.Int("waitForElectionMs", 250,
"Max milliseconds to wait for Kafka leader election before failing")
var maxMsgBytes = commandLine.Int("maxMsgBytes", 1024*1024*10,
"Max bytes to pull for a single message")
commandLine.Parse(os.Args[1:])
commandLine.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: kafkafs [options] mountpoint\n")
commandLine.PrintDefaults()
os.Exit(2)
}
if commandLine.NArg() != 1 {
commandLine.Usage()
}
mountpoint := commandLine.Arg(0)
addrs := strings.Split(*kafkaAddrs, ";")
client, err := sarama.NewClient("kafkafs", addrs,
&sarama.ClientConfig{MetadataRetries: *metadataRetries,
WaitForElection: time.Duration(*waitForElectionMs) * time.Millisecond})
if err != nil {
log.Fatalf("Error from client %s", err)
}
kClient := kafkafs.NewKafkaClient(client, int32(*maxMsgBytes))
nfs := pathfs.NewPathNodeFs(kafkafs.NewKafkaRoFs(kClient), nil)
server, _, err := nodefs.MountFileSystem(mountpoint, nfs, nil)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go unmountOnInt(c, server, client)
server.Serve()
}