etcd:如何在 raft/node.go 中定义和使用 Node 接口的新功能

问题描述 投票:0回答:1

我正在尝试定义一个新函数,该函数使用 raft/node.go 的 Node 接口中的键提议一个新条目,并替换原始 Propose 函数。

// raft/Node.go

type Node interface {
    ProposeWithKey(ctx context.Context, data []byte, key []byte) error
    Propose(ctx context.Context, data []byte) error
}

// My new function
func (n *node) ProposeWithKey(ctx context.Context, data []byte, key []byte) error {
    return n.stepWait(ctx, pb.Message{Type: pb.MsgProp, Entries: []pb.Entry{{Data: data, SfKey: key}}})
}

func (n *node) Propose(ctx context.Context, data []byte) error {
    return n.stepWait(ctx, pb.Message{Type: pb.MsgProp, Entries: []pb.Entry{{Data: data}}})
}

但是新的ProposeWithKey()无法调用,出现错误“s.r.ProposeWithKey undefined (type raftNode has no field or method ProposeWithKey)”。

err = s.r.ProposeWithKey(cctx, data, key) // error: s.r.ProposeWithKey undefined (type raftNode has no field or method ProposeWithKey)
err = s.r.Propose(cctx, data)

我很困惑,因为我定义和使用 ProposeWithKey() 完全模仿 Propose(),并且 Propose() 可以工作,但新函数 ProposeWithKey() 不能。

有 go.mod:

module go.etcd.io/etcd/v3

go 1.19

// replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
require (
    github.com/bgentry/speakeasy v0.1.0
    github.com/dustin/go-humanize v1.0.0
    github.com/spf13/cobra v1.1.3
    go.etcd.io/bbolt v1.3.7
    go.etcd.io/etcd/api/v3 v3.5.8
    go.etcd.io/etcd/client/pkg/v3 v3.5.8
    go.etcd.io/etcd/client/v2 v2.305.8
    go.etcd.io/etcd/client/v3 v3.5.8
    go.etcd.io/etcd/etcdctl/v3 v3.5.8
    go.etcd.io/etcd/etcdutl/v3 v3.5.8
    go.etcd.io/etcd/pkg/v3 v3.5.8
    go.etcd.io/etcd/raft/v3 v3.5.8
    go.etcd.io/etcd/server/v3 v3.5.8
    go.etcd.io/etcd/tests/v3 v3.5.8
    go.uber.org/zap v1.17.0
    golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
    google.golang.org/grpc v1.41.0
    gopkg.in/cheggaaa/pb.v1 v1.0.28
)

require (
    github.com/beorn7/perks v1.0.1 // indirect
    github.com/cenkalti/backoff/v4 v4.1.1 // indirect
    github.com/cespare/xxhash/v2 v2.1.1 // indirect
    github.com/coreos/go-semver v0.3.0 // indirect
    github.com/coreos/go-systemd/v22 v22.3.2 // indirect
    github.com/gogo/protobuf v1.3.2 // indirect
    github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
    github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
    github.com/golang/protobuf v1.5.2 // indirect
    github.com/google/btree v1.0.1 // indirect
    github.com/gorilla/websocket v1.4.2 // indirect
    github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
    github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
    github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
    github.com/inconshreveable/mousetrap v1.0.0 // indirect
    github.com/jonboulle/clockwork v0.2.2 // indirect
    github.com/json-iterator/go v1.1.11 // indirect
    github.com/kr/pretty v0.3.0 // indirect
    github.com/mattn/go-colorable v0.1.11 // indirect
    github.com/mattn/go-runewidth v0.0.9 // indirect
    github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
    github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
    github.com/modern-go/reflect2 v1.0.1 // indirect
    github.com/olekukonko/tablewriter v0.0.5 // indirect
    github.com/prometheus/client_golang v1.11.1 // indirect
    github.com/prometheus/client_model v0.2.0 // indirect
    github.com/prometheus/common v0.26.0 // indirect
    github.com/prometheus/procfs v0.6.0 // indirect
    github.com/rogpeppe/go-internal v1.8.1 // indirect
    github.com/sirupsen/logrus v1.7.0 // indirect
    github.com/soheilhy/cmux v0.1.5 // indirect
    github.com/spf13/pflag v1.0.5 // indirect
    github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect
    github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
    go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.25.0 // indirect
    go.opentelemetry.io/otel v1.0.1 // indirect
    go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.0.1 // indirect
    go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.0.1 // indirect
    go.opentelemetry.io/otel/sdk v1.0.1 // indirect
    go.opentelemetry.io/otel/trace v1.0.1 // indirect
    go.opentelemetry.io/proto/otlp v0.9.0 // indirect
    go.uber.org/atomic v1.7.0 // indirect
    go.uber.org/multierr v1.6.0 // indirect
    golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
    golang.org/x/net v0.7.0 // indirect
    golang.org/x/sys v0.5.0 // indirect
    golang.org/x/text v0.7.0 // indirect
    google.golang.org/genproto v0.0.0-20210624195500-8bfb893ecb84 // indirect
    google.golang.org/protobuf v1.27.1 // indirect
    gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
    gopkg.in/yaml.v2 v2.4.0 // indirect
    sigs.k8s.io/yaml v1.2.0 // indirect
)

