This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
client_fact_stats.go
411 lines (377 loc) · 13.8 KB
/
client_fact_stats.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright 2019 eBay Inc.
// Primary authors: Simon Fell, Diego Ongaro,
// Raymond Kroeker, and Sathish Kandasamy.
//
// 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
// https://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 viewclient
import (
"context"
"fmt"
"github.com/ebay/akutan/partitioning"
"github.com/ebay/akutan/rpc"
"github.com/ebay/akutan/space"
"github.com/ebay/akutan/util/cmp"
"github.com/ebay/akutan/util/errors"
"github.com/ebay/akutan/util/parallel"
"github.com/ebay/akutan/viewclient/fanout"
)
// FactStats collects data about the distributions of facts in the store. It
// fans out to many view servers, which have this data readily available, and
// collects it all together.
func (c *Client) FactStats(ctx context.Context, overallReq *rpc.FactStatsRequest) (*FactStats, error) {
results := FactStats{}
// Fan out the requests to cover both the PO hash-space and the SP hash-space.
err := parallel.Invoke(ctx,
func(ctx context.Context) error {
rpc := func(ctx context.Context, view fanout.View,
offsets []int, results func(fanout.Result)) error {
res, err := view.(*ReadFactsPOClient).Stub.FactStats(ctx, overallReq)
if err != nil {
return err
}
results(res)
return nil
}
views := c.ReadFactsPOViews()
ranges := fanout.Partition(fullRange, views)
resultsCh := make(chan fanout.Chunk, 4)
wait := drainStatsResults("PO", resultsCh, results.poStats.consume, ranges)
err := fanout.Call(ctx, ranges.StartPoints, views, rpc, resultsCh)
if err != nil {
err = fmt.Errorf("error getting PO FactStats: %v", err)
}
return errors.Any(wait(), err)
},
func(ctx context.Context) error {
rpc := func(ctx context.Context, view fanout.View,
offsets []int, results func(fanout.Result)) error {
res, err := view.(*ReadFactsSPClient).Stub.FactStats(ctx, overallReq)
if err != nil {
return err
}
results(res)
return nil
}
views := c.ReadFactsSPViews()
ranges := fanout.Partition(fullRange, views)
resultsCh := make(chan fanout.Chunk, 4)
wait := drainStatsResults("SP", resultsCh, results.spStats.consume, ranges)
err := fanout.Call(ctx, ranges.StartPoints, views, rpc, resultsCh)
if err != nil {
err = fmt.Errorf("error getting SP FactStats: %v", err)
}
return errors.Any(wait(), err)
},
)
if err != nil {
return nil, err
}
results.completed()
return &results, nil
}
// statsConsumer defines a callback function used with drainStatsResults. The
// results in 'r' cover the entire range in 'resultsRange'. Only data that's in
// the 'consumeRange' should be applied. 'consumeRange' may be equal to or
// smaller than 'resultsRange'. If smaller then statsConsumer implementations
// will be required to scale the results 'r' down appropriately.
type statsConsumer func(r *rpc.FactStatsResult, resultsRange space.Range, consumeRange space.Range)
// drainStatsResults starts a goroutine that will read results from resultsCh,
// and pass them onto the supplied consume function. drainStatsResults will deal
// with getting duplicates and overlapping results from fanout. The collective
// set of consumeRanges passed to the consume function will cover the space at
// most once (there may be gaps in the event of multiple RPC errors). consume
// may be called multiple times, but never concurrently.
//
// The returned function can be used to wait until the resultsCh is closed and
// the results have been fully processed. In the event of not having full
// coverage of the space in the stats results, an error will be returned from
// the returned waiter when called.
func drainStatsResults(hashSpace string, resultsCh chan fanout.Chunk, consume statsConsumer, partitions space.PartitionedRange) func() error {
return parallel.GoCaptureError(func() error {
offsetsDone := make([]bool, len(partitions.StartPoints))
offsetsNeeded := func(offsets []int) []int {
var res []int
for _, offset := range offsets {
if !offsetsDone[offset] {
offsetsDone[offset] = true
res = append(res, offset)
}
}
return res
}
for chunk := range resultsCh {
r := chunk.Result.(*rpc.FactStatsResult)
viewRange := chunk.View.Serves()
for _, consumeRange := range flattenRanges(offsetsNeeded(chunk.Offsets), partitions) {
consume(r, viewRange, consumeRange)
}
}
for offset, done := range offsetsDone {
if !done {
return fmt.Errorf("missing statistics for %v in space %v",
partitions.Get(offset), hashSpace)
}
}
return nil
})
}
// flattenRanges will collect the set of ranges given the offsets into the
// partitions, and collapse any adjacent ranges. Returns the resulting list
// of ranges. It assumes offsets are already in ascending order.
func flattenRanges(offsets []int, partitions space.PartitionedRange) []space.Range {
var result []space.Range
var current *space.Range
for _, offset := range offsets {
rng := partitions.Get(offset)
if current != nil && space.PointEq(rng.Start, current.End) {
current.End = rng.End
continue
}
result = append(result, rng)
current = &result[len(result)-1]
}
return result
}
// FactStats contains statistics about how facts are distributed in the overall
// database.
type FactStats struct {
totalFacts int
poStats
spStats
}
type poStats struct {
poTotalFacts int
distinctP int
frequentPCounts map[uint64]int
frequentPTotal int
distinctPO int
frequentPOCounts map[predicateObject]int
frequentPOTotal int
}
type spStats struct {
spTotalFacts int
distinctS int
frequentSCounts map[uint64]int
frequentSTotal int
distinctSP int
frequentSPCounts map[subjectPredicate]int
frequentSPTotal int
}
// predicateObject is a pair of (predicate, object) used in FactStats.
type predicateObject struct {
Predicate uint64
Object rpc.KGObject
}
// subjectPredicate is a pair of (subject, predicate) used in FactStats.
type subjectPredicate struct {
Subject uint64
Predicate uint64
}
// consume will update poStats with data from 'r', filtering down the data in
// the event that consumeRange is smaller than resultsRange.
func (stats *poStats) consume(r *rpc.FactStatsResult, resultsRange space.Range, consumeRange space.Range) {
if stats.frequentPOCounts == nil {
stats.frequentPOCounts = make(map[predicateObject]int)
}
if stats.frequentPCounts == nil {
stats.frequentPCounts = make(map[uint64]int)
}
ratio := rangeSize(consumeRange) / rangeSize(resultsRange)
stats.poTotalFacts += int(ratio * float64(r.NumFacts))
stats.distinctP = cmp.MaxInt(stats.distinctP, int(ratio*float64(r.DistinctPredicates)))
for _, item := range r.Predicates {
count := int(ratio * float64(item.Count))
stats.frequentPCounts[item.Predicate] += count
stats.frequentPTotal += count
}
stats.distinctPO += int(ratio * float64(r.DistinctPredicateObjects))
for _, item := range r.PredicateObjects {
po := predicateObject{Predicate: item.Predicate, Object: item.Object}
if ratio == 1 || consumeRange.Contains(partitioning.HashPO(po.Predicate, po.Object)) {
stats.frequentPOCounts[po] += int(item.Count)
stats.frequentPOTotal += int(item.Count)
}
}
}
// consume will update spStats with data from 'r', filtering down the data in
// the event that consumeRange is smaller than resultsRange.
func (stats *spStats) consume(r *rpc.FactStatsResult, resultsRange space.Range, consumeRange space.Range) {
if stats.frequentSPCounts == nil {
stats.frequentSPCounts = make(map[subjectPredicate]int)
}
if stats.frequentSCounts == nil {
stats.frequentSCounts = make(map[uint64]int)
}
ratio := rangeSize(consumeRange) / rangeSize(resultsRange)
stats.spTotalFacts += int(ratio * float64(r.NumFacts))
stats.distinctS = cmp.MaxInt(stats.distinctS, int(ratio*float64(r.DistinctSubjects)))
for _, item := range r.Subjects {
count := int(ratio * float64(item.Count))
stats.frequentSCounts[item.Subject] += count
stats.frequentSTotal += count
}
stats.distinctSP += int(ratio * float64(r.DistinctSubjectPredicates))
for _, item := range r.SubjectPredicates {
sp := subjectPredicate{Subject: item.Subject, Predicate: item.Predicate}
if ratio == 1 || consumeRange.Contains(partitioning.HashSP(sp.Subject, sp.Predicate)) {
stats.frequentSPCounts[sp] += int(item.Count)
stats.frequentSPTotal += int(item.Count)
}
}
}
// rangeSize returns the size of the supplied range.
func rangeSize(r space.Range) float64 {
endInf := space.PointEq(r.End, space.Infinity)
s := float64(r.Start.(space.Hash64))
if endInf {
return float64(1<<64) - s
}
e := float64(r.End.(space.Hash64))
return e - s
}
// completed will finalize the data in the FactStats, it should be called once
// all the calls to consume have completed.
func (stats *FactStats) completed() {
stats.totalFacts = cmp.MaxInt(stats.poStats.poTotalFacts, stats.spStats.spTotalFacts)
// This avoids potential issues with divide-by-zero, negative numbers, etc that
// may be caused by:
// - Testing with empty datasets
// - Gathering stats from an inconsistent snapshot
// - Poor approximations
stats.totalFacts = cmp.MaxInt(stats.totalFacts,
stats.frequentPTotal+1,
stats.frequentPOTotal+1,
stats.frequentSTotal+1,
stats.frequentSPTotal+1,
stats.distinctP+1,
stats.distinctPO+1,
stats.distinctS+1,
stats.distinctSP+1)
stats.distinctP = cmp.MaxInt(stats.distinctP, len(stats.frequentPCounts)+1)
stats.distinctPO = cmp.MaxInt(stats.distinctPO, len(stats.frequentPOCounts)+1)
stats.distinctS = cmp.MaxInt(stats.distinctS, len(stats.frequentSCounts)+1)
stats.distinctSP = cmp.MaxInt(stats.distinctSP, len(stats.frequentSPCounts)+1)
}
// ToRPCFactStats will generate an rpc fact stats result from FactStats.
func (stats *FactStats) ToRPCFactStats() *rpc.FactStatsResult {
res := &rpc.FactStatsResult{
Predicates: make([]rpc.PredicateStats, 0, len(stats.frequentPCounts)),
PredicateObjects: make([]rpc.PredicateObjectStats, 0, len(stats.frequentPOCounts)),
Subjects: make([]rpc.SubjectStats, 0, len(stats.frequentSCounts)),
SubjectPredicates: make([]rpc.SubjectPredicateStats, 0, len(stats.frequentSPCounts)),
}
for predicate, count := range stats.frequentPCounts {
res.Predicates = append(res.Predicates,
rpc.PredicateStats{Predicate: predicate, Count: uint64(count)})
}
for po, count := range stats.frequentPOCounts {
res.PredicateObjects = append(res.PredicateObjects,
rpc.PredicateObjectStats{Predicate: po.Predicate, Object: po.Object, Count: uint64(count)})
}
for subject, count := range stats.frequentSCounts {
res.Subjects = append(res.Subjects,
rpc.SubjectStats{Subject: subject, Count: uint64(count)})
}
for sp, count := range stats.frequentSPCounts {
res.SubjectPredicates = append(res.SubjectPredicates,
rpc.SubjectPredicateStats{Subject: sp.Subject, Predicate: sp.Predicate, Count: uint64(count)})
}
res.NumFacts = uint64(stats.totalFacts)
res.DistinctPredicates = uint64(stats.distinctP)
res.DistinctPredicateObjects = uint64(stats.distinctPO)
res.DistinctSubjects = uint64(stats.distinctS)
res.DistinctSubjectPredicates = uint64(stats.distinctSP)
return res
}
// BytesPerFact Implements planner.Stats.BytesPerFact().
func (stats *FactStats) BytesPerFact() int {
return 100
}
// NumFacts Implements planner.Stats.NumFacts().
func (stats *FactStats) NumFacts() int {
return stats.totalFacts
}
// NumFactsP Implements planner.Stats.NumFactsP().
func (stats *poStats) NumFactsP(predicate uint64) int {
if predicate == 0 {
facts := stats.poTotalFacts
predicates := stats.distinctP
return facts / predicates
}
count, ok := stats.frequentPCounts[predicate]
if ok {
return count
}
facts := stats.poTotalFacts - stats.frequentPTotal
predicates := stats.distinctP - len(stats.frequentPCounts)
return facts / predicates
}
// NumFactsPO Implements planner.Stats.NumFactsPO().
func (stats *poStats) NumFactsPO(predicate uint64, object rpc.KGObject) int {
if predicate == 0 || object.IsType(rpc.KtNil) {
facts := stats.poTotalFacts
numPO := stats.distinctPO
return facts / numPO
}
po := predicateObject{Predicate: predicate, Object: object}
count, ok := stats.frequentPOCounts[po]
if ok {
return count
}
facts := stats.poTotalFacts - stats.frequentPOTotal
numPO := stats.distinctPO - len(stats.frequentPOCounts)
return facts / numPO
}
// NumFactsO Implements planner.Stats.NumFactsO().
func (stats *poStats) NumFactsO(object rpc.KGObject) int {
return 1000
}
// NumFactsS Implements planner.Stats.NumFactsS().
func (stats *spStats) NumFactsS(subject uint64) int {
if subject == 0 {
facts := stats.spTotalFacts
subjects := stats.distinctS
return facts / subjects
}
count, ok := stats.frequentSCounts[subject]
if ok {
return count
}
facts := stats.spTotalFacts - stats.frequentSTotal
subjects := stats.distinctS - len(stats.frequentSCounts)
return facts / subjects
}
// NumFactsSP Implements planner.Stats.NumFactsSP().
func (stats *spStats) NumFactsSP(subject uint64, predicate uint64) int {
if subject == 0 || predicate == 0 {
facts := stats.spTotalFacts
numSP := stats.distinctSP
return facts / numSP
}
sp := subjectPredicate{Subject: subject, Predicate: predicate}
count, ok := stats.frequentSPCounts[sp]
if ok {
return count
}
facts := stats.spTotalFacts - stats.frequentSPTotal
numSP := stats.distinctSP - len(stats.frequentSPCounts)
return facts / numSP
}
// NumFactsSO Implements planner.Stats.NumFactsSO().
func (stats *FactStats) NumFactsSO(subject uint64, object rpc.KGObject) int {
s := stats.NumFactsS(subject)
o := stats.NumFactsO(object)
if s < o {
return s
}
return o
}