예제 #1
0
        public ITestCluster GetTestCluster(int dc1NodeCount, int dc2NodeCount, bool shareable = true, int maxTries = DefaultMaxClusterCreateRetries, bool startCluster = true, bool initClient = true, int currentRetryCount = 0)
        {
            ITestCluster testCluster = null;

            if (shareable)
            {
                testCluster = GetExistingClusterWithNodeCount(dc1NodeCount);
            }

            // If we found a valid shareable test cluster, then switch to it and start it
            if (testCluster != null)
            {
                Trace.TraceInformation("Found existing test cluster with nodeCount: " + dc1NodeCount + ", name: " + testCluster.Name);
                ShutDownAllCcmTestClustersExceptFor((CcmCluster)testCluster);
                testCluster.SwitchToThisAndStart();
            }
            // If no Test Cluster was found, then we need to create a new one.
            else
            {
                testCluster = CreateNewClusterAndAddToList(
                    TestUtils.GetTestClusterNameBasedOnCurrentEpochTime(), dc1NodeCount, dc2NodeCount, DefaultKeyspaceName, shareable, startCluster, 2);
            }

            // Try to initialize the cluster / session
            if (initClient)
            {
                bool clientWasInitialized = TryToInititalizeClusterClient(testCluster);
                if (!clientWasInitialized)
                {
                    RemoveTestCluster(testCluster);
                    foreach (string host in testCluster.ExpectedInitialHosts)
                    {
                        TestUtils.WaitForDown(host, DefaultCassandraPort, 30);
                    }

                    if (currentRetryCount > MaxClusterCreationRetries)
                    {
                        throw new Exception("Cluster with node count " + dc1NodeCount + " has already failed " + currentRetryCount + " times ... is there something wrong with this environment?");
                    }

                    testCluster = null; // signal that we need to try again
                }
            }

            // loop back, try again while we haven't exceeded max tries
            if (testCluster == null)
            {
                if (currentRetryCount < maxTries)
                {
                    return(GetTestCluster(dc1NodeCount, dc2NodeCount, shareable, maxTries, startCluster, initClient, ++currentRetryCount));
                }
                else
                {
                    throw new Exception("Test cluster was not created successfully!");
                }
            }

            return(testCluster);
        }