Skip to content

Commit 77de3ae

Browse files
committed
Add v2 test for getting collection shards
1 parent 2187db4 commit 77de3ae

File tree

7 files changed

+172
-27
lines changed

7 files changed

+172
-27
lines changed

test/collection_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -966,8 +966,8 @@ func TestCollectionWriteConcernSetPropInvalid(t *testing.T) {
966966
minRepl, prop.WriteConcern)
967967
}
968968

969-
// TestCollectionShards creates a collection and gets the shards' information.
970-
func TestCollectionShards(t *testing.T) {
969+
// Test_CollectionShards creates a collection and gets the shards' information.
970+
func Test_CollectionShards(t *testing.T) {
971971
if getTestMode() != testModeCluster {
972972
t.Skipf("Not a cluster mode")
973973
}
@@ -990,5 +990,5 @@ func TestCollectionShards(t *testing.T) {
990990
require.Lenf(t, dbServers, 2, "expected 2 DB servers for the shard")
991991
leaders = append(leaders, dbServers[0])
992992
}
993-
assert.NotEqualf(t, leaders[0], leaders[1], "the leader can not be on the same server")
993+
assert.NotEqualf(t, leaders[0], leaders[1], "the leader shard can not be on the same server")
994994
}

v2/arangodb/collection.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2020 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2020-2021 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
2020
// Author Adam Janikowski
21+
// Author Tomasz Mielech
2122
//
2223

2324
package arangodb
@@ -28,6 +29,9 @@ type Collection interface {
2829
Name() string
2930
Database() Database
3031

32+
// Shards fetches shards information of the collection.
33+
Shards(ctx context.Context, details bool) (CollectionShards, error)
34+
3135
// Remove removes the entire collection.
3236
// If the collection does not exist, a NotFoundError is returned.
3337
Remove(ctx context.Context) error

v2/arangodb/collection_impl.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2020 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2020-2021 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
2020
// Author Adam Janikowski
21+
// Author Tomasz Mielech
2122
//
2223

2324
package arangodb
@@ -26,8 +27,9 @@ import (
2627
"context"
2728
"net/http"
2829

29-
"github.com/arangodb/go-driver/v2/arangodb/shared"
30+
"github.com/pkg/errors"
3031

32+
"github.com/arangodb/go-driver/v2/arangodb/shared"
3133
"github.com/arangodb/go-driver/v2/connection"
3234
)
3335

@@ -104,3 +106,24 @@ func (c collection) connection() connection.Connection {
104106
func (c collection) url(api string, parts ...string) string {
105107
return c.db.url(append([]string{"_api", api, c.name}, parts...)...)
106108
}
109+
110+
// Shards fetches shards information of the collection.
111+
func (c *collection) Shards(ctx context.Context, details bool) (CollectionShards, error) {
112+
var body struct {
113+
shared.ResponseStruct `json:",inline"`
114+
CollectionShards `json:",inline"`
115+
}
116+
117+
resp, err := connection.CallGet(ctx, c.connection(), c.url("collection", "shards"), &body,
118+
connection.WithQuery("details", "true"))
119+
if err != nil {
120+
return CollectionShards{}, errors.WithStack(err)
121+
}
122+
123+
switch code := resp.Code(); code {
124+
case http.StatusOK:
125+
return body.CollectionShards, nil
126+
default:
127+
return CollectionShards{}, body.AsArangoErrorWithCode(code)
128+
}
129+
}

v2/arangodb/collection_opts.go

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2017-2021 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
2020
// Author Ewout Prangsma
21+
// Author Tomasz Mielech
2122
//
2223

2324
package arangodb
@@ -26,7 +27,7 @@ import (
2627
"time"
2728
)
2829

29-
// CollectionInfo contains information about a collection
30+
// CollectionInfo contains basic information about a collection.
3031
type CollectionInfo struct {
3132
// The identifier of the collection.
3233
ID string `json:"id,omitempty"`
@@ -42,17 +43,10 @@ type CollectionInfo struct {
4243
GloballyUniqueId string `json:"globallyUniqueId,omitempty"`
4344
}
4445

45-
// CollectionProperties contains extended information about a collection.
46-
type CollectionProperties struct {
46+
// CollectionExtendedInfo contains extended information about a collection.
47+
type CollectionExtendedInfo struct {
4748
CollectionInfo
48-
49-
// WaitForSync; If true then creating, changing or removing documents will wait until the data has been synchronized to disk.
50-
WaitForSync bool `json:"waitForSync,omitempty"`
51-
// DoCompact specifies whether or not the collection will be compacted.
52-
DoCompact bool `json:"doCompact,omitempty"`
53-
// JournalSize is the maximal size setting for journals / datafiles in bytes.
54-
JournalSize int64 `json:"journalSize,omitempty"`
55-
// CacheEnabled set cacheEnabled option in collection properties
49+
// CacheEnabled set cacheEnabled option in collection properties.
5650
CacheEnabled bool `json:"cacheEnabled,omitempty"`
5751
KeyOptions struct {
5852
// Type specifies the type of the key generator. The currently available generators are traditional and autoincrement.
@@ -62,29 +56,41 @@ type CollectionProperties struct {
6256
// the _key attribute of documents is considered an error.
6357
AllowUserKeys bool `json:"allowUserKeys,omitempty"`
6458
} `json:"keyOptions,omitempty"`
59+
// Deprecated: use 'WriteConcern' instead.
60+
MinReplicationFactor int `json:"minReplicationFactor,omitempty"`
6561
// NumberOfShards is the number of shards of the collection.
6662
// Only available in cluster setup.
6763
NumberOfShards int `json:"numberOfShards,omitempty"`
64+
// This attribute specifies the name of the sharding strategy to use for the collection.
65+
// Can not be changed after creation.
66+
ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"`
6867
// ShardKeys contains the names of document attributes that are used to determine the target shard for documents.
6968
// Only available in cluster setup.
7069
ShardKeys []string `json:"shardKeys,omitempty"`
7170
// ReplicationFactor contains how many copies of each shard are kept on different DBServers.
7271
// Only available in cluster setup.
7372
ReplicationFactor int `json:"replicationFactor,omitempty"`
74-
// Deprecated: use 'WriteConcern' instead
75-
MinReplicationFactor int `json:"minReplicationFactor,omitempty"`
73+
// WaitForSync; If true then creating, changing or removing documents will wait
74+
// until the data has been synchronized to disk.
75+
WaitForSync bool `json:"waitForSync,omitempty"`
7676
// WriteConcern contains how many copies must be available before a collection can be written.
7777
// It is required that 1 <= WriteConcern <= ReplicationFactor.
7878
// Default is 1. Not available for satellite collections.
7979
// Available from 3.6 arangod version.
8080
WriteConcern int `json:"writeConcern,omitempty"`
81+
}
82+
83+
// CollectionProperties contains extended information about a collection.
84+
type CollectionProperties struct {
85+
CollectionExtendedInfo
86+
// DoCompact specifies whether or not the collection will be compacted.
87+
DoCompact bool `json:"doCompact,omitempty"`
88+
// JournalSize is the maximal size setting for journals / datafiles in bytes.
89+
JournalSize int64 `json:"journalSize,omitempty"`
8190
// SmartJoinAttribute
8291
// See documentation for smart joins.
8392
// This requires ArangoDB Enterprise Edition.
8493
SmartJoinAttribute string `json:"smartJoinAttribute,omitempty"`
85-
// This attribute specifies the name of the sharding strategy to use for the collection.
86-
// Can not be changed after creation.
87-
ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"`
8894
// This attribute specifies that the sharding of a collection follows that of another
8995
// one.
9096
DistributeShardsLike string `json:"distributeShardsLike,omitempty"`
@@ -213,3 +219,18 @@ type CollectionStatistics struct {
213219
} `json:"revisions"`
214220
} `json:"figures"`
215221
}
222+
223+
// CollectionShards contains shards information about a collection.
224+
type CollectionShards struct {
225+
CollectionExtendedInfo
226+
// Set to create a smart edge or vertex collection.
227+
// This requires ArangoDB Enterprise Edition.
228+
IsSmart bool `json:"isSmart,omitempty"`
229+
230+
// Shards is a list of shards that belong to the collection.
231+
// Each shard contains a list of DB servers where the first one is the leader and the rest are followers.
232+
Shards map[ShardID][]ServerID `json:"shards,omitempty"`
233+
234+
// StatusString represents status as a string.
235+
StatusString string `json:"statusString,omitempty"`
236+
}

v2/arangodb/meta.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2020 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2020-2021 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
2020
// Author Adam Janikowski
21+
// Author Tomasz Mielech
2122
//
2223

2324
package arangodb
@@ -27,6 +28,12 @@ import (
2728
"github.com/pkg/errors"
2829
)
2930

31+
// ShardID is an internal identifier of a specific shard.
32+
type ShardID string
33+
34+
// ServerID identifies an ArangoDB server in a cluster.
35+
type ServerID string
36+
3037
type DocumentID string
3138

3239
// DocumentMeta contains all meta data used to identifier a document.

v2/tests/database_collection_operations_test.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2020 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2020-2021 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
2020
// Author Adam Janikowski
21+
// Author Tomasz Mielech
2122
//
2223

2324
package tests
@@ -28,12 +29,42 @@ import (
2829
"testing"
2930
"time"
3031

31-
"github.com/arangodb/go-driver/v2/arangodb"
32-
"github.com/arangodb/go-driver/v2/arangodb/shared"
3332
"github.com/google/uuid"
33+
"github.com/stretchr/testify/assert"
3434
"github.com/stretchr/testify/require"
35+
36+
"github.com/arangodb/go-driver/v2/arangodb"
37+
"github.com/arangodb/go-driver/v2/arangodb/shared"
3538
)
3639

40+
// Test_CollectionShards creates a collection and gets the shards' information.
41+
func Test_CollectionShards(t *testing.T) {
42+
requireClusterMode(t)
43+
44+
options := arangodb.CreateCollectionOptions{
45+
ReplicationFactor: 2,
46+
NumberOfShards: 2,
47+
}
48+
49+
Wrap(t, func(t *testing.T, client arangodb.Client) {
50+
WithDatabase(t, client, nil, func(db arangodb.Database) {
51+
WithCollection(t, db, &options, func(col arangodb.Collection) {
52+
shards, err := col.Shards(context.Background(), true)
53+
require.NoError(t, err)
54+
55+
require.Len(t, shards.Shards, 2, "expected 2 shards")
56+
var leaders []arangodb.ServerID
57+
58+
for _, dbServers := range shards.Shards {
59+
require.Lenf(t, dbServers, 2, "expected 2 DB servers for the shard")
60+
leaders = append(leaders, dbServers[0])
61+
}
62+
assert.NotEqualf(t, leaders[0], leaders[1], "the leader shard can not be on the same server")
63+
})
64+
})
65+
})
66+
}
67+
3768
func Test_DatabaseCollectionOperations(t *testing.T) {
3869

3970
Wrap(t, func(t *testing.T, client arangodb.Client) {

v2/tests/util_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2021 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 Tomasz Mielech
21+
//
22+
23+
package tests
24+
25+
import (
26+
"os"
27+
"strings"
28+
"testing"
29+
)
30+
31+
func getTestMode() string {
32+
return strings.TrimSpace(os.Getenv("TEST_MODE"))
33+
}
34+
35+
type mode string
36+
37+
const (
38+
testModeCluster mode = "cluster"
39+
testModeResilientSingle mode = "resilientsingle"
40+
testModeSingle mode = "single"
41+
)
42+
43+
func requireMode(t *testing.T, mode mode) {
44+
if getTestMode() != string(mode) {
45+
t.Skipf("the test requires %s mode", mode)
46+
}
47+
}
48+
49+
func requireClusterMode(t *testing.T) {
50+
requireMode(t, testModeCluster)
51+
}
52+
53+
func requireSingleMode(t *testing.T) {
54+
requireMode(t, testModeSingle)
55+
}
56+
57+
func requireResilientSingleMode(t *testing.T) {
58+
requireMode(t, testModeResilientSingle)
59+
}

0 commit comments

Comments
 (0)