Skip to content

Commit 09361c8

Browse files
committed
Get shards information
1 parent c680b93 commit 09361c8

File tree

6 files changed

+164
-9
lines changed

6 files changed

+164
-9
lines changed

collection.go

Lines changed: 63 additions & 2 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 driver
@@ -55,10 +56,13 @@ type Collection interface {
5556
// SetProperties changes properties of the collection.
5657
SetProperties(ctx context.Context, options SetCollectionPropertiesOptions) error
5758

59+
// Shards fetches shards information of the collection.
60+
Shards(ctx context.Context, details bool) (CollectionShards, error)
61+
5862
// Load the collection into memory.
5963
Load(ctx context.Context) error
6064

61-
// UnLoad the collection from memory.
65+
// Unload unloads the collection from memory.
6266
Unload(ctx context.Context) error
6367

6468
// Remove removes the entire collection.
@@ -262,3 +266,60 @@ type CollectionStatistics struct {
262266
} `json:"revisions"`
263267
} `json:"figures"`
264268
}
269+
270+
// CollectionShards contains shards information about a collection.
271+
type CollectionShards struct {
272+
CollectionInfo
273+
274+
// CacheEnabled set cacheEnabled option in collection properties
275+
CacheEnabled bool `json:"cacheEnabled,omitempty"`
276+
277+
// Set to create a smart edge or vertex collection.
278+
// This requires ArangoDB Enterprise Edition.
279+
IsSmart bool `json:"isSmart,omitempty"`
280+
281+
KeyOptions struct {
282+
// Type specifies the type of the key generator. The currently available generators are traditional and autoincrement.
283+
Type KeyGeneratorType `json:"type,omitempty"`
284+
// AllowUserKeys; if set to true, then it is allowed to supply own key values in the _key attribute of a document.
285+
// If set to false, then the key generator is solely responsible for generating keys and supplying own key values in
286+
// the _key attribute of documents is considered an error.
287+
AllowUserKeys bool `json:"allowUserKeys,omitempty"`
288+
} `json:"keyOptions,omitempty"`
289+
290+
// Deprecated: use 'WriteConcern' instead.
291+
MinReplicationFactor int `json:"minReplicationFactor,omitempty"`
292+
293+
// NumberOfShards is the number of shards of the collection.
294+
// Only available in cluster setup.
295+
NumberOfShards int `json:"numberOfShards,omitempty"`
296+
297+
// This attribute specifies the name of the sharding strategy to use for the collection.
298+
// Can not be changed after creation.
299+
ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"`
300+
301+
// ShardKeys contains the names of document attributes that are used to determine the target shard for documents.
302+
// Only available in cluster setup.
303+
ShardKeys []string `json:"shardKeys,omitempty"`
304+
305+
// Shards is a list of shards that belong to the collection.
306+
// Each shard contains a list of DB servers where the first one is the leader and the rest are followers.
307+
Shards map[ShardID][]ServerID `json:"shards,omitempty"`
308+
309+
// StatusString represents status as a string.
310+
StatusString string `json:"statusString,omitempty"`
311+
312+
// ReplicationFactor contains how many copies of each shard are kept on different DBServers.
313+
// Only available in cluster setup.
314+
ReplicationFactor int `json:"replicationFactor,omitempty"`
315+
316+
// WaitForSync; If true then creating, changing or removing documents will wait
317+
// until the data has been synchronized to disk.
318+
WaitForSync bool `json:"waitForSync,omitempty"`
319+
320+
// WriteConcern contains how many copies must be available before a collection can be written.
321+
// It is required that 1 <= WriteConcern <= ReplicationFactor.
322+
// Default is 1. Not available for satellite collections.
323+
// Available from 3.6 ArangoDB version.
324+
WriteConcern int `json:"writeConcern,omitempty"`
325+
}

collection_impl.go

Lines changed: 30 additions & 1 deletion
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 driver
@@ -190,6 +191,34 @@ func (c *collection) SetProperties(ctx context.Context, options SetCollectionPro
190191
return nil
191192
}
192193

194+
// Shards fetches shards information of the collection.
195+
func (c *collection) Shards(ctx context.Context, details bool) (CollectionShards, error) {
196+
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("collection"), "shards"))
197+
if err != nil {
198+
return CollectionShards{}, WithStack(err)
199+
}
200+
if details {
201+
req.SetQuery("details", "true")
202+
}
203+
204+
resp, err := c.conn.Do(ctx, req)
205+
if err != nil {
206+
return CollectionShards{}, WithStack(err)
207+
}
208+
209+
if err := resp.CheckStatus(200); err != nil {
210+
return CollectionShards{}, WithStack(err)
211+
}
212+
213+
shards := CollectionShards{}
214+
if err := resp.ParseBody("", &shards); err != nil {
215+
return CollectionShards{}, WithStack(err)
216+
}
217+
218+
return shards, nil
219+
220+
}
221+
193222
// Load the collection into memory.
194223
func (c *collection) Load(ctx context.Context) error {
195224
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath("collection"), "load"))

