diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/lamport.go | 4 | ||||
-rw-r--r-- | util/lamport_test.go | 2 | ||||
-rw-r--r-- | util/persisted_lamport.go | 21 |
3 files changed, 12 insertions, 15 deletions
diff --git a/util/lamport.go b/util/lamport.go index c75e1d15..4d5dcf5b 100644 --- a/util/lamport.go +++ b/util/lamport.go @@ -58,9 +58,9 @@ func (l *LamportClock) Time() LamportTime { return LamportTime(atomic.LoadUint64(&l.counter)) } -// Increment is used to increment and return the value of the lamport clock +// Increment is used to return the value of the lamport clock and increment it afterwards func (l *LamportClock) Increment() LamportTime { - return LamportTime(atomic.AddUint64(&l.counter, 1)) + return LamportTime(atomic.AddUint64(&l.counter, 1) - 1) } // Witness is called to update our local clock if necessary after diff --git a/util/lamport_test.go b/util/lamport_test.go index 0bac7d51..de7f1822 100644 --- a/util/lamport_test.go +++ b/util/lamport_test.go @@ -38,7 +38,7 @@ func TestLamportClock(t *testing.T) { t.Fatalf("bad time value") } - if l.Increment() != 1 { + if l.Increment() != 0 { t.Fatalf("bad time value") } diff --git a/util/persisted_lamport.go b/util/persisted_lamport.go index 43a2863c..c8c898e2 100644 --- a/util/persisted_lamport.go +++ b/util/persisted_lamport.go @@ -14,7 +14,8 @@ type PersistedLamport struct { func NewPersistedLamport(filePath string) *PersistedLamport { clock := &PersistedLamport{ - filePath: filePath, + LamportClock: NewLamportClock(), + filePath: filePath, } return clock } @@ -32,21 +33,17 @@ func LoadPersistedLamport(filePath string) (*PersistedLamport, error) { return clock, nil } +func (c *PersistedLamport) Increment() (LamportTime, error) { + time := c.LamportClock.Increment() + return time, c.Write() +} + func (c *PersistedLamport) Witness(time LamportTime) error { + // TODO: rework so that we write only when the clock was actually updated c.LamportClock.Witness(time) return c.Write() } -func (c *PersistedLamport) Time() LamportTime { - // Equivalent to: - // - // res = c.LamportClock.Time() - // bugClock.Increment() - // - // ... but thread safe - return c.Increment() - 1 -} - func (c *PersistedLamport) read() error { content, err := ioutil.ReadFile(c.filePath) if err != nil { @@ -76,6 +73,6 @@ func (c *PersistedLamport) Write() error { return err } - data := []byte(fmt.Sprintf("%d", c.LamportClock.Time())) + data := []byte(fmt.Sprintf("%d", c.counter)) return ioutil.WriteFile(c.filePath, data, 0644) } |