-
Notifications
You must be signed in to change notification settings - Fork 452
/
fs_merge_with_mem.go
146 lines (131 loc) · 4.8 KB
/
fs_merge_with_mem.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
// Copyright (c) 2019 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 storage
import (
"github.com/m3db/m3/src/dbnode/namespace"
"github.com/m3db/m3/src/dbnode/persist/fs"
"github.com/m3db/m3/src/dbnode/storage/block"
"github.com/m3db/m3/src/dbnode/storage/series"
"github.com/m3db/m3/src/dbnode/x/xio"
"github.com/m3db/m3/src/x/context"
"github.com/m3db/m3/src/x/ident"
xtime "github.com/m3db/m3/src/x/time"
)
// fsMergeWithMem implements fs.MergeWith, where the merge target is data in
// memory. It relies on data structures being passed in that are created
// within the shard informing fsMergeWithMem which series require merging.
// These data structures enable efficient reading of data as well as keeping
// track of which series were read so that the remaining series can be looped
// through.
type fsMergeWithMem struct {
shard databaseShard
retriever series.QueryableBlockRetriever
dirtySeries *dirtySeriesMap
dirtySeriesToWrite map[xtime.UnixNano]*idList
reuseableID *ident.ReuseableBytesID
}
func newFSMergeWithMem(
shard databaseShard,
retriever series.QueryableBlockRetriever,
dirtySeries *dirtySeriesMap,
dirtySeriesToWrite map[xtime.UnixNano]*idList,
) fs.MergeWith {
return &fsMergeWithMem{
shard: shard,
retriever: retriever,
dirtySeries: dirtySeries,
dirtySeriesToWrite: dirtySeriesToWrite,
reuseableID: ident.NewReuseableBytesID(),
}
}
func (m *fsMergeWithMem) Read(
ctx context.Context,
seriesID ident.ID,
blockStart xtime.UnixNano,
nsCtx namespace.Context,
) ([]xio.BlockReader, bool, error) {
// Check if this series is in memory (and thus requires merging).
element, exists := m.dirtySeries.Get(idAndBlockStart{
blockStart: blockStart,
id: seriesID.Bytes(),
})
if !exists {
return nil, false, nil
}
// Series is in memory, so it will get merged with disk and
// written in this loop. Therefore, we need to remove it from
// the "to write" list so that the later loop does not rewrite
// it.
m.dirtySeriesToWrite[blockStart].Remove(element)
result, ok, err := m.fetchBlocks(ctx, seriesID, blockStart, nsCtx)
if err != nil {
return nil, false, err
}
return result.Blocks, ok, nil
}
func (m *fsMergeWithMem) fetchBlocks(
ctx context.Context,
id ident.ID,
blockStart xtime.UnixNano,
nsCtx namespace.Context,
) (block.FetchBlockResult, bool, error) {
startTime := blockStart.ToTime()
currVersion, err := m.retriever.RetrievableBlockColdVersion(startTime)
if err != nil {
return block.FetchBlockResult{}, false, err
}
nextVersion := currVersion + 1
result, err := m.shard.FetchBlocksForColdFlush(ctx, id, startTime, nextVersion, nsCtx)
if err != nil {
return block.FetchBlockResult{}, false, err
}
if len(result.Blocks) > 0 {
return result, true, nil
}
return block.FetchBlockResult{}, false, nil
}
// The data passed to ForEachRemaining (through the fs.ForEachRemainingFn) is
// basically a copy that will be finalized when the context is closed, but the
// ID and tags are expected to live for as long as the caller of the MergeWith
// requires them, so they should either be NoFinalize() or passed as copies.
func (m *fsMergeWithMem) ForEachRemaining(
ctx context.Context,
blockStart xtime.UnixNano,
fn fs.ForEachRemainingFn,
nsCtx namespace.Context,
) error {
reuseableID := m.reuseableID
seriesList := m.dirtySeriesToWrite[blockStart]
for seriesElement := seriesList.Front(); seriesElement != nil; seriesElement = seriesElement.Next() {
seriesMetadata := seriesElement.Value
reuseableID.Reset(seriesMetadata.ID)
mergeWithData, hasData, err := m.fetchBlocks(ctx, reuseableID, blockStart, nsCtx)
if err != nil {
return err
}
if hasData {
err = fn(seriesMetadata, mergeWithData)
if err != nil {
return err
}
}
}
return nil
}