Skip to content

Commit

Permalink
Merge pull request #1 from drpcorg/setup-base-infra
Browse files Browse the repository at this point in the history
Setup base infra
  • Loading branch information
mrdimidium authored Mar 29, 2024
2 parents b34175f + 13d276f commit d07bbd8
Show file tree
Hide file tree
Showing 36 changed files with 227 additions and 118 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.go text eol=lf
40 changes: 40 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Package tests

on: [push]

permissions:
contents: read # allow read access to the content for analysis
pull-requests: read # allow read access to pull request
checks: write # allow write access to checks to allow the action to annotate code in the PR

jobs:
tests:
name: Go tests
strategy:
matrix:
go: ['1.21']
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with: { go-version: "${{ matrix.go }}", cache: false }

- name: Build
run: go build -v ./...
- name: Test
run: go test -race -v ./...

lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with: { go-version: "1.21", cache: false }

- name: golangci-lint
uses: golangci/golangci-lint-action@v4
with:
version: v1.57.2
skip-cache: true
25 changes: 25 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
run:
timeout: 5m
linters:
disable-all: true
enable:
- asasalint
- durationcheck
- exportloopref
- gocheckcompilerdirectives
- gosimple
- gosmopolitan
- govet
- ineffassign
- nilerr
- reassign
- staticcheck
- typecheck
- unused
- exhaustive
- gochecksumtype
issues:
exclude-rules:
- linters:
- staticcheck
text: "SA1019:"
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: test
test:
go test -race ./...

.PHONY: lint
lint:
golangci-lint run ./...
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Fast distributed cache (CRDT based)

