Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

removed stress tests, because the implimentation was pretty poor, I l…

…ook forward to a better solution that is more robust in the future.
  • Loading branch information...
commit 57baf7d2df8f9045b003887ef3e42cd9c2aad451 1 parent 6203be3
@nberardi nberardi authored
View
44 FluentCassandra_All.sln
@@ -1,6 +1,6 @@

-Microsoft Visual Studio Solution File, Format Version 11.00
-# Visual Studio 2010
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentCassandra", "src\FluentCassandra.csproj", "{EAA32600-3C2A-4B34-B9B2-5764F280FCE3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentCassandra.Sandbox", "test\FluentCassandra.Sandbox\FluentCassandra.Sandbox.csproj", "{DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}"
@@ -18,18 +18,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{BF5D39
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{1A88B962-9A09-4692-80D3-E88712A9E516}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentCassandra.StressTest", "test\FluentCassandra.StressTest\FluentCassandra.StressTest.csproj", "{AC3818E2-E260-4193-A3D1-6E3FF87383F6}"
-EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentCassandra.Tests", "test\FluentCassandra.Tests\FluentCassandra.Tests.csproj", "{9DAF7022-5820-4214-B13E-AC0A1B37691F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{AC1223D1-1638-4DEB-9CFF-8B0C43854BB0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentCassandra.LinqPad", "utility\FluentCassandra.LinqPad\FluentCassandra.LinqPad.csproj", "{CACA3463-BBEE-4C7E-AC89-49240B0D8F46}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentCassandra.UltraStressTest", "test\FluentCassandra.UltraStressTest\FluentCassandra.UltraStressTest.csproj", "{FA126D9D-9072-4FF0-B4EB-1B1F95BF65D5}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentCassandra.MoreTests", "test\FluentCassandra.MoreTests\FluentCassandra.MoreTests.csproj", "{05682974-C961-4A22-9F44-052859F00C15}"
-EndProject
Global
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = FluentCassandra.vsmdi
@@ -63,16 +57,6 @@ Global
{DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Release|Mixed Platforms.Build.0 = Release|x86
{DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Release|x86.ActiveCfg = Release|x86
{DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Release|x86.Build.0 = Release|x86
- {AC3818E2-E260-4193-A3D1-6E3FF87383F6}.Debug|Any CPU.ActiveCfg = Debug|x86
- {AC3818E2-E260-4193-A3D1-6E3FF87383F6}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
- {AC3818E2-E260-4193-A3D1-6E3FF87383F6}.Debug|Mixed Platforms.Build.0 = Debug|x86
- {AC3818E2-E260-4193-A3D1-6E3FF87383F6}.Debug|x86.ActiveCfg = Debug|x86
- {AC3818E2-E260-4193-A3D1-6E3FF87383F6}.Debug|x86.Build.0 = Debug|x86
- {AC3818E2-E260-4193-A3D1-6E3FF87383F6}.Release|Any CPU.ActiveCfg = Release|x86
- {AC3818E2-E260-4193-A3D1-6E3FF87383F6}.Release|Mixed Platforms.ActiveCfg = Release|x86
- {AC3818E2-E260-4193-A3D1-6E3FF87383F6}.Release|Mixed Platforms.Build.0 = Release|x86
- {AC3818E2-E260-4193-A3D1-6E3FF87383F6}.Release|x86.ActiveCfg = Release|x86
- {AC3818E2-E260-4193-A3D1-6E3FF87383F6}.Release|x86.Build.0 = Release|x86
{9DAF7022-5820-4214-B13E-AC0A1B37691F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9DAF7022-5820-4214-B13E-AC0A1B37691F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9DAF7022-5820-4214-B13E-AC0A1B37691F}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@@ -91,35 +75,13 @@ Global
{CACA3463-BBEE-4C7E-AC89-49240B0D8F46}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{CACA3463-BBEE-4C7E-AC89-49240B0D8F46}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{CACA3463-BBEE-4C7E-AC89-49240B0D8F46}.Release|x86.ActiveCfg = Release|Any CPU
- {FA126D9D-9072-4FF0-B4EB-1B1F95BF65D5}.Debug|Any CPU.ActiveCfg = Debug|x86
- {FA126D9D-9072-4FF0-B4EB-1B1F95BF65D5}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
- {FA126D9D-9072-4FF0-B4EB-1B1F95BF65D5}.Debug|Mixed Platforms.Build.0 = Debug|x86
- {FA126D9D-9072-4FF0-B4EB-1B1F95BF65D5}.Debug|x86.ActiveCfg = Debug|x86
- {FA126D9D-9072-4FF0-B4EB-1B1F95BF65D5}.Debug|x86.Build.0 = Debug|x86
- {FA126D9D-9072-4FF0-B4EB-1B1F95BF65D5}.Release|Any CPU.ActiveCfg = Release|x86
- {FA126D9D-9072-4FF0-B4EB-1B1F95BF65D5}.Release|Mixed Platforms.ActiveCfg = Release|x86
- {FA126D9D-9072-4FF0-B4EB-1B1F95BF65D5}.Release|Mixed Platforms.Build.0 = Release|x86
- {FA126D9D-9072-4FF0-B4EB-1B1F95BF65D5}.Release|x86.ActiveCfg = Release|x86
- {FA126D9D-9072-4FF0-B4EB-1B1F95BF65D5}.Release|x86.Build.0 = Release|x86
- {05682974-C961-4A22-9F44-052859F00C15}.Debug|Any CPU.ActiveCfg = Debug|x86
- {05682974-C961-4A22-9F44-052859F00C15}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
- {05682974-C961-4A22-9F44-052859F00C15}.Debug|Mixed Platforms.Build.0 = Debug|x86
- {05682974-C961-4A22-9F44-052859F00C15}.Debug|x86.ActiveCfg = Debug|x86
- {05682974-C961-4A22-9F44-052859F00C15}.Debug|x86.Build.0 = Debug|x86
- {05682974-C961-4A22-9F44-052859F00C15}.Release|Any CPU.ActiveCfg = Release|x86
- {05682974-C961-4A22-9F44-052859F00C15}.Release|Mixed Platforms.ActiveCfg = Release|x86
- {05682974-C961-4A22-9F44-052859F00C15}.Release|Mixed Platforms.Build.0 = Release|x86
- {05682974-C961-4A22-9F44-052859F00C15}.Release|x86.ActiveCfg = Release|x86
- {05682974-C961-4A22-9F44-052859F00C15}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
- {AC3818E2-E260-4193-A3D1-6E3FF87383F6} = {1A88B962-9A09-4692-80D3-E88712A9E516}
{9DAF7022-5820-4214-B13E-AC0A1B37691F} = {1A88B962-9A09-4692-80D3-E88712A9E516}
- {FA126D9D-9072-4FF0-B4EB-1B1F95BF65D5} = {1A88B962-9A09-4692-80D3-E88712A9E516}
- {05682974-C961-4A22-9F44-052859F00C15} = {1A88B962-9A09-4692-80D3-E88712A9E516}
+ {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55} = {1A88B962-9A09-4692-80D3-E88712A9E516}
{CACA3463-BBEE-4C7E-AC89-49240B0D8F46} = {AC1223D1-1638-4DEB-9CFF-8B0C43854BB0}
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
View
77 test/FluentCassandra.StressTest/FluentCassandra.StressTest.csproj
@@ -1,77 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
- <PropertyGroup>
- <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
- <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
- <ProductVersion>8.0.30703</ProductVersion>
- <SchemaVersion>2.0</SchemaVersion>
- <ProjectGuid>{AC3818E2-E260-4193-A3D1-6E3FF87383F6}</ProjectGuid>
- <OutputType>Exe</OutputType>
- <AppDesignerFolder>Properties</AppDesignerFolder>
- <RootNamespace>FluentCassandra.StressTest</RootNamespace>
- <AssemblyName>FluentCassandra.StressTest</AssemblyName>
- <FileAlignment>512</FileAlignment>
- <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\..\FluentCassandra\</SolutionDir>
- <RestorePackages>true</RestorePackages>
- <ReleaseVersion>1.0.0</ReleaseVersion>
- </PropertyGroup>
- <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
- <PlatformTarget>x86</PlatformTarget>
- <DebugSymbols>true</DebugSymbols>
- <DebugType>full</DebugType>
- <Optimize>false</Optimize>
- <OutputPath>bin\Debug\</OutputPath>
- <DefineConstants>DEBUG;TRACE</DefineConstants>
- <ErrorReport>prompt</ErrorReport>
- <WarningLevel>4</WarningLevel>
- </PropertyGroup>
- <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
- <PlatformTarget>x86</PlatformTarget>
- <DebugType>pdbonly</DebugType>
- <Optimize>true</Optimize>
- <OutputPath>bin\Release\</OutputPath>
- <DefineConstants>TRACE</DefineConstants>
- <ErrorReport>prompt</ErrorReport>
- <WarningLevel>4</WarningLevel>
- </PropertyGroup>
- <PropertyGroup>
- <StartupObject>FluentCassandra.StressTest.Program</StartupObject>
- </PropertyGroup>
- <ItemGroup>
- <Reference Include="System" />
- <Reference Include="System.Core" />
- <Reference Include="System.Numerics" />
- <Reference Include="System.Xml.Linq" />
- <Reference Include="System.Data.DataSetExtensions" />
- <Reference Include="Microsoft.CSharp" />
- <Reference Include="System.Data" />
- <Reference Include="System.Xml" />
- <Reference Include="xunit">
- <HintPath>..\..\packages\xunit.1.9.0.1566\lib\xunit.dll</HintPath>
- </Reference>
- </ItemGroup>
- <ItemGroup>
- <Compile Include="Main.cs" />
- <Compile Include="StressTest1.cs" />
- <Compile Include="StressTest2.cs" />
- <Compile Include="Properties\AssemblyInfo.cs" />
- </ItemGroup>
- <ItemGroup>
- <ProjectReference Include="..\..\src\FluentCassandra.csproj">
- <Project>{EAA32600-3C2A-4B34-B9B2-5764F280FCE3}</Project>
- <Name>FluentCassandra</Name>
- </ProjectReference>
- </ItemGroup>
- <ItemGroup>
- <None Include="packages.config" />
- </ItemGroup>
- <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
- <Import Project="$(SolutionDir)\.nuget\nuget.targets" />
- <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
- Other similar extension points exist, see Microsoft.Common.targets.
- <Target Name="BeforeBuild">
- </Target>
- <Target Name="AfterBuild">
- </Target>
- -->
-</Project>
View
35 test/FluentCassandra.StressTest/Main.cs
@@ -1,35 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-namespace FluentCassandra.StressTest
-{
- class Program1
- {
- static void Main(string[] args)
- {
- Console.Error.WriteLine("Stress Test 1");
- try
- {
- //tressTest1.Test();
- }
- catch (Exception ex)
- {
- Console.Error.WriteLine(ex.Message);
- Console.Error.WriteLine(ex.StackTrace);
- }
- Console.Error.WriteLine("");
- Console.Error.WriteLine("Stress Test 2");
- try
- {
- StressTest.StressTest2.Test();
- }
- catch (Exception ex)
- {
- Console.Error.WriteLine(ex.Message);
- Console.Error.WriteLine(ex.StackTrace);
- }
- }
- }
-}
View
36 test/FluentCassandra.StressTest/Properties/AssemblyInfo.cs
@@ -1,36 +0,0 @@
-using System.Reflection;
-using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
-
-// General Information about an assembly is controlled through the following
-// set of attributes. Change these attribute values to modify the information
-// associated with an assembly.
-[assembly: AssemblyTitle("FluentCassandra.StressTest")]
-[assembly: AssemblyDescription("")]
-[assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("Microsoft")]
-[assembly: AssemblyProduct("FluentCassandra.StressTest")]
-[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
-[assembly: AssemblyTrademark("")]
-[assembly: AssemblyCulture("")]
-
-// Setting ComVisible to false makes the types in this assembly not visible
-// to COM components. If you need to access a type in this assembly from
-// COM, set the ComVisible attribute to true on that type.
-[assembly: ComVisible(false)]
-
-// The following GUID is for the ID of the typelib if this project is exposed to COM
-[assembly: Guid("326ca67f-6a52-4925-8d05-e5bf0af7eff3")]
-
-// Version information for an assembly consists of the following four values:
-//
-// Major Version
-// Minor Version
-// Build Number
-// Revision
-//
-// You can specify all the values or you can default the Build and Revision Numbers
-// by using the '*' as shown below:
-// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
View
119 test/FluentCassandra.StressTest/StressTest1.cs
@@ -1,119 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Threading.Tasks;
-using FluentCassandra.Types;
-using FluentCassandra.Connections;
-using FluentCassandra.Apache.Cassandra;
-
-namespace FluentCassandra.StressTest
-{
- internal class Program
- {
- private static int count = 10000;
- private static int dataLength = 1024;
- private static int threadCount = 8;
- private static string keyspaceName = "Blog";
- private static Server server = new Server("localhost");
-
- private static void SendDebugToConsole()
- {
- // Disable Debug traces
- Trace.Listeners.Clear();
-
- // Disable Debug assert message boxes
- using (DefaultTraceListener listener = new DefaultTraceListener())
- {
- listener.AssertUiEnabled = false;
- Trace.Listeners.Add(listener);
- }
-
- // Restore Debug traces to NUnit's Console.Out tab.
- Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
- }
-
- private static void SetupKeyspace()
- {
- using (var db = new CassandraContext(keyspace: keyspaceName, server: server))
- {
- if (!db.KeyspaceExists(keyspaceName))
- db.AddKeyspace(new KsDef {
- Name = keyspaceName,
- Replication_factor = 1,
- Strategy_class = "org.apache.cassandra.locator.SimpleStrategy",
- Cf_defs = new List<CfDef>()
- });
-
- if (!db.Keyspace.ColumnFamilyExists("Posts"))
- db.AddColumnFamily(new CfDef {
- Name = "Posts",
- Keyspace = keyspaceName,
- Column_type = "Super",
- Comparator_type = "UTF8Type",
- Subcomparator_type = "UTF8Type",
- Comment = "Used for blog posts."
- });
- }
- }
-
- private static void Main(string[] args)
- {
- SendDebugToConsole();
- SetupKeyspace();
-
- Task[] tasks = new Task[threadCount];
-
- Stopwatch watch = new Stopwatch();
- watch.Start();
-
- for (int i = 0; i < threadCount; i++)
- {
- tasks[i] = Task.Factory.StartNew(DoWork);
- }
-
- Task.WaitAll(tasks);
- watch.Stop();
-
- double rate = (count * threadCount) / watch.Elapsed.TotalSeconds;
- double throughput = rate * dataLength;
- Console.WriteLine("Total Completed: " + watch.Elapsed + "\tRate: " + rate + "\tThroughput: " + throughput);
- Console.ReadKey();
- }
-
- private static void DoWork()
- {
- using (var db = new CassandraContext(keyspace: keyspaceName, server: server))
- {
- Stopwatch watch = new Stopwatch();
- watch.Start();
-
- Random random = new Random();
- byte[] data = new byte[dataLength];
- random.NextBytes(data);
- int errors = 0;
-
- for (int i = 0; i < count; i++)
- {
- // Insert
- Guid postId = Guid.NewGuid();
- string titleName = i.ToString();
-
- var family = db.GetColumnFamily<UTF8Type, UTF8Type>("Posts");
-
- dynamic post = family.CreateRecord(postId);
- dynamic details = post.CreateSuperColumn();
-
- details.Body = data;
-
- post[DateTime.Now] = details;
- db.Attach(post);
- db.SaveChanges();
- }
-
- double rate = count / watch.Elapsed.TotalSeconds;
- double throughput = rate * data.Length;
- Console.WriteLine("Completed: " + watch.Elapsed + "\tRate: " + rate + "\tThroughput: " + throughput + "\tErrors:" + errors);
- }
- }
- }
-}
View
445 test/FluentCassandra.StressTest/StressTest2.cs
@@ -1,445 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using FluentCassandra.Connections;
-using System.Threading;
-using System.Globalization;
-using Xunit;
-using System.Diagnostics;
-
-namespace FluentCassandra.StressTest
-{
- /// <summary>
- /// The performance test on rapid insert/select/delete operations from mutlithreaded environment
- /// </summary>
- class StressTest2
- {
- public class CassandraJob
- {
- string command;
- string logic;
- Guid id;
- int n = 0;
- Guid ackId;
-
- public CassandraJob(FluentCassandra.Linq.ICqlRow job, Guid ackId)
- {
- command = job["command"];
- logic = job["logic"];
- id = job["id"];
- this.ackId = ackId;
- }
-
- public string GetCommand()
- {
- return command;
- }
-
- public string GetLogic()
- {
- return logic;
- }
-
- public Guid GetID()
- {
- return id;
- }
-
- public Guid GetAckID()
- {
- return ackId;
- }
- }
- private static string KeyspaceName = "stresstest2";
- private static readonly Server Server = new Server("localhost");
-
- [Fact]
- public static void Test()
- {
- int TestTimeInMinutes = 10;
-
- int ThreadCount = 50;
- int ThreadCount2 = 10;
- int TimeOut_Sec = 5;
- bool usePooling = true;
-
- object alive_monitor = new object();
- bool alive_condition = true;
- List<Thread> threads = new List<Thread>();
-
- int thrStarted_cnt = 0;
- object thrStarted_monitor = new object();
-
- //initialize
- CassandraContext main_db = new CassandraContext(new ConnectionBuilder(keyspace: KeyspaceName, server: Server, cqlVersion: CqlVersion.Cql3, pooling: usePooling));
- {
- if (main_db.KeyspaceExists(KeyspaceName))
- main_db.DropKeyspace(KeyspaceName);
-
- var keyspace = new CassandraKeyspace(new CassandraKeyspaceSchema
- {
- Name = KeyspaceName,
- }, main_db);
-
- keyspace.TryCreateSelf();
-
- CreateTablesIfNotExists(main_db);
- }
-
- for (int tI = 0; tI < ThreadCount; tI++)
- {
- int thrNo = tI;
- var thr = new Thread(() =>
- {
-
- Console.Write("(*" + thrNo + ")");
- try
- {
- CassandraContext db = new CassandraContext(new ConnectionBuilder(keyspace: KeyspaceName, server: Server, cqlVersion: CqlVersion.Cql3, pooling: usePooling));
- lock (thrStarted_monitor)
- {
- thrStarted_cnt++;
- Monitor.PulseAll(thrStarted_monitor);
- }
-
- while (true)
- {
- var job = GetJob(db, 10);
- if (job != null)
- {
- Console.Write("-");
- if (job.GetLogic() == null || job.GetCommand() == null)
- Console.WriteLine("Error");
- DeleteJob(db, job);
- }
- else
- {
- lock (alive_monitor)
- {
- if (!alive_condition)
- return;
- else
- Monitor.Wait(alive_monitor, TimeOut_Sec * 1000);
- }
- }
- }
- }
- finally
- {
- Console.Write("(~" + thrNo + ")");
- }
- });
- threads.Add(thr);
- thr.Start();
- }
-
- for (int tI = 0; tI < ThreadCount2; tI++)
- {
- int thrNo = tI;
- var thr = new Thread(() =>
- {
- Console.Write("<*" + thrNo + ">");
- try
- {
- CassandraContext db = new CassandraContext(new ConnectionBuilder(keyspace: KeyspaceName, server: Server, cqlVersion: CqlVersion.Cql3, pooling: usePooling));
- lock (thrStarted_monitor)
- {
- thrStarted_cnt++;
- Monitor.PulseAll(thrStarted_monitor);
- }
-
- while (true)
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < 100; i++)
- sb.AppendLine(Guid.NewGuid().ToString());
- AddJob(db, sb.ToString(), Guid.NewGuid().ToString());
- Console.Write("+");
- Thread.Sleep(100);
- lock (alive_monitor)
- {
- if (!alive_condition)
- return;
- }
- }
- }
- finally
- {
- Console.Write("<~" + thrNo + ">");
- }
- });
- threads.Add(thr);
- thr.Start();
- }
-
- while (true)
- {
- lock (thrStarted_monitor)
- {
- Monitor.Wait(thrStarted_monitor);
- if (thrStarted_cnt == ThreadCount+ThreadCount2)
- break;
- }
- }
-
- //wait for ten minutes
- Thread.Sleep(TestTimeInMinutes * 60 * 1000);
-
- lock (alive_monitor)
- {
- alive_condition = false;
- Monitor.PulseAll(alive_monitor);
- }
- foreach (var thr in threads)
- {
- thr.Join();
- }
-
- var j = main_db.ExecuteQuery(SelectCQL_Main(1)).ToArray();
- var c = main_db.ExecuteQuery(SelectCQL_Trans(1)).ToArray();
- if (j.Count() > 0 || c.Count() > 0)
- Console.WriteLine("Error");
-
- Console.WriteLine("Finished");
- }
-
- static ThreadLocal<Random> rnd = new ThreadLocal<Random>((() => new Random(Guid.NewGuid().GetHashCode())));
-
- public static void DeleteJob(CassandraContext db, CassandraJob job)
- {
- db.ExecuteNonQuery(DeleteRowCQL_Main((job as CassandraJob).GetID()));
- db.ExecuteNonQuery(DeleteRowCQL_Trans((job as CassandraJob).GetID()));
- }
-
- public static void AddJob(CassandraContext db, string logic, string command)
- {
- var id = Guid.NewGuid();
- db.ExecuteNonQuery(InsertRowCQL(id, logic, command));
- }
-
- public static CassandraJob GetJob(CassandraContext db, int hide_time_sec)
- {
- loop:
- dynamic job = null;
- for (int i = 0; i < 1000; i++)
- {
- var jobs = db.ExecuteQuery(SelectCQL_Main(1000)).ToArray();
- var mx = jobs.Length < 1000 ? jobs.Length - 1 : 1000;
- if (mx == -1)
- break;
- job = jobs[rnd.Value.Next(mx)];
- if (job["hidden_till"] < ToUnixTime(DateTimeOffset.UtcNow))
- break;
- }
- if (job != null)
- {
- var myID = Guid.NewGuid();
- var ret = new CassandraJob(job, myID);
- db.ExecuteNonQuery(HideRowCQL_Trans(ret.GetID(), ret.GetAckID()));
- var cnt = db.ExecuteQuery(SelectCQL_Trans(ret.GetID(), 1000)).ToArray();
- var counter = cnt.First()["count"];
- if (counter > 1)
- {
- db.ExecuteNonQuery(DeleteRowCQL_Trans(ret.GetID()));
- goto loop;
- }
- if (counter == 0)
- goto loop;
-
- db.ExecuteNonQuery(HideRowCQL_Main2(ret.GetID(), hide_time_sec,ret.GetLogic(),ret.GetCommand()));
-
- return ret;
- }
- else
- return null;
- }
-
- public static void CreateTablesIfNotExists(CassandraContext db)
- {
- try
- {
- db.ExecuteNonQuery(string.Format(@"
-CREATE TABLE {0} (
- id uuid,
- logic text,
- command text,
- hidden_till timestamp,
- PRIMARY KEY (id));
-", TableName("main")));
- }
- catch (CassandraException) { }
- try
- {
-
- db.ExecuteNonQuery(string.Format(@"
-CREATE TABLE {0} (
- id uuid,
- oid uuid,
- progress double,
- last_access timestamp,
- PRIMARY KEY (id,oid));
-", TableName("trans")));
- }
- catch (CassandraException) { }
- try
- {
- db.ExecuteNonQuery(string.Format(@"
-CREATE TABLE {0} (
- id uuid,
- oid uuid,
- what int,
- when timestamp,
- info text,
- PRIMARY KEY (id,oid,what,when));
-", TableName("out")));
- }
- catch (CassandraException) { }
-
-
- }
- protected static string Encode(string str)
- {
- return '\'' + str.Replace("\'", "\'\'") + '\'';
- }
-
- protected void DeleteTables()
- {
- throw new NotImplementedException();
- }
-
- protected static string TableName(string pfx)
- {
- return "scheduler_" + pfx;
- }
-
-
- protected static readonly DateTimeOffset UnixStart = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
-
- protected static long ToUnixTime(DateTimeOffset dt)
- {
- // this was changed from .NET Ticks to the Unix Epoch to be compatible with other cassandra libraries
- return Convert.ToInt64(Math.Floor((dt - UnixStart).TotalMilliseconds));
- }
-
-
- protected static string InsertRowCQL(Guid id, string logic, string command)
- {
- return string.Format(@"
-INSERT INTO {0}
-(id,logic,command, hidden_till)
-VALUES ({1},{2},{3},{4})",
- TableName("main"),
- id.ToString(),
- Encode(logic),
- Encode(command),
- 0
- );
- }
-
- protected static string HideRowCQL_Main(Guid id, int hide_time_sec)
- {
- return string.Format(@"
-UPDATE {0}
-SET hidden_till={1}
-WHERE id={2}",
- TableName("main"),
- ToUnixTime(DateTimeOffset.UtcNow.AddSeconds(hide_time_sec)),
- id.ToString()
- );
- }
-
- protected static string HideRowCQL_Main2(Guid id, int hide_time_sec, string logic, string command)
- {
- return string.Format(@"
-UPDATE {0}
-SET hidden_till={1}, logic={2}, command={3}
-WHERE id={4}",
- TableName("main"),
- ToUnixTime(DateTimeOffset.UtcNow.AddSeconds(hide_time_sec)),
- Encode(logic),
- Encode(command),
- id.ToString()
- );
- }
-
- protected static string HideRowCQL_Trans(Guid id, Guid oid)
- {
- return string.Format(@"
-INSERT INTO {0}
-(id,oid,progress,last_access)
-VALUES ({1},{2},0,0)",
- TableName("trans"),
- id.ToString(),
- oid.ToString()
- );
- }
-
- protected static string DeleteRowCQL_Main(Guid id)
- {
- return string.Format(@"
-DELETE FROM {0}
-WHERE id={1}",
- TableName("main"),
- id.ToString()
- );
- }
-
- protected static string DeleteRowCQL_Trans(Guid id)
- {
- return string.Format(@"
-DELETE FROM {0}
-WHERE id={1}",
- TableName("trans"),
- id.ToString()
- );
- }
-
- protected static string SelectCQL_Main(int limit)
- {
- return string.Format(@"
-SELECT *
-FROM {0} LIMIT {1}",
- TableName("main"), limit.ToString()
- );
- }
- protected static string SelectCQL_Trans(int limit)
- {
- return string.Format(@"
-SELECT *
-FROM {0} LIMIT {1}",
- TableName("trans"), limit.ToString()
- );
- }
- protected static string SelectCQL_Trans(Guid id, int limit)
- {
- return string.Format(@"
-SELECT count(1)
-FROM {0} WHERE id={1} LIMIT {2}",
- TableName("trans"), id.ToString(), limit.ToString()
- );
- }
-
- protected static string SetProgressRowCQL_Trans(Guid id, Guid oid, double percentDone)
- {
- return string.Format(@"
-UPDATE {0}
-SET progress={1}
-WHERE id={2} and oid = {3}",
- TableName("trans"),
- percentDone.ToString(CultureInfo.InvariantCulture),
- id.ToString(),
- oid.ToString()
- );
- }
-
- protected static string AddInfoRowCQL_Out(Guid id, Guid oid, int what, string info)
- {
- return string.Format(@"
-INSERT INTO {0}
- (id,oid,what,when,info)
-VALUES ({1},{2},{3},{4},{5});
-", TableName("out"), id.ToString(), oid.ToString(), what.ToString(), ToUnixTime(DateTimeOffset.UtcNow), Encode(info));
- }
- }
-}

0 comments on commit 57baf7d

Please sign in to comment.
Something went wrong with that request. Please try again.