-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
290 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package chotki | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/drpcorg/chotki/protocol" | ||
"github.com/drpcorg/chotki/rdx" | ||
) | ||
|
||
var ErrNotCounter error = fmt.Errorf("not a counter") | ||
|
||
type AtomicCounter struct { | ||
db *Chotki | ||
rid rdx.ID | ||
offset uint64 | ||
localValue atomic.Int64 | ||
tlv atomic.Value | ||
rdt atomic.Value | ||
loaded atomic.Bool | ||
lock sync.Mutex | ||
expiration time.Time | ||
updatePeriod time.Duration | ||
} | ||
|
||
// creates counter that has two properties | ||
// - its atomic as long as you use single instance to do all increments, creating multiple instances will break this guarantee | ||
// - it can ease CPU load if updatePeiod > 0, in that case it will not read from db backend | ||
// current value of the counter | ||
// | ||
// Because we use LSM backend writes are cheap, reads are expensive. You can trade off up to date value of counter | ||
// for less CPU cycles | ||
func NewAtomicCounter(db *Chotki, rid rdx.ID, offset uint64, updatePeriod time.Duration) *AtomicCounter { | ||
return &AtomicCounter{ | ||
db: db, | ||
rid: rid, | ||
offset: offset, | ||
updatePeriod: updatePeriod, | ||
} | ||
} | ||
|
||
func (a *AtomicCounter) load() error { | ||
a.lock.Lock() | ||
defer a.lock.Unlock() | ||
now := time.Now() | ||
if a.loaded.Load() && now.Sub(a.expiration) < 0 { | ||
return nil | ||
} | ||
rdt, tlv, err := a.db.ObjectFieldTLV(a.rid.ToOff(a.offset)) | ||
if err != nil { | ||
return err | ||
} | ||
a.rdt.Store(rdt) | ||
a.loaded.Store(true) | ||
a.tlv.Store(tlv) | ||
switch rdt { | ||
case rdx.ZCounter: | ||
a.localValue.Store(rdx.Znative(tlv)) | ||
case rdx.Natural: | ||
a.localValue.Store(int64(rdx.Nnative(tlv))) | ||
default: | ||
return ErrNotCounter | ||
} | ||
a.expiration = now.Add(a.updatePeriod) | ||
return nil | ||
} | ||
|
||
// Loads (if needed) and increments counter | ||
func (a *AtomicCounter) Increment(ctx context.Context, val int64) (int64, error) { | ||
err := a.load() | ||
if err != nil { | ||
return 0, err | ||
} | ||
if val == 2 { | ||
fmt.Println(1) | ||
} | ||
rdt := a.rdt.Load().(byte) | ||
a.localValue.Add(val) | ||
var dtlv []byte | ||
a.lock.Lock() | ||
tlv := a.tlv.Load().([]byte) | ||
switch rdt { | ||
case rdx.Natural: | ||
dtlv = rdx.Ndelta(tlv, uint64(a.localValue.Load()), a.db.Clock()) | ||
case rdx.ZCounter: | ||
dtlv = rdx.Zdelta(tlv, a.localValue.Load(), a.db.Clock()) | ||
default: | ||
return 0, ErrNotCounter | ||
} | ||
a.tlv.Store(dtlv) | ||
a.lock.Unlock() | ||
changes := make(protocol.Records, 0) | ||
changes = append(changes, protocol.Record('F', rdx.ZipUint64(uint64(a.offset)))) | ||
changes = append(changes, protocol.Record(rdt, dtlv)) | ||
a.db.CommitPacket(ctx, 'E', a.rid, changes) | ||
return a.localValue.Load(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package chotki | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/cockroachdb/pebble" | ||
"github.com/drpcorg/chotki/protocol" | ||
"github.com/drpcorg/chotki/rdx" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAtomicCounter(t *testing.T) { | ||
dir, err := os.MkdirTemp("", "*") | ||
assert.NoError(t, err) | ||
|
||
a, err := Open(dir, Options{ | ||
Src: 0x1a, | ||
Name: "test replica", | ||
Options: pebble.Options{ErrorIfExists: true}, | ||
}) | ||
assert.NoError(t, err) | ||
|
||
cid, err := a.NewClass(context.Background(), rdx.ID0, Field{Name: "test", RdxType: rdx.Natural}) | ||
assert.NoError(t, err) | ||
|
||
rid, err := a.NewObjectTLV(context.Background(), cid, protocol.Records{protocol.Record('N', rdx.Ntlv(0))}) | ||
assert.NoError(t, err) | ||
|
||
counterA := NewAtomicCounter(a, rid, 1, 0) | ||
counterB := NewAtomicCounter(a, rid, 1, 0) | ||
|
||
res, err := counterA.Increment(context.Background(), 1) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 1, res) | ||
|
||
res, err = counterB.Increment(context.Background(), 1) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 2, res) | ||
|
||
res, err = counterA.Increment(context.Background(), 1) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 3, res) | ||
} | ||
|
||
func TestAtomicCounterWithPeriodicUpdate(t *testing.T) { | ||
dira, err := os.MkdirTemp("", "*") | ||
assert.NoError(t, err) | ||
|
||
a, err := Open(dira, Options{ | ||
Src: 0x1a, | ||
Name: "test replica", | ||
Options: pebble.Options{ErrorIfExists: true}, | ||
}) | ||
assert.NoError(t, err) | ||
|
||
dirb, err := os.MkdirTemp("", "*") | ||
assert.NoError(t, err) | ||
|
||
b, err := Open(dirb, Options{ | ||
Src: 0x1b, | ||
Name: "test replica2", | ||
Options: pebble.Options{ErrorIfExists: true}, | ||
}) | ||
assert.NoError(t, err) | ||
|
||
cid, err := a.NewClass( | ||
context.Background(), rdx.ID0, | ||
Field{Name: "test", RdxType: rdx.Natural}, | ||
Field{Name: "test2", RdxType: rdx.ZCounter}, | ||
) | ||
assert.NoError(t, err) | ||
|
||
rid, err := a.NewObjectTLV( | ||
context.Background(), cid, | ||
protocol.Records{ | ||
protocol.Record('N', rdx.Ntlv(0)), | ||
protocol.Record('Z', rdx.Ztlv(0)), | ||
}, | ||
) | ||
assert.NoError(t, err) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
for i := 1; i <= 2; i++ { | ||
|
||
counterA := NewAtomicCounter(a, rid, uint64(i), 100*time.Millisecond) | ||
counterB := NewAtomicCounter(b, rid, uint64(i), 0) | ||
|
||
// first increment | ||
res, err := counterA.Increment(ctx, 1) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 1, res) | ||
|
||
syncData(a, b) | ||
|
||
// increment from another replica | ||
res, err = counterB.Increment(ctx, 1) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 2, res) | ||
|
||
syncData(a, b) | ||
|
||
// this increment does not account data from other replica because current value is cached | ||
res, err = counterA.Increment(ctx, 1) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 2, res) | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
|
||
// after wait we increment, and we get actual value | ||
res, err = counterA.Increment(ctx, 1) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 4, res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.