package embed

import "go.etcd.io/etcd/server/v3/embed"

Package embed provides bindings for embedding an etcd server in a program.

Launch an embedded etcd server using the configuration defaults:

import (
	"log"
	"time"

	"go.etcd.io/etcd/server/v3/embed"
)

func main() {
	cfg := embed.NewConfig()
	cfg.Dir = "default.etcd"
	e, err := embed.StartEtcd(cfg)
	if err != nil {
		log.Fatal(err)
	}
	defer e.Close()
	select {
	case <-e.Server.ReadyNotify():
		log.Printf("Server is ready!")
	case <-time.After(60 * time.Second):
		e.Server.Stop() // trigger a shutdown
		log.Printf("Server took too long to start!")
	}
	log.Fatal(<-e.Err())
}

Index

Constants

const (
	ClusterStateFlagNew      = "new"
	ClusterStateFlagExisting = "existing"

	DefaultName                        = "default"
	DefaultMaxSnapshots                = 5
	DefaultMaxWALs                     = 5
	DefaultMaxTxnOps                   = uint(128)
	DefaultWarningApplyDuration        = 100 * time.Millisecond
	DefaultWarningUnaryRequestDuration = 300 * time.Millisecond
	DefaultMaxRequestBytes             = 1.5 * 1024 * 1024
	DefaultMaxConcurrentStreams        = math.MaxUint32
	DefaultGRPCKeepAliveMinTime        = 5 * time.Second
	DefaultGRPCKeepAliveInterval       = 2 * time.Hour
	DefaultGRPCKeepAliveTimeout        = 20 * time.Second
	DefaultDowngradeCheckTime          = 5 * time.Second
	DefaultAutoCompactionMode          = "periodic"
	DefaultAutoCompactionRetention     = "0"
	DefaultAuthToken                   = "simple"
	DefaultCompactHashCheckTime        = time.Minute
	DefaultLoggingFormat               = "json"

	DefaultDiscoveryDialTimeout       = 2 * time.Second
	DefaultDiscoveryRequestTimeOut    = 5 * time.Second
	DefaultDiscoveryKeepAliveTime     = 2 * time.Second
	DefaultDiscoveryKeepAliveTimeOut  = 6 * time.Second
	DefaultDiscoveryInsecureTransport = true
	DefaultSelfSignedCertValidity     = 1
	DefaultTLSMinVersion              = string(tlsutil.TLSVersion12)

	DefaultListenPeerURLs   = "http://localhost:2380"
	DefaultListenClientURLs = "http://localhost:2379"

	DefaultLogOutput = "default"
	JournalLogOutput = "systemd/journal"
	StdErrLogOutput  = "stderr"
	StdOutLogOutput  = "stdout"

	// DefaultLogRotationConfig is the default configuration used for log rotation.
	// Log rotation is disabled by default.
	// MaxSize    = 100 // MB
	// MaxAge     = 0 // days (no limit)
	// MaxBackups = 0 // no limit
	// LocalTime  = false // use computers local time, UTC by default
	// Compress   = false // compress the rotated log in gzip format
	DefaultLogRotationConfig = `{"maxsize": 100, "maxage": 0, "maxbackups": 0, "localtime": false, "compress": false}`

	// ExperimentalDistributedTracingAddress is the default collector address.
	// TODO: delete in v3.7
	// Deprecated: Use DefaultDistributedTracingAddress instead. Will be decommissioned in v3.7.
	ExperimentalDistributedTracingAddress = "localhost:4317"
	// DefaultDistributedTracingAddress is the default collector address.
	DefaultDistributedTracingAddress = "localhost:4317"
	// ExperimentalDistributedTracingServiceName is the default etcd service name.
	// TODO: delete in v3.7
	// Deprecated: Use DefaultDistributedTracingServiceName instead. Will be decommissioned in v3.7.
	ExperimentalDistributedTracingServiceName = "etcd"
	// DefaultDistributedTracingServiceName is the default etcd service name.
	DefaultDistributedTracingServiceName = "etcd"

	DefaultExperimentalTxnModeWriteWithSharedBuffer = true

	// DefaultStrictReconfigCheck is the default value for "--strict-reconfig-check" flag.
	// It's enabled by default.
	DefaultStrictReconfigCheck = true

	ServerFeatureGateFlagName = "feature-gates"
)

Variables