[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/drpcorg/chotki)
[![MIT License](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/drpcorg/chotki/main/LICENSE)
[![Build Status](https://github.com/drpcorg/chotki/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/drpcorg/chotki/actions/workflows/test.yml)

<img align="right" width="30%" src="chotki.jpg">
The mission of the system is to keep and update real-time statistics, such as
quotas, counters, billing and suchlike. Update propagation time is expected to
Expand All @@ -21,3 +25,8 @@ newly joining and re-joining replicas. Overall, we take every shortcut to make
the store lightweight and fast while focusing on our specific usecase
(distributed counters, mainly).

## Installation

```bash
go get -u github.com/drpcorg/chotki
```
17 changes: 9 additions & 8 deletions chotki.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package main
package chotki

import (
"encoding/binary"
"errors"
"fmt"
"github.com/cockroachdb/pebble"
"github.com/learn-decentralized-systems/toyqueue"
"github.com/learn-decentralized-systems/toytlv"
"net"
"os"
"sync"

"github.com/cockroachdb/pebble"
"github.com/learn-decentralized-systems/toyqueue"
"github.com/learn-decentralized-systems/toytlv"
)

type Packet []byte
Expand Down Expand Up @@ -39,7 +40,7 @@ type Chotki struct {
lock sync.Mutex
idlock sync.Mutex

tcp toytlv.TCPDepot
tcp toytlv.TCPDepot //nolint:golint,unused

opts Options

Expand Down Expand Up @@ -107,7 +108,7 @@ func merger(key, value []byte) (pebble.ValueMerger, error) {
pma := PebbleMergeAdaptor{
id: id,
rdt: rdt,
vals: append([][]byte{value}),
vals: [][]byte{value},
}
return &pma, nil
}
Expand Down Expand Up @@ -236,7 +237,7 @@ func (ch *Chotki) Drain(recs toyqueue.Records) (err error) {
ch.warn("bad packet: %s", err.Error())
continue
}
apply = append(apply, packet)
apply = append(apply, packet) //nolint:staticcheck
if id.Src() == ch.src && id > ch.last {
if id.Off() != 0 {
return ErrBadPacket
Expand Down Expand Up @@ -286,7 +287,7 @@ func (ch *Chotki) Drain(recs toyqueue.Records) (err error) {
_, _ = fmt.Fprintf(os.Stderr, "unsupported packet %c skipped\n", lit)
}
if !yv && err == nil {
err = ch.db.Apply(&pb, &WriteOptions)
_ = ch.db.Apply(&pb, &WriteOptions)
}
}
if err != nil { // fixme separate packets
Expand Down
7 changes: 4 additions & 3 deletions chotki_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main
package chotki

import (
"github.com/learn-decentralized-systems/toyqueue"
"github.com/stretchr/testify/assert"
"os"
"testing"

"github.com/learn-decentralized-systems/toyqueue"
"github.com/stretchr/testify/assert"
)

func TestChotki_Debug(t *testing.T) {
Expand Down
45 changes: 23 additions & 22 deletions repl.go → cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/ergochat/readline"
"github.com/learn-decentralized-systems/toyqueue"
"github.com/learn-decentralized-systems/toytlv"
"io"
"os"
"strings"

"github.com/drpcorg/chotki"
"github.com/ergochat/readline"
"github.com/learn-decentralized-systems/toyqueue"
"github.com/learn-decentralized-systems/toytlv"
)

var completer = readline.NewPrefixCompleter(
Expand All @@ -30,10 +32,10 @@ func filterInput(r rune) (rune, bool) {
return r, true
}

func ShowObject(chotki *Chotki, id ID) error {
i := chotki.ObjectIterator(id)
func ShowObject(conn *chotki.Chotki, id chotki.ID) error {
i := conn.ObjectIterator(id)
for i.Valid() {
id, rdt := OKeyIdRdt(i.Key())
id, rdt := chotki.OKeyIdRdt(i.Key())
_, _ = fmt.Fprintf(os.Stderr, "%c#%d\t\n", rdt, id.Off())
}
return nil
Expand All @@ -42,48 +44,47 @@ func ShowObject(chotki *Chotki, id ID) error {
var ErrBadObjectJson = errors.New("bad JSON object serialization")
var ErrUnsupportedType = errors.New("unsupported field type")

func CreateObjectFromList(chotki *Chotki, list []interface{}) (id ID, err error) {
func CreateObjectFromList(conn *chotki.Chotki, list []interface{}) (id chotki.ID, err error) {
packet := toyqueue.Records{}
// todo ref type json
// todo add id, ref
for _, f := range list {
var rdt byte
var body []byte
switch f.(type) {
switch t := f.(type) {
case int64:
rdt = 'C'
body = CState(f.(int64))
body = chotki.CState(t)
case float64:
rdt = 'N'
case string:
str := f.(string)
id := ParseBracketedID([]byte(str))
if id != BadId { // check for id-ness
id := chotki.ParseBracketedID([]byte(t))
if id != chotki.BadId { // check for id-ness
rdt = 'L'
body = LState(id, 0)
body = chotki.LState(id, 0)
} else {
rdt = 'S'
body = Stlv(str)
body = chotki.Stlv(t)
}
default:
err = ErrUnsupportedType
return
}
packet = append(packet, toytlv.Record(rdt, body))
}
return chotki.CommitPacket('O', ID0, packet)
return conn.CommitPacket('O', chotki.ID0, packet)
}

// ["{10-4f8-0}", +1, "string", 1.0, ...]
func CreateObject(chotki *Chotki, jsn []byte) (id ID, err error) {
func CreateObject(conn *chotki.Chotki, jsn []byte) (id chotki.ID, err error) {
var parsed interface{}
err = json.Unmarshal(jsn, &parsed)
if err != nil {
return
}
switch parsed.(type) {
case []interface{}:
id, err = CreateObjectFromList(chotki, parsed.([]interface{}))
switch t := parsed.(type) {
case []any:
id, err = CreateObjectFromList(conn, t)
default:
err = ErrBadObjectJson
}
Expand All @@ -108,7 +109,7 @@ func main() {
defer l.Close()
l.CaptureExitSignal()

re := Chotki{}
re := chotki.Chotki{}

if len(os.Args) > 1 {
rno := uint64(1)
Expand Down Expand Up @@ -161,8 +162,8 @@ func main() {
// subscribe
case "show", "list":
for _, arg := range args {
id := ParseIDString(arg)
if id == BadId {
id := chotki.ParseIDString(arg)
if id == chotki.BadId {
_, _ = fmt.Fprintf(os.Stderr, "bad id %s\n", arg)
break
}
Expand Down
40 changes: 20 additions & 20 deletions counter.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
package main
package chotki

import (
"github.com/learn-decentralized-systems/toytlv"
)

type counter64 int64
type Counter64 int64

func (c *Counter64) Apply(state []byte) {
sum, _ := parseC(state, 0)
*c = Counter64(sum)
}

func (c Counter64) Diff(id ID, state []byte) (changes []byte) {
sum, mine := parseC(state, id.Src())
if sum != int64(c) {
d := int64(c) - sum
new_own := mine + d
changes = toytlv.Concat(
toytlv.Record('I', id.ZipBytes()),
toytlv.Record('C', ZipUint64(ZigZagInt64(new_own))),
)
}
return
}

func ProbeID(lit byte, input []byte) ID {
idbody, _ := toytlv.Take(lit, input)
Expand Down Expand Up @@ -72,21 +90,3 @@ func parseC(state []byte, src uint64) (sum, mine int64) {
}
return
}

func (c *counter64) Apply(state []byte) {
sum, _ := parseC(state, 0)
*c = counter64(sum)
}

func (c counter64) Diff(id ID, state []byte) (changes []byte) {
sum, mine := parseC(state, id.Src())
if sum != int64(c) {
d := int64(c) - sum
new_own := mine + d
changes = toytlv.Concat(
toytlv.Record('I', id.ZipBytes()),
toytlv.Record('C', ZipUint64(ZigZagInt64(new_own))),
)
}
return
}
5 changes: 3 additions & 2 deletions counter_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main
package chotki

import (
"testing"

"github.com/learn-decentralized-systems/toytlv"
"github.com/stretchr/testify/assert"
"testing"
)

func TestCounterMerge(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions debug.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main
package chotki

import (
hex2 "encoding/hex"
"fmt"
"github.com/cockroachdb/pebble"
"os"

"github.com/cockroachdb/pebble"
)

func ChotkiKVString(key, value []byte) string {
Expand Down
2 changes: 1 addition & 1 deletion escape.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package chotki

// https://raw.githubusercontent.com/buger/jsonparser/master/escape.go

Expand Down
2 changes: 1 addition & 1 deletion hs_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package chotki

/*
import (
Expand Down
Loading

0 comments on commit d07bbd8

Please sign in to comment.