Skip to content

Commit

Permalink
Introduce sharding rules to MongoDB collections
Browse files Browse the repository at this point in the history
  • Loading branch information
sejongk committed Sep 13, 2023
1 parent 3ff801a commit 9323464
Show file tree
Hide file tree
Showing 7 changed files with 342 additions and 0 deletions.
Empty file added build/docker/sharding/README.md
Empty file.
181 changes: 181 additions & 0 deletions build/docker/sharding/full/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
version: '3'
services:

# Config Server
config1:
image: mongo
container_name: mongo-config1
command: mongod --port 27017 --configsvr --replSet config-rs --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27100:27017
restart: always
networks:
- mongo
config2:
image: mongo
container_name: mongo-config2
command: mongod --port 27017 --configsvr --replSet config-rs --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27101:27017
restart: always
networks:
- mongo
config3:
image: mongo
container_name: mongo-config3
command: mongod --port 27017 --configsvr --replSet config-rs --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27102:27017
restart: always
networks:
- mongo

# Mongos
mongos1:
image: mongo
container_name: mongos1
command: mongos --port 27017 --configdb config-rs/config1:27017,config2:27017,config3:27017 --bind_ip_all
ports:
- 27110:27017
restart: always
volumes:
- ./scripts:/scripts
networks:
- mongo
mongos2:
image: mongo
container_name: mongos2
command: mongos --port 27017 --configdb config-rs/config1:27017,config2:27017,config3:27017 --bind_ip_all
ports:
- 27111:27017
restart: always
volumes:
- ./scripts:/scripts
networks:
- mongo
mongos3:
image: mongo
container_name: mongos3
command: mongos --port 27017 --configdb config-rs/config1:27017,config2:27017,config3:27017 --bind_ip_all
ports:
- 27112:27017
restart: always
volumes:
- ./scripts:/scripts
networks:
- mongo

# Shards
# Shards 1
shard1-1:
image: mongo
container_name: mongo-shard1-1
command: mongod --port 27017 --shardsvr --replSet shard-rs-1 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27120:27017
restart: always
networks:
- mongo
shard1-2:
image: mongo
container_name: mongo-shard1-2
command: mongod --port 27017 --shardsvr --replSet shard-rs-1 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27121:27017
restart: always
networks:
- mongo
shard1-3:
image: mongo
container_name: mongo-shard1-3
command: mongod --port 27017 --shardsvr --replSet shard-rs-1 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27122:27017
restart: always
networks:
- mongo

# Shards 2
shard2-1:
image: mongo
container_name: mongo-shard2-1
command: mongod --port 27017 --shardsvr --replSet shard-rs-2 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27123:27017
restart: always
networks:
- mongo
shard2-2:
image: mongo
container_name: mongo-shard2-2
command: mongod --port 27017 --shardsvr --replSet shard-rs-2 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27124:27017
restart: always
networks:
- mongo
shard2-3:
image: mongo
container_name: mongo-shard2-3
command: mongod --port 27017 --shardsvr --replSet shard-rs-2 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27125:27017
restart: always
networks:
- mongo

# Shards 3
shard3-1:
image: mongo
container_name: mongo-shard3-1
command: mongod --port 27017 --shardsvr --replSet shard-rs-3 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27126:27017
restart: always
networks:
- mongo
shard3-2:
image: mongo
container_name: mongo-shard3-2
command: mongod --port 27017 --shardsvr --replSet shard-rs-3 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27127:27017
restart: always
networks:
- mongo
shard3-3:
image: mongo
container_name: mongo-shard3-3
command: mongod --port 27017 --shardsvr --replSet shard-rs-3 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27128:27017
restart: always
networks:
- mongo

networks:
mongo:
119 changes: 119 additions & 0 deletions build/docker/sharding/min/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
version: '3'
services:

# Config Server
config1:
image: mongo:7.0.1
container_name: mongo-config1
command:
- /bin/sh
- -c
- | # run the init script for the primary replica after mongod has been started.
sh -c "sleep 5 && mongosh < /scripts/init-config1.js" &
mongod --port 27017 --configsvr --replSet config-rs --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27100:27017
restart: always