var (
	ErrConflictBootstrapFlags = fmt.Errorf("multiple discovery or bootstrap flags are set. " +
		"Choose one of \"initial-cluster\", \"discovery\", \"discovery-endpoints\" or \"discovery-srv\"")
	ErrUnsetAdvertiseClientURLsFlag = fmt.Errorf("--advertise-client-urls is required when --listen-client-urls is set explicitly")
	ErrLogRotationInvalidLogOutput  = fmt.Errorf("--log-outputs requires a single file path when --log-rotate-config-json is defined")

	DefaultInitialAdvertisePeerURLs = "http://localhost:2380"
	DefaultAdvertiseClientURLs      = "http://localhost:2379"
)
var (
	// CompactorModePeriodic is periodic compaction mode
	// for "Config.AutoCompactionMode" field.
	// If "AutoCompactionMode" is CompactorModePeriodic and
	// "AutoCompactionRetention" is "1h", it automatically compacts
	// compacts storage every hour.
	CompactorModePeriodic = v3compactor.ModePeriodic

	// CompactorModeRevision is revision-based compaction mode
	// for "Config.AutoCompactionMode" field.
	// If "AutoCompactionMode" is CompactorModeRevision and
	// "AutoCompactionRetention" is "1000", it compacts log on
	// revision 5000 when the current revision is 6000.
	// This runs every 5-minute if enough of logs have proceeded.
	CompactorModeRevision = v3compactor.ModeRevision
)
var ErrMissingClientTLSInfoForMetricsURL = errors.New("client TLS key/cert (--cert-file, --key-file) must be provided for metrics secure url")

Functions

func NewZapLoggerBuilder

func NewZapLoggerBuilder(lg *zap.Logger) func(*Config) error

NewZapLoggerBuilder generates a zap logger builder that sets given logger for embedded etcd.

func SetFeatureGatesFromExperimentalFlags

func SetFeatureGatesFromExperimentalFlags(fg featuregate.FeatureGate, getExperimentalFlagVal func(string) *bool, featureGatesVal string) error

SetFeatureGatesFromExperimentalFlags sets the feature gate values if the feature gate is not explicitly set while their corresponding experimental flags are explicitly set, for all the features in ExperimentalFlagToFeatureMap. TODO: remove after all experimental flags are deprecated.

func WrapCORS

func WrapCORS(cors map[string]struct{}, h http.Handler) http.Handler

WrapCORS wraps existing handler with CORS. TODO: deprecate this after v2 proxy deprecate

Types

type Config

