/
object_writer.go
335 lines (254 loc) · 7.97 KB
/
object_writer.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package object
import (
"context"
"encoding/json"
"io"
"sync"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/gather"
"github.com/kopia/kopia/repo/compression"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/logging"
"github.com/kopia/kopia/repo/splitter"
)
var log = logging.Module("object")
const indirectContentPrefix = "x"
// Writer allows writing content to the storage and supports automatic deduplication and encryption
// of written data.
type Writer interface {
io.WriteCloser
// Checkpoint returns ID of an object consisting of all contents written to storage so far.
// This may not include some data buffered in the writer.
// In case nothing has been written yet, returns empty object ID.
Checkpoint() (ID, error)
// Result returns object ID representing all bytes written to the writer.
Result() (ID, error)
}
type contentIDTracker struct {
mu sync.Mutex
contents map[content.ID]bool
}
func (t *contentIDTracker) addContentID(contentID content.ID) {
t.mu.Lock()
defer t.mu.Unlock()
if t.contents == nil {
t.contents = make(map[content.ID]bool)
}
t.contents[contentID] = true
}
func (t *contentIDTracker) contentIDs() []content.ID {
t.mu.Lock()
defer t.mu.Unlock()
result := make([]content.ID, 0, len(t.contents))
for k := range t.contents {
result = append(result, k)
}
return result
}
type objectWriter struct {
ctx context.Context
om *Manager
compressor compression.Compressor
prefix content.ID
buffer gather.WriteBuffer
totalLength int64
currentPosition int64
indirectIndexGrowMutex sync.Mutex
indirectIndex []indirectObjectEntry
indirectIndexBuf [4]indirectObjectEntry // small buffer so that we avoid allocations most of the time
description string
splitter splitter.Splitter
// provides mutual exclusion of all public APIs (Write, Result, Checkpoint)
mu sync.Mutex
asyncWritesSemaphore chan struct{} // async writes semaphore or nil
asyncWritesWG sync.WaitGroup
contentWriteErrorMutex sync.Mutex
contentWriteError error // stores async write error, propagated in Result()
}
func (w *objectWriter) Close() error {
// wait for any async writes to complete
w.asyncWritesWG.Wait()
if w.splitter != nil {
w.splitter.Close()
}
w.buffer.Close()
w.om.closedWriter(w)
return nil
}
func (w *objectWriter) Write(data []byte) (n int, err error) {
w.mu.Lock()
defer w.mu.Unlock()
dataLen := len(data)
w.totalLength += int64(dataLen)
for len(data) > 0 {
n := w.splitter.NextSplitPoint(data)
if n < 0 {
// no split points in the buffer
w.buffer.Append(data)
break
}
// found a split point after `n` bytes, write first n bytes then flush and repeat with the remainder.
w.buffer.Append(data[0:n])
if err := w.flushBuffer(); err != nil {
return 0, err
}
data = data[n:]
}
return dataLen, nil
}
func (w *objectWriter) flushBuffer() error {
length := w.buffer.Length()
// hold a lock as we may grow the index
w.indirectIndexGrowMutex.Lock()
chunkID := len(w.indirectIndex)
w.indirectIndex = append(w.indirectIndex, indirectObjectEntry{})
w.indirectIndex[chunkID].Start = w.currentPosition
w.indirectIndex[chunkID].Length = int64(length)
w.currentPosition += int64(length)
w.indirectIndexGrowMutex.Unlock()
defer w.buffer.Reset()
if w.asyncWritesSemaphore == nil {
return w.saveError(w.prepareAndWriteContentChunk(chunkID, w.buffer.Bytes()))
}
// acquire write semaphore
w.asyncWritesSemaphore <- struct{}{}
w.asyncWritesWG.Add(1)
asyncBuf := gather.NewWriteBuffer()
w.buffer.Bytes().WriteTo(asyncBuf) // nolint:errcheck
go func() {
defer func() {
// release write semaphore and buffer
<-w.asyncWritesSemaphore
asyncBuf.Close()
w.asyncWritesWG.Done()
}()
if err := w.prepareAndWriteContentChunk(chunkID, asyncBuf.Bytes()); err != nil {
log(w.ctx).Errorf("async write error: %v", err)
_ = w.saveError(err)
}
}()
return nil
}
func (w *objectWriter) prepareAndWriteContentChunk(chunkID int, data gather.Bytes) error {
var b gather.WriteBuffer
defer b.Close()
// allocate buffer to hold either compressed bytes or the uncompressed
comp := content.NoCompression
objectComp := w.compressor
// do not compress in this layer, instead pass comp to the content manager.
if w.om.contentMgr.SupportsContentCompression() && w.compressor != nil {
comp = w.compressor.HeaderID()
objectComp = nil
}
// contentBytes is what we're going to write to the content manager, it potentially uses bytes from b
contentBytes, isCompressed, err := maybeCompressedContentBytes(objectComp, data, &b)
if err != nil {
return errors.Wrap(err, "unable to prepare content bytes")
}
contentID, err := w.om.contentMgr.WriteContent(w.ctx, contentBytes, w.prefix, comp)
if err != nil {
return errors.Wrapf(err, "unable to write content chunk %v of %v: %v", chunkID, w.description, err)
}
// update index under a lock
w.indirectIndexGrowMutex.Lock()
w.indirectIndex[chunkID].Object = maybeCompressedObjectID(contentID, isCompressed)
w.indirectIndexGrowMutex.Unlock()
return nil
}
func (w *objectWriter) saveError(err error) error {
if err != nil {
// store write error so that we fail at Result() later.
w.contentWriteErrorMutex.Lock()
w.contentWriteError = err
w.contentWriteErrorMutex.Unlock()
}
return err
}
func maybeCompressedObjectID(contentID content.ID, isCompressed bool) ID {
oid := DirectObjectID(contentID)
if isCompressed {
oid = Compressed(oid)
}
return oid
}
func maybeCompressedContentBytes(comp compression.Compressor, input gather.Bytes, output *gather.WriteBuffer) (data gather.Bytes, isCompressed bool, err error) {
if comp != nil {
if err := comp.Compress(output, input.Reader()); err != nil {
return gather.Bytes{}, false, errors.Wrap(err, "compression error")
}
if output.Length() < input.Length() {
return output.Bytes(), true, nil
}
}
return input, false, nil
}
func (w *objectWriter) Result() (ID, error) {
w.mu.Lock()
defer w.mu.Unlock()
// no need to hold a lock on w.indirectIndexGrowMutex, since growing index only happens synchronously
// and never in parallel with calling Result()
if w.buffer.Length() > 0 || len(w.indirectIndex) == 0 {
if err := w.flushBuffer(); err != nil {
return "", err
}
}
return w.checkpointLocked()
}
// Checkpoint returns object ID which represents portion of the object that has already been written.
// The result may be an empty object ID if nothing has been flushed yet.
func (w *objectWriter) Checkpoint() (ID, error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.checkpointLocked()
}
func (w *objectWriter) checkpointLocked() (ID, error) {
// wait for any in-flight asynchronous writes to finish
w.asyncWritesWG.Wait()
if w.contentWriteError != nil {
return "", w.contentWriteError
}
if len(w.indirectIndex) == 0 {
return "", nil
}
if len(w.indirectIndex) == 1 {
return w.indirectIndex[0].Object, nil
}
iw := &objectWriter{
ctx: w.ctx,
om: w.om,
compressor: nil,
description: "LIST(" + w.description + ")",
splitter: w.om.newSplitter(),
prefix: w.prefix,
}
if iw.prefix == "" {
// force a prefix for indirect contents to make sure they get packaged into metadata (q) blobs.
iw.prefix = indirectContentPrefix
}
defer iw.Close() //nolint:errcheck
if err := writeIndirectObject(iw, w.indirectIndex); err != nil {
return "", err
}
oid, err := iw.Result()
if err != nil {
return "", err
}
return IndirectObjectID(oid), nil
}
func writeIndirectObject(w io.Writer, entries []indirectObjectEntry) error {
ind := indirectObject{
StreamID: "kopia:indirect",
Entries: entries,
}
if err := json.NewEncoder(w).Encode(ind); err != nil {
return errors.Wrap(err, "unable to write indirect object index")
}
return nil
}
// WriterOptions can be passed to Repository.NewWriter().
type WriterOptions struct {
Description string
Prefix content.ID // empty string or a single-character ('g'..'z')
Compressor compression.Name
AsyncWrites int // allow up to N content writes to be asynchronous
}