Repository: cassandra
Updated Branches:
  refs/heads/trunk 8174dabf3 -> 374d0cbae


Abort startup if ports are in use (Windows)

patch by jmckenzie, reviewed by pthompson for CASSANDRA-8179


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/e3f4c6de
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/e3f4c6de
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/e3f4c6de

Branch: refs/heads/trunk
Commit: e3f4c6defc72372b1ab9b5cf7be63cf3a6886fd6
Parents: 2e37655
Author: Joshua McKenzie <jmcken...@apache.org>
Authored: Fri Oct 31 11:47:11 2014 -0500
Committer: Joshua McKenzie <jmcken...@apache.org>
Committed: Fri Oct 31 11:47:11 2014 -0500

----------------------------------------------------------------------
 bin/cassandra.ps1      | 94 +++++++++++++++++++++++++++++++++++++++++++++
 conf/cassandra-env.ps1 |  5 +++
 2 files changed, 99 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/e3f4c6de/bin/cassandra.ps1
----------------------------------------------------------------------
diff --git a/bin/cassandra.ps1 b/bin/cassandra.ps1
index 53286ab..3884fa5 100644
--- a/bin/cassandra.ps1
+++ b/bin/cassandra.ps1
@@ -95,6 +95,7 @@ Function Main
     }
     else
     {
+        VerifyPortsAreAvailable
         RunCassandra($f)
     }
 }
@@ -279,6 +280,99 @@ WARNING! Failed to write pidfile to $pidfile.  
stop-server.bat and
 }
 
 #-----------------------------------------------------------------------------
+Function VerifyPortsAreAvailable
+{
+    # Need to confirm 5 different ports are available or die if any are 
currently bound
+    # From cassandra.yaml:
+    #   storage_port
+    #   ssl_storage_port
+    #   native_transport_port
+    #   rpc_port, which we'll match to rpc_address
+    # and from env: JMX_PORT which we cache in our environment during 
SetCassandraEnvironment for this check
+    $toMatch = 
@("storage_port:","ssl_storage_port:","native_transport_port:","rpc_port")
+    $yaml = Get-Content "$env:CASSANDRA_CONF\cassandra.yaml"
+
+    $listenAddress = "unknown"
+    $rpcAddress = "unknown"
+    foreach ($line in $yaml)
+    {
+        if ($line -match "^listen_address:")
+        {
+            $args = $line -Split ":"
+            $listenAddress = $args[1] -replace " ", ""
+        }
+        if ($line -match "^rpc_address:")
+        {
+            $args = $line -Split ":"
+            $rpcAddress = $args[1] -replace " ", ""
+        }
+    }
+    if ($listenAddress -eq "unknown")
+    {
+        echo "Failed to parse listen_address from cassandra.yaml to check open 
ports.  Aborting startup."
+        Exit
+    }
+    if ($rpcAddress -eq "unknown")
+    {
+        echo "Failed to parse rpc_address from cassandra.yaml to check open 
ports.  Aborting startup."
+        Exit
+    }
+
+    foreach ($line in $yaml)
+    {
+        foreach ($match in $toMatch)
+        {
+            if ($line -match "^$match")
+            {
+                if ($line.contains("rpc"))
+                {
+                    CheckPort $rpcAddress $line
+                }
+                else
+                {
+                    CheckPort $listenAddress $line
+                }
+            }
+        }
+    }
+    CheckPort $listenAddress "jmx_port: $env:JMX_PORT"
+}
+
+#-----------------------------------------------------------------------------
+Function CheckPort([string]$listenAddress, [string]$configLine)
+{
+    $split = $configLine -Split ":"
+    if ($split.Length -ne 2)
+    {
+        echo "Invalid cassandra.yaml config line parsed while checking for 
available ports:"
+        echo "$configLine"
+        echo "Aborting startup"
+        Exit
+    }
+    else
+    {
+        $port = $split[1] -replace " ", ""
+
+        # start an async connect to the ip/port combo, give it 25ms, and error 
out if it succeeded
+        $tcpobject = new-Object system.Net.Sockets.TcpClient
+        $connect = $tcpobject.BeginConnect($listenAddress, $port, $null, $null)
+        $wait = $connect.AsyncWaitHandle.WaitOne(25, $false)
+
+        if (!$wait)
+        {
+            # still trying to connect, if it's not serviced in 25ms we'll 
assume it's not open
+            $tcpobject.Close()
+        }
+        else
+        {
+            $tcpobject.EndConnect($connect) | out-Null
+            echo "Cassandra port already in use ($configLine).  Aborting"
+            Exit
+        }
+    }
+}
+
+#-----------------------------------------------------------------------------
 Function ValidateArguments
 {
     if ($install -and $uninstall)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/e3f4c6de/conf/cassandra-env.ps1
----------------------------------------------------------------------
diff --git a/conf/cassandra-env.ps1 b/conf/cassandra-env.ps1
index 69bc6d1..906db7a 100644
--- a/conf/cassandra-env.ps1
+++ b/conf/cassandra-env.ps1
@@ -312,6 +312,9 @@ Function SetCassandraEnvironment
     # JMX connections.
     $JMX_PORT="7199"
 
+    # store in env to check if it's avail in verification
+    $env:JMX_PORT=$JMX_PORT
+
     $env:JVM_OPTS = "$env:JVM_OPTS -Dlog4j.defaultInitOverride=true"
 
     # some JVMs will fill up their heap when accessed via JMX, see 
CASSANDRA-6541
@@ -405,3 +408,5 @@ Function SetCassandraEnvironment
 
     $env:JVM_OPTS = "$env:JVM_OPTS 
-Dlog4j.configuration=log4j-server.properties"
 }
+
+

Reply via email to