|
| 1 | +package jocko |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/travisjeffery/jocko/commitlog" |
| 8 | + "github.com/travisjeffery/jocko/protocol" |
| 9 | +) |
| 10 | + |
| 11 | +func (b *Broker) handleFetchSendFile(ctx *Context, r *protocol.FetchRequest) { |
| 12 | + sp := span(ctx, b.tracer, "fetch") |
| 13 | + defer sp.Finish() |
| 14 | + resp := protocol.Response{ |
| 15 | + CorrelationID: ctx.header.CorrelationID, |
| 16 | + } |
| 17 | + conn := ctx.Conn |
| 18 | + fres := &protocol.FetchResponse{ |
| 19 | + Responses: make(protocol.FetchTopicResponses, len(r.Topics)), |
| 20 | + } |
| 21 | + msgSetLen := 0 |
| 22 | + fres.APIVersion = r.APIVersion |
| 23 | + maxBufSize := 0 |
| 24 | + // calculate total length of message set |
| 25 | + //TODO calc max buf length |
| 26 | + maxBufSize = 1024 |
| 27 | + for i, topic := range r.Topics { |
| 28 | + fr := &protocol.FetchTopicResponse{ |
| 29 | + Topic: topic.Topic, |
| 30 | + PartitionResponses: make([]*protocol.FetchPartitionResponse, len(topic.Partitions)), |
| 31 | + } |
| 32 | + for j, p := range topic.Partitions { |
| 33 | + fpres := &protocol.FetchPartitionResponse{} |
| 34 | + fpres.Partition = p.Partition |
| 35 | + replica, err := b.replicaLookup.Replica(topic.Topic, p.Partition) |
| 36 | + if err != nil { |
| 37 | + panic(err) |
| 38 | + } |
| 39 | + var rdrErr error |
| 40 | + fpres.FileHandle, fpres.SendOffset, fpres.SendSize, rdrErr = replica.Log.SendfileParams(p.FetchOffset, p.MaxBytes) |
| 41 | + if rdrErr != nil { |
| 42 | + panic(rdrErr) |
| 43 | + } |
| 44 | + msgSetLen += fpres.SendSize |
| 45 | + //get length of record |
| 46 | + // |
| 47 | + fr.PartitionResponses[j] = fpres |
| 48 | + } |
| 49 | + fres.Responses[i] = fr |
| 50 | + } |
| 51 | + lenEnc := new(protocol.LenEncoder) |
| 52 | + err := fres.Encode(lenEnc) |
| 53 | + if err != nil { |
| 54 | + panic(err) |
| 55 | + } |
| 56 | + // set length field |
| 57 | + resp.Size = int32(lenEnc.Length + msgSetLen) |
| 58 | + err = sendRes(&resp, maxBufSize, fres, conn) |
| 59 | + if err != nil { |
| 60 | + panic(err) |
| 61 | + } |
| 62 | + return |
| 63 | +} |
| 64 | +func sendRes(resp *protocol.Response, |
| 65 | + maxSize int, |
| 66 | + r *protocol.FetchResponse, |
| 67 | + conn *net.TCPConn) error { |
| 68 | + b := make([]byte, maxSize) |
| 69 | + e := protocol.NewByteEncoder(b) |
| 70 | + // outer response |
| 71 | + correlationIDSize := int32(4) |
| 72 | + e.PutInt32(resp.Size + correlationIDSize) |
| 73 | + e.PutInt32(resp.CorrelationID) |
| 74 | + //fetch response |
| 75 | + var err error |
| 76 | + if r.APIVersion >= 1 { |
| 77 | + e.PutInt32(int32(r.ThrottleTime / time.Millisecond)) |
| 78 | + } |
| 79 | + |
| 80 | + if err = e.PutArrayLength(len(r.Responses)); err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + for _, response := range r.Responses { |
| 84 | + if err = e.PutString(response.Topic); err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + if err = e.PutArrayLength(len(response.PartitionResponses)); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + for _, p := range response.PartitionResponses { |
| 91 | + if err = sendResOfPartition(conn, p, e, r.APIVersion); err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + return nil |
| 97 | +} |
| 98 | +func sendResOfPartition( |
| 99 | + conn *net.TCPConn, |
| 100 | + r *protocol.FetchPartitionResponse, |
| 101 | + e *protocol.ByteEncoder, |
| 102 | + version int16) error { |
| 103 | + e.PutInt32(r.Partition) |
| 104 | + e.PutInt16(r.ErrorCode) |
| 105 | + e.PutInt64(r.HighWatermark) |
| 106 | + var err error |
| 107 | + if version >= 4 { |
| 108 | + e.PutInt64(r.LastStableOffset) |
| 109 | + |
| 110 | + if err = e.PutArrayLength(len(r.AbortedTransactions)); err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + for _, t := range r.AbortedTransactions { |
| 114 | + t.Encode(e) |
| 115 | + } |
| 116 | + } |
| 117 | + e.PutInt32(int32(r.SendSize)) |
| 118 | + //log.Info.Println("encoder offset", e.GetOffset()) |
| 119 | + conn.Write(e.Bytes()[:e.GetOffset()]) |
| 120 | + e.SetOffset(0) |
| 121 | + chunkSize := 4096 |
| 122 | + if _, err = commitlog.Sendfile(conn, r.FileHandle, r.SendOffset, r.SendSize, chunkSize); err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + return nil |
| 127 | +} |
0 commit comments