/
writer.go
178 lines (133 loc) · 4.05 KB
/
writer.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
package sgload
import (
"fmt"
"time"
)
type WriterSpec struct {
// How long writers should try to delay between writes
// (subtracting out the time they are blocked during actual write)
DelayBetweenWrites time.Duration
}
type Writer struct {
Agent
WriterSpec
OutboundDocs chan []Document // The Docfeeder pushes outbound docs to the writer
PushedDocs chan<- []DocumentMetadata // After docs are sent, push to this channel
}
func NewWriter(agentSpec AgentSpec, spec WriterSpec) *Writer {
outboundDocs := make(chan []Document)
writer := &Writer{
Agent: Agent{
AgentSpec: agentSpec,
},
WriterSpec: spec,
OutboundDocs: outboundDocs,
}
writer.setupExpVarStats(writersProgressStats)
return writer
}
func (w *Writer) Run() {
defer w.FinishedWg.Done()
numDocsPushed := 0
w.createWriterSGUserIfNeeded()
w.waitUntilAllSGUsersCreated()
for {
select {
case docs := <-w.OutboundDocs:
timeBlockedDuringWrite := time.Duration(0)
switch len(docs) {
case 1:
doc := docs[0]
_, ok := doc["_terminal"]
if ok {
logger.Info("Writer finished", "agent.ID", w.ID, "numdocs", numDocsPushed)
return
}
timeBeforeWrite := time.Now()
docRevPair, err := w.DataStore.CreateDocument(doc, w.AttachSizeBytes, true)
if err != nil {
panic(fmt.Sprintf("Error creating doc in datastore. Doc: %v, Err: %v", doc, err))
}
timeBlockedDuringWrite = time.Since(timeBeforeWrite)
docRevPairs := []DocumentMetadata{docRevPair}
w.notifyDocsPushed(docRevPairs)
numDocsPushed += len(docRevPairs)
default:
timeBeforeWrite := time.Now()
docRevPairs, err := w.DataStore.BulkCreateDocumentsRetry(docs, true)
if err != nil {
panic(fmt.Sprintf("Error creating docs in datastore. Docs: %v, Err: %v", docs, err))
}
timeBlockedDuringWrite = time.Since(timeBeforeWrite)
w.notifyDocsPushed(docRevPairs)
numDocsPushed += len(docRevPairs)
}
w.ExpVarStats.Add("NumDocsPushed", int64(len(docs)))
globalProgressStats.Add("TotalNumDocsPushed", int64(len(docs)))
logger.Debug(
"Writer pushed docs",
"writer",
w.Agent.UserCred.Username,
"numpushed",
len(docs),
"totalpushed",
numDocsPushed,
)
if timeBlockedDuringWrite > 10*time.Second {
logger.Warn("Writer took a while to write", "delta", timeBlockedDuringWrite, "user", w.Agent.UserCred.Username)
}
w.maybeDelayBetweenWrites(timeBlockedDuringWrite)
}
}
}
func (w *Writer) createWriterSGUserIfNeeded() {
defer globalProgressStats.Add("NumWriterUsers", 1)
w.createSGUserIfNeeded([]string{"*"})
}
func updateCreatedAtTimestamp(docs []Document) {
for _, doc := range docs {
doc["created_at"] = time.Now().Format(time.RFC3339Nano)
}
}
func (w *Writer) maybeDelayBetweenWrites(timeBlockedDuringWrite time.Duration) {
timeToSleep := w.WriterSpec.DelayBetweenWrites - timeBlockedDuringWrite
if timeToSleep > time.Duration(0) {
logger.Debug(
"Writer delay between writes",
"writer",
w.Agent.UserCred.Username,
"delay",
timeToSleep,
)
time.Sleep(timeToSleep)
}
}
func (w *Writer) SetApproxExpectedDocsWritten(numdocs int) {
w.ExpVarStats.Add("ApproxTotalDocs", int64(numdocs))
globalProgressStats.Add("TotalNumDocsPushedExpected", int64(numdocs))
}
func (w *Writer) notifyDocsPushed(docs []DocumentMetadata) {
start := time.Now()
if w.PushedDocs != nil {
w.PushedDocs <- docs
}
delta := time.Since(start)
if delta > time.Second {
logger.Warn("Writer took more than 1s notify updater docs pushed", "writer", w.UserCred.Username, "numdocs", len(docs), "delta", delta)
}
}
func (w *Writer) AddToDataStore(docs []Document) {
switch w.BatchSize {
case 1:
for _, doc := range docs {
logger.Debug("Push single doc to writer", "writer", w.UserCred.Username, "channels", doc["channels"])
w.OutboundDocs <- []Document{doc}
}
default:
docBatches := breakIntoBatches(w.BatchSize, docs)
for _, docBatch := range docBatches {
logger.Debug("Push doc batch to writer", "writer", w.UserCred.Username, "batchsize", len(docBatch))
w.OutboundDocs <- docBatch
}
}
}