Skip to content

Commit 31c1023

Browse files
authored
Modify shard keys based on level of collections (#774)
1 parent 18a460f commit 31c1023

36 files changed

+658
-1001
lines changed

admin/client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func (c *Client) UpdateProject(
238238
func (c *Client) ListDocuments(
239239
ctx context.Context,
240240
projectName string,
241-
previousRefKey types.DocRefKey,
241+
previousID string,
242242
pageSize int32,
243243
isForward bool,
244244
includeSnapshot bool,
@@ -247,8 +247,7 @@ func (c *Client) ListDocuments(
247247
ctx,
248248
connect.NewRequest(&api.ListDocumentsRequest{
249249
ProjectName: projectName,
250-
PreviousId: previousRefKey.ID.String(),
251-
PreviousKey: previousRefKey.Key.String(),
250+
PreviousId: previousID,
252251
PageSize: pageSize,
253252
IsForward: isForward,
254253
IncludeSnapshot: includeSnapshot,

api/types/resource_ref_key.go

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,21 @@
1717
package types
1818

1919
import (
20-
"errors"
2120
"fmt"
22-
"strings"
23-
24-
"github.com/yorkie-team/yorkie/pkg/document/key"
2521
)
2622

2723
// EmptyDocRefKey is an empty value of DocRefKey.
2824
var EmptyDocRefKey = DocRefKey{"", ""}
2925

30-
// ErrInvalidDocRefKeyStringFormat is returned when the input of DocRefKey Set is invalid.
31-
var ErrInvalidDocRefKeyStringFormat = errors.New("use the format 'docKey,docID' for the string of the docRefKey")
32-
3326
// DocRefKey represents an identifier used to reference a document.
3427
type DocRefKey struct {
35-
Key key.Key
36-
ID ID
28+
ProjectID ID
29+
DocID ID
3730
}
3831

3932
// String returns the string representation of the given DocRefKey.
4033
func (r DocRefKey) String() string {
41-
return fmt.Sprintf("Document (%s.%s)", r.Key, r.ID)
42-
}
43-
44-
// Set parses the given string (format: `{docKey},{docID}`) and assigns the values
45-
// to the given DocRefKey.
46-
// NOTE(sejongk): This function is necessary for Viper, an external command-line module.
47-
func (r *DocRefKey) Set(v string) error {
48-
parsed := strings.Split(v, ",")
49-
if len(parsed) != 2 {
50-
return ErrInvalidDocRefKeyStringFormat
51-
}
52-
r.Key = key.Key(parsed[0])
53-
r.ID = ID(parsed[1])
54-
return nil
55-
}
56-
57-
// Type returns the type string of the given DocRefKey, used in cli help text.
58-
// NOTE(sejongk): This function is necessray for Viper, an external command-line module.
59-
func (r DocRefKey) Type() string {
60-
return "DocumentRefKey"
34+
return fmt.Sprintf("Document (%s.%s)", r.ProjectID, r.DocID)
6135
}
6236

6337
// SnapshotRefKey represents an identifier used to reference a snapshot.
@@ -68,5 +42,5 @@ type SnapshotRefKey struct {
6842

6943
// String returns the string representation of the given SnapshotRefKey.
7044
func (r SnapshotRefKey) String() string {
71-
return fmt.Sprintf("Snapshot (%s.%s.%d)", r.DocRefKey.Key, r.DocRefKey.ID, r.ServerSeq)
45+
return fmt.Sprintf("Snapshot (%s.%s.%d)", r.ProjectID, r.DocID, r.ServerSeq)
7246
}

api/types/resource_ref_key_test.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

api/yorkie/v1/admin.pb.go

Lines changed: 141 additions & 152 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/yorkie/v1/admin.proto

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ message UpdateProjectResponse {
9494

9595
message ListDocumentsRequest {
9696
string project_name = 1;
97-
string previous_key = 6;
9897
string previous_id = 2;
9998
int32 page_size = 3;
10099
bool is_forward = 4;

api/yorkie/v1/yorkie.pb.go

Lines changed: 106 additions & 126 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/yorkie/v1/yorkie.proto

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ message DetachDocumentResponse {
7777

7878
message WatchDocumentRequest {
7979
string client_id = 1;
80-
string document_key = 3;
8180
string document_id = 2;
8281
}
8382

@@ -115,7 +114,6 @@ message PushPullChangesResponse {
115114

116115
message BroadcastRequest {
117116
string client_id = 1;
118-
string document_key = 5;
119117
string document_id = 2;
120118
string topic = 3;
121119
bytes payload = 4;

build/docker/sharding/scripts/init-mongos1.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,28 @@ function findAnotherShard(shard) {
1010
}
1111

1212
function shardOfChunk(minKeyOfChunk) {
13-
return db.getSiblingDB("config").chunks.findOne({ min: { key: minKeyOfChunk } }).shard
13+
return db.getSiblingDB("config").chunks.findOne({ min: { project_id: minKeyOfChunk } }).shard
1414
}
1515

1616
// Shard the database for the mongo client test
1717
const mongoClientDB = "test-yorkie-meta-mongo-client"
1818
sh.enableSharding(mongoClientDB)
19-
sh.shardCollection(mongoClientDB + ".documents", { key: 1 })
20-
sh.shardCollection(mongoClientDB + ".changes", { doc_key: 1 })
21-
sh.shardCollection(mongoClientDB + ".snapshots", { doc_key: 1 })
22-
sh.shardCollection(mongoClientDB + ".syncedseqs", { doc_key: 1 })
19+
sh.shardCollection(mongoClientDB + ".documents", { project_id: 1 })
20+
sh.shardCollection(mongoClientDB + ".changes", { doc_id: 1 })
21+
sh.shardCollection(mongoClientDB + ".snapshots", { doc_id: 1 })
22+
sh.shardCollection(mongoClientDB + ".syncedseqs", { doc_id: 1 })
2323

24-
// Split the inital range at "duplicateIDTestDocKey5" to allow doc_ids duplicate in different shards.
25-
const docSplitKey = "duplicateIDTestDocKey5"
26-
sh.splitAt(mongoClientDB + ".documents", { key: docSplitKey })
24+
// Split the inital range at `splitPoint` to allow doc_ids duplicate in different shards.
25+
const splitPoint = ObjectId("500000000000000000000000")
26+
sh.splitAt(mongoClientDB + ".documents", { project_id: splitPoint })
2727
// Move the chunk to another shard.
28-
db.adminCommand({ moveChunk: mongoClientDB + ".documents", find: { key: docSplitKey }, to: findAnotherShard(shardOfChunk(docSplitKey)) })
28+
db.adminCommand({ moveChunk: mongoClientDB + ".documents", find: { project_id: splitPoint }, to: findAnotherShard(shardOfChunk(splitPoint)) })
2929

3030
// Shard the database for the server test
3131
const serverDB = "test-yorkie-meta-server"
3232
sh.enableSharding(serverDB)
33-
sh.shardCollection(serverDB + ".documents", { key: 1 })
34-
sh.shardCollection(serverDB + ".changes", { doc_key: 1 })
35-
sh.shardCollection(serverDB + ".snapshots", { doc_key: 1 })
36-
sh.shardCollection(serverDB + ".syncedseqs", { doc_key: 1 })
33+
sh.shardCollection(serverDB + ".documents", { project_id: 1 })
34+
sh.shardCollection(serverDB + ".changes", { doc_id: 1 })
35+
sh.shardCollection(serverDB + ".snapshots", { doc_id: 1 })
36+
sh.shardCollection(serverDB + ".syncedseqs", { doc_id: 1 })
37+

client/client.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,8 @@ func (c *Client) Watch(
424424
stream, err := c.client.WatchDocument(
425425
ctx,
426426
withShardKey(connect.NewRequest(&api.WatchDocumentRequest{
427-
ClientId: c.id.String(),
428-
DocumentKey: doc.Key().String(),
429-
DocumentId: attachment.docID.String(),
427+
ClientId: c.id.String(),
428+
DocumentId: attachment.docID.String(),
430429
},
431430
), c.options.APIKey, doc.Key().String()))
432431
if err != nil {
@@ -698,11 +697,10 @@ func (c *Client) broadcast(ctx context.Context, doc *document.Document, topic st
698697
_, err := c.client.Broadcast(
699698
ctx,
700699
withShardKey(connect.NewRequest(&api.BroadcastRequest{
701-
ClientId: c.id.String(),
702-
DocumentKey: doc.Key().String(),
703-
DocumentId: attachment.docID.String(),
704-
Topic: topic,
705-
Payload: payload,
700+
ClientId: c.id.String(),
701+
DocumentId: attachment.docID.String(),
702+
Topic: topic,
703+
Payload: payload,
706704
},
707705
), c.options.APIKey, doc.Key().String()))
708706
if err != nil {

cmd/yorkie/document/list.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ import (
2626
"github.com/spf13/viper"
2727

2828
"github.com/yorkie-team/yorkie/admin"
29-
"github.com/yorkie-team/yorkie/api/types"
3029
"github.com/yorkie-team/yorkie/cmd/yorkie/config"
3130
"github.com/yorkie-team/yorkie/pkg/units"
3231
)
3332

3433
var (
35-
previousRefKey types.DocRefKey
36-
pageSize int32
37-
isForward bool
34+
previousID string
35+
pageSize int32
36+
isForward bool
3837
)
3938

4039
func newListCommand() *cobra.Command {
@@ -64,7 +63,7 @@ func newListCommand() *cobra.Command {
6463
}()
6564

6665
ctx := context.Background()
67-
documents, err := cli.ListDocuments(ctx, projectName, previousRefKey, pageSize, isForward, true)
66+
documents, err := cli.ListDocuments(ctx, projectName, previousID, pageSize, isForward, true)
6867
if err != nil {
6968
return err
7069
}
@@ -101,10 +100,11 @@ func newListCommand() *cobra.Command {
101100

102101
func init() {
103102
cmd := newListCommand()
104-
cmd.Flags().Var(
105-
&previousRefKey,
106-
"previous-refKey",
107-
"The previous document refKey to start from. Use the format 'docKey,docID' for the input.",
103+
cmd.Flags().StringVar(
104+
&previousID,
105+
"previous-id",
106+
"",
107+
"The previous document ID to start from",
108108
)
109109
cmd.Flags().Int32Var(
110110
&pageSize,

0 commit comments

Comments
 (0)