v2 #2

Merged
johnnylee merged 4 commits from v2 into master 2023-11-17 10:06:57 +00:00
11 changed files with 196 additions and 266 deletions
Showing only changes of commit 270347a05c - Show all commits

View File

@ -1,52 +0,0 @@
package rep
import (
"encoding/binary"
"encoding/json"
"net"
"path/filepath"
"time"
"git.crumpington.com/public/jldb/lib/errs"
)
// ----------------------------------------------------------------------------
func lockFilePath(rootDir string) string {
return filepath.Join(rootDir, "lock")
}
func walRootDir(rootDir string) string {
return filepath.Join(rootDir, "wal")
}
func stateFilePath(rootDir string) string {
return filepath.Join(rootDir, "state")
}
// ----------------------------------------------------------------------------
func sendJSON(
item any,
conn net.Conn,
timeout time.Duration,
) error {
buf := bufPoolGet()
defer bufPoolPut(buf)
if err := json.NewEncoder(buf).Encode(item); err != nil {
return errs.Unexpected.WithErr(err)
}
sizeBuf := make([]byte, 2)
binary.LittleEndian.PutUint16(sizeBuf, uint16(buf.Len()))
conn.SetWriteDeadline(time.Now().Add(timeout))
buffers := net.Buffers{sizeBuf, buf.Bytes()}
if _, err := buffers.WriteTo(conn); err != nil {
return errs.IO.WithErr(err)
}
return nil
}

View File

