Skip to content

Commit 0fc06da

Browse files
committed
Added initial Replication support
1 parent 6a26595 commit 0fc06da

7 files changed

+216
-0
lines changed

client.go

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ type Client interface {
5757

5858
// Server/cluster administration functions
5959
ClientServerAdmin
60+
61+
// Replication functions
62+
ClientReplication
6063
}
6164

6265
// ClientConfig contains all settings needed to create a client.

client_replication.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package driver
24+
25+
// ClientReplication provides methods needed to access replication functionality from a client.
26+
type ClientReplication interface {
27+
// Replication provides access to replication specific operations.
28+
Replication() Replication
29+
}

client_replication_impl.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package driver
24+
25+
// Replication provides access to replication specific operations.
26+
func (c *client) Replication() Replication {
27+
return c
28+
}

context.go

+14
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const (
5454
keyEnforceReplicationFactor ContextKey = "arangodb-enforceReplicationFactor"
5555
keyConfigured ContextKey = "arangodb-configured"
5656
keyFollowLeaderRedirect ContextKey = "arangodb-followLeaderRedirect"
57+
keyDBServerID ContextKey = "arangodb-dbserverID"
5758
)
5859

5960
// WithRevision is used to configure a context to make document
@@ -200,6 +201,11 @@ func WithFollowLeaderRedirect(parent context.Context, value bool) context.Contex
200201
return context.WithValue(contextOrBackground(parent), keyFollowLeaderRedirect, value)
201202
}
202203

204+
// WithDBServerID is used to configure a context that includes an ID of a specific DBServer.
205+
func WithDBServerID(parent context.Context, id string) context.Context {
206+
return context.WithValue(contextOrBackground(parent), keyDBServerID, id)
207+
}
208+
203209
type contextSettings struct {
204210
Silent bool
205211
WaitForSync bool
@@ -214,6 +220,7 @@ type contextSettings struct {
214220
EnforceReplicationFactor *bool
215221
Configured *bool
216222
FollowLeaderRedirect *bool
223+
DBServerID string
217224
}
218225

219226
// applyContextSettings returns the settings configured in the context in the given request.
@@ -327,6 +334,13 @@ func applyContextSettings(ctx context.Context, req Request) contextSettings {
327334
result.FollowLeaderRedirect = &followLeaderRedirect
328335
}
329336
}
337+
// DBServerID
338+
if v := ctx.Value(keyDBServerID); v != nil {
339+
if id, ok := v.(string); ok {
340+
req.SetQuery("DBserver", id)
341+
result.DBServerID = id
342+
}
343+
}
330344
return result
331345
}
332346

replication.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package driver
24+
25+
import (
26+
"context"
27+
)
28+
29+
// Replication provides access to replication related operations.
30+
type Replication interface {
31+
// Get the inventory of the server containing all collections (with entire details) of a database.
32+
// When this function is called on a coordinator is a cluster, an ID of a DBServer must be provided
33+
// using a context that is prepare with `WithDBServerID`.
34+
DatabaseInventory(ctx context.Context, db Database) (DatabaseInventory, error)
35+
}

replication_impl.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package driver
24+
25+
import (
26+
"context"
27+
"path"
28+
)
29+
30+
// Get the inventory of a server containing all collections (with entire details) of a database.
31+
func (c *client) DatabaseInventory(ctx context.Context, db Database) (DatabaseInventory, error) {
32+
req, err := c.conn.NewRequest("GET", path.Join("_db", db.Name(), "_api/replication/inventory"))
33+
if err != nil {
34+
return DatabaseInventory{}, WithStack(err)
35+
}
36+
applyContextSettings(ctx, req)
37+
resp, err := c.conn.Do(ctx, req)
38+
if err != nil {
39+
return DatabaseInventory{}, WithStack(err)
40+
}
41+
if err := resp.CheckStatus(200); err != nil {
42+
return DatabaseInventory{}, WithStack(err)
43+
}
44+
var result DatabaseInventory
45+
if err := resp.ParseBody("", &result); err != nil {
46+
return DatabaseInventory{}, WithStack(err)
47+
}
48+
return result, nil
49+
}

test/replication_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package test
24+
25+
import (
26+
"context"
27+
"testing"
28+
)
29+
30+
// TestReplicationDatabaseInventory tests the Replication.DatabaseInventory method.
31+
func TestReplicationDatabaseInventory(t *testing.T) {
32+
ctx := context.Background()
33+
c := createClientFromEnv(t, true)
34+
rep := c.Replication()
35+
db, err := c.Database(ctx, "_system")
36+
if err != nil {
37+
t.Fatalf("Failed to open _system database: %s", describe(err))
38+
}
39+
inv, err := rep.DatabaseInventory(ctx, db)
40+
if err != nil {
41+
t.Fatalf("DatabaseInventory failed: %s", describe(err))
42+
}
43+
if len(inv.Collections) == 0 {
44+
t.Error("Expected multiple collections, got 0")
45+
}
46+
foundSystemCol := false
47+
for _, col := range inv.Collections {
48+
if col.Parameters.Name == "" {
49+
t.Error("Expected non-empty name")
50+
}
51+
if col.Parameters.IsSystem {
52+
foundSystemCol = true
53+
}
54+
}
55+
if !foundSystemCol {
56+
t.Error("Expected multiple system collections, found none")
57+
}
58+
}

0 commit comments

Comments
 (0)