public Response HandleAppendMessages(BinaryStream buffer) { var request = AppendRequest.ReadFromStream(buffer); if (!_storage.ValidatePartitionNumber(request.Topic, request.Partition)) return null; for (int i = 0; i < request.Messages.Count; i++) { var message = request.Messages[i]; //For testing purpose only if (message.Payload.Length < 20) { var text = Encoding.UTF8.GetString(message.Payload); if (text == "end!//") Console.WriteLine("Done!"); } _storage.Append(request.Topic, request.Partition, message.Payload); // Flushing to OS cashe _storage.Flush(); } return null; }
public override void WriteToStream(BinaryStream buffer) { buffer.Writer.Write(Topic); buffer.Writer.Write(Partition); buffer.Writer.Write(Offset); buffer.Writer.Write(BlockSize); }
public static FetchResponse ReadFromStream(BinaryStream stream) { var request = new FetchResponse(); request.Partition = stream.Reader.ReadInt32(); var length = stream.Reader.ReadInt32(); request.Data = stream.Reader.ReadBytes(length); return request; }
public static AppendRequest ReadFromStream(BinaryStream buffer) { var request = new AppendRequest(); request.Topic = buffer.Reader.ReadString(); request.Partition = buffer.Reader.ReadInt32(); request.Messages = new MessageReader(buffer).ReadAllMessages().ToList(); return request; }
public static FetchRequest ReadFromStream(BinaryStream buffer) { var request = new FetchRequest { Topic = buffer.Reader.ReadString(), Partition = buffer.Reader.ReadInt32(), Offset = buffer.Reader.ReadInt32(), BlockSize = buffer.Reader.ReadInt32() }; return request; }
public static MultiFetchResponse ReadFromStream(BinaryStream stream) { var request = new MultiFetchResponse(); var length = stream.Reader.ReadInt32(); request.FetchResponses = new List<FetchResponse>(length); for (int i = 0; i < length; i++) { request.FetchResponses.Add(FetchResponse.ReadFromStream(stream)); } return request; }
public Response HandleBrokerInfo(BinaryStream stream) { // Not used for now var request = BrokerInfoRequest.ReadFromStream(stream); var response = new BrokerInfoResponse(); response.HostName = _configuration.HostName; response.BrokerId = _configuration.BrokerId; response.PullPort = _configuration.PullPort; response.NumberOfPartitions = _configuration.NumberOfPartitions; response.NumberOfPartitionsPerTopic = _configuration.NumberOfPartitionsPerTopic; return response; }
public static MultiFetchRequest ReadFromStream(BinaryStream buffer) { var result = new MultiFetchRequest(); var count = buffer.Reader.ReadInt32(); result.FetchRequests = new List<FetchRequest>(count); for(int i = 0; i < count; i++) { var requestType = buffer.Reader.ReadInt16(); result.FetchRequests.Add(FetchRequest.ReadFromStream(buffer)); } return result; }
public override void WriteToStream(BinaryStream stream) { stream.Writer.Write(HostName); stream.Writer.Write(BrokerId); stream.Writer.Write(PullPort); stream.Writer.Write(NumberOfPartitions); // Writing dictionary of <String, Int32> stream.Writer.Write(NumberOfPartitionsPerTopic.Count); foreach (var pair in NumberOfPartitionsPerTopic) { stream.Writer.Write(pair.Key); stream.Writer.Write(pair.Value); } }
public Response HandleLoadMessages(BinaryStream buffer) { var request = FetchRequest.ReadFromStream(buffer); if (!_storage.ValidatePartitionNumber(request.Topic, request.Partition)) return null; var block = _storage.ReadMessagesBlock(request.Topic, request.Partition, request.Offset, request.BlockSize); var response = new FetchResponse(); response.Partition = request.Partition; response.Data = (block.Length == 0) ? new byte[0] : block.Data; return response; }
public static BrokerInfoResponse ReadFromStream(BinaryStream stream) { var request = new BrokerInfoResponse(); request.HostName = stream.Reader.ReadString(); request.BrokerId = stream.Reader.ReadInt32(); request.PullPort = stream.Reader.ReadInt32(); request.NumberOfPartitions = stream.Reader.ReadInt32(); // Reading dictionary of <String, Int32> var length = stream.Reader.ReadInt32(); for (int i = 0; i < length; i++) { var topic = stream.Reader.ReadString(); var partitionsNumber = stream.Reader.ReadInt32(); request.NumberOfPartitionsPerTopic.Add(topic, partitionsNumber); } return request; }
public override void WriteToStream(BinaryStream buffer) { buffer.Writer.Write(FetchRequests.Count); foreach (var fetchRequest in FetchRequests) fetchRequest.WriteToStream(buffer); }
public abstract void WriteToStream(BinaryStream buffer);
public abstract void WriteToStream(BinaryStream stream);
public Response HandleMultiFetch(BinaryStream buffer) { var count = buffer.Reader.ReadInt32(); MultiFetchResponse response = new MultiFetchResponse(); response.FetchResponses = new List<FetchResponse>(); for (int i = 0; i < count; i++) { var result = HandleLoadMessages(buffer); response.FetchResponses.Add((FetchResponse) result); } return response; }
public static BrokerInfoRequest ReadFromStream(BinaryStream buffer) { return new BrokerInfoRequest(); }
public override void WriteToStream(BinaryStream buffer) { }
/// <summary> /// Map request to the handler function of the following signature: /// Response SomeHandler(BinaryStream) /// </summary> public Func<BinaryStream, Response> MapHandlers(RequestType requestType, BinaryStream buffer) { switch (requestType) { case RequestType.AppendRequest: return HandleAppendMessages; case RequestType.FetchRequest: return HandleLoadMessages; case RequestType.MultiFetchRequest: return HandleMultiFetch; case RequestType.BrokerInfoRequest: return HandleBrokerInfo; } return null; }
public override void WriteToStream(BinaryStream stream) { stream.Writer.Write(Partition); stream.Writer.Write(Data.Length); stream.Writer.Write(Data); }
public MessageWriter(BinaryStream buffer) { _buffer = buffer; _writer = buffer.Writer; }
public MessageReader(BinaryStream buffer) { _buffer = buffer; _stream = buffer.Stream; _reader = buffer.Reader; }
public override void WriteToStream(BinaryStream stream) { stream.Writer.Write(FetchResponses.Count); foreach (var fetchResponse in FetchResponses) fetchResponse.WriteToStream(stream); }
public override void WriteToStream(BinaryStream buffer) { buffer.Writer.Write(Topic); buffer.Writer.Write(Partition); new MessageWriter(buffer).WriteMessage(Messages); }