Skip to content

Instantly share code, notes, and snippets.

@dsolovay dsolovay/ShardTest.ps1
Last active Nov 25, 2015

Embed
What would you like to do?
Create MongoDB replica sets, shards, and test data
# Powershell Script for creating a 3x5 shard cluster
# Based on Mongo University M101 lecture, "Building a Shared Environemnt" https://youtu.be/dn45G2yw20A
$rootpath = "/temp/mongoshards/"
new-module -scriptblock {function report($text) {
write-output $("-" * $text.length)
write-output $text
write-output $("-" * $text.length)
write-output ""
}} | Out-Null
report "Remove temporary directory"
remove-item $rootpath -recurse
report "Create data directories"
new-item -type directory -path $rootpath | Out-Null
report "Create mongod instances"
$shards = 0..2
foreach ($shard in $shards)
{
$rss = 0..2
foreach ($rs in $rss)
{
$dbpath = "$rootpath/data/shard${shard}/r${rs}"
new-item -type directory -path $dbpath | Out-Null
# Start mongod processes
$port = 30000 + ($shard * 3) + $rs
$args = "--replSet s$shard --logpath $rootpath/s${shard}_r${rs}.log --dbpath $dbpath --port $port --oplogSize 64 --smallfiles"
$process = start-process mongod.exe $args
}
report "Configure replica sets"
$port1 = 30000 + $shard * 3
$port2 = 30000 + $shard * 3 + 1
$port3 = 30000 + $shard * 3 + 2
$configBlock = "{_id: ""s$shard"", members: [ {_id:0, host:""localhost:$port1""}, {_id:1, host:""localhost:$port2""}, {_id:2, host:""localhost:$port3""}]}"
echo "rs.initiate($configBlock)" | mongo --port $port1
}
report "Check PRIMARY elected for each replica set"
while ($True)
{
$response1 = (echo "rs.status()" | mongo -port 30000)
$response2 = (echo "rs.status()" | mongo -port 30003)
$response3 = (echo "rs.status()" | mongo -port 30006)
if (($response1 -clike "*PRIMARY*") -and ($response2 -clike "*PRIMARY*") -and ($response3 -clike "*PRIMARY*")) {
break
}
Start-Sleep -s 1
Write-Output "."
}
report "PRIMARY elected"
report "Create config servers"
$cfg_a = "${rootpath}/data/config_a"
$cfg_b = "${rootpath}/data/config_b"
$cfg_c = "${rootpath}/data/config_c"
new-item -type directory -path $cfg_a
new-item -type directory -path $cfg_b
new-item -type directory -path $cfg_c
$arg_a = "--dbpath $cfg_a --logpath ${rootpath}/cfg-a.log --configsvr --smallfiles --port 40000"
$arg_b = "--dbpath $cfg_b --logpath ${rootpath}/cfg-b.log --configsvr --smallfiles --port 40001"
$arg_c = "--dbpath $cfg_c --logpath ${rootpath}/cfg-c.log --configsvr --smallfiles --port 40002"
start-process mongod $arg_a
start-process mongod $arg_b
start-process mongod $arg_c
report "Config servers up"
report "Launch mongos"
$args_s = "--port 50000 --logpath ${rootpath}/mongos-1.log --configdb localhost:40000,localhost:40001,localhost:40002"
start-process mongos $args_s
report "Check mongos online on port 50000"
while($true)
{
$output = echo "" | mongo localhost:50000 2> null
if (-not ($output -like "*failed*")) {break}
Start-Sleep -s 1
Write-Output "."
}
report "Mongos avaiable at port 50000"
report "Configure shards"
echo "db.adminCommand( { addshard: ""s0/localhost:30000"" })" | mongo --quiet --port 50000
echo "db.adminCommand( { addshard: ""s1/localhost:30003"" })" | mongo --quiet --port 50000
echo "db.adminCommand( { addshard: ""s2/localhost:30006"" })" | mongo --quiet --port 50000
echo "db.adminCommand( { enableSharding:""school"" })" | mongo --port 50000
echo "db.adminCommand( { shardCollection:""school.students"", key:{student_id:1} })" | mongo --port 50000
report "Generate 100,000 documents"
$mongoUniversityScript = "db=db.getSiblingDB(`"school`");
types = ['exam', 'quiz', 'homework', 'homework'];
// 10,000 students
for (i = 0; i < 10000; i++) {
// take 10 classes
for (class_counter = 0; class_counter < 10; class_counter ++) {
scores = []
// and each class has 4 grades
for (j = 0; j < 4; j++) {
scores.push({'type':types[j],'score':Math.random()*100});
}
// there are 500 different classes that they can take
class_id = Math.floor(Math.random()*501); // get a class id between 0 and 500
record = {'student_id':i, 'scores':scores, 'class_id':class_id};
db.students.insert(record);
}
}"
echo $mongoUniversityScript | mongo --port 50000 --quiet
report "Total records, records in shard 1, 2, and 3"
echo "db.students.count()" | mongo school --port 50000
echo "db.students.count()" | mongo school --port 30000
echo "db.students.count()" | mongo school --port 30003
echo "db.students.count()" | mongo school --port 30006
report "sh.status() output"
echo "sh.status()" | mongo --port 50000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.