Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handle HandleStreamingReader (performance improvements) #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions buffer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package fuse

import "unsafe"
import (
"bytes"
"sync"
"unsafe"
)

// buffer provides a mechanism for constructing a message from
// multiple segments.
type buffer []byte

const hdrSize = unsafe.Sizeof(outHeader{})

// alloc allocates size bytes and returns a pointer to the new
// segment.
func (w *buffer) alloc(size uintptr) unsafe.Pointer {
Expand All @@ -29,7 +35,24 @@ func (w *buffer) reset() {
}

func newBuffer(extra uintptr) buffer {
const hdrSize = unsafe.Sizeof(outHeader{})
buf := make(buffer, hdrSize, hdrSize+extra)
return buf
}

// readBufPool is a pool of read request data
var readBufPool = sync.Pool{
New: func() interface{} {
return make([]byte, int(hdrSize)+maxWrite)
},
}

func newStreamingBuffer() *bytes.Buffer {
buf := bytes.NewBuffer(readBufPool.Get().([]byte))
buf.Truncate(int(hdrSize))

return buf
}

func returnBuffer(buf []byte) {
readBufPool.Put(buf)
}
29 changes: 27 additions & 2 deletions fs/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@ type HandleReader interface {
Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error
}

type HandleStreamingReader interface {
// StreamingRead requests to read data from the handle.
//
// In contrast to `HandleReader` this interface uses a
// StreamingReadResponse which implements the io.Writer
// interface.

StreamingRead(ctx context.Context, req *fuse.ReadRequest, resp *fuse.StreamingReadResponse) error
}

type HandleWriter interface {
// Write requests to write data into the handle at the given offset.
// Store the amount of data written in resp.Size.
Expand Down Expand Up @@ -1241,14 +1251,29 @@ func (c *Server) handleRequest(ctx context.Context, node Node, snode *serveNode,
r.Respond(s)
return nil
}
h, ok := handle.(HandleReader)
if h, ok := handle.(HandleReader); ok {
if err := h.Read(ctx, r, s); err != nil {
return err
}

done(s)
r.Respond(s)
return nil
}
h, ok := handle.(HandleStreamingReader)
if !ok {
err := handleNotReaderError{handle: handle}
return err
}
if err := h.Read(ctx, r, s); err != nil {

s := fuse.NewStreamingReadResponse()
if err := h.StreamingRead(ctx, r, s); err != nil {
return err
}

done(s)
r.StreamingRespond(s)
return nil
}
done(s)
r.Respond(s)
Expand Down
31 changes: 31 additions & 0 deletions fs/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,37 @@ func TestReadAllWithHandleRead(t *testing.T) {
testReadAll(t, mnt.Dir+"/child")
}

type readWithStreamingHandleRead struct {
}

func (readWithStreamingHandleRead) Attr(ctx context.Context, a *fuse.Attr) error {
a.Mode = 0666
a.Size = uint64(len(hi))

return nil
}

func (readWithStreamingHandleRead) StreamingRead(ctx context.Context, req *fuse.ReadRequest, resp *fuse.StreamingReadResponse) error {
_, err := io.Copy(resp, bytes.NewReader([]byte(hi)))
if err != nil {
return fuse.EIO
}

return nil
}

func TestStreamingReadAllWithHandleRead(t *testing.T) {
t.Parallel()
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{&fstestutil.ChildMap{"child": readWithStreamingHandleRead{}}}, nil)

if err != nil {
t.Fatal(err)
}
defer mnt.Close()

testReadAll(t, mnt.Dir+"/child")
}

type readFlags struct {
fstestutil.File
fileFlags record.Recorder
Expand Down
28 changes: 28 additions & 0 deletions fuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,12 @@ func (r *ReadRequest) Respond(resp *ReadResponse) {
r.respond(buf)
}

func (r *ReadRequest) StreamingRespond(resp *StreamingReadResponse) {
buf := resp.Data()
r.respond(buf)
returnBuffer(buf)
}

// A ReadResponse is the response to a ReadRequest.
type ReadResponse struct {
Data []byte
Expand All @@ -1773,6 +1779,28 @@ func (r *ReadResponse) String() string {
return fmt.Sprintf("Read %d", len(r.Data))
}

func NewStreamingReadResponse() *StreamingReadResponse {
return &StreamingReadResponse{buf: newStreamingBuffer()}
}

// A StreamingReadResponse is the response to a ReadRequests wich
// supports streaming the response via a io.Writer
type StreamingReadResponse struct {
buf *bytes.Buffer
}

func (resp *StreamingReadResponse) Write(p []byte) (int, error) {
return resp.buf.Write(p)
}

func (resp *StreamingReadResponse) Data() []byte {
return resp.buf.Bytes()
}

func (r *StreamingReadResponse) String() string {
return fmt.Sprintf("Read %d", len(r.Data()))
}

type jsonReadResponse struct {
Len uint64
}
Expand Down