forked from unrealsync/unrealsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
286 lines (255 loc) · 6.3 KB
/
log.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
package main
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"time"
)
const (
logMaxSize = 50 * 1048576
)
type SortableStrings []string
type BufBlocker struct {
buf []byte
sent chan bool
}
var (
outLogWriteFp *os.File
outLogPos int64
outLogReadFps map[string]*os.File
outLogReadPos map[string]int64
outLogReadOldSize map[string]int64
outLogMutex sync.Mutex
)
func (r SortableStrings) Len() int {
return len(r)
}
func (r SortableStrings) Less(i, j int) bool {
return strings.Compare(r[i], r[j]) > 0
}
func (r SortableStrings) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
func initializeLogs() {
createOutLog()
outLogReadFps = make(map[string]*os.File)
outLogReadPos = make(map[string]int64)
outLogReadOldSize = make(map[string]int64)
}
func writeToOutLog(action string, buf []byte) {
outLogMutex.Lock()
defer outLogMutex.Unlock()
_, err := fmt.Fprintf(outLogWriteFp, "%s%10d%s", action, len(buf), buf)
if err != nil {
fatalLn(err)
}
outLogPos, err = outLogWriteFp.Seek(0, io.SeekCurrent)
debugLn("outlogpos:", outLogPos, " after action:", action)
if outLogPos > logMaxSize {
for _, oldSize := range outLogReadOldSize {
if oldSize != 0 {
debugLn("could not reopen log, not all readers are reading from actual")
return
}
}
progressLn("Rotating outlog")
createOutLog()
}
return
}
func createOutLog() {
logFilePath := getLogFilePath(repoLogFilename)
if outLogWriteFp != nil {
outLogWriteFp.Close()
os.Remove(logFilePath)
}
var err error
outLogWriteFp, err = os.OpenFile(logFilePath, os.O_APPEND|os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
fatalLn("Cannot open ", logFilePath, ": ", err.Error())
}
for hostname := range outLogReadOldSize {
// invalidate readers
outLogReadOldSize[hostname] = outLogPos
}
outLogPos = 0
}
func openOutLogForRead(hostname string, continuation bool) (err error) {
outLogMutex.Lock()
defer outLogMutex.Unlock()
fp, ok := outLogReadFps[hostname]
if ok {
progressLn("Closing old log fp for ", hostname)
err = fp.Close()
if err != nil {
return
}
}
progressLn("Opening log for ", hostname)
fp, err = os.Open(getLogFilePath(repoLogFilename))
if err != nil {
return
}
outLogReadFps[hostname] = fp
if continuation {
_, err = fp.Seek(outLogPos, io.SeekStart)
if err != nil {
return
}
outLogReadPos[hostname] = outLogPos
} else {
outLogReadPos[hostname] = 0
}
outLogReadOldSize[hostname] = 0
return
}
func doSendChanges(stream chan BufBlocker, hostname string, stopChan chan bool, errorCh chan error) {
var err error
buf := make([]byte, maxDiffSize+20) // maxDiffSize limits only diff itself, so extra action+len required, each 10 bytes
var pos int64
var bufLen int
bufBlocker := BufBlocker{buf: buf, sent: make(chan bool)}
doSendChangesLoop:
for {
select {
case <-stopChan:
progressLn("Got stop sendChanges")
break doSendChangesLoop
default:
}
outLogMutex.Lock()
localOutLogPos := outLogPos
localOldSize := outLogReadOldSize[hostname]
localReadPos := outLogReadPos[hostname]
fp := outLogReadFps[hostname]
outLogMutex.Unlock()
if localReadPos == localOutLogPos && localOldSize == 0 {
time.Sleep(time.Millisecond * 20)
continue
}
bufLen, err = readLogEntry(fp, buf)
if err == io.EOF {
err = openOutLogForRead(hostname, false)
if err != nil {
sendErrorNonBlocking(errorCh, err)
break
}
continue
}
if err != nil {
sendErrorNonBlocking(errorCh, err)
break
}
pos, err = fp.Seek(0, io.SeekCurrent)
if err != nil {
sendErrorNonBlocking(errorCh, err)
break
}
bufBlocker.buf = buf[0:bufLen]
select {
case stream <- bufBlocker:
case <-stopChan:
progressLn("Got stop sendChanges2")
break doSendChangesLoop
}
select {
case <-bufBlocker.sent:
case <-stopChan:
progressLn("Got stop sendChanges3")
break doSendChangesLoop
}
outLogMutex.Lock()
outLogReadPos[hostname] = pos
outLogMutex.Unlock()
debugLn("hostname:", hostname, " pos:", pos, " after reading", string(buf[0:10]))
}
}
func printStatusThread(clients map[string]*Client) {
var sendQueueSize int64
prevStatusesOk := false
mem := new(runtime.MemStats)
for {
statuses := make([]string, 0)
outLogMutex.Lock()
for hostname, oldSize := range outLogReadOldSize {
if oldSize != 0 {
sendQueueSize = oldSize - outLogReadPos[hostname] + outLogPos
statuses = append(statuses, hostname+" "+formatLength(int(sendQueueSize))+"*")
} else if outLogReadPos[hostname] != outLogPos {
sendQueueSize = outLogPos - outLogReadPos[hostname]
statuses = append(statuses, hostname+" "+formatLength(int(sendQueueSize)))
} else {
sendQueueSize = 0
}
if err := clients[hostname].notifySendQueueSize(sendQueueSize); err != nil {
progressLn("removing " + hostname + " from outLogReadOldSize")
delete(outLogReadOldSize, hostname)
}
}
outLogMutex.Unlock()
sort.Sort(SortableStrings(statuses))
runtime.ReadMemStats(mem)
if len(statuses) > 0 {
progress("Pending diffs: ", strings.Join(statuses, "; "))
prevStatusesOk = false
} else if !prevStatusesOk {
progress("All diffs were sent mem.Sys:", formatLength(int(mem.Sys)))
prevStatusesOk = true
}
time.Sleep(time.Millisecond * 300)
}
}
// read a single entry from log into buf
func readLogEntry(fp *os.File, buf []byte) (bufLen int, err error) {
outLogMutex.Lock()
defer outLogMutex.Unlock()
var n, diffLen int
// action
n, err = io.ReadFull(fp, buf[0:10])
if err != nil {
return
}
if n != 10 {
err = errors.New("read into action unexpected bytes:" + fmt.Sprint(n) + " instead of 10")
return
}
// diff length
n, err = io.ReadFull(fp, buf[10:20])
if err != nil {
return
}
if n != 10 {
err = errors.New("read into len unexpected bytes:" + fmt.Sprint(n) + " instead of 10")
return
}
// diff
diffLen, err = strconv.Atoi(strings.TrimSpace(string(buf[10:20])))
if err != nil {
return
}
// no payload as for PING
if diffLen == 0 {
bufLen = 20
return
}
n, err = io.ReadFull(fp, buf[20:20+diffLen])
if err != nil {
return
}
if n != diffLen {
err = errors.New("read into buf unexpected bytes:" + fmt.Sprint(n) + " instead of " + fmt.Sprint(diffLen))
return
}
bufLen = 20 + diffLen
return
}
func getLogFilePath(relativePath string) string {
return filepath.Join(repoPath, relativePath)
}