/
stream_reader.go
166 lines (143 loc) · 4.46 KB
/
stream_reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2018 The Chubao Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package stream
import (
"fmt"
"golang.org/x/net/context"
"io"
"sync"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/util/log"
)
// One inode corresponds to one streamer. All the requests to the same inode will be queued.
// TODO rename streamer here is not a good name as it also handles overwrites, not just stream write.
type Streamer struct {
client *ExtentClient
inode uint64
parentInode uint64
status int32
refcnt int
idle int // how long there is no new request
traversed int // how many times the streamer is traversed
extents *ExtentCache
once sync.Once
handler *ExtentHandler // current open handler
dirtylist *DirtyExtentList // dirty handlers
dirty bool // whether current open handler is in the dirty list
request chan interface{} // request channel, write/flush/close
done chan struct{} // stream writer is being closed
writeLock sync.Mutex
}
// NewStreamer returns a new streamer.
func NewStreamer(client *ExtentClient, inode uint64) *Streamer {
s := new(Streamer)
s.client = client
s.inode = inode
s.parentInode = 0
s.extents = NewExtentCache(inode)
s.request = make(chan interface{}, 64)
s.done = make(chan struct{})
s.dirtylist = NewDirtyExtentList()
go s.server()
return s
}
func (s *Streamer) SetParentInode(inode uint64) {
s.parentInode = inode
}
// String returns the string format of the streamer.
func (s *Streamer) String() string {
return fmt.Sprintf("Streamer{ino(%v)}", s.inode)
}
// TODO should we call it RefreshExtents instead?
func (s *Streamer) GetExtents() error {
return s.extents.Refresh(s.inode, s.client.getExtents)
}
// GetExtentReader returns the extent reader.
// TODO: use memory pool
func (s *Streamer) GetExtentReader(ek *proto.ExtentKey) (*ExtentReader, error) {
partition, err := s.client.dataWrapper.GetDataPartition(ek.PartitionId)
if err != nil {
return nil, err
}
reader := NewExtentReader(s.inode, ek, partition, s.client.dataWrapper.FollowerRead())
return reader, nil
}
func (s *Streamer) read(data []byte, offset int, size int) (total int, err error) {
var (
readBytes int
reader *ExtentReader
requests []*ExtentRequest
revisedRequests []*ExtentRequest
)
ctx := context.Background()
s.client.readLimiter.Wait(ctx)
requests = s.extents.PrepareReadRequests(offset, size, data)
for _, req := range requests {
if req.ExtentKey == nil {
continue
}
if req.ExtentKey.PartitionId == 0 || req.ExtentKey.ExtentId == 0 {
s.writeLock.Lock()
if err = s.IssueFlushRequest(); err != nil {
s.writeLock.Unlock()
return 0, err
}
revisedRequests = s.extents.PrepareReadRequests(offset, size, data)
s.writeLock.Unlock()
break
}
}
if revisedRequests != nil {
requests = revisedRequests
}
filesize, _ := s.extents.Size()
log.LogDebugf("read: ino(%v) requests(%v) filesize(%v)", s.inode, requests, filesize)
for _, req := range requests {
if req.ExtentKey == nil {
for i := range req.Data {
req.Data[i] = 0
}
if req.FileOffset+req.Size > filesize {
if req.FileOffset > filesize {
return
}
req.Size = filesize - req.FileOffset
total += req.Size
err = io.EOF
//if total == 0 {
// log.LogErrorf("read: ino(%v) req(%v) filesize(%v)", s.inode, req, filesize)
//}
return
}
// Reading a hole, just fill zero
total += req.Size
log.LogDebugf("Stream read hole: ino(%v) req(%v) total(%v)", s.inode, req, total)
} else {
reader, err = s.GetExtentReader(req.ExtentKey)
if err != nil {
break
}
readBytes, err = reader.Read(req)
log.LogDebugf("Stream read: ino(%v) req(%v) readBytes(%v) err(%v)", s.inode, req, readBytes, err)
total += readBytes
if err != nil || readBytes < req.Size {
if total == 0 {
log.LogErrorf("Stream read: ino(%v) req(%v) readBytes(%v) err(%v)", s.inode, req, readBytes, err)
}
break
}
}
}
return
}