# Shards
# Shards 1
shard1-1:
image: mongo:7.0.1
container_name: mongo-shard1-1
command:
- /bin/sh
- -c
- | # run the init script for the primary replica after mongod has been started.
sh -c "sleep 5 && mongosh < /scripts/init-shard1-1.js" &
mongod --port 27017 --shardsvr --replSet shard-rs-1 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27110:27017
restart: always

shard1-2:
image: mongo:7.0.1
container_name: mongo-shard1-2
command: mongod --port 27017 --shardsvr --replSet shard-rs-1 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27111:27017
restart: always

shard1-3:
image: mongo:7.0.1
container_name: mongo-shard1-3
command: mongod --port 27017 --shardsvr --replSet shard-rs-1 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27112:27017
restart: always


# Shards 2
shard2-1:
image: mongo:7.0.1
container_name: mongo-shard2-1
command:
- /bin/sh
- -c
- | # run the init script for the primary replica after mongod has been started.
sh -c "sleep 5 && mongosh < /scripts/init-shard2-1.js" &
mongod --port 27017 --shardsvr --replSet shard-rs-2 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27113:27017
restart: always

shard2-2:
image: mongo:7.0.1
container_name: mongo-shard2-2
command: mongod --port 27017 --shardsvr --replSet shard-rs-2 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27114:27017
restart: always

shard2-3:
image: mongo:7.0.1
container_name: mongo-shard2-3
command: mongod --port 27017 --shardsvr --replSet shard-rs-2 --bind_ip_all
volumes:
- ./scripts:/scripts
ports:
- 27115:27017
restart: always


# Mongos
mongos1:
image: mongo:7.0.1
container_name: mongos1
command:
- /bin/sh
- -c
- | # run the init script for the primary replica after the config servers and the shards has been configured
sh -c "sleep 20 && mongosh < /scripts/init-mongos1.js" &
mongos --port 27017 --configdb config-rs/config1:27017 --bind_ip_all
ports:
- 27120:27017
restart: always
volumes:
- ./scripts:/scripts

depends_on:
- config1
- shard1-1
- shard1-2
- shard1-3
- shard2-1
- shard2-2
- shard2-3
11 changes: 11 additions & 0 deletions build/docker/sharding/min/scripts/init-config1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
rs.initiate(
{
_id: "config-rs",
configsvr: true,
members: [
{ _id : 0, host : "config1:27017" },
]
}
)


11 changes: 11 additions & 0 deletions build/docker/sharding/min/scripts/init-mongos1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sh.addShard( "shard-rs-1/shard1-1:27017,shard1-2:27017,shard1-3:27017")
sh.addShard( "shard-rs-2/shard2-1:27017,shard2-2:27017,shard2-3:27017")

sh.enableSharding("yorkie-meta")
sh.shardCollection("yorkie-meta.projects", { _id : "hashed" } )
sh.shardCollection("yorkie-meta.users", { username : "hashed" } )
sh.shardCollection("yorkie-meta.clients", { project_id : "hashed" } )
sh.shardCollection("yorkie-meta.documents", { project_id : "hashed" } )
sh.shardCollection("yorkie-meta.changes", { doc_id : "hashed" } )
sh.shardCollection("yorkie-meta.snapshots", { doc_id : "hashed" } )
sh.shardCollection("yorkie-meta.syncedseqs", { doc_id : "hashed" } )
10 changes: 10 additions & 0 deletions build/docker/sharding/min/scripts/init-shard1-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rs.initiate(
{
_id : "shard-rs-1",
members: [
{ _id : 0, host : "shard1-1:27017" },
{ _id : 1, host : "shard1-2:27017" },
{ _id : 2, host : "shard1-3:27017" }
]
}
)
10 changes: 10 additions & 0 deletions build/docker/sharding/min/scripts/init-shard2-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rs.initiate(
{
_id : "shard-rs-2",
members: [
{ _id : 0, host : "shard2-1:27017" },
{ _id : 1, host : "shard2-2:27017" },
{ _id : 2, host : "shard2-3:27017" }
]
}
)

0 comments on commit 9323464

Please sign in to comment.