-
-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
294 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package commitlog | ||
|
||
import ( | ||
"log" | ||
"net" | ||
"os" | ||
"runtime" | ||
"syscall" | ||
) | ||
|
||
const maxSendfileSize int = 4 << 20 | ||
|
||
func Sendfile(conn *net.TCPConn, file *os.File, offsetInt int64, size int, chunkSize int) (int, error) { | ||
offset := &offsetInt | ||
defer func() { | ||
runtime.KeepAlive(offset) | ||
}() | ||
written := 0 | ||
var remain int = size | ||
n := chunkSize | ||
if chunkSize > maxSendfileSize { | ||
chunkSize = maxSendfileSize | ||
} | ||
src := int(file.Fd()) | ||
rawConn, err := conn.SyscallConn() | ||
rawConn.Write(func(dst uintptr) bool { | ||
defer func() { log.Println("returned") }() | ||
for remain > 0 { | ||
if n > remain { | ||
n = remain | ||
} | ||
var err1 error | ||
log.Println("params:", n, "offset:", *offset) | ||
//todo: for bsd and darwin, pass different offset | ||
n, err1 = syscall.Sendfile(int(dst), src, offset, n) | ||
log.Println("after:", n, "offset:", *offset) | ||
if err1 != nil { | ||
log.Println("sent error:", err1.Error()) | ||
} | ||
if n > 0 { | ||
written += n | ||
remain -= n | ||
} else if n == 0 && err1 == nil { | ||
break | ||
} | ||
if err1 == syscall.EAGAIN || err1 == syscall.EWOULDBLOCK { | ||
|
||
if n == -1 { | ||
n = chunkSize | ||
} | ||
log.Println("got eagain") | ||
return false | ||
// waitpread, gopark | ||
} | ||
if err1 != nil { | ||
// This includes syscall.ENOSYS (no kernel | ||
// support) and syscall.EINVAL (fd types which | ||
// don't implement sendfile) | ||
err = err1 | ||
break | ||
} | ||
} | ||
return true | ||
}) | ||
log.Println("written", written) | ||
|
||
return written, err | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package jocko | ||
|
||
import ( | ||
"net" | ||
"time" | ||
|
||
"github.com/travisjeffery/jocko/commitlog" | ||
"github.com/travisjeffery/jocko/protocol" | ||
) | ||
|
||
func (b *Broker) handleFetchSendFile(ctx *Context, r *protocol.FetchRequest) { | ||
sp := span(ctx, b.tracer, "fetch") | ||
defer sp.Finish() | ||
resp := protocol.Response{ | ||
CorrelationID: ctx.header.CorrelationID, | ||
} | ||
conn := ctx.Conn | ||
fres := &protocol.FetchResponse{ | ||
Responses: make(protocol.FetchTopicResponses, len(r.Topics)), | ||
} | ||
msgSetLen := 0 | ||
fres.APIVersion = r.APIVersion | ||
maxBufSize := 0 | ||
// calculate total length of message set | ||
//TODO calc max buf length | ||
maxBufSize = 1024 | ||
for i, topic := range r.Topics { | ||
fr := &protocol.FetchTopicResponse{ | ||
Topic: topic.Topic, | ||
PartitionResponses: make([]*protocol.FetchPartitionResponse, len(topic.Partitions)), | ||
} | ||
for j, p := range topic.Partitions { | ||
fpres := &protocol.FetchPartitionResponse{} | ||
fpres.Partition = p.Partition | ||
replica, err := b.replicaLookup.Replica(topic.Topic, p.Partition) | ||
if err != nil { | ||
panic(err) | ||
} | ||
var rdrErr error | ||
fpres.FileHandle, fpres.SendOffset, fpres.SendSize, rdrErr = replica.Log.SendfileParams(p.FetchOffset, p.MaxBytes) | ||
if rdrErr != nil { | ||
panic(rdrErr) | ||
} | ||
msgSetLen += fpres.SendSize | ||
//get length of record | ||
// | ||
fr.PartitionResponses[j] = fpres | ||
} | ||
fres.Responses[i] = fr | ||
} | ||
lenEnc := new(protocol.LenEncoder) | ||
err := fres.Encode(lenEnc) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// set length field | ||
resp.Size = int32(lenEnc.Length + msgSetLen) | ||
err = sendRes(&resp, maxBufSize, fres, conn) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return | ||
} | ||
func sendRes(resp *protocol.Response, | ||
maxSize int, | ||
r *protocol.FetchResponse, | ||
conn *net.TCPConn) error { | ||
b := make([]byte, maxSize) | ||
e := protocol.NewByteEncoder(b) | ||
// outer response | ||
correlationIDSize := int32(4) | ||
e.PutInt32(resp.Size + correlationIDSize) | ||
e.PutInt32(resp.CorrelationID) | ||
//fetch response | ||
var err error | ||
if r.APIVersion >= 1 { | ||
e.PutInt32(int32(r.ThrottleTime / time.Millisecond)) | ||
} | ||
|
||
if err = e.PutArrayLength(len(r.Responses)); err != nil { | ||
return err | ||
} | ||
for _, response := range r.Responses { | ||
if err = e.PutString(response.Topic); err != nil { | ||
return err | ||
} | ||
if err = e.PutArrayLength(len(response.PartitionResponses)); err != nil { | ||
return err | ||
} | ||
for _, p := range response.PartitionResponses { | ||
if err = sendResOfPartition(conn, p, e, r.APIVersion); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
func sendResOfPartition( | ||
conn *net.TCPConn, | ||
r *protocol.FetchPartitionResponse, | ||
e *protocol.ByteEncoder, | ||
version int16) error { | ||
e.PutInt32(r.Partition) | ||
e.PutInt16(r.ErrorCode) | ||
e.PutInt64(r.HighWatermark) | ||
var err error | ||
if version >= 4 { | ||
e.PutInt64(r.LastStableOffset) | ||
|
||
if err = e.PutArrayLength(len(r.AbortedTransactions)); err != nil { | ||
return err | ||
} | ||
for _, t := range r.AbortedTransactions { | ||
t.Encode(e) | ||
} | ||
} | ||
e.PutInt32(int32(r.SendSize)) | ||
//log.Info.Println("encoder offset", e.GetOffset()) | ||
conn.Write(e.Bytes()[:e.GetOffset()]) | ||
e.SetOffset(0) | ||
chunkSize := 4096 | ||
if _, err = commitlog.Sendfile(conn, r.FileHandle, r.SendOffset, r.SendSize, chunkSize); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.