forked from couchbase/indexing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.go
231 lines (196 loc) · 4.73 KB
/
feed.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"net/http"
_ "net/http/pprof"
"net/url"
"os"
"runtime/pprof"
"strconv"
"strings"
"time"
"github.com/couchbase/indexing/secondary/dcp"
mcd "github.com/couchbase/indexing/secondary/dcp/transport"
mc "github.com/couchbase/indexing/secondary/dcp/transport/client"
)
const clusterAddr = "http://localhost:9000"
var options struct {
clusterAddr string
bucket string
maxVb int
tick int
debug bool
closeVbs []string
cpuprofile string
memprofile string
}
func argParse() {
var closeVbs string
flag.StringVar(&options.bucket, "bucket", "default",
"bucket to connect to")
flag.IntVar(&options.maxVb, "maxvb", 1024,
"number configured vbuckets")
flag.IntVar(&options.tick, "tick", 1000,
"timer tick in mS to log information")
flag.StringVar(&closeVbs, "close", "",
"comma separated list of vbucket numbers")
flag.BoolVar(&options.debug, "debug", false,
"number configured vbuckets")
flag.StringVar(&options.cpuprofile, "cpuprofile", "",
"write cpu profile to file")
flag.StringVar(&options.memprofile, "memprofile", "",
"write memory profile to this file")
flag.Parse()
args := flag.Args()
if len(args) < 1 {
options.clusterAddr = clusterAddr
} else {
options.clusterAddr = args[0]
}
options.closeVbs = strings.Split(closeVbs, ",")
}
func usage() {
fmt.Fprintf(os.Stderr,
"%v [flags] http://user:pass@host:8091/\n\nFlags:\n",
os.Args[0])
flag.PrintDefaults()
os.Exit(64)
}
// Flush the bucket before trying this program
func main() {
argParse()
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
if options.cpuprofile != "" {
f, err := os.Create(options.cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if options.memprofile != "" {
f, err := os.Create(options.memprofile)
if err != nil {
log.Fatal(err)
}
defer pprof.WriteHeapProfile(f)
defer f.Close()
}
bucket := getBucket()
//addKVset(bucket, 1000)
//return
// start dcp feed
dcpConfig := map[string]interface{}{
"genChanSize": 10000,
"dataChanSize": 10000,
"numConnections": 4,
}
name := couchbase.NewDcpFeedName(fmt.Sprintf("%v", time.Now().UnixNano()))
feed, err := bucket.StartDcpFeed(name, 0, 0xABCD, dcpConfig)
if err != nil {
log.Print(" Failed to start stream ", err)
return
}
opaque := uint16(10)
// request stream for all vbuckets
for i := 0; i < options.maxVb; i++ {
err := feed.DcpRequestStream(
uint16(i) /*vbno*/, opaque, 0 /*flag*/, 0, /*vbuuid*/
0 /*seqStart*/, 0xFFFFFFFFFFFFFFFF /*seqEnd*/, 0 /*snaps*/, 0)
if err != nil {
fmt.Printf("%s", err.Error())
}
}
// observe the mutations from the channel.
events(feed, 2000)
opaque += 1
if len(options.closeVbs) > 0 {
for _, vb := range options.closeVbs {
if len(vb) > 0 {
vbno, err := strconv.Atoi(vb)
if err != nil {
log.Fatal(err)
}
if err := feed.DcpCloseStream(uint16(vbno), opaque); err != nil {
log.Printf("error while closing stream %d: %v", vbno, err)
}
}
}
}
events(feed, 100000)
feed.Close()
}
func events(feed *couchbase.DcpFeed, timeoutMs int) {
var timeout <-chan time.Time
mutations := 0
done := true
tick := time.Tick(time.Duration(options.tick) * time.Millisecond)
if timeoutMs > 0 {
timeout = time.Tick(time.Duration(timeoutMs) * time.Millisecond)
}
loop:
for {
select {
case e := <-feed.C:
if e.Opcode == mcd.DCP_MUTATION {
mutations += 1
} else {
log.Printf("Received {%s, %d(vb), %d(opq), %s}\n",
e.Opcode, e.VBucket, e.Opaque, e.Status)
}
handleEvent(e)
done = false
case <-tick:
log.Printf("Mutation count %d", mutations)
if timeout == nil && done {
break loop
}
done = true
case <-timeout:
break loop
}
}
}
func handleEvent(e *mc.DcpEvent) {
if e.Opcode == mcd.DCP_MUTATION && options.debug {
log.Printf("got mutation %s", e.Value)
}
}
func addKVset(b *couchbase.Bucket, count int) {
for i := 0; i < count; i++ {
key := fmt.Sprintf("key%v", i+1000000)
val_len := rand.Intn(10*1024) + rand.Intn(10*1024)
value := fmt.Sprintf("This is a test key %d", val_len)
err := b.Set(key, 0, value)
if err != nil {
panic(err)
}
if i%100000 == 0 {
fmt.Printf("\n Added %d keys", i)
}
}
}
func getBucket() *couchbase.Bucket {
u, err := url.Parse(options.clusterAddr)
mf(err, "parse")
if options.bucket == "" && u.User != nil {
options.bucket = u.User.Username()
}
c, err := couchbase.Connect(u.String())
mf(err, "connect - "+u.String())
p, err := c.GetPool("default")
mf(err, "pool")
bucket, err := p.GetBucket(options.bucket)
mf(err, "bucket")
return bucket
}
func mf(err error, msg string) {
if err != nil {
log.Fatalf("%v: %v", msg, err)
}
}