Esempio n. 1
0
        internal Response(Frame frame)
        {
            if (frame == null)
            {
                throw new ArgumentNullException("frame");
            }
            if (frame.Body == null)
            {
                throw new InvalidOperationException("Response body of the received frame was null");
            }
            if (!frame.Header.Flags.HasFlag(HeaderFlag.Compression) && frame.Header.BodyLength > frame.Body.Length - frame.Body.Position)
            {
                throw new DriverInternalError(string.Format(
                                                  "Response body length should be contained in stream: Expected {0} but was {1} (position {2})",
                                                  frame.Header.BodyLength, frame.Body.Length - frame.Body.Position, frame.Body.Position));
            }

            Reader = new FrameReader(frame.Body);

            if (frame.Header.Flags.HasFlag(HeaderFlag.Tracing))
            {
                //If a response frame has the tracing flag set, the first item in its body is the trace id
                var buffer = new byte[16];
                Reader.Read(buffer, 0, 16);
                TraceId = new Guid(TypeCodec.GuidShuffle(buffer));
            }
        }
Esempio n. 2
0
        internal AbstractResponse(ResponseFrame frame)
        {
            BeBinaryReader = new BEBinaryReader(frame.Body);

            if ((frame.Header.Flags & 0x02) == 0x02)
            {
                var buffer = new byte[16];
                BeBinaryReader.Read(buffer, 0, 16);
                TraceId = new Guid(TypeCodec.GuidShuffle(buffer));
            }
        }
Esempio n. 3
0
        public void TokenAware_VNodes_Test()
        {
            var testCluster = TestClusterManager.CreateNew(3, new TestClusterOptions {
                UseVNodes = true
            });
            var cluster = Cluster.Builder().AddContactPoint(testCluster.InitialContactPoint).Build();

            try
            {
                var session = cluster.Connect();
                Assert.AreEqual(256, cluster.AllHosts().First().Tokens.Count());
                session.Execute("CREATE KEYSPACE ks1 WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1}");
                Thread.Sleep(3000);
                session.ChangeKeyspace("ks1");
                session.Execute("CREATE TABLE tbl1 (id uuid primary key)");
                Thread.Sleep(3000);
                var ps     = session.Prepare("INSERT INTO tbl1 (id) VALUES (?)");
                var traces = new List <QueryTrace>();
                for (var i = 0; i < 10; i++)
                {
                    var id    = Guid.NewGuid();
                    var bound = ps
                                .Bind(id)
                                .SetRoutingKey(new RoutingKey()
                    {
                        RawRoutingKey = TypeCodec.GuidShuffle(id.ToByteArray())
                    })
                                .EnableTracing();
                    var rs = session.Execute(bound);
                    traces.Add(rs.Info.QueryTrace);
                }
                //Check that there weren't any hops
                foreach (var t in traces)
                {
                    //The coordinator must be the only one executing the query
                    Assert.True(t.Events.All(e => e.Source.ToString() == t.Coordinator.ToString()), "There were trace events from another host for coordinator " + t.Coordinator);
                }
            }
            finally
            {
                cluster.Dispose();
                testCluster.Remove();
            }
        }
        public void Ctor_TraceFlagSet_TraceIdIsSet()
        {
            // Arrange
            var header = new FrameHeader {
                Flags = AbstractResponse.TraceFlagValue
            };
            var rnd    = new Random();
            var buffer = new byte[16];

            rnd.NextBytes(buffer);
            var expected = new Guid(TypeCodec.GuidShuffle(buffer));
            var body     = new MemoryStream(buffer);
            var frame    = new ResponseFrame(header, body);

            // Act
            var uut = new AbstractResponse(frame);

            // Assert
            Assert.AreEqual(expected, uut.TraceId);
        }
Esempio n. 5
0
        internal AbstractResponse(ResponseFrame frame)
        {
            if (frame == null)
            {
                throw new ArgumentNullException("frame");
            }
            if (frame.Body == null)
            {
                throw new InvalidOperationException("Response body of the received frame was null");
            }

            BeBinaryReader = new BEBinaryReader(frame.Body);

            // If a response frame has the tracing flag set, its body contains a tracing ID.
            // the frame body.
            if ((frame.Header.Flags & FrameHeader.HeaderFlag.Tracing) != 0)
            {
                var buffer = new byte[16];
                BeBinaryReader.Read(buffer, 0, 16);
                TraceId = new Guid(TypeCodec.GuidShuffle(buffer));
            }
        }
Esempio n. 6
0
        public void TokenAwareTargetsPartitionGuidNoHopsQueryTest()
        {
            var builder     = Cluster.Builder().WithLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()));
            var clusterInfo = TestUtils.CcmSetup(4, builder);

            try
            {
                var session = clusterInfo.Session;
                session.Execute(String.Format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, TestUtils.SIMPLE_KEYSPACE, 1));
                Thread.Sleep(3000);
                session.ChangeKeyspace(TestUtils.SIMPLE_KEYSPACE);
                Thread.Sleep(3000);
                session.Execute(String.Format("CREATE TABLE {0} (k uuid PRIMARY KEY, i int)", TABLE));
                var traces = new List <QueryTrace>();
                for (var i = 0; i < 10; i++)
                {
                    var key       = Guid.NewGuid();
                    var statement = new SimpleStatement(String.Format("INSERT INTO test (k, i) VALUES ({0}, {1})", key, i))
                                    .SetRoutingKey(
                        new RoutingKey()
                    {
                        RawRoutingKey = TypeCodec.GuidShuffle(key.ToByteArray())
                    })
                                    .EnableTracing();
                    var rs = session.Execute(statement);
                    traces.Add(rs.Info.QueryTrace);
                }
                //Check that there weren't any hops
                foreach (var t in traces)
                {
                    //The coordinator must be the only one executing the query
                    Assert.True(t.Events.All(e => e.Source.ToString() == t.Coordinator.ToString()), "There were trace events from another host for coordinator " + t.Coordinator);
                }
            }
            finally
            {
                TestUtils.CcmRemove(clusterInfo);
            }
        }
Esempio n. 7
0
        public void TokenAware_Guid_NoHops()
        {
            // Setup
            PolicyTestTools policyTestTools = new PolicyTestTools();
            ITestCluster    testCluster     = TestClusterManager.GetTestCluster(3);

            testCluster.Builder = Cluster.Builder().WithLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()));
            testCluster.InitClient();

            // Test
            var    session         = testCluster.Session;
            string uniqueTableName = TestUtils.GetUniqueTableName();

            policyTestTools.CreateSchema(session);
            session.Execute(String.Format("CREATE TABLE {0} (k uuid PRIMARY KEY, i int)", uniqueTableName));
            var traces = new List <QueryTrace>();

            for (var i = 0; i < 10; i++)
            {
                var key       = Guid.NewGuid();
                var statement = new SimpleStatement(String.Format("INSERT INTO " + uniqueTableName + " (k, i) VALUES ({0}, {1})", key, i))
                                .SetRoutingKey(
                    new RoutingKey()
                {
                    RawRoutingKey = TypeCodec.GuidShuffle(key.ToByteArray())
                })
                                .EnableTracing();
                var rs = session.Execute(statement);
                traces.Add(rs.Info.QueryTrace);
            }
            //Check that there weren't any hops
            foreach (var t in traces)
            {
                //The coordinator must be the only one executing the query
                Assert.True(t.Events.All(e => e.Source.ToString() == t.Coordinator.ToString()), "There were trace events from another host for coordinator " + t.Coordinator);
            }
        }