replace (
    go.etcd.io/etcd/api/v3 => ./api
    go.etcd.io/etcd/client/pkg/v3 => ./client/pkg
    go.etcd.io/etcd/client/v2 => ./client/v2
    go.etcd.io/etcd/client/v3 => ./client/v3
    go.etcd.io/etcd/etcdctl/v3 => ./etcdctl
    go.etcd.io/etcd/etcdutl/v3 => ./etcdutl
    go.etcd.io/etcd/pkg/v3 => ./pkg
    go.etcd.io/etcd/raft/v3 => ./raft
    go.etcd.io/etcd/server/v3 => ./server
    go.etcd.io/etcd/tests/v3 => ./tests
)

go list -m all

的结果
go.etcd.io/etcd/v3
cloud.google.com/go v0.46.3
cloud.google.com/go/bigquery v1.0.1
cloud.google.com/go/datastore v1.0.0
cloud.google.com/go/firestore v1.1.0
cloud.google.com/go/pubsub v1.0.1
cloud.google.com/go/storage v1.0.0
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9
github.com/BurntSushi/toml v0.3.1
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802
github.com/OneOfOne/xxhash v1.2.2
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d
github.com/antihax/optional v1.0.0
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310
github.com/beorn7/perks v1.0.1
github.com/bgentry/speakeasy v0.1.0
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c
github.com/cenkalti/backoff/v4 v4.1.1
github.com/census-instrumentation/opencensus-proto v0.2.1
github.com/cespare/xxhash v1.1.0
github.com/cespare/xxhash/v2 v2.1.1
github.com/client9/misspell v0.3.4
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403
github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158
github.com/cockroachdb/datadriven v1.0.2
github.com/coreos/bbolt v1.3.2
github.com/coreos/etcd v3.3.13+incompatible
github.com/coreos/go-semver v0.3.0
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e
github.com/coreos/go-systemd/v22 v22.3.2
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f
github.com/cpuguy83/go-md2man/v2 v2.0.0
github.com/creack/pty v1.1.11
github.com/davecgh/go-spew v1.1.1
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954
github.com/dustin/go-humanize v1.0.0
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021
github.com/envoyproxy/protoc-gen-validate v0.1.0
github.com/etcd-io/gofail v0.0.0-20190801230047-ad7f989257ca
github.com/fatih/color v1.7.0
github.com/fsnotify/fsnotify v1.4.7
github.com/ghodss/yaml v1.0.0
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1
github.com/go-kit/kit v0.9.0
github.com/go-kit/log v0.1.0
github.com/go-logfmt/logfmt v0.5.0
github.com/go-stack/stack v1.8.0
github.com/godbus/dbus/v5 v5.0.4
github.com/gogo/protobuf v1.3.2
github.com/golang-jwt/jwt/v4 v4.4.2
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
github.com/golang/mock v1.3.1
github.com/golang/protobuf v1.5.2
github.com/google/btree v1.0.1
github.com/google/go-cmp v0.5.6
github.com/google/gofuzz v1.0.0
github.com/google/martian v2.1.0+incompatible
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f
github.com/google/renameio v0.1.0
github.com/google/uuid v1.1.2
github.com/googleapis/gax-go/v2 v2.0.5
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1
github.com/gorilla/websocket v1.4.2
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/hashicorp/consul/api v1.1.0
github.com/hashicorp/consul/sdk v0.1.1
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-cleanhttp v0.5.1
github.com/hashicorp/go-immutable-radix v1.0.0
github.com/hashicorp/go-msgpack v0.5.3
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/go-rootcerts v1.0.0
github.com/hashicorp/go-sockaddr v1.0.0
github.com/hashicorp/go-syslog v1.0.0
github.com/hashicorp/go-uuid v1.0.1
github.com/hashicorp/go.net v0.0.1
github.com/hashicorp/golang-lru v0.5.1
github.com/hashicorp/hcl v1.0.0
github.com/hashicorp/logutils v1.0.0
github.com/hashicorp/mdns v1.0.0
github.com/hashicorp/memberlist v0.1.3
github.com/hashicorp/serf v0.8.2
github.com/inconshreveable/mousetrap v1.0.0
github.com/jonboulle/clockwork v0.2.2
github.com/jpillora/backoff v1.0.0
github.com/json-iterator/go v1.1.11
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024
github.com/jtolds/gls v4.20.0+incompatible
github.com/julienschmidt/httprouter v1.3.0
github.com/kisielk/errcheck v1.5.0
github.com/kisielk/gotool v1.0.0
github.com/konsorten/go-windows-terminal-sequences v1.0.3
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515
github.com/kr/pretty v0.3.0
github.com/kr/pty v1.1.1
github.com/kr/text v0.2.0
github.com/magiconair/properties v1.8.1
github.com/mattn/go-colorable v0.1.11
github.com/mattn/go-isatty v0.0.14
github.com/mattn/go-runewidth v0.0.9
github.com/matttproud/golang_protobuf_extensions v1.0.1
github.com/miekg/dns v1.0.14
github.com/mitchellh/cli v1.0.0
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/go-testing-interface v1.0.0
github.com/mitchellh/gox v0.4.0
github.com/mitchellh/iochan v1.0.0
github.com/mitchellh/mapstructure v1.1.2
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
github.com/modern-go/reflect2 v1.0.1
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f
github.com/oklog/ulid v1.3.1
github.com/olekukonko/tablewriter v0.0.5
github.com/opentracing/opentracing-go v1.1.0
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c
github.com/pelletier/go-toml v1.2.0
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0
github.com/posener/complete v1.1.1
github.com/prometheus/client_golang v1.11.1
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.26.0
github.com/prometheus/procfs v0.6.0
github.com/prometheus/tsdb v0.7.1
github.com/rogpeppe/fastuuid v1.2.0
github.com/rogpeppe/go-internal v1.8.1
github.com/russross/blackfriday/v2 v2.0.1
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529
github.com/shurcooL/sanitized_anchor_name v1.0.0
github.com/sirupsen/logrus v1.7.0
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d
github.com/smartystreets/goconvey v1.6.4
github.com/soheilhy/cmux v0.1.5
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72
github.com/spf13/afero v1.1.2
github.com/spf13/cast v1.3.0
github.com/spf13/cobra v1.1.3
github.com/spf13/jwalterweatherman v1.0.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.0
github.com/stretchr/objx v0.1.1
github.com/stretchr/testify v1.8.1
github.com/subosito/gotenv v1.2.0
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802
github.com/urfave/cli v1.22.4
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2
github.com/yuin/goldmark v1.3.5
go.etcd.io/bbolt v1.3.7
go.etcd.io/etcd/api/v3 v3.5.8 => ./api
go.etcd.io/etcd/client/pkg/v3 v3.5.8 => ./client/pkg
go.etcd.io/etcd/client/v2 v2.305.8 => ./client/v2
go.etcd.io/etcd/client/v3 v3.5.8 => ./client/v3
go.etcd.io/etcd/etcdctl/v3 v3.5.8 => ./etcdctl
go.etcd.io/etcd/etcdutl/v3 v3.5.8 => ./etcdutl
go.etcd.io/etcd/pkg/v3 v3.5.8 => ./pkg
go.etcd.io/etcd/raft/v3 v3.5.8 => ./raft
go.etcd.io/etcd/server/v3 v3.5.8 => ./server
go.etcd.io/etcd/tests/v3 v3.5.8 => ./tests
go.etcd.io/gofail v0.1.0
go.opencensus.io v0.22.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.25.0
go.opentelemetry.io/otel v1.0.1
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.0.1
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.0.1
go.opentelemetry.io/otel/sdk v1.0.1
go.opentelemetry.io/otel/trace v1.0.1
go.opentelemetry.io/proto/otlp v0.9.0
go.uber.org/atomic v1.7.0
go.uber.org/multierr v1.6.0
go.uber.org/zap v1.17.0
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4
golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136
golang.org/x/image v0.0.0-20190802002840-cff245a6509b
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4
golang.org/x/net v0.7.0
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
golang.org/x/sys v0.5.0
golang.org/x/term v0.5.0
golang.org/x/text v0.7.0
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
golang.org/x/tools v0.1.12
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
google.golang.org/api v0.13.0
google.golang.org/appengine v1.6.1
google.golang.org/genproto v0.0.0-20210624195500-8bfb893ecb84
google.golang.org/grpc v1.41.0
google.golang.org/protobuf v1.27.1
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15
gopkg.in/cheggaaa/pb.v1 v1.0.28
gopkg.in/errgo.v2 v2.1.0
gopkg.in/ini.v1 v1.51.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gopkg.in/resty.v1 v1.12.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
honnef.co/go/tools v0.0.1-2019.2.3
rsc.io/binaryregexp v0.2.0
sigs.k8s.io/yaml v1.2.0

如何解决这个问题以及为什么

go etcd raft
1个回答
0
投票

问题解决了。 原因是新函数的第一个字母没有大写。

© www.soinside.com 2019 - 2024. All rights reserved.