@ -1,10 +1,10 @@
package rep package rep
import ( import (
"encoding/binary"
"encoding/json" "encoding/json"
"io"
"net" "net"
"net/http"
"strings"
"sync" "sync"
"time" "time"
@ -14,166 +14,96 @@ import (
) )
type client struct { type client struct {
// Mutex-protected variables. client *http.Client
lock sync.Mutex
closed bool
conn net.Conn
// The following are constant. // The following are constant.
endpoint string endpoint string
psk []byte pskBytes [64]byte
timeout time.Duration timeout time.Duration
buf []byte lock sync.Mutex
conn net.Conn
} }
func newClient(endpoint, psk string, timeout time.Duration) *client { func newClient(endpoint, psk string, timeout time.Duration) *client {
b := make([]byte, 256) httpClient := &http.Client{
copy(b, []byte(psk)) Timeout: timeout,
}
if !strings.HasSuffix(endpoint, "/") {
endpoint += "/"
}
return &client{ return &client{
client: httpClient,
endpoint: endpoint, endpoint: endpoint,
psk: b, pskBytes: pskToBytes(psk),
timeout: timeout, timeout: timeout,
} }
} }
func (c *client) GetInfo() (info Info, err error) { func (c *client) GetInfo() (info Info, err error) {
err = c.withConn(cmdGetInfo, func(conn net.Conn) error { req, err := http.NewRequest(http.MethodGet, c.endpoint+pathGetInfo, nil)
return c.recvJSON(&info, conn, c.timeout) if err != nil {
}) return info, errs.Unexpected.WithErr(err)
return info, err }
req.SetBasicAuth("", string(c.pskBytes[:]))
resp, err := c.client.Do(req)
if err != nil {
return info, errs.IO.WithErr(err)
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
return info, errs.IO.WithErr(err)
}
return info, nil
} }
func (c *client) RecvState(recv func(net.Conn) error) error { func (c *client) RecvState(recv func(net.Conn) error) error {
return c.withConn(cmdSendState, recv) err := c.dialConnect(c.endpoint + pathSendState)
if err != nil {
return err
}
defer c.conn.Close()
return recv(c.conn)
} }
func (c *client) StreamWAL(w *wal.WAL) error { func (c *client) StreamWAL(w *wal.WAL) error {
return c.withConn(cmdStreamWAL, func(conn net.Conn) error { err := c.dialConnect(c.endpoint + pathStreamWAL)
return w.Recv(conn, c.timeout) if err != nil {
}) return err
}
defer c.conn.Close()
return w.Recv(c.conn, c.timeout)
} }
func (c *client) Close() { func (c *client) Close() {
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
c.closed = true
if c.conn != nil { if c.conn != nil {
c.conn.Close() c.conn.Close()
c.conn = nil
} }
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
func (c *client) writeCmd(cmd byte) error { func (c *client) dialConnect(endpoint string) error {
c.conn.SetWriteDeadline(time.Now().Add(c.timeout)) conn, err := httpconn.Dial(endpoint)
if _, err := c.conn.Write([]byte{cmd}); err != nil {
return errs.IO.WithErr(err)
}
return nil
}
func (c *client) dial() error {
c.conn = nil
conn, err := httpconn.Dial(c.endpoint)
if err != nil { if err != nil {
return err return err
} }
conn.SetWriteDeadline(time.Now().Add(c.timeout)) conn.SetWriteDeadline(time.Now().Add(c.timeout))
if _, err := conn.Write(c.psk); err != nil { if _, err := conn.Write(c.pskBytes[:]); err != nil {
conn.Close()
return errs.IO.WithErr(err) return errs.IO.WithErr(err)
} }
c.lock.Lock()
defer c.lock.Unlock()
c.conn = conn c.conn = conn
return nil return nil
} }
func (c *client) withConn(cmd byte, fn func(net.Conn) error) error {
conn, err := c.getConn(cmd)
if err != nil {
return err
}
if err := fn(conn); err != nil {
conn.Close()
return err
}
return nil
}
func (c *client) getConn(cmd byte) (net.Conn, error) {
c.lock.Lock()
defer c.lock.Unlock()
if c.closed {
return nil, errs.IO.WithErr(io.EOF)
}
dialed := false
if c.conn == nil {
if err := c.dial(); err != nil {
return nil, err
}
dialed = true
}
if err := c.writeCmd(cmd); err != nil {
if dialed {
c.conn = nil
return nil, err
}
if err := c.dial(); err != nil {
return nil, err
}
if err := c.writeCmd(cmd); err != nil {
return nil, err
}
}
return c.conn, nil
}
func (c *client) recvJSON(
item any,
conn net.Conn,
timeout time.Duration,
) error {
if cap(c.buf) < 2 {
c.buf = make([]byte, 0, 1024)
}
buf := c.buf[:2]
conn.SetReadDeadline(time.Now().Add(timeout))
if _, err := io.ReadFull(conn, buf); err != nil {
return errs.IO.WithErr(err)
}
size := binary.LittleEndian.Uint16(buf)
if cap(buf) < int(size) {
buf = make([]byte, size)
c.buf = buf
}
buf = buf[:size]
if _, err := io.ReadFull(conn, buf); err != nil {
return errs.IO.WithErr(err)
}
if err := json.Unmarshal(buf, item); err != nil {
return errs.Unexpected.WithErr(err)
}
return nil
}

View File

@ -0,0 +1,54 @@
package rep
import (
"crypto/subtle"
"io"
"log"
"net"
"net/http"
"time"
"git.crumpington.com/public/jldb/lib/httpconn"
)
func (rep *Replicator) handlerLogf(pattern string, args ...any) {
log.Printf("[HTTP-HANDLER] "+pattern, args...)
}
// checkBasicAuth tests whether the caller has provided the appropriate basic auth
// header. The caller should provide the PSK as the basic-auth password.
func (rep *Replicator) checkBasicAuth(w http.ResponseWriter, r *http.Request) bool {
_, pwd, _ := r.BasicAuth()
if subtle.ConstantTimeCompare([]byte(pwd), rep.pskBytes[:]) != 1 {
rep.handlerLogf("PSK mismatch.")
http.Error(w, "not authorized", http.StatusUnauthorized)
return false
}
return true
}
// acceptConnect accepts a CONNECT request and checks the PSK.
func (rep *Replicator) acceptConnect(w http.ResponseWriter, r *http.Request) net.Conn {
conn, err := httpconn.Accept(w, r)
if err != nil {
rep.handlerLogf("Failed to accept connection: %s", err)
return nil
}
psk := [64]byte{}
conn.SetReadDeadline(time.Now().Add(rep.conf.NetTimeout))
if _, err := io.ReadFull(conn, psk[:]); err != nil {
conn.Close()
rep.handlerLogf("Failed to read PSK: %v", err)
return nil
}
if subtle.ConstantTimeCompare(psk[:], rep.pskBytes[:]) != 1 {
conn.Close()
rep.handlerLogf("PSK mismatch.")
return nil
}
return conn
}

View File

@ -1,82 +1,69 @@
package rep package rep
import ( import (
"crypto/subtle" "encoding/json"
"log"
"net/http" "net/http"
"time" "path"
"git.crumpington.com/public/jldb/lib/httpconn"
) )
const ( const (
cmdGetInfo = 10 pathGetInfo = "get-info"
cmdSendState = 20 pathSendState = "send-state"
cmdStreamWAL = 30 pathStreamWAL = "stream-wal"
) )
// ---------------------------------------------------------------------------
func (rep *Replicator) Handle(w http.ResponseWriter, r *http.Request) { func (rep *Replicator) Handle(w http.ResponseWriter, r *http.Request) {
logf := func(pattern string, args ...any) { // We'll handle two types of requests: HTTP GET requests for JSON, or
log.Printf("[HTTP-HANDLER] "+pattern, args...) // streaming requets for state or wall.
base := path.Base(r.URL.Path)
switch base {
case pathGetInfo:
rep.handleGetInfo(w, r)
case pathSendState:
rep.handleSendState(w, r)
case pathStreamWAL:
rep.handleStreamWAL(w, r)
default:
http.Error(w, "not found", http.StatusNotFound)
}
} }
conn, err := httpconn.Accept(w, r) func (rep *Replicator) handleGetInfo(w http.ResponseWriter, r *http.Request) {
if err != nil { if !rep.checkBasicAuth(w, r) {
logf("Failed to accept connection: %s", err) return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(rep.Info()); err != nil {
rep.handlerLogf("Failed to send info: %s", err)
}
}
func (rep *Replicator) handleSendState(w http.ResponseWriter, r *http.Request) {
conn := rep.acceptConnect(w, r)
if conn == nil {
return return
} }
defer conn.Close() defer conn.Close()
psk := make([]byte, 256)
conn.SetReadDeadline(time.Now().Add(rep.conf.NetTimeout))
if _, err := conn.Read(psk); err != nil {
logf("Failed to read PSK: %v", err)
return
}
expected := rep.pskBytes
if subtle.ConstantTimeCompare(expected, psk) != 1 {
logf("PSK mismatch.")
return
}
cmd := make([]byte, 1)
for {
conn.SetReadDeadline(time.Now().Add(rep.conf.NetTimeout))
if _, err := conn.Read(cmd); err != nil {
logf("Read failed: %v", err)
return
}
switch cmd[0] {
case cmdGetInfo:
if err := sendJSON(rep.Info(), conn, rep.conf.NetTimeout); err != nil {
logf("Failed to send info: %s", err)
return
}
case cmdSendState:
if err := rep.sendState(conn); err != nil { if err := rep.sendState(conn); err != nil {
if !rep.stopped() { if !rep.stopped() {
logf("Failed to send state: %s", err) rep.handlerLogf("Failed to send state: %s", err)
}
} }
return
} }
case cmdStreamWAL: func (rep *Replicator) handleStreamWAL(w http.ResponseWriter, r *http.Request) {
conn := rep.acceptConnect(w, r)
if conn == nil {
return
}
defer conn.Close()
err := rep.wal.Send(conn, rep.conf.NetTimeout) err := rep.wal.Send(conn, rep.conf.NetTimeout)
if !rep.stopped() { if !rep.stopped() {
logf("Failed when sending WAL: %s", err) rep.handlerLogf("Failed when streaming WAL: %s", err)
}
return
}
} }
} }

17
lib/rep/paths.go Normal file
View File

@ -0,0 +1,17 @@
package rep
import (
"path/filepath"
)
func lockFilePath(rootDir string) string {
return filepath.Join(rootDir, "lock")
}
func walRootDir(rootDir string) string {
return filepath.Join(rootDir, "wal")
}
func stateFilePath(rootDir string) string {
return filepath.Join(rootDir, "state")
}

View File

@ -1,21 +0,0 @@
package rep
import (
"bytes"
"sync"
)
var bufPool = sync.Pool{
New: func() any {
return &bytes.Buffer{}
},
}
func bufPoolGet() *bytes.Buffer {
return bufPool.Get().(*bytes.Buffer)
}
func bufPoolPut(b *bytes.Buffer) {
b.Reset()
bufPool.Put(b)
}

16
lib/rep/psk.go Normal file
View File

@ -0,0 +1,16 @@
package rep
import (
"crypto/sha256"
"encoding/hex"
)
func pskToBytes(in string) [64]byte {
b := sha256.Sum256([]byte(in))
dst := [64]byte{}
i := hex.Encode(dst[:], b[:])
if i != 64 {
panic(i)
}
return dst
}

View File

@ -27,9 +27,7 @@ func (rep *Replicator) loadConfigDefaults() {
} }
rep.conf = conf rep.conf = conf
rep.pskBytes = pskToBytes(conf.ReplicationPSK)
rep.pskBytes = make([]byte, 256)
copy(rep.pskBytes, []byte(conf.ReplicationPSK))
} }
func (rep *Replicator) initDirectories() error { func (rep *Replicator) initDirectories() error {

View File

@ -30,9 +30,8 @@ func (rep *Replicator) runWALRecvrOnce() {
log.Printf("[WAL-RECVR] "+pattern, args...) log.Printf("[WAL-RECVR] "+pattern, args...)
} }
if err := rep.client.StreamWAL(rep.wal); err != nil { err := rep.client.StreamWAL(rep.wal)
if !rep.stopped() { if !rep.stopped() {
logf("Recv failed: %v", err) logf("Recv failed: %v", err)
} }
} }
}

View File

@ -60,7 +60,8 @@ type Replicator struct {
conf Config conf Config
lockFile *os.File lockFile *os.File
pskBytes []byte pskBytes [64]byte // 64 ascii characters. See pskToBytes.
wal *wal.WAL wal *wal.WAL
appendNotify chan struct{} appendNotify chan struct{}

View File

@ -11,6 +11,7 @@ import (
"testing" "testing"
"time" "time"
"git.crumpington.com/public/jldb/lib/errs"
"git.crumpington.com/public/jldb/lib/wal" "git.crumpington.com/public/jldb/lib/wal"
) )
@ -83,7 +84,7 @@ func newApp(t *testing.T, id int64, conf Config) *TestApp {
Apply: a.apply, Apply: a.apply,
}, conf) }, conf)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(errs.FmtDetails(err))
} }
return a return a