Skip to content

Instantly share code, notes, and snippets.

@csarrazi
Last active May 28, 2019 15:23
Show Gist options
  • Save csarrazi/704d68d4bbfe368d3c495cc1994dba02 to your computer and use it in GitHub Desktop.
Save csarrazi/704d68d4bbfe368d3c495cc1994dba02 to your computer and use it in GitHub Desktop.
Start a local sharded cluster on a local environment.
param(
[Parameter(mandatory = $True)][string]$path,
[switch]$force = $False,
[int]$portPrefix = 27000,
[int]$numShards = 3
)
function LaunchMongoD() {
param(
[string]$processType = "shard",
[int]$number = 0
)
[string]$dbPath = "$($path)/$($processType)_$($number)"
$folderExists = Test-Path $dbPath
if ($folderExists -eq $False) {
New-Item -Path $dbPath -ItemType directory
}
[string]$dbPath = resolve-path $dbPath
[string]$logPath = "$($dbPath)/mongod.log"
[int]$port = $portPrefix + $number
switch ($processType) {
"shard" { $port = $port + 100; Break }
"config" { $port = $port + 200; Break }
}
$p = Start-Process mongod -ArgumentList "--wiredTigerCacheSizeGB 1 --port $($port) --dbpath $($dbPath) --replSet $($processType)_$($number) --$($processType)svr --logpath $($logPath) --setParameter maxLogSizeKB=500" -NoNewWindow -PassThru
$processes.Add($p)
}
function ConfigureMongoD() {
param(
[string]$processType = "shard",
[int]$number = 0
)
[int]$port = $portPrefix + $number
switch ($processType) {
"shard" { $port = $port + 100; Break }
"config" { $port = $port + 200; Break }
}
Start-Process mongo -ArgumentList "--host mongodb://127.0.0.1:$($port)/ --norc --quiet --eval ""rs.initiate({ _id: '$($processType)_$($number)', members: [{ _id: 0, host: '127.0.0.1:$($port)'}] })""" -NoNewWindow -Wait
}
function ConfigureShardedCluster() {
$collectionsToShard = @(
"test.foo",
"test.bar"
)
for ($i = 0; $i -lt $numShards; $i++) {
[int]$port = $portPrefix + 100 + $i
Write-Output "Configuring shard $($i)"
Start-Process mongo -ArgumentList "--norc --quiet --eval ""sh.addShard('shard_$($i)/127.0.0.1:$($port)');""" -NoNewWindow -Wait
}
Start-Process mongo -ArgumentList "--norc --quiet --eval ""sh.enableSharding('test');"""
foreach ($collectionName in $collectionsToShard) {
Start-Process mongo -ArgumentList "--norc --quiet --eval ""sh.shardCollection('$($collectionName)', {_id: 'hashed' });"""
}
}
function LaunchMongoS() {
param(
[int]$port = 27017
)
[string]$dbPath = "$($path)/mongos"
$folderExists = Test-Path $dbPath
if ($folderExists -eq $False) {
New-Item -Path $dbPath -ItemType directory
}
[string]$dbPath = resolve-path $dbPath
[string]$logPath = "$($dbPath)/mongod.log"
$p = Start-Process mongos -ArgumentList "--port $($port) --logpath $($logPath) --configdb config_0/127.0.0.1:27200" -NoNewWindow -PassThru
$processes.Add($p)
}
function WaitOpenPort() {
param(
[int]$port = 27017
)
Write-Output "Waiting for port $($port) to be opened"
$connected = $False
while ($connected -eq $False) {
$connected = checkOpenPort -port $port
if ($connected -eq $False) {
Start-Sleep -Milliseconds 100
}
}
}
function checkOpenPort() {
param(
[int]$port
)
$connected = $False
try {
$tcp = New-Object System.Net.Sockets.TcpClient
$tcp.ReceiveTimeout = 100
$tcp.SendTimeout = 100
$tcp.Connect("127.0.0.1", $port)
$connected = $True
}
catch {}
finally {
$tcp.Close()
}
return $connected
}
try {
if ((Get-Command "mongod" -ErrorAction SilentlyContinue) -eq $null) {
Throw "Unable to find mongod.exe in your path"
}
if ((checkOpenPort -port 27017) -eq $True) {
Throw "Port 27017 is already used by another process"
}
$folderExists = Test-Path $path
[switch]$configureCluster = $False
if ($force -eq $True) {
Write-Output "Removing data folder before reconfiguring cluster"
Remove-Item -Path $path -Recurse
}
if ($folderExists -eq $False -or $force -eq $True) {
$configureCluster = $True
New-Item -Path $path -ItemType directory
}
$path = resolve-path $path
$processes = New-Object System.Collections.ArrayList
for ($i = 0; $i -lt $numShards; $i++) {
Write-Output "Starting shard $($i)"
LaunchMongoD -number $i
}
Write-Output "Starting config server"
LaunchMongoD -processType "config"
WaitOpenPort -port ($portPrefix + 200)
if ($configureCluster -eq $True) {
for ($i = 0; $i -lt $numShards; $i++) {
ConfigureMongoD -number $i
}
ConfigureMongoD -processType "config"
}
LaunchMongoS
WaitOpenPort
if ($configureCluster -eq $True) {
ConfigureShardedCluster
}
Write-Output "Cluster launched. Press Ctrl + C to stop."
while (1) {
Start-Sleep -Seconds 10
}
}
catch {
$errorMessage = $_.Exception.Message
$failedItem = $_.Exception.ItemName
Write-Error "Error: $($errorMessage) - $($failedItem)"
}
finally {
Write-Output "End of work, closing all processes"
foreach ($process in $processes) {
$process.Kill()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment