forked from couchbase/cbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed_dcp.go
140 lines (120 loc) · 3.21 KB
/
feed_dcp.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
// Copyright (c) 2014 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an "AS
// IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language
// governing permissions and limitations under the License.
package main
import (
"fmt"
"github.com/couchbase/gomemcached"
log "github.com/couchbaselabs/clog"
"github.com/couchbaselabs/go-couchbase"
)
type DCPFeed struct {
name string
url string
poolName string
bucketName string
bucketUUID string
streams map[string]Stream
closeCh chan bool
}
func NewDCPFeed(name, url, poolName, bucketName, bucketUUID string,
streams map[string]Stream) (*DCPFeed, error) {
rv := DCPFeed{
name: name,
url: url,
poolName: poolName,
bucketName: bucketName,
bucketUUID: bucketUUID,
streams: streams,
closeCh: make(chan bool),
}
return &rv, nil
}
func (t *DCPFeed) Name() string {
return t.name
}
func (t *DCPFeed) Start() error {
log.Printf("DCPFeed.Start, name: %s", t.Name())
go ExponentialBackoffLoop(t.Name(),
func() int {
progress, err := t.feed()
if err != nil {
log.Printf("DCPFeed name: %s, progress: %d, err: %v",
t.Name(), progress, err)
}
return progress
},
FEED_SLEEP_INIT_MS, // Milliseconds.
FEED_BACKOFF_FACTOR, // Backoff.
FEED_SLEEP_MAX_MS)
return nil
}
func (t *DCPFeed) feed() (int, error) {
// TODO: Check closeCh.
bucket, err := couchbase.GetBucket(t.url, t.poolName, t.bucketName)
if err != nil {
return 0, err
}
defer bucket.Close()
if t.bucketUUID != "" && t.bucketUUID != bucket.UUID {
bucket.Close()
return -1, fmt.Errorf("mismatched bucket uuid,"+
"bucketName: %s, bucketUUID: %s, bucket.UUID: %s",
t.bucketName, t.bucketUUID, bucket.UUID)
}
// TODO: See if UprFeed name is important.
feed, err := bucket.StartUprFeed("index" /*name*/, 0)
if err != nil {
return 0, err
}
defer feed.Close()
err = feed.UprRequestStream(
uint16(0), /*vbno - TODO: use the vbno's*/
uint32(0), /*opaque*/
0, /*flag*/
0, /*vbuuid*/
0, /*seqStart - TODO: use the seqno's*/
0xFFFFFFFFFFFFFFFF, /*seqEnd*/
0, /*snaps*/
0)
if err != nil {
return 0, err
}
for {
select {
case <-t.closeCh:
return -1, nil
case uprEvent, ok := <-feed.C:
if !ok {
break
}
if uprEvent.Opcode == gomemcached.UPR_MUTATION {
// TODO: Handle dispatch to streams correctly.
t.streams[""] <- &StreamUpdate{
id: uprEvent.Key,
body: uprEvent.Value,
}
} else if uprEvent.Opcode == gomemcached.UPR_DELETION {
// TODO: Handle dispatch to streams correctly.
t.streams[""] <- &StreamDelete{
id: uprEvent.Key,
}
}
}
}
return 1, nil
}
func (t *DCPFeed) Close() error {
close(t.closeCh)
return nil
}
func (t *DCPFeed) Streams() map[string]Stream {
return t.streams
}