-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathactive_replicator_common_collections.go
210 lines (179 loc) · 8.41 KB
/
active_replicator_common_collections.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
// Copyright 2023-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package db
import (
"encoding/json"
"fmt"
"github.com/couchbase/go-blip"
"github.com/couchbase/sync_gateway/base"
)
func getScopeAndCollectionName(scopeAndCollectionStr string) (base.ScopeAndCollectionName, error) {
scopeName, collectionName, err := parseScopeAndCollection(scopeAndCollectionStr)
if err != nil {
return base.ScopeAndCollectionName{}, err
} else if scopeName == nil || collectionName == nil {
return base.ScopeAndCollectionName{}, fmt.Errorf("scope and collection name must be specified: %q", scopeAndCollectionStr)
}
return base.ScopeAndCollectionName{Scope: *scopeName, Collection: *collectionName}, nil
}
// validateCollectionsConfig validates the collections config for the active replicator.
func (arc *activeReplicatorCommon) validateCollectionsConfig() error {
// ensure remote collection set is the same length as local collection set
if remoteLen := len(arc.config.CollectionsRemote); remoteLen > 0 {
if localLen := len(arc.config.CollectionsLocal); localLen != remoteLen {
return fmt.Errorf("local and remote collections must be the same length... had %d and %d", localLen, remoteLen)
}
}
// ensure channel filter set is the same length as local collection set
if collectionsChannelFilterLen := len(arc.config.CollectionsChannelFilter); collectionsChannelFilterLen > 0 {
if localLen := len(arc.config.CollectionsLocal); localLen != collectionsChannelFilterLen {
return fmt.Errorf("local collections and channel filter set must be the same length... had %d and %d", localLen, collectionsChannelFilterLen)
}
if channelFilterLen := len(arc.config.FilterChannels); channelFilterLen > 0 {
return fmt.Errorf("channel filter and collection channel filter set cannot both be set")
}
}
return nil
}
// buildGetCollectionsMessage returns a GetCollections BLIP message for the given collection names.
func (arc *activeReplicatorCommon) buildGetCollectionsMessage(remoteCollectionsKeyspaces base.ScopeAndCollectionNames) (*blip.Message, error) {
getCollectionsCheckpointIDs := make([]string, 0, len(remoteCollectionsKeyspaces))
for i := 0; i < len(remoteCollectionsKeyspaces); i++ {
getCollectionsCheckpointIDs = append(getCollectionsCheckpointIDs, arc.CheckpointID)
}
return NewGetCollectionsMessage(GetCollectionsRequestBody{
Collections: remoteCollectionsKeyspaces.ScopeAndCollectionNames(),
CheckpointIDs: getCollectionsCheckpointIDs,
})
}
// buildCollectionsSetWithExplicitMappings returns a list of local collection names, and remote collection names according to any explicit mappings set.
func (arc *activeReplicatorCommon) buildCollectionsSetWithExplicitMappings() (localCollectionsKeyspaces, remoteCollectionsKeyspaces base.ScopeAndCollectionNames, err error) {
if arc.config.CollectionsLocal != nil {
for i, localScopeAndCollection := range arc.config.CollectionsLocal {
localScopeAndCollectionName, err := getScopeAndCollectionName(localScopeAndCollection)
if err != nil {
return nil, nil, err
}
localCollectionsKeyspaces = append(localCollectionsKeyspaces, localScopeAndCollectionName)
// remap collection name to remote if set
if len(arc.config.CollectionsRemote) > 0 && arc.config.CollectionsRemote[i] != "" {
base.DebugfCtx(arc.ctx, base.KeyReplicate, "Mapping local %q to remote %q", localScopeAndCollection, arc.config.CollectionsRemote[i])
remoteScopeAndCollectionName, err := getScopeAndCollectionName(arc.config.CollectionsRemote[i])
if err != nil {
return nil, nil, err
}
remoteCollectionsKeyspaces = append(remoteCollectionsKeyspaces, remoteScopeAndCollectionName)
} else {
// no mapping set - use local collection name
remoteCollectionsKeyspaces = append(remoteCollectionsKeyspaces, localScopeAndCollectionName)
}
}
} else {
// collections to replicate wasn't set - so build a full set based on local database
for _, dbCollection := range arc.blipSyncContext.blipContextDb.CollectionByID {
localCollectionsKeyspaces = append(localCollectionsKeyspaces, base.ScopeAndCollectionName{Scope: dbCollection.ScopeName, Collection: dbCollection.Name})
remoteCollectionsKeyspaces = append(remoteCollectionsKeyspaces, base.ScopeAndCollectionName{Scope: dbCollection.ScopeName, Collection: dbCollection.Name})
}
}
return localCollectionsKeyspaces, remoteCollectionsKeyspaces, nil
}
// _initCollections will negotiate the set of collections with the peer using GetCollections and returns the set of checkpoints for each of them.
func (arc *activeReplicatorCommon) _initCollections() ([]replicationCheckpoint, error) {
if err := arc.validateCollectionsConfig(); err != nil {
return nil, err
}
localCollectionsKeyspaces, remoteCollectionsKeyspaces, err := arc.buildCollectionsSetWithExplicitMappings()
if err != nil {
return nil, err
}
msg, err := arc.buildGetCollectionsMessage(remoteCollectionsKeyspaces)
if err != nil {
return nil, err
}
if !arc.blipSender.Send(msg) {
return nil, fmt.Errorf("unable to send GetCollections message")
}
var resp []json.RawMessage
r := msg.Response()
if errDomain, ok := r.Properties[BlipErrorDomain]; ok {
errCode := r.Properties[BlipErrorCode]
if errDomain == "BLIP" && errCode == "404" {
return nil, fmt.Errorf("Remote does not support collections")
}
return nil, fmt.Errorf("Error getting collections from remote: %s %s", errDomain, errCode)
}
rBody, err := r.Body()
if err != nil {
return nil, err
}
err = base.JSONUnmarshal(rBody, &resp)
if err != nil {
return nil, fmt.Errorf("couldn't unmarshal response body: %q: %w", rBody, err)
}
blipSyncCollectionContexts := make([]*blipSyncCollectionContext, len(resp))
collectionCheckpoints := make([]replicationCheckpoint, len(resp))
for i, checkpointBody := range resp {
// json-iterator handling for nil json.RawMessage (cannot unmarshal nil)
if checkpointBody == nil {
return nil, fmt.Errorf("peer does not have collection %q", remoteCollectionsKeyspaces[i])
}
var checkpoint *replicationCheckpoint
err = base.JSONUnmarshal(checkpointBody, &checkpoint)
if err != nil {
return nil, fmt.Errorf("couldn't unmarshal checkpoint body: %q: %w", checkpointBody, err)
}
// stdlib json handling for explicit "null" json.RawMessage
if checkpoint == nil {
return nil, fmt.Errorf("peer does not have collection %q", remoteCollectionsKeyspaces[i])
}
if checkpoint.LastSeq == "" {
// collection valid but no checkpoint - start from sequence zero
checkpoint.LastSeq = CreateZeroSinceValue().String()
}
// remap back to local collection name for the active side of the replication
dbCollection, err := arc.blipSyncContext.blipContextDb.GetDatabaseCollection(localCollectionsKeyspaces[i].ScopeName(), localCollectionsKeyspaces[i].CollectionName())
if err != nil {
return nil, err
}
collectionContext := newBlipSyncCollectionContext(arc.blipSyncContext.loggingCtx, dbCollection)
blipSyncCollectionContexts[i] = collectionContext
collectionCheckpoints[i] = *checkpoint
arc.namedCollections[localCollectionsKeyspaces[i]] = &activeReplicatorCollection{
collectionIdx: base.IntPtr(i),
metadataStore: arc.config.ActiveDB.MetadataStore,
collectionDataStore: dbCollection.dataStore,
}
}
arc.blipSyncContext.collections.set(blipSyncCollectionContexts)
return collectionCheckpoints, nil
}
// forEachCollection runs the callback function for each collection on the replicator.
func (arc *activeReplicatorCommon) forEachCollection(callback func(*activeReplicatorCollection) error) error {
if arc.config.CollectionsEnabled {
for _, collection := range arc.namedCollections {
if err := callback(collection); err != nil {
return err
}
}
} else {
if err := callback(arc.defaultCollection); err != nil {
return err
}
}
return nil
}
// getFilteredChannels returns the filtered channels.
// collectionIdx can be nil if replicating without collections enabled.
func (config ActiveReplicatorConfig) getFilteredChannels(collectionIdx *int) []string {
if collectionIdx != nil {
if len(config.CollectionsChannelFilter)-1 >= *collectionIdx {
return config.CollectionsChannelFilter[*collectionIdx]
}
}
return config.FilterChannels
}