/
main.go
147 lines (135 loc) · 4.06 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
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
// Copyright (c) 2013 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 (
"flag"
"github.com/couchbaselabs/indexing/api"
imclient "github.com/couchbaselabs/indexing/index_manager/client"
ast "github.com/couchbaselabs/tuqtng/ast"
"github.com/prataprc/go-couchbase"
"log"
)
// TODO:
// [1] the node in which router runs will have to be mentioned in indexinfo
// structure. once that is available change the projector accordingly
//
// 1. Automatic reconnect when indexer fails and restarts.
// 2. Randomly kill projector and restart must work from where the vuuid,seqno
// was left off.
// 3. Try CREATE and DROP index multiple times.
var options struct {
kvhost string
imhost string
inhost string // TODO: [1]
nconn int
proto string
}
const (
GETSEQUENCE_VECTOR string = "MutationManager.GetSequenceVectors"
PROCESS_1MUTATION string = "MutationManager.ProcessSingleMutation"
DEFAULT_NCONN int = 8
)
type bucketMeta struct {
vector api.SequenceVector
indexMap map[string]*api.IndexInfo
indexExprs map[string][]ast.Expression
}
type bucketMap map[string]*bucketMeta
// Supervisor messages
type ImNotify struct {
serverUuid string
}
type ExitRoutine struct {
kill chan bool
err error
}
type projectorInfo struct {
imanager *imclient.RestClient
serverUuid string
buckets bucketMap
}
type streamer interface {
openFeed(api.SequenceVector) error
closeFeed()
}
func main() {
argParse()
// Couchbase client, pool and default bucket
couch, err := couchbase.Connect("http://" + options.kvhost)
if err != nil {
log.Fatalf("Error connecting: %v", err)
}
pool, err := couch.GetPool("default")
if err != nil {
log.Fatalf("Error getting pool: %v", err)
}
p := &projectorInfo{
imanager: imclient.NewRestClient("http://" + options.imhost),
}
superch := make(chan interface{})
for {
workers := make([]chan bool, 0)
if p.serverUuid, err = p.getMetaData(); err != nil {
continue
}
for bucket, bmeta := range p.buckets {
bw := NewBucketWorker(BucketWorkerCmd{
client: couch,
pool: &pool,
bucketname: bucket,
bmeta: bmeta,
nconn: options.nconn,
rpcurl: options.inhost,
})
// Workers to push mutations to indexer through parallel connection
for _, iclients := range bw.mclients {
for _, s := range iclients {
killch := make(chan bool)
go push2Indexer(s, superch, killch)
workers = append(workers, killch)
}
}
// Workers to gather UprEvents from upr-feed.
killch := make(chan bool)
go bw.run(superch, killch)
workers = append(workers, killch)
}
go p.waitNotify(p.serverUuid, superch)
select {
case msg, _ := <-superch:
if notify, ok := msg.(ImNotify); ok {
p.serverUuid = notify.serverUuid
} else if exit, ok := msg.(ExitRoutine); ok {
log.Println(exit.err)
} else {
panic("Unknown supervisor message")
}
}
// Kill all workers
for _, ch := range workers {
close(ch)
}
}
// TODO: index_manager/client/client.go should implement Close() api.
// p.imanager.Close()
p.close()
}
func argParse() {
flag.StringVar(&options.kvhost, "kvhost", "localhost:11211",
"Port to connect to kv-cluster")
flag.StringVar(&options.inhost, "inhost", "localhost:8096",
"Port to connect to indexer node") // TODO [1]
flag.StringVar(&options.imhost, "imhost", "localhost:8094",
"Port to connect to index-manager node")
flag.StringVar(&options.proto, "proto", "upr",
"Use either `tap` or `upr`")
flag.IntVar(&options.nconn, "nconn", DEFAULT_NCONN,
"Number of indexer (rpc) connections ber bucket")
flag.Parse()
}