Inheritance: IDisposable
Esempio n. 1
0
        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;
        }
Esempio n. 2
0
 public override void WriteToStream(BinaryStream buffer)
 {
     buffer.Writer.Write(Topic);
     buffer.Writer.Write(Partition);
     buffer.Writer.Write(Offset);
     buffer.Writer.Write(BlockSize);
 }
Esempio n. 3
0
 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;
 }
Esempio n. 4
0
 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;
 }
Esempio n. 5
0
        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;
        }
Esempio n. 6
0
        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;
        }
Esempio n. 7
0
        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;
        }
Esempio n. 8
0
        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;
        }
Esempio n. 9
0
        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);
            }
        }
Esempio n. 10
0
        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;
        }
Esempio n. 11
0
        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;
        }
Esempio n. 12
0
 public override void WriteToStream(BinaryStream buffer)
 {
     buffer.Writer.Write(FetchRequests.Count);
     foreach (var fetchRequest in FetchRequests)
         fetchRequest.WriteToStream(buffer);
 }
Esempio n. 13
0
 public abstract void WriteToStream(BinaryStream buffer);
Esempio n. 14
0
 public abstract void WriteToStream(BinaryStream stream);
Esempio n. 15
0
        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;
        }
Esempio n. 16
0
 public static BrokerInfoRequest ReadFromStream(BinaryStream buffer)
 {
     return new BrokerInfoRequest();
 }
Esempio n. 17
0
 public override void WriteToStream(BinaryStream buffer)
 {
 }
Esempio n. 18
0
        /// <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;
        }
Esempio n. 19
0
 public override void WriteToStream(BinaryStream stream)
 {
     stream.Writer.Write(Partition);
     stream.Writer.Write(Data.Length);
     stream.Writer.Write(Data);
 }
Esempio n. 20
0
 public MessageWriter(BinaryStream buffer)
 {
     _buffer = buffer;
     _writer = buffer.Writer;
 }
Esempio n. 21
0
 public MessageReader(BinaryStream buffer)
 {
     _buffer = buffer;
     _stream = buffer.Stream;
     _reader = buffer.Reader;
 }
Esempio n. 22
0
 public override void WriteToStream(BinaryStream stream)
 {
     stream.Writer.Write(FetchResponses.Count);
     foreach (var fetchResponse in FetchResponses)
         fetchResponse.WriteToStream(stream);
 }
Esempio n. 23
0
 public override void WriteToStream(BinaryStream buffer)
 {
     buffer.Writer.Write(Topic);
     buffer.Writer.Write(Partition);
     new MessageWriter(buffer).WriteMessage(Messages);
 }