2020-05-15 17:59:37 +03:00
|
|
|
// +build kvdb_etcd
|
|
|
|
|
2020-02-18 21:26:57 +03:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
2020-05-25 19:00:22 +03:00
|
|
|
"context"
|
2020-02-18 21:26:57 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2020-12-09 00:50:03 +03:00
|
|
|
"sync/atomic"
|
2020-02-18 21:26:57 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coreos/etcd/embed"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// readyTimeout is the time until the embedded etcd instance should start.
|
|
|
|
readyTimeout = 10 * time.Second
|
|
|
|
|
2020-12-09 00:50:03 +03:00
|
|
|
// defaultEtcdPort is the start of the range for listening ports of
|
|
|
|
// embedded etcd servers. Ports are monotonically increasing starting
|
|
|
|
// from this number and are determined by the results of getFreePort().
|
|
|
|
defaultEtcdPort = 2379
|
|
|
|
)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
2020-12-09 00:50:03 +03:00
|
|
|
var (
|
|
|
|
// lastPort is the last port determined to be free for use by a new
|
|
|
|
// embedded etcd server. It should be used atomically.
|
|
|
|
lastPort uint32 = defaultEtcdPort
|
|
|
|
)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
2020-12-09 00:50:03 +03:00
|
|
|
// getFreePort returns the first port that is available for listening by a new
|
|
|
|
// embedded etcd server. It panics if no port is found and the maximum available
|
|
|
|
// TCP port is reached.
|
|
|
|
func getFreePort() int {
|
|
|
|
port := atomic.AddUint32(&lastPort, 1)
|
|
|
|
for port < 65535 {
|
|
|
|
// If there are no errors while attempting to listen on this
|
|
|
|
// port, close the socket and return it as available.
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", port)
|
|
|
|
l, err := net.Listen("tcp4", addr)
|
|
|
|
if err == nil {
|
|
|
|
err := l.Close()
|
|
|
|
if err == nil {
|
|
|
|
return int(port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
port = atomic.AddUint32(&lastPort, 1)
|
2020-02-18 21:26:57 +03:00
|
|
|
}
|
|
|
|
|
2020-12-09 00:50:03 +03:00
|
|
|
// No ports available? Must be a mistake.
|
|
|
|
panic("no ports available for listening")
|
2020-02-18 21:26:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEmbeddedEtcdInstance creates an embedded etcd instance for testing,
|
|
|
|
// listening on random open ports. Returns the backend config and a cleanup
|
|
|
|
// func that will stop the etcd instance.
|
|
|
|
func NewEmbeddedEtcdInstance(path string) (*BackendConfig, func(), error) {
|
|
|
|
cfg := embed.NewConfig()
|
|
|
|
cfg.Dir = path
|
|
|
|
|
|
|
|
// To ensure that we can submit large transactions.
|
2020-07-10 16:29:48 +03:00
|
|
|
cfg.MaxTxnOps = 8192
|
|
|
|
cfg.MaxRequestBytes = 16384 * 1024
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Listen on random free ports.
|
|
|
|
clientURL := fmt.Sprintf("127.0.0.1:%d", getFreePort())
|
|
|
|
peerURL := fmt.Sprintf("127.0.0.1:%d", getFreePort())
|
|
|
|
cfg.LCUrls = []url.URL{{Host: clientURL}}
|
|
|
|
cfg.LPUrls = []url.URL{{Host: peerURL}}
|
|
|
|
|
|
|
|
etcd, err := embed.StartEtcd(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-etcd.Server.ReadyNotify():
|
|
|
|
case <-time.After(readyTimeout):
|
|
|
|
etcd.Close()
|
|
|
|
return nil, nil,
|
|
|
|
fmt.Errorf("etcd failed to start after: %v", readyTimeout)
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:38:37 +03:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
2020-02-18 21:26:57 +03:00
|
|
|
connConfig := &BackendConfig{
|
2020-08-10 17:38:37 +03:00
|
|
|
Ctx: ctx,
|
2020-05-08 19:38:46 +03:00
|
|
|
Host: "http://" + peerURL,
|
|
|
|
User: "user",
|
|
|
|
Pass: "pass",
|
|
|
|
InsecureSkipVerify: true,
|
2020-02-18 21:26:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return connConfig, func() {
|
2020-08-10 17:38:37 +03:00
|
|
|
cancel()
|
2020-02-18 21:26:57 +03:00
|
|
|
etcd.Close()
|
|
|
|
}, nil
|
|
|
|
}
|