-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathword_count.go
112 lines (97 loc) · 2.49 KB
/
word_count.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 (
"context"
"fmt"
"net"
"os"
"regexp"
"strconv"
"strings"
"github.com/bcongdon/corral"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
tracing "github.com/vhive-serverless/vSwarm/utils/tracing/go"
)
type wordCount struct{}
func (w wordCount) Map(ctx context.Context, key, value string, emitter corral.Emitter) {
re := regexp.MustCompile(`[^a-zA-Z0-9\s]+`)
sanitized := strings.ToLower(re.ReplaceAllString(value, " "))
for _, word := range strings.Fields(sanitized) {
if len(word) == 0 {
continue
}
err := emitter.Emit(ctx, word, strconv.Itoa(1))
if err != nil {
fmt.Println(err)
}
}
}
func (w wordCount) Reduce(ctx context.Context, key string, values corral.ValueIterator, emitter corral.Emitter) {
count := 0
for range values.Iter() {
count++
}
err := emitter.Emit(ctx, key, strconv.Itoa(count))
if err != nil {
fmt.Println(err)
}
}
func main() {
if tracing.IsTracingEnabled() {
shutdown, err := tracing.InitBasicTracer("http://zipkin.istio-system.svc.cluster.local:9411/api/v2/spans", "producer")
if err != nil {
logrus.Fatal("Failed to initialize tracing", err)
}
defer shutdown()
}
if os.Getenv("CORRAL_DRIVER") == "1" {
driverMain()
} else {
workerMain()
}
}
func workerMain() {
job := corral.NewJob(wordCount{}, wordCount{})
options := []corral.Option{
corral.WithSplitSize(10 * 1024),
corral.WithMapBinSize(10 * 1024),
}
driver := corral.NewDriver(job, options...)
driver.Main(context.Background())
}
type server struct {
UnimplementedGreeterServer
}
func driverMain() {
port := os.Getenv("PORT")
lis, err := net.Listen("tcp", ":"+port)
if err != nil {
logrus.Fatal("Failed to listen: ", err)
}
defer lis.Close()
logrus.Infof("Listening on :%s", port)
var server server
var grpcServer *grpc.Server
if tracing.IsTracingEnabled() {
grpcServer = tracing.GetGRPCServerWithUnaryInterceptor()
} else {
grpcServer = grpc.NewServer()
}
RegisterGreeterServer(grpcServer, &server)
reflection.Register(grpcServer)
err = grpcServer.Serve(lis)
if err != nil {
logrus.Fatal("Failed to serve: ", err)
}
}
func (s *server) SayHello(ctx context.Context, req *HelloRequest) (*HelloReply, error) {
job := corral.NewJob(wordCount{}, wordCount{})
options := []corral.Option{
corral.WithSplitSize(10 * 1024),
corral.WithMapBinSize(10 * 1024),
}
driver := corral.NewDriver(job, options...)
driver.Main(ctx)
return &HelloReply{Message: fmt.Sprintf("Hello, %s!", req.Name)}, nil
}