edge_collection_impl.go

Lines changed: 11 additions & 1 deletion
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 driver
@@ -126,6 +127,15 @@ func (c *edgeCollection) SetProperties(ctx context.Context, options SetCollectio
126127
return nil
127128
}
128129

130+
// Shards fetches shards information of the collection.
131+
func (c *edgeCollection) Shards(ctx context.Context, details bool) (CollectionShards, error) {
132+
result, err := c.rawCollection().Shards(ctx, details)
133+
if err != nil {
134+
return result, WithStack(err)
135+
}
136+
return result, nil
137+
}
138+
129139
// Load the collection into memory.
130140
func (c *edgeCollection) Load(ctx context.Context) error {
131141
if err := c.rawCollection().Load(ctx); err != nil {

test/collection_test.go

Lines changed: 29 additions & 1 deletion
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 test
@@ -964,3 +965,30 @@ func TestCollectionWriteConcernSetPropInvalid(t *testing.T) {
964965
assert.Equalf(t, defaultWriteConcern, prop.WriteConcern, "MinReplicationFactor not updated, expected %d, found %d",
965966
minRepl, prop.WriteConcern)
966967
}
968+
969+
// TestCollectionShards creates a collection and gets the shards' information.
970+
func TestCollectionShards(t *testing.T) {
971+
if getTestMode() != testModeCluster {
972+
t.Skipf("Not a cluster mode")
973+
}
974+
975+
databaseName := getThisFunctionName()
976+
c := createClientFromEnv(t, true)
977+
db := ensureDatabase(nil, c, databaseName, nil, t)
978+
name := "test_collection_set_properties"
979+
col, err := db.CreateCollection(nil, name, &driver.CreateCollectionOptions{
980+
ReplicationFactor: 2,
981+
NumberOfShards: 2,
982+
})
983+
require.NoError(t, err)
984+
985+
shards, err := col.Shards(context.Background(), true)
986+
require.NoError(t, err)
987+
require.Len(t, shards.Shards, 2, "expected 2 shards")
988+
var leaders []driver.ServerID
989+
for _, dbServers := range shards.Shards {
990+
require.Lenf(t, dbServers, 2, "expected 2 DB servers for the shard")
991+
leaders = append(leaders, dbServers[0])
992+
}
993+
assert.NotEqualf(t, leaders[0], leaders[1], "the leader can not be on the same server")
994+
}

test/util.go

Lines changed: 19 additions & 2 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 test
@@ -28,13 +29,14 @@ import (
2829
"encoding/json"
2930
"fmt"
3031
"os"
32+
"runtime"
3133
"strconv"
3234
"strings"
3335
"testing"
3436
"time"
3537

38+
"github.com/dchest/uniuri"
3639
"github.com/google/uuid"
37-
3840
"github.com/stretchr/testify/require"
3941

4042
driver "github.com/arangodb/go-driver"
@@ -212,3 +214,18 @@ func min(max int, ints ...int) int {
212214

213215
return z
214216
}
217+
218+
// getThisFunctionName returns the name of the function of the caller.
219+
func getThisFunctionName() string {
220+
programCounters := make([]uintptr, 10)
221+
// skip this function and 'runtime.Callers' function
222+
runtime.Callers(2, programCounters)
223+
functionPackage := runtime.FuncForPC(programCounters[0])
224+
225+
function := strings.Split(functionPackage.Name(), ".")
226+
if len(function) > 1 {
227+
return function[len(function)-1] + "_" + uniuri.NewLen(6)
228+
}
229+
230+
return function[0] + "_" + uniuri.NewLen(6)
231+
}

vertex_collection_impl.go

Lines changed: 12 additions & 2 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 driver
@@ -126,6 +127,15 @@ func (c *vertexCollection) SetProperties(ctx context.Context, options SetCollect
126127
return nil
127128
}
128129

130+
// Shards fetches shards information of the collection.
131+
func (c *vertexCollection) Shards(ctx context.Context, details bool) (CollectionShards, error) {
132+
result, err := c.rawCollection().Shards(ctx, details)
133+
if err != nil {
134+
return result, WithStack(err)
135+
}
136+
return result, nil
137+
}
138+
129139
// Load the collection into memory.
130140
func (c *vertexCollection) Load(ctx context.Context) error {
131141
if err := c.rawCollection().Load(ctx); err != nil {
@@ -134,7 +144,7 @@ func (c *vertexCollection) Load(ctx context.Context) error {
134144
return nil
135145
}
136146

137-
// UnLoad the collection from memory.
147+
// Unload unloads the collection from memory.
138148
func (c *vertexCollection) Unload(ctx context.Context) error {
139149
if err := c.rawCollection().Unload(ctx); err != nil {
140150
return WithStack(err)

0 commit comments

Comments
 (0)