-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathsnapshot_metadata_write.go
173 lines (145 loc) · 4.7 KB
/
snapshot_metadata_write.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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package fs
import (
"fmt"
"os"
"github.com/m3db/m3/src/dbnode/digest"
"github.com/m3db/m3/src/dbnode/generated/proto/snapshot"
xerrors "github.com/m3db/m3x/errors"
"github.com/gogo/protobuf/proto"
)
type cleanupFn func() error
// NewSnapshotMetadataWriter constructs a new snapshot metadata writer.
func NewSnapshotMetadataWriter(opts Options) *SnapshotMetadataWriter {
return &SnapshotMetadataWriter{
opts: opts,
metadataFdWithDigest: digest.NewFdWithDigestWriter(opts.WriterBufferSize()),
digestBuf: digest.NewBuffer(),
}
}
// SnapshotMetadataWriter is a writer for SnapshotMetadata.
type SnapshotMetadataWriter struct {
opts Options
metadataFdWithDigest digest.FdWithDigestWriter
digestBuf digest.Buffer
}
// SnapshotMetadataWriteArgs are the arguments for SnapshotMetadataWriter.Write.
type SnapshotMetadataWriteArgs struct {
ID SnapshotMetadataIdentifier
CommitlogIdentifier []byte
}
func (w *SnapshotMetadataWriter) Write(args SnapshotMetadataWriteArgs) (finalErr error) {
var (
prefix = w.opts.FilePathPrefix()
snapshotsDir = SnapshotsDirPath(prefix)
// Set this up so that we can defer cleanup functions without ignoring
// the errors.
cleanupFns = []cleanupFn{}
deferCleanup = func(f cleanupFn) {
cleanupFns = append(cleanupFns, f)
}
)
defer func() {
multiErr := xerrors.MultiError{}
if finalErr != nil {
multiErr = multiErr.Add(finalErr)
}
for _, f := range cleanupFns {
err := f()
if err != nil {
multiErr = multiErr.Add(err)
}
}
finalErr = multiErr.FinalError()
}()
if err := os.MkdirAll(snapshotsDir, w.opts.NewDirectoryMode()); err != nil {
return err
}
snapshotsDirFile, err := os.Open(snapshotsDir)
if err != nil {
return err
}
deferCleanup(snapshotsDirFile.Close)
metadataPath := snapshotMetadataFilePathFromIdentifier(prefix, args.ID)
metadataFile, err := OpenWritable(metadataPath, w.opts.NewFileMode())
if err != nil {
return err
}
w.metadataFdWithDigest.Reset(metadataFile)
deferCleanup(w.metadataFdWithDigest.Close)
metadataBytes, err := proto.Marshal(&snapshot.Metadata{
SnapshotIndex: args.ID.Index,
SnapshotUUID: []byte(args.ID.UUID.String()),
CommitlogID: args.CommitlogIdentifier,
})
if err != nil {
return err
}
written, err := w.metadataFdWithDigest.Write(metadataBytes)
if err != nil {
return err
}
if written != len(metadataBytes) {
// Should never happen
return fmt.Errorf("did not complete writing metadata bytes, should have written: %d but wrote: %d bytes",
written, len(metadataBytes))
}
err = w.metadataFdWithDigest.Flush()
if err != nil {
return err
}
// Ensure the file is written out.
err = w.sync(metadataFile, snapshotsDirFile)
if err != nil {
return err
}
checkpointPath := snapshotMetadataCheckpointFilePathFromIdentifier(prefix, args.ID)
checkpointFile, err := OpenWritable(checkpointPath, w.opts.NewFileMode())
if err != nil {
return err
}
deferCleanup(checkpointFile.Close)
// digestBuf does not need to be reset.
err = w.digestBuf.WriteDigestToFile(
checkpointFile, w.metadataFdWithDigest.Digest().Sum32())
if err != nil {
return err
}
// Ensure the file is written out.
err = w.sync(checkpointFile, snapshotsDirFile)
if err != nil {
return err
}
return nil
}
// sync ensures that the provided file is persisted to disk by syncing it, as well
// as its parent directory (ensuring the file is discoverable in the parent inode.)
func (w *SnapshotMetadataWriter) sync(file, parent *os.File) error {
err := file.Sync()
if err != nil {
return err
}
err = parent.Sync()
if err != nil {
return err
}
return nil
}