Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions server/gcas/gcas_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,21 @@ func (g *GcasImpl) Put(ctx context.Context, hash Hash, data []byte) error {
return ErrNoNodes{}
}

idx := rand.Intn(len(nodes))
node := nodes[idx]

err := node.cas.Put(ctx, hash, data)

if err != nil {
return err
rand.Shuffle(len(nodes), func(i, j int) {
nodes[i], nodes[j] = nodes[j], nodes[i]
})

var lastErr error
for _, node := range nodes {
err := node.cas.Put(ctx, hash, data)
if err == nil {
_, err = g.db.ExecContext(ctx, "INSERT INTO chunks (hash, size, node_id) VALUES (?, ?, ?)", hash[:], len(data), node.id)
return err
}
lastErr = err
}

_, err = g.db.ExecContext(ctx, "INSERT INTO chunks (hash, size, node_id) VALUES (?, ?, ?)", hash[:], len(data), node.id)
return err
return lastErr
}

var _ GCAS = (*GcasImpl)(nil)