/
govec.go
605 lines (513 loc) · 16.8 KB
/
govec.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// Package govec is a vector clock logging library
package govec
import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"sync"
"time"
"github.com/DistributedClocks/GoVector/govec/vclock"
ct "github.com/daviddengcn/go-colortext"
"github.com/vmihailenco/msgpack/v5"
)
var (
logToTerminal = false
_ msgpack.CustomEncoder = (*VClockPayload)(nil)
_ msgpack.CustomDecoder = (*VClockPayload)(nil)
)
// LogPriority controls the minimum priority of logging events which
// will be logged.
type LogPriority int
// LogPriority enum provides all the valid Priority Levels that can be
// used to log events with.
const (
DEBUG LogPriority = iota
INFO
WARNING
ERROR
FATAL
)
// colorLookup is the array of status to string from runtime/proc.go
var colorLookup = [...]ct.Color{
DEBUG: ct.Green,
INFO: ct.White,
WARNING: ct.Yellow,
ERROR: ct.Red,
FATAL: ct.Magenta,
}
// prefixLookup translates priority enums into strings
var prefixLookup = [...]string{
DEBUG: "DEBUG",
INFO: "INFO",
WARNING: "WARNING",
ERROR: "ERROR",
FATAL: "FATAL",
}
// GoLogConfig controls the logging parameters of GoLog and is taken as
// input to GoLog initialization. See defaults in GetDefaultConfig.
type GoLogConfig struct {
// Buffered denotes if the logging events are buffered until flushed. This option
// increase logging performance at the cost of safety.
Buffered bool
// PrintOnScreen denotes if logging events are printed to screen.
PrintOnScreen bool
// AppendLog determines to continue writing to a log from a prior execution.
AppendLog bool
// UseTimestamps determines to log real time timestamps for TSVis
UseTimestamps bool
// EncodingStrategy for customizable interoperability
EncodingStrategy func(interface{}) ([]byte, error)
// DecodingStrategy for customizable interoperability
DecodingStrategy func([]byte, interface{}) error
// LogToFile determines to write logging events to a file
LogToFile bool
// Priority determines the minimum priority event to log
Priority LogPriority
// InitialVC is the initial vector clock value, nil by default
InitialVC vclock.VClock
}
// GetDefaultConfig returns the default GoLogConfig with default values
// for various fields.
func GetDefaultConfig() GoLogConfig {
config := GoLogConfig{
Buffered: false,
PrintOnScreen: false,
AppendLog: false,
UseTimestamps: false,
LogToFile: true,
Priority: INFO,
InitialVC: nil,
}
return config
}
// GoLogOptions provides logging parameters for individual logging statements
type GoLogOptions struct {
// The Log priority for this event
Priority LogPriority
}
// GetDefaultLogOptions returns the default GoLogOptions with default values
// for the fields
func GetDefaultLogOptions() GoLogOptions {
o := GoLogOptions{Priority: INFO}
return o
}
// SetPriority returns a new GoLogOptions object with its priority field
// set to Priority. Follows the builder pattern.
// Priority : (GoLogPriority) The Priority that the new GoLogOptions object must have
func (o *GoLogOptions) SetPriority(Priority LogPriority) GoLogOptions {
opts := *o
opts.Priority = Priority
return opts
}
// VClockPayload is the data structure that is actually end on the wire
type VClockPayload struct {
Pid string
VcMap map[string]uint64
Payload interface{}
}
// PrintDataBytes prints the Data Stuct as Bytes
func (d *VClockPayload) PrintDataBytes() {
fmt.Printf("%x \n", d.Pid)
fmt.Printf("%X \n", d.VcMap)
fmt.Printf("%X \n", d.Payload)
}
// String returns VClockPayload's pid as a string
func (d *VClockPayload) String() (s string) {
s += "-----DATA START -----\n"
s += string(d.Pid[:])
s += "-----DATA END -----\n"
return
}
// EncodeMsgpack is a custom encoder function, needed for msgpack interoperability
func (d *VClockPayload) EncodeMsgpack(enc *msgpack.Encoder) error {
var err error
err = enc.EncodeString(d.Pid)
if err != nil {
return err
}
err = enc.Encode(d.Payload)
if err != nil {
return err
}
err = enc.EncodeMapLen(len(d.VcMap))
if err != nil {
return err
}
for key, value := range d.VcMap {
err = enc.EncodeString(key)
if err != nil {
return err
}
err = enc.EncodeUint(value)
if err != nil {
return err
}
}
return nil
}
// DecodeMsgpack is a custom decoder function, needed for msgpack
// interoperability
func (d *VClockPayload) DecodeMsgpack(dec *msgpack.Decoder) error {
var err error
pid, err := dec.DecodeString()
if err != nil {
return err
}
d.Pid = pid
err = dec.Decode(&d.Payload)
if err != nil {
return err
}
mapLen, err := dec.DecodeMapLen()
if err != nil {
return err
}
var vcMap map[string]uint64
vcMap = make(map[string]uint64)
for i := 0; i < mapLen; i++ {
key, err := dec.DecodeString()
if err != nil {
return err
}
value, err := dec.DecodeUint64()
if err != nil {
return err
}
vcMap[key] = value
}
err = dec.DecodeMulti(&d.Pid, &d.Payload, &d.VcMap)
d.VcMap = vcMap
if err != nil {
return err
}
return nil
}
// GoLog struct provides an interface to creating and maintaining
// vector timestamp entries in the generated log file. GoLogs are
// initialized with Configs which control logger output, format, and
// frequency.
type GoLog struct {
// Local Process ID
pid string
// Local vector clock in bytes
currentVC vclock.VClock
// Flag to Printf the logs made by Local Program
printonscreen bool
// If true GoLog will write to a file
logging bool
// If true logs are buffered in memory and flushed to disk upon
// calling flush. Logs can be lost if a program stops prior to
// flushing buffered logs.
buffered bool
// Flag to include timestamps when logging events
usetimestamps bool
// Flag to indicate if the log file will contain multiple executions
appendLog bool
// Flag to indicate if the message broadcast is on
broadcast bool
// Priority level at which all events are logged
priority LogPriority
// Logfile name
logfile string
// buffered string
output string
// encoding and decoding strategies for network messages
encodingStrategy func(interface{}) ([]byte, error)
decodingStrategy func([]byte, interface{}) error
// Internal logger for printing errors
logger *log.Logger
mutex sync.RWMutex
}
// InitGoVector returns a GoLog which generates a logs prefixed with
// processid, to a file name logfilename.log. Any old log with the same
// name will be trucated. Config controls logging options. See GoLogConfig for more details.
func InitGoVector(processid string, logfilename string, config GoLogConfig) *GoLog {
gv := &GoLog{}
gv.pid = processid
if logToTerminal {
gv.logger = log.New(os.Stdout, "[GoVector]:", log.Lshortfile)
} else {
var buf bytes.Buffer
gv.logger = log.New(&buf, "[GoVector]:", log.Lshortfile)
}
//Set parameters from config
gv.printonscreen = config.PrintOnScreen
gv.usetimestamps = config.UseTimestamps
gv.priority = config.Priority
gv.logging = config.LogToFile
gv.buffered = config.Buffered
gv.appendLog = config.AppendLog
gv.output = ""
// Use the default encoder/decoder. As of July 2017 this is msgPack.
if config.EncodingStrategy == nil || config.DecodingStrategy == nil {
gv.setEncoderDecoder(defaultEncoder, defaultDecoder)
} else {
gv.setEncoderDecoder(config.EncodingStrategy, config.DecodingStrategy)
}
// We create a new Vector Clock with processname and config.InitialClock
// as the initial time
var vc1 vclock.VClock
if config.InitialVC == nil {
vc1 = vclock.New()
vc1.Set(processid, 0)
} else {
vc1 = config.InitialVC
}
gv.currentVC = vc1
//Starting File IO . If Log exists, Log Will be deleted and A New one will be created
logname := logfilename + "-Log.txt"
gv.logfile = logname
if gv.logging {
gv.prepareLogFile()
}
return gv
}
func (gv *GoLog) prepareLogFile() {
_, err := os.Stat(gv.logfile)
if err == nil {
if !gv.appendLog {
gv.logger.Println(gv.logfile, "exists! ... Deleting ")
os.Remove(gv.logfile)
} else {
executionnumber := time.Now().Format(time.UnixDate)
gv.logger.Println("Execution Number is ", executionnumber)
executionstring := "=== Execution #" + executionnumber + " ==="
gv.logThis(executionstring, "", "", gv.priority)
return
}
}
// Create directory path to log if it doesn't exist.
if err := os.MkdirAll(filepath.Dir(gv.logfile), 0750); err != nil {
gv.logger.Println(err)
}
//Creating new Log
file, err := os.Create(gv.logfile)
if err != nil {
gv.logger.Println(err)
}
file.Close()
if gv.appendLog {
executionnumber := time.Now().Format(time.UnixDate)
gv.logger.Println("Execution Number is ", executionnumber)
executionstring := "=== Execution #" + executionnumber + " ==="
gv.logThis(executionstring, "", "", gv.priority)
}
gv.currentVC.Tick(gv.pid)
ok := gv.logThis("Initialization Complete", gv.pid, gv.currentVC.ReturnVCString(), gv.priority)
if ok == false {
gv.logger.Println("Something went Wrong, Could not Log!")
}
}
// GetCurrentVC returns the current vector clock
func (gv *GoLog) GetCurrentVC() vclock.VClock {
return gv.currentVC
}
// setEncoderDecoder Sets the Encoding and Decoding functions which are to be used by the logger
// Encoder (func(interface{}) ([]byte, error)) : function to be used for encoding
// Decoder (func([]byte, interface{}) error) : function to be used for decoding
func (gv *GoLog) setEncoderDecoder(encoder func(interface{}) ([]byte, error), decoder func([]byte, interface{}) error) {
gv.encodingStrategy = encoder
gv.decodingStrategy = decoder
}
// By default, encoding is performed by msgpack
func defaultEncoder(payload interface{}) ([]byte, error) {
return msgpack.Marshal(payload)
}
// By default, decoding network payloads is perfomed by msgpack
func defaultDecoder(buf []byte, payload interface{}) error {
return msgpack.Unmarshal(buf, payload)
}
// EnableBufferedWrites enables buffered writes to the log file. All
// the log messages are only written to the LogFile via an explicit
// call to the function Flush. Note: Buffered writes are automatically
// disabled.
func (gv *GoLog) EnableBufferedWrites() {
gv.buffered = true
}
// DisableBufferedWrites disables buffered writes to the log file. All
// the log messages from now on will be written to the Log file
// immediately. Writes all the existing log messages that haven't been
// written to Log file yet.
func (gv *GoLog) DisableBufferedWrites() {
gv.buffered = false
if gv.output != "" {
gv.Flush()
}
}
// Flush writes the log messages stored in the buffer to the Log File.
// This function should be used by the application to also force writes
// in the case of interrupts and crashes. Note: Calling Flush when
// BufferedWrites is disabled is essentially a no-op.
func (gv *GoLog) Flush() bool {
complete := true
file, err := os.OpenFile(gv.logfile, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
complete = false
}
defer file.Close()
if _, err = file.WriteString(gv.output); err != nil {
complete = false
}
gv.output = ""
return complete
}
func (gv *GoLog) printColoredMessage(LogMessage string, Priority LogPriority) {
color := colorLookup[Priority]
prefix := prefixLookup[Priority]
ct.Foreground(color, true)
fmt.Print(time.Now().Format(time.UnixDate) + ":" + prefix + "-")
ct.ResetColor()
fmt.Println(LogMessage)
}
// Logs a message along with a processID and a vector clock, the VCString
// must be a valid vector clock, true is returned on success. logThis
// is the innermost logging function internally used by all other
// logging functions
func (gv *GoLog) logThis(Message string, ProcessID string, VCString string, Priority LogPriority) bool {
var (
complete = true
buffer bytes.Buffer
)
if gv.usetimestamps {
buffer.WriteString(strconv.FormatInt(time.Now().UnixNano(), 10))
buffer.WriteString(" ")
}
buffer.WriteString(ProcessID)
buffer.WriteString(" ")
buffer.WriteString(VCString)
buffer.WriteString("\n")
buffer.WriteString(Message)
buffer.WriteString("\n")
output := buffer.String()
gv.output += output
if !gv.buffered {
complete = gv.Flush()
}
if gv.printonscreen == true {
gv.printColoredMessage(Message, Priority)
}
return complete
}
// logWriteWrapper is a helper function for wrapping common logging
// behaviour associated with logThis
func (gv *GoLog) logWriteWrapper(mesg, errMesg string, priority LogPriority) (success bool) {
if gv.logging == true {
prefix := prefixLookup[priority]
wrappedMesg := prefix + " " + mesg
success = gv.logThis(wrappedMesg, gv.pid, gv.currentVC.ReturnVCString(), priority)
if !success {
gv.logger.Println(errMesg)
}
}
return
}
// Increment GoVectors local clock by 1
func (gv *GoLog) tickClock() {
_, found := gv.currentVC.FindTicks(gv.pid)
if !found {
gv.logger.Println("Couldn't find this process's id in its own vector clock!")
}
gv.currentVC.Tick(gv.pid)
}
// LogLocalEvent implements LogLocalEvent with priority
// levels. If the priority of the logger is lower than or equal to the
// priority of this event then the current vector timestamp is
// incremented and the message is logged it into the Log File. A color
// coded string is also printed on the console.
// * LogMessage (string) : Message to be logged
// * Priority (LogPriority) : Priority at which the message is to be logged
func (gv *GoLog) LogLocalEvent(mesg string, opts GoLogOptions) (logSuccess bool) {
logSuccess = true
gv.mutex.Lock()
if opts.Priority >= gv.priority {
gv.tickClock()
logSuccess = gv.logWriteWrapper(mesg, "Something went Wrong, Could not Log LocalEvent!", opts.Priority)
}
gv.mutex.Unlock()
return
}
// PrepareSend is meant to be used immediately before sending.
// LogMessage will be logged along with the time of send
// buf is encode-able data (structure or basic type)
// Returned is an encoded byte array with logging information.
// This function is meant to be called before sending a packet. Usually,
// it should Update the Vector Clock for its own process, package with
// the clock using gob support and return the new byte array that should
// be sent onwards using the Send Command
func (gv *GoLog) PrepareSend(mesg string, buf interface{}, opts GoLogOptions) (encodedBytes []byte) {
//Converting Vector Clock from Bytes and Updating the gv clock
if !gv.broadcast {
gv.mutex.Lock()
if opts.Priority >= gv.priority {
gv.tickClock()
gv.logWriteWrapper(mesg, "Something went wrong, could not log prepare send", opts.Priority)
d := VClockPayload{Pid: gv.pid, VcMap: gv.currentVC.GetMap(), Payload: buf}
// encode the Clock Payload
var err error
encodedBytes, err = gv.encodingStrategy(&d)
if err != nil {
gv.logger.Println(err.Error())
}
// return encodedBytes which can be sent off and received on the other end!
}
gv.mutex.Unlock()
} else {
// Broadcast: do not acquire the lock, tick the clock or log an event as
// all been done by StartBroadcast
d := VClockPayload{Pid: gv.pid, VcMap: gv.currentVC.GetMap(), Payload: buf}
var err error
encodedBytes, err = gv.encodingStrategy(&d)
if err != nil {
gv.logger.Println(err.Error())
}
}
return
}
func (gv *GoLog) mergeIncomingClock(mesg string, e VClockPayload, Priority LogPriority) {
// First, tick the local clock
gv.tickClock()
gv.currentVC.Merge(e.VcMap)
gv.logWriteWrapper(mesg, "Something went Wrong, Could not Log!", Priority)
}
// UnpackReceive is used to unmarshall network data into local structures.
// LogMessage will be logged along with the vector time receive happened
// buf is the network data, previously packaged by PrepareSend unpack is
// a pointer to a structure, the same as was packed by PrepareSend.
// This function is meant to be called immediately after receiving
// a packet. It unpacks the data by the program, the vector clock. It
// updates vector clock and logs it. and returns the user data
func (gv *GoLog) UnpackReceive(mesg string, buf []byte, unpack interface{}, opts GoLogOptions) {
gv.mutex.Lock()
if opts.Priority >= gv.priority {
e := VClockPayload{}
e.Payload = unpack
// Just use msgpack directly
err := gv.decodingStrategy(buf, &e)
if err != nil {
gv.logger.Println(err.Error())
}
// Increment and merge the incoming clock
gv.mergeIncomingClock(mesg, e, opts.Priority)
}
gv.mutex.Unlock()
}
// StartBroadcast allows to use vector clocks in the context of casual broadcasts
// sent via RPC. Any call to StartBroadcast must have a corresponding call to
// StopBroadcast, otherwise a deadlock will occur. All RPC calls made in-between
// the calls to StartBroadcast and StopBroadcast will be logged as a single event,
// will be sent out with the same vector clock and will represent broadcast messages
// from the current process to the process pool.
func (gv *GoLog) StartBroadcast(mesg string, opts GoLogOptions) {
gv.mutex.Lock()
gv.tickClock()
gv.logWriteWrapper(mesg, "Something went wrong, could not log prepare send", opts.Priority)
gv.broadcast = true
}
// StopBroadcast is called once all RPC calls of a message broadcast have been sent.
func (gv *GoLog) StopBroadcast() {
gv.broadcast = false
gv.mutex.Unlock()
}