type Config struct {
	Name string `json:"name"`
	Dir  string `json:"data-dir"`
	//revive:disable-next-line:var-naming
	WalDir string `json:"wal-dir"`

	// SnapshotCount is the number of committed transactions that trigger a snapshot to disk.
	// TODO: remove it in 3.7.
	// Deprecated: Will be decommissioned in v3.7.
	SnapshotCount uint64 `json:"snapshot-count"`

	// ExperimentalSnapshotCatchUpEntries is the number of entries for a slow follower
	// to catch-up after compacting the raft storage entries.
	// We expect the follower has a millisecond level latency with the leader.
	// The max throughput is around 10K. Keep a 5K entries is enough for helping
	// follower to catch up.
	// TODO: remove in v3.7.
	// Note we made a mistake in https://github.com/etcd-io/etcd/pull/15033. The json tag
	// `*-catch-up-*` isn't consistent with the command line flag `*-catchup-*`.
	// Deprecated: Use SnapshotCatchUpEntries instead. Will be removed in v3.7.
	ExperimentalSnapshotCatchUpEntries uint64 `json:"experimental-snapshot-catch-up-entries"`

	// SnapshotCatchUpEntries is the number of entires for a slow follower
	// to catch-up after compacting the raft storage entries.
	// We expect the follower has a millisecond level latency with the leader.
	// The max throughput is around 10K. Keep a 5K entries is enough for helping
	// follower to catch up.
	SnapshotCatchUpEntries uint64 `json:"snapshot-catchup-entries"`

	// MaxSnapFiles is the maximum number of snapshot files.
	// TODO: remove it in 3.7.
	// Deprecated: Will be removed in v3.7.
	MaxSnapFiles uint `json:"max-snapshots"`
	//revive:disable-next-line:var-naming
	MaxWalFiles uint `json:"max-wals"`

	// TickMs is the number of milliseconds between heartbeat ticks.
	// TODO: decouple tickMs and heartbeat tick (current heartbeat tick = 1).
	// make ticks a cluster wide configuration.
	TickMs     uint `json:"heartbeat-interval"`
	ElectionMs uint `json:"election-timeout"`

	// InitialElectionTickAdvance is true, then local member fast-forwards
	// election ticks to speed up "initial" leader election trigger. This
	// benefits the case of larger election ticks. For instance, cross
	// datacenter deployment may require longer election timeout of 10-second.
	// If true, local node does not need wait up to 10-second. Instead,
	// forwards its election ticks to 8-second, and have only 2-second left
	// before leader election.
	//
	// Major assumptions are that:
	//  - cluster has no active leader thus advancing ticks enables faster
	//    leader election, or
	//  - cluster already has an established leader, and rejoining follower
	//    is likely to receive heartbeats from the leader after tick advance
	//    and before election timeout.
	//
	// However, when network from leader to rejoining follower is congested,
	// and the follower does not receive leader heartbeat within left election
	// ticks, disruptive election has to happen thus affecting cluster
	// availabilities.
	//
	// Disabling this would slow down initial bootstrap process for cross
	// datacenter deployments. Make your own tradeoffs by configuring
	// --initial-election-tick-advance at the cost of slow initial bootstrap.
	//
	// If single-node, it advances ticks regardless.
	//
	// See https://github.com/etcd-io/etcd/issues/9333 for more detail.
	InitialElectionTickAdvance bool `json:"initial-election-tick-advance"`

	// BackendBatchInterval is the maximum time before commit the backend transaction.
	BackendBatchInterval time.Duration `json:"backend-batch-interval"`
	// BackendBatchLimit is the maximum operations before commit the backend transaction.
	BackendBatchLimit int `json:"backend-batch-limit"`
	// BackendFreelistType specifies the type of freelist that boltdb backend uses (array and map are supported types).
	BackendFreelistType string `json:"backend-bbolt-freelist-type"`
	QuotaBackendBytes   int64  `json:"quota-backend-bytes"`
	MaxTxnOps           uint   `json:"max-txn-ops"`
	MaxRequestBytes     uint   `json:"max-request-bytes"`

	// MaxConcurrentStreams specifies the maximum number of concurrent
	// streams that each client can open at a time.
	MaxConcurrentStreams uint32 `json:"max-concurrent-streams"`

	//revive:disable:var-naming
	ListenPeerUrls, ListenClientUrls, ListenClientHttpUrls []url.URL
	AdvertisePeerUrls, AdvertiseClientUrls                 []url.URL

	ClientTLSInfo transport.TLSInfo
	ClientAutoTLS bool
	PeerTLSInfo   transport.TLSInfo
	PeerAutoTLS   bool

	// SelfSignedCertValidity specifies the validity period of the client and peer certificates
	// that are automatically generated by etcd when you specify ClientAutoTLS and PeerAutoTLS,
	// the unit is year, and the default is 1
	SelfSignedCertValidity uint `json:"self-signed-cert-validity"`

	// CipherSuites is a list of supported TLS cipher suites between
	// client/server and peers. If empty, Go auto-populates the list.
	// Note that cipher suites are prioritized in the given order.
	CipherSuites []string `json:"cipher-suites"`

	// TlsMinVersion is the minimum accepted TLS version between client/server and peers.
	//revive:disable-next-line:var-naming
	TlsMinVersion string `json:"tls-min-version"`

	// TlsMaxVersion is the maximum accepted TLS version between client/server and peers.
	//revive:disable-next-line:var-naming
	TlsMaxVersion string `json:"tls-max-version"`

	ClusterState          string `json:"initial-cluster-state"`
	DNSCluster            string `json:"discovery-srv"`
	DNSClusterServiceName string `json:"discovery-srv-name"`
	Dproxy                string `json:"discovery-proxy"`

	Durl         string                      `json:"discovery"`
	DiscoveryCfg v3discovery.DiscoveryConfig `json:"discovery-config"`

	InitialCluster      string `json:"initial-cluster"`
	InitialClusterToken string `json:"initial-cluster-token"`
	StrictReconfigCheck bool   `json:"strict-reconfig-check"`

	// AutoCompactionMode is either 'periodic' or 'revision'.
	AutoCompactionMode string `json:"auto-compaction-mode"`
	// AutoCompactionRetention is either duration string with time unit
	// (e.g. '5m' for 5-minute), or revision unit (e.g. '5000').
	// If no time unit is provided and compaction mode is 'periodic',
	// the unit defaults to hour. For example, '5' translates into 5-hour.
	AutoCompactionRetention string `json:"auto-compaction-retention"`

	// GRPCKeepAliveMinTime is the minimum interval that a client should
	// wait before pinging server. When client pings "too fast", server
	// sends goaway and closes the connection (errors: too_many_pings,
	// http2.ErrCodeEnhanceYourCalm). When too slow, nothing happens.
	// Server expects client pings only when there is any active streams
	// (PermitWithoutStream is set false).
	GRPCKeepAliveMinTime time.Duration `json:"grpc-keepalive-min-time"`
	// GRPCKeepAliveInterval is the frequency of server-to-client ping
	// to check if a connection is alive. Close a non-responsive connection
	// after an additional duration of Timeout. 0 to disable.
	GRPCKeepAliveInterval time.Duration `json:"grpc-keepalive-interval"`
	// GRPCKeepAliveTimeout is the additional duration of wait
	// before closing a non-responsive connection. 0 to disable.
	GRPCKeepAliveTimeout time.Duration `json:"grpc-keepalive-timeout"`

	// GRPCAdditionalServerOptions is the additional server option hook
	// for changing the default internal gRPC configuration. Note these
	// additional configurations take precedence over the existing individual
	// configurations if present. Please refer to
	// https://github.com/etcd-io/etcd/pull/14066#issuecomment-1248682996
	GRPCAdditionalServerOptions []grpc.ServerOption `json:"grpc-additional-server-options"`

	// SocketOpts are socket options passed to listener config.
	SocketOpts transport.SocketOpts `json:"socket-options"`

	// PreVote is true to enable Raft Pre-Vote.
	// If enabled, Raft runs an additional election phase
	// to check whether it would get enough votes to win
	// an election, thus minimizing disruptions.
	PreVote bool `json:"pre-vote"`

	CORS map[string]struct{}

	// HostWhitelist lists acceptable hostnames from HTTP client requests.
	// Client origin policy protects against "DNS Rebinding" attacks
	// to insecure etcd servers. That is, any website can simply create
	// an authorized DNS name, and direct DNS to "localhost" (or any
	// other address). Then, all HTTP endpoints of etcd server listening
	// on "localhost" becomes accessible, thus vulnerable to DNS rebinding
	// attacks. See "CVE-2018-5702" for more detail.
	//
	// 1. If client connection is secure via HTTPS, allow any hostnames.
	// 2. If client connection is not secure and "HostWhitelist" is not empty,
	//    only allow HTTP requests whose Host field is listed in whitelist.
	//
	// Note that the client origin policy is enforced whether authentication
	// is enabled or not, for tighter controls.
	//
	// By default, "HostWhitelist" is "*", which allows any hostnames.
	// Note that when specifying hostnames, loopback addresses are not added
	// automatically. To allow loopback interfaces, leave it empty or set it "*",
	// or add them to whitelist manually (e.g. "localhost", "127.0.0.1", etc.).
	//
	// CVE-2018-5702 reference:
	// - https://bugs.chromium.org/p/project-zero/issues/detail?id=1447#c2
	// - https://github.com/transmission/transmission/pull/468
	// - https://github.com/etcd-io/etcd/issues/9353
	HostWhitelist map[string]struct{}

	// UserHandlers is for registering users handlers and only used for
	// embedding etcd into other applications.
	// The map key is the route path for the handler, and
	// you must ensure it can't be conflicted with etcd's.
	UserHandlers map[string]http.Handler `json:"-"`
	// ServiceRegister is for registering users' gRPC services. A simple usage example:
	//	cfg := embed.NewConfig()
	//	cfg.ServiceRegister = func(s *grpc.Server) {
	//		pb.RegisterFooServer(s, &fooServer{})
	//		pb.RegisterBarServer(s, &barServer{})
	//	}
	//	embed.StartEtcd(cfg)
	ServiceRegister func(*grpc.Server) `json:"-"`

	AuthToken  string `json:"auth-token"`
	BcryptCost uint   `json:"bcrypt-cost"`

	// AuthTokenTTL in seconds of the simple token
	AuthTokenTTL uint `json:"auth-token-ttl"`

	// ExperimentalInitialCorruptCheck defines to check data corrution on boot.
	// TODO: delete in v3.7
	// Deprecated: Use InitialCorruptCheck Feature Gate instead. Will be decommissioned in v3.7.
	ExperimentalInitialCorruptCheck bool `json:"experimental-initial-corrupt-check"`
	// ExperimentalCorruptCheckTime is the duration of time between cluster corruption check passes.
	// TODO: delete in v3.7
	// Deprecated: Use CorruptCheckTime instead. Will be decommissioned in v3.7.
	ExperimentalCorruptCheckTime time.Duration `json:"experimental-corrupt-check-time"`
	// CorruptCheckTime is the duration of time between cluster corruption check passes.
	CorruptCheckTime time.Duration `json:"corrupt-check-time"`
	// ExperimentalCompactHashCheckEnabled enables leader to periodically check followers compaction hashes.
	// TODO: delete in v3.7
	// Deprecated: Use CompactHashCheck Feature Gate. Will be decommissioned in v3.7.
	ExperimentalCompactHashCheckEnabled bool `json:"experimental-compact-hash-check-enabled"`
	// ExperimentalCompactHashCheckTime is the duration of time between leader checks followers compaction hashes.
	// TODO: delete in v3.7
	// Deprecated: Use CompactHashCheckTime instead. Will be decommissioned in v3.7.
	ExperimentalCompactHashCheckTime time.Duration `json:"experimental-compact-hash-check-time"`
	// CompactHashCheckTime is the duration of time between leader checks followers compaction hashes.
	CompactHashCheckTime time.Duration `json:"compact-hash-check-time"`

	// ExperimentalEnableLeaseCheckpoint enables leader to send regular checkpoints to other members to prevent reset of remaining TTL on leader change.
	ExperimentalEnableLeaseCheckpoint bool `json:"experimental-enable-lease-checkpoint"`
	// ExperimentalEnableLeaseCheckpointPersist enables persisting remainingTTL to prevent indefinite auto-renewal of long lived leases. Always enabled in v3.6. Should be used to ensure smooth upgrade from v3.5 clusters with this feature enabled.
	// Requires experimental-enable-lease-checkpoint to be enabled.
	// TODO: Delete in v3.7
	// Deprecated: To be decommissioned in v3.7.
	ExperimentalEnableLeaseCheckpointPersist bool `json:"experimental-enable-lease-checkpoint-persist"`
	// ExperimentalCompactionBatchLimit Sets the maximum revisions deleted in each compaction batch.
	// TODO: Delete in v3.7
	// Deprecated: Use CompactionBatchLimit instead. Will be decommissioned in v3.7.
	ExperimentalCompactionBatchLimit int `json:"experimental-compaction-batch-limit"`
	// CompactionBatchLimit Sets the maximum revisions deleted in each compaction batch.
	CompactionBatchLimit int `json:"compaction-batch-limit"`
	// ExperimentalCompactionSleepInterval is the sleep interval between every etcd compaction loop.
	// TODO: Delete in v3.7
	// Deprecated: Use CompactionSleepInterval instead. Will be decommissioned in v3.7.
	ExperimentalCompactionSleepInterval time.Duration `json:"experimental-compaction-sleep-interval"`
	// CompactionSleepInterval is the sleep interval between every etcd compaction loop.
	CompactionSleepInterval time.Duration `json:"compaction-sleep-interval"`
	// ExperimentalWatchProgressNotifyInterval is the time duration of periodic watch progress notifications.
	// TODO: Delete in v3.7
	// Deprecated: Use WatchProgressNotifyInterval instead. Will be decommissioned in v3.7.
	ExperimentalWatchProgressNotifyInterval time.Duration `json:"experimental-watch-progress-notify-interval"`
	// WatchProgressNotifyInterval is the time duration of periodic watch progress notifications.
	WatchProgressNotifyInterval time.Duration `json:"watch-progress-notify-interval"`
	// ExperimentalWarningApplyDuration is the time duration after which a warning is generated if applying request
	// takes more time than this value.
	// TODO: Delete in v3.7
	// Deprecated: Use WarningApplyDuration instead. Will be decommissioned in v3.7.
	ExperimentalWarningApplyDuration time.Duration `json:"experimental-warning-apply-duration"`
	// WarningApplyDuration is the time duration after which a warning is generated if applying request
	WarningApplyDuration time.Duration `json:"warning-apply-duration"`
	// ExperimentalBootstrapDefragThresholdMegabytes is the minimum number of megabytes needed to be freed for etcd server to
	// consider running defrag during bootstrap. Needs to be set to non-zero value to take effect.
	// TODO: Delete in v3.7
	// Deprecated: Use BootstrapDefragThresholdMegabytes instead. Will be decommissioned in v3.7.
	ExperimentalBootstrapDefragThresholdMegabytes uint `json:"experimental-bootstrap-defrag-threshold-megabytes"`
	// BootstrapDefragThresholdMegabytes is the minimum number of megabytes needed to be freed for etcd server to
	BootstrapDefragThresholdMegabytes uint `json:"bootstrap-defrag-threshold-megabytes"`
	// WarningUnaryRequestDuration is the time duration after which a warning is generated if applying
	// unary request takes more time than this value.
	WarningUnaryRequestDuration time.Duration `json:"warning-unary-request-duration"`
	// ExperimentalWarningUnaryRequestDuration is the time duration after which a warning is generated if applying
	// TODO: Delete in v3.7
	// Deprecated: Use WarningUnaryRequestDuration. Will be decommissioned in v3.7.
	ExperimentalWarningUnaryRequestDuration time.Duration `json:"experimental-warning-unary-request-duration"`
	// MaxLearners sets a limit to the number of learner members that can exist in the cluster membership.
	MaxLearners int `json:"max-learners"`

	// ForceNewCluster starts a new cluster even if previously started; unsafe.
	ForceNewCluster bool `json:"force-new-cluster"`

	EnablePprof           bool   `json:"enable-pprof"`
	Metrics               string `json:"metrics"`
	ListenMetricsUrls     []url.URL
	ListenMetricsUrlsJSON string `json:"listen-metrics-urls"`

	// ExperimentalEnableDistributedTracing indicates if experimental tracing using OpenTelemetry is enabled.
	// TODO: delete in v3.7
	// Deprecated: Use EnableDistributedTracing instead. Will be decommissioned in v3.7.
	ExperimentalEnableDistributedTracing bool `json:"experimental-enable-distributed-tracing"`
	// EnableDistributedTracing indicates if tracing using OpenTelemetry is enabled.
	EnableDistributedTracing bool `json:"enable-distributed-tracing"`
	// ExperimentalDistributedTracingAddress is the address of the OpenTelemetry Collector.
	// Can only be set if ExperimentalEnableDistributedTracing is true.
	// TODO: delete in v3.7
	// Deprecated: Use DistributedTracingAddress instead. Will be decommissioned in v3.7.
	ExperimentalDistributedTracingAddress string `json:"experimental-distributed-tracing-address"`
	// DistributedTracingAddress is the address of the OpenTelemetry Collector.
	// Can only be set if EnableDistributedTracing is true.
	DistributedTracingAddress string `json:"distributed-tracing-address"`
	// ExperimentalDistributedTracingServiceName is the name of the service.
	// Can only be used if ExperimentalEnableDistributedTracing is true.
	// TODO: delete in v3.7
	// Deprecated: Use DistributedTracingServiceName instead. Will be decommissioned in v3.7.
	ExperimentalDistributedTracingServiceName string `json:"experimental-distributed-tracing-service-name"`
	// DistributedTracingServiceName is the name of the service.
	// Can only be used if EnableDistributedTracing is true.
	DistributedTracingServiceName string `json:"distributed-tracing-service-name"`
	// ExperimentalDistributedTracingServiceInstanceID is the ID key of the service.
	// This ID must be unique, as helps to distinguish instances of the same service
	// that exist at the same time.
	// Can only be used if ExperimentalEnableDistributedTracing is true.
	// TODO: delete in v3.7
	// Deprecated: Use DistributedTracingServiceInstanceID instead. Will be decommissioned in v3.7.
	ExperimentalDistributedTracingServiceInstanceID string `json:"experimental-distributed-tracing-instance-id"`
	// DistributedTracingServiceInstanceID is the ID key of the service.
	// This ID must be unique, as helps to distinguish instances of the same service
	// that exist at the same time.
	// Can only be used if EnableDistributedTracing is true.
	DistributedTracingServiceInstanceID string `json:"distributed-tracing-instance-id"`
	// ExperimentalDistributedTracingSamplingRatePerMillion is the number of samples to collect per million spans.
	// Defaults to 0.
	// TODO: delete in v3.7
	// Deprecated: Use DistributedTracingSamplingRatePerMillion instead. Will be decommissioned in v3.7.
	ExperimentalDistributedTracingSamplingRatePerMillion int `json:"experimental-distributed-tracing-sampling-rate"`
	// DistributedTracingSamplingRatePerMillion is the number of samples to collect per million spans.
	// Defaults to 0.
	DistributedTracingSamplingRatePerMillion int `json:"distributed-tracing-sampling-rate"`

	// ExperimentalPeerSkipClientSanVerification determines whether to skip verification of SAN field
	// in client certificate for peer connections.
	// TODO: Delete in v3.7
	// Deprecated: Use `peer-skip-client-san-verification` instead. Will be decommissioned in v3.7.
	ExperimentalPeerSkipClientSanVerification bool `json:"experimental-peer-skip-client-san-verification"`

	// Logger is logger options: currently only supports "zap".
	// "capnslog" is removed in v3.5.
	Logger string `json:"logger"`
	// LogLevel configures log level. Only supports debug, info, warn, error, panic, or fatal. Default 'info'.
	LogLevel string `json:"log-level"`
	// LogFormat set log encoding. Only supports json, console. Default is 'json'.
	LogFormat string `json:"log-format"`
	// LogOutputs is either:
	//  - "default" as os.Stderr,
	//  - "stderr" as os.Stderr,
	//  - "stdout" as os.Stdout,
	//  - file path to append server logs to.
	// It can be multiple when "Logger" is zap.
	LogOutputs []string `json:"log-outputs"`
	// EnableLogRotation enables log rotation of a single LogOutputs file target.
	EnableLogRotation bool `json:"enable-log-rotation"`
	// LogRotationConfigJSON is a passthrough allowing a log rotation JSON config to be passed directly.
	LogRotationConfigJSON string `json:"log-rotation-config-json"`
	// ZapLoggerBuilder is used to build the zap logger.
	ZapLoggerBuilder func(*Config) error

	// EnableGRPCGateway enables grpc gateway.
	// The gateway translates a RESTful HTTP API into gRPC.
	EnableGRPCGateway bool `json:"enable-grpc-gateway"`

	// UnsafeNoFsync disables all uses of fsync.
	// Setting this is unsafe and will cause data loss.
	UnsafeNoFsync bool `json:"unsafe-no-fsync"`

	// ExperimentalDowngradeCheckTime is the duration between two downgrade status checks (in seconds).
	// TODO: Delete `ExperimentalDowngradeCheckTime` in v3.7.
	// Deprecated: Use DowngradeCheckTime instead. Will be decommissioned in v3.7.
	ExperimentalDowngradeCheckTime time.Duration `json:"experimental-downgrade-check-time"`
	// DowngradeCheckTime is the duration between two downgrade status checks (in seconds).
	DowngradeCheckTime time.Duration `json:"downgrade-check-time"`

	// MemoryMlock enables mlocking of etcd owned memory pages.
	// The setting improves etcd tail latency in environments were:
	//   - memory pressure might lead to swapping pages to disk
	//   - disk latency might be unstable
	// Currently all etcd memory gets mlocked, but in future the flag can
	// be refined to mlock in-use area of bbolt only.
	MemoryMlock bool `json:"memory-mlock"`

	// ExperimentalMemoryMlock enables mlocking of etcd owned memory pages.
	// TODO: Delete in v3.7
	// Deprecated: Use MemoryMlock instad. To be decommissioned in v3.7.
	ExperimentalMemoryMlock bool `json:"experimental-memory-mlock"`

	// ExperimentalTxnModeWriteWithSharedBuffer enables write transaction to use a shared buffer in its readonly check operations.
	// TODO: Delete in v3.7
	// Deprecated: Use TxnModeWriteWithSharedBuffer Feature Flag. Will be decommissioned in v3.7.
	ExperimentalTxnModeWriteWithSharedBuffer bool `json:"experimental-txn-mode-write-with-shared-buffer"`

	// ExperimentalStopGRPCServiceOnDefrag enables etcd gRPC service to stop serving client requests on defragmentation.
	// TODO: Delete in v3.7
	// Deprecated: Use StopGRPCServiceOnDefrag Feature Flag. Will be decommissioned in v3.7.
	ExperimentalStopGRPCServiceOnDefrag bool `json:"experimental-stop-grpc-service-on-defrag"`

	// V2Deprecation describes phase of API & Storage V2 support.
	// Do not set this field for embedded use cases, as it has no effect. However, setting it will not cause any harm.
	// TODO: Delete in v3.8
	// Deprecated: The default value is enforced, to be removed in v3.8.
	V2Deprecation config.V2DeprecationEnum `json:"v2-deprecation"`

	// ServerFeatureGate is a server level feature gate
	ServerFeatureGate featuregate.FeatureGate
	// FlagsExplicitlySet stores if a flag is explicitly set from the cmd line or config file.
	FlagsExplicitlySet map[string]bool
	// contains filtered or unexported fields
}

