forked from facebookarchive/flashback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops_dispatcher.go
75 lines (67 loc) · 1.74 KB
/
ops_dispatcher.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
package flashback
import (
"time"
)
func NewBestEffortOpsDispatcher(reader OpsReader, opsSize int, logger *Logger) chan *Op {
queue := make([]*Op, opsSize, opsSize)
i := 0
// preload all the ops to avoid any overhead for fetching ops.
logger.Info("Started preloading ops: as fast as possible")
epoch := time.Now()
reportStatus := func() {
logger.Infof("%d ops loaded, %.2f ops/sec\n", reader.OpsRead(),
float64(reader.OpsRead())/time.Now().Sub(epoch).Seconds())
}
defer reportStatus()
for ; i < opsSize && !reader.AllLoaded(); i++ {
op := reader.Next()
if op == nil {
break
}
queue[i] = op
if i != 0 && i%30000 == 0 {
reportStatus()
}
}
opChannel := make(chan *Op, 10000)
// start a gorountine to dispatch these ops as fast as workers can handle.
go func() {
logger.Info("Started dispatching ops: as fast as possible")
for i, op := range queue {
queue[i] = nil
opChannel <- op
}
close(opChannel)
logger.Info("Dispatching ended")
}()
return opChannel
}
func NewByTimeOpsDispatcher(reader OpsReader, opsSize int, logger *Logger) chan *Op {
opChannel := make(chan *Op, 5000)
go func() {
logger.Info("Started replaying ops by time")
now_epoch := time.Now()
epoch := time.Unix(0, 0)
for i := 0; i < opsSize && !reader.AllLoaded(); i++ {
op := reader.Next()
if op == nil {
break
}
if epoch.Unix() == 0 {
epoch = op.Timestamp
}
elapsed := op.Timestamp.Sub(epoch)
currentClapsed := time.Now().Sub(now_epoch)
if elapsed > currentClapsed {
time.Sleep(elapsed - currentClapsed)
}
opChannel <- op
if reader.OpsRead()%10000 == 0 {
logger.Info("Timestamp for latest op: ", op.Timestamp)
}
}
logger.Info("Dispatching ended")
close(opChannel)
}()
return opChannel
}