Skip to content

Commit eef541d

Browse files
author
Rafał Warzycha
authored
Merge pull request #2 from senssei/feature/1
Add sharding by location partion
2 parents b9dcf0a + 576ff71 commit eef541d

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
This is a simple 3 node replica mongodb setup based on offcial `mongo` docker image using `docker-compose` described in my blogpost at https://warzycha.pl/mongo-db-sharding-docker-example/.
44

5+
For details description, steps and discussion go to:
6+
7+
1. https://warzycha.pl/mongo-db-sharding-docker-example/
8+
2. https://warzycha.pl/mongo-db-location-based-segments/
9+
510
# Run
611

712
```
8-
>docker-compose -f docker-compose.1.yml -f docker-compose.2.yml -f docker-compose.cnf.yml -f docker-compose.shard.yml up
13+
docker-compose -f docker-compose.1.yml -f docker-compose.2.yml -f docker-compose.cnf.yml -f docker-compose.shard.yml up
914
```
1015

1116
# Tests
@@ -49,6 +54,12 @@ this should return something similar to:
4954
5055
```
5156

57+
# Sharding configuration
58+
59+
Connect to 'mongos' router and run `queries/shard-status.js` for shard status.
60+
61+
To establish location based partitioning on it just run `queries/init.js`.
62+
5263
# Issues and limitations
5364

5465
It's sometimes stuck on 'mongo-router | 2017-01-16T21:29:48.573+0000 W NETWORK [replSetDistLockPinger] No primary detected for

queries/init.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
sh.removeShardTag("rs1", "US");
2+
3+
sh.removeShardTag("rs2", "EU");
4+
5+
sh.addShardTag("rs1", "US");
6+
7+
sh.addShardTag("rs2", "EU");
8+
9+
sh.disableBalancing("test.sample");
10+
11+
db.sample.drop();
12+
13+
db.createCollection("sample");
14+
15+
db.sample.createIndex( { location: 1, _id: 1 } )
16+
17+
sh.addTagRange(
18+
"test.sample",
19+
{ "location" : "US", "_id" : MinKey },
20+
{ "location" : "US", "_id" : MaxKey },
21+
"US"
22+
);
23+
24+
sh.addTagRange(
25+
"test.sample",
26+
{ "location" : "EU", "_id" : MinKey },
27+
{ "location" : "EU", "_id" : MaxKey },
28+
"EU"
29+
)
30+
31+
32+
sh.enableSharding("test");
33+
34+
sh.shardCollection("test.sample",{ location: 1, _id: 1 });
35+
36+
sh.enableBalancing("test.sample");
37+
38+
db.sample.insert({
39+
"_id" : ObjectId("5787936b94afebe02398521a"),
40+
"location": "US",
41+
"__v" : 0
42+
});
43+
44+
db.sample.insert({
45+
"_id" : ObjectId("5787a08c94afebe023985224"),
46+
"location": "EU",
47+
"__v" : 0
48+
});
49+
50+
sh.startBalancer();
51+
52+
db.sample.find();

queries/shard-status.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sh.status();

0 commit comments

Comments
 (0)