Config holds the arguments for configuring an etcd server.

func ConfigFromFile

func ConfigFromFile(path string) (*Config, error)

func NewConfig

func NewConfig() *Config

NewConfig creates a new Config populated with default values.

func (*Config) AddFlags

func (cfg *Config) AddFlags(fs *flag.FlagSet)

func (*Config) ClientSelfCert

func (cfg *Config) ClientSelfCert() (err error)

func (*Config) ElectionTicks

func (cfg *Config) ElectionTicks() int

func (*Config) GetDNSClusterNames

func (cfg *Config) GetDNSClusterNames() ([]string, error)

GetDNSClusterNames uses DNS SRV records to get a list of initial nodes for cluster bootstrapping. This function will return a list of one or more nodes, as well as any errors encountered while performing service discovery. Note: Because this checks multiple sets of SRV records, discovery should only be considered to have failed if the returned node list is empty.

func (*Config) GetLogger

func (cfg *Config) GetLogger() *zap.Logger

GetLogger returns the logger.

func (*Config) InferLocalAddr

func (cfg *Config) InferLocalAddr() string

InferLocalAddr tries to determine the LocalAddr used when communicating with an etcd peer. If SetMemberLocalAddr is true, then it will try to get the host from AdvertisePeerUrls by searching for the first URL with a specified non-loopback address. Otherwise, it defaults to empty string and the LocalAddr used will be the default for the Golang HTTP client.

