public void HasNextWithMoreThanHalfBlacklistedTest() { RoundRobinServerManager target = new RoundRobinServerManager(new ConnectionBuilder("Server=unit-test-1")); Server srv1 = null; Server srv2 = new Server("unit-test-2"); Server srv3 = new Server("unit-test-3"); Server srv4 = new Server("unit-test-4"); target.Add(srv2); target.Add(srv3); target.Add(srv4); List<Server> servers = new List<Server> { new Server("unit-test-1"), srv2, srv3, srv4 }; for (int i = 0; i < 4; i++) { Server srv = target.Next(); Assert.True(servers[i].ToString().Equals(srv.ToString(), StringComparison.OrdinalIgnoreCase)); if(i == 0) { srv1 = srv; } } target.ErrorOccurred(srv2); target.ErrorOccurred(srv3); Assert.True(target.HasNext); target.ErrorOccurred(srv1); Assert.True(target.HasNext); target.ErrorOccurred(srv4); Assert.False(target.HasNext); }
/// <summary> /// /// </summary> /// <param name="builder"></param> internal Connection(Server server, ConnectionBuilder builder) { Created = DateTime.Now; Server = server; var socket = new TSocket(server.Host, server.Port, server.Timeout * 1000); switch(builder.ConnectionType) { case ConnectionType.Simple: _transport = socket; break; case ConnectionType.Buffered: _transport = new TBufferedTransport(socket, builder.BufferSize); break; case ConnectionType.Framed: _transport = new TFramedTransport(socket); break; } _protocol = new TBinaryProtocol(_transport); _client = new Cassandra.Client(_protocol); }
/// <summary> /// /// </summary> /// <param name="server"></param> /// <param name="connectionType"></param> /// <param name="bufferSize"></param> public Connection(Server server, ConnectionType connectionType, int bufferSize) { Created = DateTime.UtcNow; Server = server; ConnectionType = connectionType; BufferSize = bufferSize; InitTransportAndClient(); }
public LinqToCqlObjectsTests() { var keyspaceName = "Testing"; var server = new Server("localhost"); _db = new CassandraContext(keyspace: keyspaceName, server: server); _family = _db.GetColumnFamily<AsciiType>("Users"); }
public void Add(Server server) { lock (_lock) { _servers.Add(server); _serverQueue.Enqueue(server); } }
public LinqToCqlTests() { var keyspaceName = ConfigurationManager.AppSettings["TestKeySpace"]; var server = new Server(ConfigurationManager.AppSettings["TestServer"]); _db = new CassandraContext(new ConnectionBuilder(keyspace: keyspaceName, server: server, cqlVersion: CqlVersion.Cql)); _family = _db.GetColumnFamily<AsciiType>("Users"); }
public void Remove(Server server) { using (TimedLock.Lock(_lock)) { _servers.Remove(server); _serverQueue = new Queue<Server>(_servers); } }
public void Add(Server server) { using (TimedLock.Lock(_lock)) { _servers.Add(server); _serverQueue.Enqueue(server); } }
public void BlackList(Server server) { Debug.WriteLine(server + " has been blacklisted", "connection"); lock (_lock) { _blackListed.Add(server); } }
public LinqToCqlObjectsTests() { var keyspaceName = ConfigurationManager.AppSettings["TestKeySpace"]; var server = new Server(ConfigurationManager.AppSettings["TestServer"]); _db = new CassandraContext(keyspace: keyspaceName, server: server); _family = _db.GetColumnFamily<AsciiType>("Users"); }
public void Add(Server server) { lock(_lock) { _server = server; _failed = false; _recoveryTimer.Change(Timeout.Infinite,Timeout.Infinite); } }
/// <summary> /// /// </summary> /// <param name="builder"></param> internal Connection(Server server) { Created = DateTime.Now; Server = server; TTransport socket = new TSocket(server.Host, server.Port, server.Timeout); _transport = new TFramedTransport(socket); _protocol = new TBinaryProtocol(_transport); _client = new Cassandra.Client(_protocol); }
public void ErrorOccurred(Server server, Exception exc = null) { Debug.WriteLineIf(exc != null, exc, "connection"); lock(_lock) { if(_failed) return; _failed = true; _recoveryTimer.Change(_recoveryTimerInterval, Timeout.Infinite); } }
public void BlackList(Server server) { Debug.WriteLine(server + " has been blacklisted", "connection"); lock (_lock) { if (_blackListed.Add(server)) { _serverQueue.Clear(); foreach (Server srv in _servers) { if (!IsBlackListed(srv)) { _serverQueue.Enqueue(srv); } } } } }
public ConnectionBuilder(string keyspace, Server server, bool pooling = false, int minPoolSize = 0, int maxPoolSize = 100, int connectionLifetime = 0, ConnectionType connectionType = ConnectionType.Framed, int bufferSize = 1024, ConsistencyLevel read = ConsistencyLevel.QUORUM, ConsistencyLevel write = ConsistencyLevel.QUORUM, string cqlVersion = FluentCassandra.Connections.CqlVersion.ServerDefault, bool compressCqlQueries = true, string username = null, string password = null) { Keyspace = keyspace; Servers = new List<Server>() { server }; ConnectionTimeout = TimeSpan.FromSeconds(server.Timeout); Pooling = pooling; MinPoolSize = minPoolSize; MaxPoolSize = maxPoolSize; ConnectionLifetime = TimeSpan.FromSeconds(connectionLifetime); ConnectionType = connectionType; BufferSize = bufferSize; ReadConsistency = read; WriteConsistency = write; CqlVersion = cqlVersion; CompressCqlQueries = compressCqlQueries; Username = username; Password = password; ConnectionString = GetConnectionString(); }
public void CanBlackListAndCleanQueueTest() { RoundRobinServerManager target = new RoundRobinServerManager(new ConnectionBuilder("Server=unit-test-1,unit-test-2,unit-test-3")); Server srv = new Server("unit-test-4"); target.Add(srv); bool gotServer4 = false; for (int i = 0; i < 4; i++) { Server server = target.Next(); if (server.ToString().Equals(srv.ToString(), StringComparison.OrdinalIgnoreCase)) { gotServer4 = true; break; } } Assert.True(gotServer4); target.ErrorOccurred(srv); gotServer4 = false; for (int i = 0; i < 4; i++) { Server server = target.Next(); if (server.Equals(srv)) { gotServer4 = true; break; } } Assert.False(gotServer4); }
public void Add(Server server) { _server = server; }
public void Remove(Server server) { lock (_lock) { _servers.Remove(server); _serverQueue = new Queue<Server>(); _blackListed.RemoveWhere(x => x == server); foreach (var s in _servers) { if (!_blackListed.Contains(s)) _serverQueue.Enqueue(s); } } }
public void ErrorOccurred(Server server, Exception exc = null) { Debug.WriteLineIf(exc != null, exc, "connection"); BlackList(server); }
public void Remove(Server server) { throw new NotImplementedException(); }
public _CassandraSetup() { var keyspaceName = "Testing"; var server = new Server("localhost"); if (!CassandraSession.KeyspaceExists(server, keyspaceName)) CassandraSession.AddKeyspace(server, new KsDef { Name = "Testing", Replication_factor = 1, Strategy_class = "org.apache.cassandra.locator.SimpleStrategy", Cf_defs = new List<CfDef>() }); var keyspace = new CassandraKeyspace(keyspaceName); if (!keyspace.ColumnFamilyExists(server, "Standard")) keyspace.AddColumnFamily(server, new CfDef { Name = "Standard", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "AsciiType", Comment = "Used for testing Standard family." }); if (!keyspace.ColumnFamilyExists(server, "StandardAsciiType")) keyspace.AddColumnFamily(server, new CfDef { Name = "StandardAsciiType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "AsciiType", Comment = "Used for testing Standard family." }); if (!keyspace.ColumnFamilyExists(server, "StandardBytesType")) keyspace.AddColumnFamily(server, new CfDef { Name = "StandardBytesType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "BytesType", Comment = "Used for testing BytesType family." }); if (!keyspace.ColumnFamilyExists(server, "StandardIntegerType")) keyspace.AddColumnFamily(server, new CfDef { Name = "StandardIntegerType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "IntegerType", Comment = "Used for testing IntegerType family." }); if (!keyspace.ColumnFamilyExists(server, "StandardLexicalUUIDType")) keyspace.AddColumnFamily(server, new CfDef { Name = "StandardLexicalUUIDType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "LexicalUUIDType", Comment = "Used for testing LexicalUUIDType family." }); if (!keyspace.ColumnFamilyExists(server, "StandardLongType")) keyspace.AddColumnFamily(server, new CfDef { Name = "StandardLongType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "LongType", Comment = "Used for testing LongType family." }); if (!keyspace.ColumnFamilyExists(server, "StandardTimeUUIDType")) keyspace.AddColumnFamily(server, new CfDef { Name = "StandardTimeUUIDType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "TimeUUIDType", Comment = "Used for testing TimeUUIDType family." }); if (!keyspace.ColumnFamilyExists(server, "StandardUTF8Type")) keyspace.AddColumnFamily(server, new CfDef { Name = "StandardUTF8Type", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "UTF8Type", Comment = "Used for testing UTF8Type family." }); if (!keyspace.ColumnFamilyExists(server, "Super")) keyspace.AddColumnFamily(server, new CfDef { Name = "Super", Keyspace = "Testing", Column_type = "Super", Comparator_type = "AsciiType", Subcomparator_type = "AsciiType", Comment = "Used for testing Super family." }); DB = new CassandraContext(keyspaceName, server); DB.ThrowErrors = true; Family = DB.GetColumnFamily<AsciiType>("Standard"); SuperFamily = DB.GetColumnFamily<AsciiType, AsciiType>("Super"); Family.RemoveAllRows(); SuperFamily.RemoveAllRows(); Family.InsertColumn(TestKey1, "Test1", Math.PI); Family.InsertColumn(TestKey1, "Test2", Math.PI); Family.InsertColumn(TestKey1, "Test3", Math.PI); SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test1", Math.PI); SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test2", Math.PI); SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test3", Math.PI); Family.InsertColumn(TestKey2, "Test1", Math.PI); Family.InsertColumn(TestKey2, "Test2", Math.PI); Family.InsertColumn(TestKey2, "Test3", Math.PI); SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test1", Math.PI); SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test2", Math.PI); SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test3", Math.PI); }
private bool IsBlackListed(Server server) { return _blackListed.Contains(server); }
public void Remove(Server server) { Debug.WriteLine(server + " has been removed", "connection"); lock (_lock) { _servers.Remove(server); _blackListed.RemoveWhere(x => x == server); RefreshServerQueue(); } }
public void ErrorOccurred(Server server, Exception exc = null) { Debug.WriteLineIf(exc != null, exc, "connection"); Debug.WriteLine(server + " has been blacklisted", "connection"); lock (_lock) { if (_blackListed.Add(server)) RefreshServerQueue(); } }
private static void Main(string[] args) { var keyspaceName = "Blog"; var server = new Server("localhost"); if (!CassandraSession.KeyspaceExists(server, keyspaceName)) CassandraSession.AddKeyspace(server, new KsDef { Name = keyspaceName, Replication_factor = 1, Strategy_class = "org.apache.cassandra.locator.SimpleStrategy", Cf_defs = new List<CfDef>() }); var keyspace = new CassandraKeyspace(keyspaceName); if (!keyspace.ColumnFamilyExists(server, "Posts")) keyspace.AddColumnFamily(server, new CfDef { Name = "Posts", Keyspace = keyspaceName, Column_type = "Super", Comparator_type = "UTF8Type", Subcomparator_type = "UTF8Type", Comment = "Used for blog posts." }); if (!keyspace.ColumnFamilyExists(server, "Comments")) keyspace.AddColumnFamily(server, new CfDef { Name = "Comments", Keyspace = keyspaceName, Column_type = "Super", Comparator_type = "TimeUUIDType", Subcomparator_type = "UTF8Type", Comment = "Used for blog post comments." }); using (var db = new CassandraContext(keyspace: keyspaceName, server: server)) { var family = db.GetColumnFamily<UTF8Type, UTF8Type>("Posts"); // create post dynamic post = family.CreateRecord(key: "first-blog-post"); // create post details dynamic postDetails = post.CreateSuperColumn(); postDetails.Title = "My First Cassandra Post"; postDetails.Body = "Blah. Blah. Blah. about my first post on how great Cassandra is to work with."; postDetails.Author = "Nick Berardi"; postDetails.PostedOn = DateTimeOffset.Now; // create post tags dynamic tags = post.CreateSuperColumn(); tags[0] = "Cassandra"; tags[1] = ".NET"; tags[2] = "Database"; tags[3] = "NoSQL"; // add properties to post post.Details = postDetails; post.Tags = tags; // attach the post to the database Console.WriteLine("attaching record"); db.Attach(post); // save the changes Console.WriteLine("saving changes"); db.SaveChanges(); // get the post back from the database Console.WriteLine("getting 'first-blog-post'"); dynamic getPost = family.Get("first-blog-post").FirstOrDefault(); // show details dynamic getPostDetails = getPost.Details; Console.WriteLine( String.Format("=={0} by {1}==\n{2}", getPostDetails.Title, getPostDetails.Author, getPostDetails.Body )); // show tags Console.Write("tags:"); foreach (var tag in getPost.Tags) Console.Write(String.Format("{0}:{1},", tag.ColumnName, tag.ColumnValue)); Console.WriteLine(); // get the comments family var commentsFamily = db.GetColumnFamily<TimeUUIDType, UTF8Type>("Comments"); dynamic postComments = commentsFamily.CreateRecord(key: "first-blog-post"); // lets attach it to the database before we add the comments db.Attach(postComments); // add 5 comments for (int i = 0; i < 5; i++) { dynamic comment = postComments.CreateSuperColumn(); comment.Name = i + " Nick Berardi"; comment.Email = i + " [email protected]"; comment.Website = i + " www.coderjournal.com"; comment.Comment = i + " Wow fluent cassandra is really great and easy to use."; postComments[GuidGenerator.GenerateTimeBasedGuid()] = comment; Console.WriteLine("Comment " + i + " Done"); Thread.Sleep(TimeSpan.FromSeconds(5)); } // save the comments db.SaveChanges(); DateTime lastDate = DateTime.Now; for (int page = 0; page < 2; page++) { // lets back the date off by a millisecond so we don't get paging overlaps lastDate = lastDate.AddMilliseconds(-1D); Console.WriteLine("Showing page " + page + " starting at " + lastDate.ToLocalTime()); var comments = commentsFamily.Get("first-blog-post") .Reverse() .Fetch(lastDate) .Take(3) .FirstOrDefault(); foreach (dynamic comment in comments) { var dateTime = GuidGenerator.GetDateTime((Guid)comment.ColumnName); Console.WriteLine(String.Format("{0:T} : {1} ({2} - {3})", dateTime.ToLocalTime(), comment.Name, comment.Email, comment.Website )); lastDate = dateTime; } } } Console.Read(); }
public SingleServerManager(IConnectionBuilder builder) { _server = builder.Servers[0]; _recoveryTimerInterval = (long)builder.ServerPollingInterval.TotalMilliseconds; _recoveryTimer = new Timer(ServerRecover); }
/// <summary> /// /// </summary> /// <param name="server"></param> /// <param name="builder"></param> public Connection(Server server, IConnectionBuilder builder) : this(server, builder.ConnectionType, builder.BufferSize) { }
public void Remove(Server server) { throw new NotSupportedException("You cannot remove a server since SingleServerManager supports one server. Call the Add method to change the server."); }
public void ErrorOccurred(Server server, Exception exc = null) { throw new NotImplementedException(); }
public SingleServerManager(IConnectionBuilder builder) { _server = builder.Servers[0]; }