private Stream Connect_InitiatorSide(string fromVertexName, string fromVertexOutput, string toVertexName, string toVertexInput, bool reverse) { bool killRemote = true; // we have no way of receiving connections var _vertexTableManager = _clientLibrary._vertexTableManager; // Need to get the latest address & port var row = reverse ? _vertexTableManager.GetRowForActiveVertex(fromVertexName) : _vertexTableManager.GetRowForActiveVertex(toVertexName); var _row = _vertexTableManager.GetRowForInstance(row.InstanceName); // Send request to CRA instance Stream ns = null; try { if (!_clientLibrary.TryGetSenderStreamFromPool(_row.Address, _row.Port.ToString(), out ns)) { TcpClient client = new TcpClient(_row.Address, _row.Port); client.NoDelay = true; ns = _clientLibrary.SecureStreamConnectionDescriptor .CreateSecureClient(client.GetStream(), row.InstanceName); } } catch { return(null); } if (!reverse) { ns.WriteInt32((int)CRATaskMessageType.CONNECT_VERTEX_RECEIVER); } else { ns.WriteInt32((int)CRATaskMessageType.CONNECT_VERTEX_RECEIVER_REVERSE); } ns.WriteByteArray(Encoding.UTF8.GetBytes(fromVertexName)); ns.WriteByteArray(Encoding.UTF8.GetBytes(fromVertexOutput)); ns.WriteByteArray(Encoding.UTF8.GetBytes(toVertexName)); ns.WriteByteArray(Encoding.UTF8.GetBytes(toVertexInput)); ns.WriteInt32(killRemote ? 1 : 0); CRAErrorCode result = (CRAErrorCode)ns.ReadInt32(); if (result != 0) { Debug.WriteLine("Client received error code: " + result); ns.Dispose(); return(null); } else { return(ns); } }
protected override void WriteData(Stream buffer) { buffer.WriteShortByteArray(QueryId); buffer.WriteShort((short)Parameters.Count); foreach (var prm in Parameters) buffer.WriteByteArray(prm); buffer.WriteShort((short)CqlConsistency); }
protected override void WriteData(Stream buffer) { buffer.WriteShortByteArray(QueryId); buffer.WriteShort((short)Parameters.Count); foreach (var prm in Parameters) { buffer.WriteByteArray(prm); } buffer.WriteShort((short)CqlConsistency); }
protected override void WriteFrame(IFrameWriter fw) { Stream stream = fw.WriteOnlyStream; stream.WriteShortByteArray(_id); stream.WriteUShort((ushort)_columnSpecs.Length); foreach (var columnData in _mapperIn.MapToColumns(_dataSource, _columnSpecs)) { stream.WriteByteArray(columnData.RawData); } stream.WriteUShort((ushort)ConsistencyLevel); fw.SetMessageType(MessageOpcodes.Batch); }
public void Serialize(Stream stream) { stream.WritePascalString(TableName); stream.WritePascalString(PartitionKey); stream.WriteByteArray(Data); }
internal async Task <CRAErrorCode> Connect_InitiatorSide( string fromVertexName, string fromVertexOutput, string toVertexName, string toVertexInput, bool reverse, bool sharding = true, bool killIfExists = true, bool killRemote = true) { VertexInfo row; try { // Need to get the latest address & port row = (await(reverse ? _vertexInfoProvider.GetRowForActiveVertex(fromVertexName) : _vertexInfoProvider.GetRowForActiveVertex(toVertexName))).Value; } catch { return(CRAErrorCode.ActiveVertexNotFound); } // If from and to vertices are on the same (this) instance, // we can convert a "reverse" connection into a normal connection if (reverse && (row.InstanceName == InstanceName)) { reverse = false; } CancellationTokenSource oldSource; var conn = reverse ? inConnections : outConnections; if (conn.TryGetValue(fromVertexName + ":" + fromVertexOutput + ":" + toVertexName + ":" + toVertexInput, out oldSource)) { if (killIfExists) { Debug.WriteLine("Deleting prior connection - it will automatically reconnect"); oldSource.Cancel(); } return(CRAErrorCode.Success); } if (TryFusedConnect(row.InstanceName, fromVertexName, fromVertexOutput, toVertexName, toVertexInput)) { return(CRAErrorCode.Success); } // Re-check the connection table as someone may have successfully // created a fused connection if (conn.TryGetValue(fromVertexName + ":" + fromVertexOutput + ":" + toVertexName + ":" + toVertexInput, out oldSource)) { if (killIfExists) { Debug.WriteLine("Deleting prior connection - it will automatically reconnect"); oldSource.Cancel(); } return(CRAErrorCode.Success); } // Send request to CRA instance Stream ns = null; var _row = (await _vertexInfoProvider.GetRowForInstanceVertex(row.InstanceName, "")) .Value; try { // Get a stream connection from the pool if available if (!_craClient.TryGetSenderStreamFromPool(_row.Address, _row.Port.ToString(), out ns)) { var client = new TcpClient(); client.NoDelay = true; if (!client.ConnectAsync(_row.Address, _row.Port).Wait(_tcpConnectTimeoutMs)) { throw new Exception("Failed to connect."); } ns = _craClient.SecureStreamConnectionDescriptor .CreateSecureClient(client.GetStream(), row.InstanceName); } } catch { return(CRAErrorCode.ConnectionEstablishFailed); } if (!reverse) { ns.WriteInt32((int)CRATaskMessageType.CONNECT_VERTEX_RECEIVER); } else { ns.WriteInt32((int)CRATaskMessageType.CONNECT_VERTEX_RECEIVER_REVERSE); } ns.WriteByteArray(Encoding.UTF8.GetBytes(fromVertexName)); ns.WriteByteArray(Encoding.UTF8.GetBytes(fromVertexOutput)); ns.WriteByteArray(Encoding.UTF8.GetBytes(toVertexName)); ns.WriteByteArray(Encoding.UTF8.GetBytes(toVertexInput)); ns.WriteInt32(killRemote ? 1 : 0); CRAErrorCode result = (CRAErrorCode)ns.ReadInt32(); if (result != 0) { Debug.WriteLine("Error occurs while establishing the connection!!"); return(result); } else { CancellationTokenSource source = new CancellationTokenSource(); if (!reverse) { if (outConnections.TryAdd(fromVertexName + ":" + fromVertexOutput + ":" + toVertexName + ":" + toVertexInput, source)) { var tmp = Task.Run(() => EgressToStream( fromVertexName, fromVertexOutput, toVertexName, toVertexInput, reverse, ns, source, _row.Address, _row.Port, sharding)); return(CRAErrorCode.Success); } else { source.Dispose(); ns.Close(); Console.WriteLine("Race adding connection - deleting outgoing stream"); return(CRAErrorCode.ConnectionAdditionRace); } } else { if (inConnections.TryAdd(fromVertexName + ":" + fromVertexOutput + ":" + toVertexName + ":" + toVertexInput, source)) { var tmp = Task.Run(() => IngressFromStream( fromVertexName, fromVertexOutput, toVertexName, toVertexInput, reverse, ns, source, _row.Address, _row.Port, sharding)); return(CRAErrorCode.Success); } else { source.Dispose(); ns.Close(); Debug.WriteLine("Race adding connection - deleting outgoing stream"); return(CRAErrorCode.ConnectionAdditionRace); } } } }
/// <summary> /// Write uint 16 /// </summary> /// <param name="stream"></param> /// <param name="value"></param> /// <param name="netOrder"></param> /// <returns></returns> public static void WriteUInt16(this Stream stream, ushort value, bool netOrder = false) { stream.WriteByteArray(value.ToBytes(netOrder)); }
/// <summary> /// Writes the data to buffer. /// </summary> /// <param name="buffer"> The buffer. </param> protected override void WriteData(Stream buffer) { if(ProtocolVersion == 1) { throw new ProtocolException(ProtocolVersion, ErrorCode.Protocol, "Batch frames are supported from Cassandra Version 2.0.0 and up."); } buffer.WriteByte((byte)Type); buffer.WriteShort((ushort)Commands.Count); foreach(var command in Commands) { if(command.IsPrepared) { buffer.WriteByte(1); buffer.WriteShortByteArray(command.QueryId); } else { buffer.WriteByte(0); buffer.WriteLongString(command.CqlQuery); } if(command.ParameterValues != null) { byte[][] paramValues = command.ParameterValues.Serialize(ProtocolVersion); var length = (ushort)paramValues.Length; buffer.WriteShort(length); for(var i = 0; i < length; i++) { buffer.WriteByteArray(paramValues[i]); } } else buffer.WriteShort(0); } buffer.WriteConsistency(CqlConsistency); if(ProtocolVersion >= 3) { var flags = (byte)((SerialConsistency.HasValue ? 16 : 0) | (Timestamp.HasValue ? 32 : 0)); buffer.WriteByte(flags); if(SerialConsistency.HasValue) buffer.WriteShort((ushort)SerialConsistency.Value); if(Timestamp.HasValue) buffer.WriteLong(Timestamp.Value.ToTimestamp()); } }
/// <summary> /// Writes the query parameters. /// </summary> /// <param name="buffer">The buffer.</param> protected void WriteQueryParameters(Stream buffer) { buffer.WriteConsistency(CqlConsistency); var flags = (byte)((Parameters != null ? 1 : 0) | (SkipMetaData ? 2 : 0) | (PageSize.HasValue ? 4 : 0) | (PagingState != null ? 8 : 0) | (SerialConsistency.HasValue ? 16 : 0) | (ProtocolVersion >= 3 && Timestamp.HasValue ? 32 : 0)); buffer.WriteByte(flags); if(Parameters != null) { buffer.WriteShort((ushort)Parameters.Count); foreach(var value in Parameters) buffer.WriteByteArray(value); } if(PageSize.HasValue) buffer.WriteInt(PageSize.Value); if(PagingState != null) buffer.WriteByteArray(PagingState); if(SerialConsistency.HasValue) buffer.WriteShort((ushort)SerialConsistency.Value); if(ProtocolVersion >= 3 && Timestamp.HasValue) buffer.WriteLong(Timestamp.Value.ToTimestamp()*1000); //convert milliseconds to microseconds }
protected override void WriteData(Stream buffer) { buffer.WriteByteArray(SaslResponse); }