Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

Commit a5b7aeb

Browse files
committed
Fix tests and remove old offset calculation
1 parent 8348d4c commit a5b7aeb

File tree

3 files changed

+58
-77
lines changed

3 files changed

+58
-77
lines changed

transaction/incoming.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,16 @@ func handleIncomingReport(t *tx, rep *lproto.Report) (*lproto.TxMsg_Report, *lpr
171171
//seq starts at 1 so after 1 iteration it'll be at 3
172172
//only the master can terminate a sequence
173173
if numTrips >= config.Iterations {
174-
offset, err := calculateAverageOffset(t.tripTimes, t.offsets)
175-
offset2, latency, _ := calculateAverageOffsetLatency(t.trips)
174+
offset, latency, err := calculateAverageOffsetLatency(t.trips)
176175
fin := &lproto.Fin{}
177176
if err != nil {
178177
fin.Error = err.Error()
179178
llog.Error("error calculating avg offset", kv.Set("err", err))
180179
} else {
181180
fin.Offset = offset
182181
kv["offset"] = offset
183-
kv["offset2"] = offset2
184182
absOff := math.Abs(offset)
185-
absOff2 := math.Abs(offset2)
186-
llog.Info("slave offset", kv, llog.KV{"absOffset": absOff, "absOffset2": absOff2, "latency": latency})
183+
llog.Info("slave offset", kv, llog.KV{"absOffset": absOff, "latency": latency})
187184
if config.Threshold < absOff {
188185
llog.Warn("slave offset is over threshold", kv)
189186
}

transaction/util.go

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"math"
66
"sort"
7-
"time"
87
)
98

109
type Trip struct {
@@ -19,46 +18,8 @@ func (a int64s) Len() int { return len(a) }
1918
func (a int64s) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
2019
func (a int64s) Less(i, j int) bool { return a[i] < a[j] }
2120

22-
//returns the average milliseconds of the durations
23-
func calculateAverageOffset(tripTimes []time.Duration, offsets []time.Duration) (float64, error) {
24-
if len(tripTimes) == 0 {
25-
return 0, errors.New("finalizing transaction with 0 iterations")
26-
}
27-
if len(tripTimes) != len(offsets) {
28-
return 0, errors.New("finalizing transaction with invalid iterations")
29-
}
30-
31-
maxNanosecs := math.MaxFloat64
32-
//first we need to calculate the 80th percentile of the tripTimes
33-
//we only want to keep track of those and discard the others
34-
if len(tripTimes) > 2 {
35-
sortedTimes := make([]float64, len(tripTimes))
36-
for i, v := range tripTimes {
37-
sortedTimes[i] = float64(v.Nanoseconds())
38-
}
39-
sort.Float64s(sortedTimes)
40-
percentIndex := int64(float64(len(sortedTimes)) * 0.8)
41-
maxNanosecs = sortedTimes[percentIndex]
42-
}
43-
var n float64
44-
var totalTimes float64
45-
var totalOffsets float64
46-
count := 0.0
47-
for i, v := range tripTimes {
48-
n = float64(v.Nanoseconds())
49-
//only accept this trip if its less than the max allowed time
50-
if n < maxNanosecs {
51-
totalTimes += n / 1000000
52-
totalOffsets += float64(offsets[i]) / 1000000
53-
count++
54-
}
55-
}
56-
//totalTimes is the total of all the RTTs but offset is only affected 1 way
57-
//so divide RTT by 2 to get one-way time
58-
return (totalOffsets + (totalTimes / 2)) / count, nil
59-
}
60-
61-
//returns the average milliseconds of the durations
21+
// returns the average milliseconds of the durations
22+
// and also includes the average latency in milliseconds
6223
func calculateAverageOffsetLatency(trips []Trip) (float64, float64, error) {
6324
if len(trips) == 0 {
6425
return 0, 0, errors.New("finalizing transaction with 0 trips")
@@ -74,7 +35,7 @@ func calculateAverageOffsetLatency(trips []Trip) (float64, float64, error) {
7435
}
7536
sort.Sort(sortedTimes)
7637
// make sure we have at most the length of the trips - 1 and at least 3
77-
percentIndex := int64(math.Min(float64(len(trips)) - 1, math.Max(3, (float64(len(sortedTimes)) * 0.6))))
38+
percentIndex := int64(math.Min(float64(len(trips))-1, math.Max(3, (float64(len(sortedTimes))*0.6))))
7839
maxNanosecs = sortedTimes[percentIndex]
7940
}
8041
var latency float64
@@ -91,7 +52,7 @@ func calculateAverageOffsetLatency(trips []Trip) (float64, float64, error) {
9152
latency = float64(t.RTT) / 2.0
9253
// the Slave will have the opposite offset (since it's subtracting
9354
// its time minus the master's) so we negate it here
94-
// devide by 2 to get the average
55+
// divide by 2 to get the average
9556
offset = ((float64(t.MasterDiff) + latency) - (float64(t.SlaveDiff) + latency)) / 2.0
9657
totalOffsets += float64(offset) / 1e6
9758
totalLatency += latency / 1e6

transaction/util_test.go

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,73 @@ package transaction
22

33
import (
44
. "testing"
5-
"time"
65

76
"github.com/stretchr/testify/assert"
87
)
98

109
func TestCalcAvg1(t *T) {
11-
d := []time.Duration{
12-
time.Duration(2) * time.Millisecond,
10+
trips := []Trip{
11+
// offset: 10 latency: 1
12+
Trip{
13+
MasterDiff: 9000000,
14+
SlaveDiff: -11000000,
15+
RTT: 2000000,
16+
},
1317
}
14-
o := []time.Duration{
15-
1000000,
16-
}
17-
a, err := calculateAverageOffset(d, o)
18+
o, l, err := calculateAverageOffsetLatency(trips)
1819
assert.Nil(t, err)
19-
assert.Equal(t, float64(2), a)
20+
assert.Equal(t, float64(10), o)
21+
assert.Equal(t, float64(1), l)
2022
}
2123

2224
func TestCalcAvg2(t *T) {
23-
d := []time.Duration{
24-
time.Duration(1) * time.Millisecond,
25-
time.Duration(1) * time.Millisecond,
26-
}
27-
o := []time.Duration{
28-
1000000,
29-
1000000,
25+
trips := []Trip{
26+
// offset: 10 latency: 1
27+
Trip{
28+
MasterDiff: 9000000,
29+
SlaveDiff: -11000000,
30+
RTT: 2000000,
31+
},
32+
33+
// offset: 20 latency: 1
34+
Trip{
35+
MasterDiff: 19000000,
36+
SlaveDiff: -21000000,
37+
RTT: 2000000,
38+
},
3039
}
31-
a, err := calculateAverageOffset(d, o)
40+
o, l, err := calculateAverageOffsetLatency(trips)
3241
assert.Nil(t, err)
33-
assert.Equal(t, float64(1.5), a)
42+
assert.Equal(t, float64(15), o)
43+
assert.Equal(t, float64(1), l)
3444
}
3545

3646
func TestCalcAvg3(t *T) {
37-
//the outlier (2) should be eliminated so this should match TestCalcAvg2
38-
d := []time.Duration{
39-
time.Duration(1) * time.Millisecond,
40-
time.Duration(1) * time.Millisecond,
41-
time.Duration(2) * time.Millisecond,
42-
}
43-
o := []time.Duration{
44-
1000000,
45-
1000000,
46-
9000000,
47+
trips := []Trip{
48+
// offset: 10 latency: 1
49+
Trip{
50+
MasterDiff: 9000000,
51+
SlaveDiff: -11000000,
52+
RTT: 2000000,
53+
},
54+
55+
// offset: 20 latency: 1
56+
Trip{
57+
MasterDiff: 19000000,
58+
SlaveDiff: -21000000,
59+
RTT: 2000000,
60+
},
61+
62+
// offset: 40 latency: 2
63+
// this should be eliminated
64+
Trip{
65+
MasterDiff: 38000000,
66+
SlaveDiff: -32000000,
67+
RTT: 4000000,
68+
},
4769
}
48-
a, err := calculateAverageOffset(d, o)
70+
o, l, err := calculateAverageOffsetLatency(trips)
4971
assert.Nil(t, err)
50-
assert.Equal(t, float64(1.5), a)
72+
assert.Equal(t, float64(15), o)
73+
assert.Equal(t, float64(1), l)
5174
}

0 commit comments

Comments
 (0)