func (*Config) InitialClusterFromName

func (cfg *Config) InitialClusterFromName(name string) (ret string)

func (*Config) IsNewCluster

func (cfg *Config) IsNewCluster() bool

func (*Config) PeerSelfCert

func (cfg *Config) PeerSelfCert() (err error)

func (*Config) PeerURLsMapAndToken

func (cfg *Config) PeerURLsMapAndToken(which string) (urlsmap types.URLsMap, token string, err error)

PeerURLsMapAndToken sets up an initial peer URLsMap and cluster token for bootstrap or discovery.

func (*Config) SetupGlobalLoggers

func (cfg *Config) SetupGlobalLoggers()

SetupGlobalLoggers configures 'global' loggers (grpc, zapGlobal) based on the cfg.

The method is not executed by embed server by default (since 3.5) to enable setups where grpc/zap.Global logging is configured independently or spans separate lifecycle (like in tests).

func (*Config) UpdateDefaultClusterFromName

func (cfg *Config) UpdateDefaultClusterFromName(defaultInitialCluster string) (string, error)

UpdateDefaultClusterFromName updates cluster advertise URLs with, if available, default host, if advertise URLs are default values(localhost:2379,2380) AND if listen URL is 0.0.0.0. e.g. advertise peer URL localhost:2380 or listen peer URL 0.0.0.0:2380 then the advertise peer host would be updated with machine's default host, while keeping the listen URL's port. User can work around this by explicitly setting URL with 127.0.0.1. It returns the default hostname, if used, and the error, if any, from getting the machine's default host. TODO: check whether fields are set instead of whether fields have default value

