feat: remove peers (CLI + HTTP) so retired nodes stop being retried (#11)
Closes #10. Peers were append-only: `UpsertPeer`/`MergePeer` existed, nothing could remove a row. A retired node therefore stayed in the table forever and failed on every anti-entropy sweep — the laptop node has been retrying a dead LAN peer (`http://192.168.8.240:8931`) every minute for weeks, with no fix short of hand-editing SQLite. ## Changes - `KnoxDB.DeletePeer(identifier)` — exact match on `peer_id` **or** `addr`; empty identifier rejected. - `DELETE /v1/peers/{peer_id}` and `DELETE /v1/peers?addr=<url>` — the query form exists because addresses contain slashes. Both sit behind the same bearer-token middleware as the rest of the gossip API, so they inherit TLS + `KNOX_AUTH_TOKEN`. - `knox gossip peer list` / `knox gossip peer rm <peer-id|address>`. - The CLI warns when the removed address is still in `KNOX_PEERS`, because a static seed is re-merged on the next sweep and would silently come back. ## Caveat worth knowing Deletion only sticks for peers learned through membership gossip. Anything still listed in `KNOX_PEERS` will be re-learned; remove it from the environment too. ## Verification - `internal/db/gossip_test.go` — delete by id, by address, unknown peer is a no-op, empty identifier rejected - `internal/watch/gossip_test.go` — 404 for unknown, 200 by id, 200 by `?addr=` - `go vet ./...`, `go test ./...` green - Used live: removed the stale `192.168.8.240` peer on the laptop node; the every-minute `no route to host` errors stopped on the next sweep Reviewed-on: #11 Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
This commit was merged in pull request #11.
This commit is contained in:
@@ -53,6 +53,10 @@ func (n *Node) Handler() http.Handler {
|
||||
mux.HandleFunc("GET /v1/log", n.handleLog)
|
||||
mux.HandleFunc("POST /v1/obs/batch", n.handleBatch)
|
||||
mux.HandleFunc("GET /v1/diff", n.handleDiff)
|
||||
mux.HandleFunc("DELETE /v1/peers/{id}", n.handleDeletePeer)
|
||||
// Addresses contain slashes, so they travel as ?addr= rather than a path
|
||||
// segment.
|
||||
mux.HandleFunc("DELETE /v1/peers", n.handleDeletePeer)
|
||||
return mux
|
||||
}
|
||||
|
||||
@@ -183,6 +187,31 @@ func (n *Node) handleDiff(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
// handleDeletePeer forgets a peer, either by id in the path or by ?addr= for
|
||||
// addresses that contain slashes. Deletion is not permanent against a static
|
||||
// seed: KNOX_PEERS is re-merged on the next sweep, so a seeded peer must also
|
||||
// be removed from the environment.
|
||||
func (n *Node) handleDeletePeer(w http.ResponseWriter, r *http.Request) {
|
||||
key := r.PathValue("id")
|
||||
if addr := r.URL.Query().Get("addr"); addr != "" {
|
||||
key = addr
|
||||
}
|
||||
if key == "" {
|
||||
http.Error(w, "peer id or address required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
deleted, err := n.Kdb.DeletePeer(key)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if deleted == 0 {
|
||||
http.Error(w, "no such peer: "+key, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]any{"deleted": deleted})
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, v any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
enc := json.NewEncoder(w)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -363,3 +364,47 @@ func TestClientPushSurfacesHTTPError(t *testing.T) {
|
||||
t.Fatal("expected push error on HTTP 500, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleDeletePeer: the peer list is otherwise append-only, so there must be
|
||||
// a way to forget a node that moved or was decommissioned. Both addressing
|
||||
// forms are accepted, and an unknown peer is a 404 rather than a silent success.
|
||||
func TestHandleDeletePeer(t *testing.T) {
|
||||
b := tmpKnoxDB(t)
|
||||
node := &Node{Kdb: b, Name: "A"}
|
||||
sv := httptest.NewServer(node.Handler())
|
||||
defer sv.Close()
|
||||
|
||||
if err := b.MergePeer("0123456789abcdef0123456789abcdef", "http://10.0.0.9:8931", "old"); err != nil {
|
||||
t.Fatalf("merge: %v", err)
|
||||
}
|
||||
|
||||
del := func(path string) int {
|
||||
req, _ := http.NewRequest(http.MethodDelete, sv.URL+path, nil)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("delete %s: %v", path, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return resp.StatusCode
|
||||
}
|
||||
|
||||
if code := del("/v1/peers/unknown-id"); code != http.StatusNotFound {
|
||||
t.Errorf("unknown peer: got %d, want 404", code)
|
||||
}
|
||||
if code := del("/v1/peers/0123456789abcdef0123456789abcdef"); code != http.StatusOK {
|
||||
t.Errorf("by id: got %d, want 200", code)
|
||||
}
|
||||
if peers, _ := b.ListPeers(); len(peers) != 0 {
|
||||
t.Fatalf("peer not deleted: %+v", peers)
|
||||
}
|
||||
|
||||
if err := b.MergePeer("ffff", "http://10.0.0.10:8931", "old2"); err != nil {
|
||||
t.Fatalf("merge2: %v", err)
|
||||
}
|
||||
if code := del("/v1/peers?addr=" + url.QueryEscape("http://10.0.0.10:8931")); code != http.StatusOK {
|
||||
t.Errorf("by addr: got %d, want 200", code)
|
||||
}
|
||||
if peers, _ := b.ListPeers(); len(peers) != 0 {
|
||||
t.Fatalf("peer not deleted by addr: %+v", peers)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user