Inheritance: IDisposable
Exemplo 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;
        }
Exemplo 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);
 }
Exemplo 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;
 }
Exemplo 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;
 }
Exemplo 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;
        }
Exemplo 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;
        }
Exemplo 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;
        }
Exemplo 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;
        }
Exemplo 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);
            }
        }
Exemplo 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;
        }
Exemplo 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;
        }
Exemplo n.º 12
0
 public override void WriteToStream(BinaryStream buffer)
 {
     buffer.Writer.Write(FetchRequests.Count);
     foreach (var fetchRequest in FetchRequests)
         fetchRequest.WriteToStream(buffer);
 }
Exemplo n.º 13
0
 public abstract void WriteToStream(BinaryStream buffer);
Exemplo n.º 14
0
 public abstract void WriteToStream(BinaryStream stream);
Exemplo 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;
        }
Exemplo n.º 16
0
 public static BrokerInfoRequest ReadFromStream(BinaryStream buffer)
 {
     return new BrokerInfoRequest();
 }
Exemplo n.º 17
0
 public override void WriteToStream(BinaryStream buffer)
 {
 }
Exemplo 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;
        }
Exemplo n.º 19
0
 public override void WriteToStream(BinaryStream stream)
 {
     stream.Writer.Write(Partition);
     stream.Writer.Write(Data.Length);
     stream.Writer.Write(Data);
 }
Exemplo n.º 20
0
 public MessageWriter(BinaryStream buffer)
 {
     _buffer = buffer;
     _writer = buffer.Writer;
 }
Exemplo n.º 21
0
 public MessageReader(BinaryStream buffer)
 {
     _buffer = buffer;
     _stream = buffer.Stream;
     _reader = buffer.Reader;
 }
Exemplo n.º 22
0
 public override void WriteToStream(BinaryStream stream)
 {
     stream.Writer.Write(FetchResponses.Count);
     foreach (var fetchResponse in FetchResponses)
         fetchResponse.WriteToStream(stream);
 }
Exemplo n.º 23
0
 public override void WriteToStream(BinaryStream buffer)
 {
     buffer.Writer.Write(Topic);
     buffer.Writer.Write(Partition);
     new MessageWriter(buffer).WriteMessage(Messages);
 }