func (*Config) V2DeprecationEffective

func (cfg *Config) V2DeprecationEffective() config.V2DeprecationEnum

func (*Config) Validate

func (cfg *Config) Validate() error

Validate ensures that '*embed.Config' fields are properly configured.

type Etcd

type Etcd struct {
	Peers   []*peerListener
	Clients []net.Listener

	Server *etcdserver.EtcdServer
	// contains filtered or unexported fields
}

Etcd contains a running etcd server and its listeners.

func StartEtcd

func StartEtcd(inCfg *Config) (e *Etcd, err error)

StartEtcd launches the etcd server and HTTP handlers for client/server communication. The returned Etcd.Server is not guaranteed to have joined the cluster. Wait on the Etcd.Server.ReadyNotify() channel to know when it completes and is ready for use.

func (*Etcd) Close

func (e *Etcd) Close()

Close gracefully shuts down all servers/listeners. Client requests will be terminated with request timeout. After timeout, enforce remaning requests be closed immediately.

The rough workflow to shut down etcd:

  1. close the `stopc` channel, so that all error handlers (child goroutines) won't send back any errors anymore;
  2. stop the http and grpc servers gracefully, within request timeout;
  3. close all client and metrics listeners, so that etcd server stops receiving any new connection;
  4. call the cancel function to close the gateway context, so that all gateway connections are closed.
  5. stop etcd server gracefully, and ensure the main raft loop goroutine is stopped;
  6. stop all peer listeners, so that it stops receiving peer connections and messages (wait up to 1-second);
  7. wait for all child goroutines (i.e. client handlers, peer handlers and metrics handlers) to exit;
  8. close the `errc` channel to release the resource. Note that it's only safe to close the `errc` after step 7 above is done, otherwise the child goroutines may send errors back to already closed `errc` channel.

func (*Etcd) Config

func (e *Etcd) Config() Config

Config returns the current configuration.

func (*Etcd) Err

func (e *Etcd) Err() <-chan error

Err - return channel used to report errors during etcd run/shutdown. Since etcd 3.5 the channel is being closed when the etcd is over.

func (*Etcd) GetLogger

func (e *Etcd) GetLogger() *zap.Logger

GetLogger returns the logger.

Source Files

config.go config_logging.go config_logging_journal_unix.go config_tracing.go doc.go etcd.go serve.go util.go

Version
v3.6.0 (latest)
Published
May 15, 2025
Platform
js/wasm
Imports
78 packages
Last checked
1 second ago

Tools for package owners.