Skip to content

Commit

Permalink
6a
Browse files Browse the repository at this point in the history
  • Loading branch information
Teiva Harsanyi committed Feb 27, 2023
1 parent b07a1cb commit d4df543
Show file tree
Hide file tree
Showing 363 changed files with 197,366 additions and 30 deletions.
30 changes: 0 additions & 30 deletions challenge-5c-kafka-log/main_test.go

This file was deleted.

10 changes: 10 additions & 0 deletions challenge-6a-totally-available-transactions/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/teivah/gossip-glomers/challenge-6a-totally-available-transactions

go 1.20

require (
github.com/jepsen-io/maelstrom/demo/go v0.0.0-20230113211434-22f433519054
github.com/sirupsen/logrus v1.9.0
)

require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
17 changes: 17 additions & 0 deletions challenge-6a-totally-available-transactions/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jepsen-io/maelstrom/demo/go v0.0.0-20230113211434-22f433519054 h1:NF9yM6z/+jj+LpIsv9dBzF3o4IOgVjqg6hkpuxy55mc=
github.com/jepsen-io/maelstrom/demo/go v0.0.0-20230113211434-22f433519054/go.mod h1:i6aVIs5AIOOaQF1lAisBm7DDeWM1Iopf+26UxjagsCU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
100 changes: 100 additions & 0 deletions challenge-6a-totally-available-transactions/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
"encoding/json"
"os"
"strconv"
"sync"

maelstrom "github.com/jepsen-io/maelstrom/demo/go"
log "github.com/sirupsen/logrus"
)

func init() {
f, err := os.OpenFile("/tmp/maelstrom.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
log.SetOutput(f)
}

func main() {
n := maelstrom.NewNode()
kv := maelstrom.NewLinKV(n)
s := &server{
n: n,
kv: kv,
store: make(map[int]int),
}

n.Handle("init", wrap(s.initHandler))
n.Handle("txn", wrap(s.txnHandler))

if err := n.Run(); err != nil {
log.Fatal(err)
}
}

func wrap(f func(msg maelstrom.Message) error) func(msg maelstrom.Message) error {
return func(msg maelstrom.Message) error {
if err := f(msg); err != nil {
log.Error(err)
return err
}
return nil
}
}

type server struct {
n *maelstrom.Node
kv *maelstrom.KV
nodeID string
id int

mu sync.Mutex
store map[int]int
}

func (s *server) initHandler(_ maelstrom.Message) error {
s.nodeID = s.n.ID()
id, err := strconv.Atoi(s.nodeID[1:])
if err != nil {
return err
}
s.id = id
return nil
}

type txnReq struct {
MsgId int `json:"msg_id"`
Txn [][]any `json:"txn"`
}

func (s *server) txnHandler(msg maelstrom.Message) error {
var req txnReq
if err := json.Unmarshal(msg.Body, &req); err != nil {
return err
}

res := make([][]any, 0, len(req.Txn))
s.mu.Lock()
for _, txn := range req.Txn {
op := make([]any, len(txn))
copy(op, txn)
if txn[0] == "r" {
a := int(txn[1].(float64))
op[2] = s.store[a]
} else if txn[0] == "w" {
a := int(txn[1].(float64))
b := int(txn[2].(float64))
s.store[a] = b
}
res = append(res, op)
}
s.mu.Unlock()

return s.n.Reply(msg, map[string]any{
"type": "txn_ok",
"txn": res,
})
}
7 changes: 7 additions & 0 deletions challenge-6a-totally-available-transactions/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

cwd=$(pwd)
go build -o bin
cd $MAELSTROM_PATH
./maelstrom test -w txn-rw-register --bin $cwd/bin --node-count 1 --time-limit 20 --rate 1000 --concurrency 2n --consistency-models read-uncommitted --availability total
cd $cwd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d4df543

Please sign in to comment.