Gossip Glommers Challenge 4 how to set :src in request to seq-kv?

So I am trying to read from the seq-kv service as soon as my node starts to find out whether a key exists in the kv store or not. If it doesn’t exist then my code will send a write request to initialize a key with 0. I am assuming that all nodes need to update the same key and since any of the nodes could be the first one to initialize it the nodes should check for it before serving any requests from maelstrom clients.

The code at the start of the main function looks something like this

func main() {
	n := maelstrom.NewNode()
	kv := maelstrom.NewSeqKV(n)
	ctx := context.Background()
	key := "counter"
	_, err := kv.Read(ctx, key)
	if errors.As(err, &maelstrom.RPCError{}) {
		e := err.(*maelstrom.RPCError)
		if e.Code == maelstrom.KeyDoesNotExist {
			kv.Write(ctx, key, 0)
		}
	}

	n.Handle("add", func(msg maelstrom.Message) error {...})

	n.Handle("read", func(msg maelstrom.Message) error {...})

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

Now running this i get an error stating


{:dest "seq-kv", :body {:key "counter", :msg_id 1, :type "read"}}

This is malformed because:

{:src missing-required-key}

But how do i set the :src key using the seq-kv API? the Read method itself calls this internal method https://github.com/jepsen-io/maelstrom/blob/main/demo/go/kv.go#L108 that only sets the :dst and doesn’t set any :src

am i missing something here?

the errors seems to have gone away after i moved the call inside a init handler

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.