diff --git a/.gitignore b/.gitignore index 05d1833..78f2ea6 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ .idea/**/usage.statistics.xml .idea/**/dictionaries .idea/**/shelf +.idea/* # AWS User-specific .idea/**/aws.xml @@ -115,3 +116,8 @@ fabric.properties .idea/**/azureSettings.xml # End of https://www.toptal.com/developers/gitignore/api/goland + +raft_data +./raft_data/* +./raft_data +/raft_data \ No newline at end of file diff --git a/.idea/gCache.iml b/.idea/gCache.iml new file mode 100644 index 0000000..7ee078d --- /dev/null +++ b/.idea/gCache.iml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..53461f8 --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +# Go parameters +GOCMD := go +GOBUILD := $(GOCMD) build +GOTEST := $(GOCMD) test +GORUN := $(GOCMD) run +GOBIN := $(shell pwd)/bin +LeaderPort := ":3000" +BINARY_NAME := gCache +RandomPort := $(shell shuf -i 5000-9999 -n 1) + +.PHONY: all build run run-follower test clean + +all: build + +build: + $(GOBUILD) -o "$(GOBIN)/$(BINARY_NAME)" + +run: + $(GOBIN)/$(BINARY_NAME) --listenaddr $(LeaderPort) + +frun: + $(GOBIN)/$(BINARY_NAME) --leaderaddr $(LeaderPort) --listenaddr :$(RandomPort) + +crun: + go run client/main.go + +lb: + haproxy -f haproxy.cfg + +test: + $(GOTEST) + +clean: + rm -rf $(GOBIN)/$(BINARY_NAME) \ No newline at end of file diff --git a/client/cmd/root.go b/client/cmd/root.go index a0b5b00..675e3f6 100644 --- a/client/cmd/root.go +++ b/client/cmd/root.go @@ -4,7 +4,7 @@ import ( "bufio" "fmt" "github.com/olekukonko/tablewriter" - "github.com/ragoob/gCache/pkg/client" + "github.com/ragoob/gCache/pkg/grpc/client" "github.com/spf13/cobra" "os" "strings" @@ -33,7 +33,7 @@ var rootCmd = &cobra.Command{ } func Execute() { - rootCmd.Flags().StringVarP(&serverAddr, "serveraddr", "s", ":3000", "Server address") + rootCmd.Flags().StringVarP(&serverAddr, "serveraddr", "s", ":8080", "Server address") defer gClient.Close() if err := rootCmd.Execute(); err != nil { @@ -43,7 +43,6 @@ func Execute() { } func runRoot(cmd *cobra.Command, args []string) { - scanner := bufio.NewScanner(os.Stdin) welcomePrint() diff --git a/client/main.go b/client/main.go index 0bf0134..fdbe7f3 100644 --- a/client/main.go +++ b/client/main.go @@ -6,4 +6,5 @@ import ( func main() { cmd.Execute() + } diff --git a/cmd/command.go b/cmd/command.go index 2df6ddf..37f1406 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -109,7 +109,7 @@ func (c *SetCmd) GetBytes() []byte { binary.Write(buf, binary.LittleEndian, int32(len(c.Val))) binary.Write(buf, binary.LittleEndian, c.Val) binary.Write(buf, binary.LittleEndian, int32(c.Duration)) - binary.Write(buf, binary.LittleEndian, c.Replication) + binary.Write(buf, binary.LittleEndian, c.Duration) return buf.Bytes() } @@ -193,3 +193,16 @@ func parseJoinCommand(r io.Reader) *JoinCmd { binary.Read(r, binary.LittleEndian, &cmd.Addr) return cmd } + +func ParseSetCommand(r io.Reader) (*SetCmd, error) { + var cmd Command + if err := binary.Read(r, binary.LittleEndian, &cmd); err != nil { + return nil, err + } + switch cmd { + case Set: + return parseSetCommand(r), nil + default: + return nil, fmt.Errorf("invalid command") + } +} diff --git a/db/db.go b/db/db.go index 8578b92..d640910 100644 --- a/db/db.go +++ b/db/db.go @@ -9,6 +9,8 @@ import ( type DB interface { Set([]byte, []byte, time.Duration) error Get([]byte) ([]byte, error) + Store() map[string][]byte + Clear() } type Cache struct { @@ -43,3 +45,15 @@ func (c *Cache) Set(key []byte, val []byte, duration time.Duration) error { return nil } + +func (c *Cache) Store() map[string][]byte { + return c.store +} + +func (c *Cache) Clear() { + c.lock.Lock() + defer c.lock.Unlock() + + // Clear the existing store + c.store = make(map[string][]byte) +} diff --git a/go.mod b/go.mod index 1e3cc59..e77caed 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,35 @@ module github.com/ragoob/gCache go 1.20 require ( + github.com/Jille/raft-grpc-leader-rpc v1.1.0 + github.com/Jille/raft-grpc-transport v1.4.0 + github.com/Jille/raftadmin v1.2.0 + github.com/google/uuid v1.3.0 + github.com/hashicorp/raft v1.5.0 github.com/olekukonko/tablewriter v0.0.5 github.com/spf13/cobra v1.7.0 + golang.org/x/net v0.9.0 + google.golang.org/grpc v1.56.2 + google.golang.org/protobuf v1.31.0 ) require ( + github.com/armon/go-metrics v0.4.1 // indirect + github.com/fatih/color v1.13.0 // indirect + github.com/golang/protobuf v1.5.3 // indirect + github.com/hashicorp/errwrap v1.0.0 // indirect + github.com/hashicorp/go-hclog v1.5.0 // indirect + github.com/hashicorp/go-immutable-radix v1.3.1 // indirect + github.com/hashicorp/go-msgpack v1.1.5 // indirect + github.com/hashicorp/go-multierror v1.1.1 // indirect + github.com/hashicorp/golang-lru v0.5.4 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/mattn/go-colorable v0.1.12 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-runewidth v0.0.9 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/sys v0.7.0 // indirect + golang.org/x/text v0.9.0 // indirect + google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect ) diff --git a/go.sum b/go.sum index a26f94e..851f726 100644 --- a/go.sum +++ b/go.sum @@ -1,14 +1,320 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/DataDog/datadog-go v2.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= +github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= +github.com/Jille/raft-grpc-leader-rpc v1.1.0 h1:u36rmA4tjp+4FSdZ17jg/1sfSCYNQIe5bzzwvW0iVTM= +github.com/Jille/raft-grpc-leader-rpc v1.1.0/go.mod h1:l+pK+uPuqpFDFcPmyUPSng4257UXrST0Vc3Lo4XwVB0= +github.com/Jille/raft-grpc-transport v1.4.0 h1:Kwk+IceQD8MpLKOulBu2ignX+aZAEjOhffEhN44sdzQ= +github.com/Jille/raft-grpc-transport v1.4.0/go.mod h1:afVUd8LQKUUo3V/ToLBH3mbSyvivRlMYCDK0eJRGTfQ= +github.com/Jille/raftadmin v1.2.0 h1:hMLFUK7iKpeXP+CoIhNMWj+F53XOLSjMDSia0C60cps= +github.com/Jille/raftadmin v1.2.0/go.mod h1:vtVEpToPGTUPVwwunypWDpi69JpdnHMhWRUlc/65U+Y= +github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= +github.com/armon/go-metrics v0.0.0-20190430140413-ec5e00d3c878/go.mod h1:3AMJUQhVx52RsWOnlkpikZr01T/yAVN2gn0861vByNg= +github.com/armon/go-metrics v0.3.9/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc= +github.com/armon/go-metrics v0.4.1 h1:hR91U9KYmb6bLBYLQjyM+3j+rcd/UhE+G78SFnF8gJA= +github.com/armon/go-metrics v0.4.1/go.mod h1:E6amYzXo6aW1tqzoZGT755KkbgrJsSdpwZ+3JqfkOG4= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag= +github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= +github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= +github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= +github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= +github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= +github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= +github.com/hashicorp/go-hclog v0.9.1/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= +github.com/hashicorp/go-hclog v0.16.2/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= +github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+13c= +github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= +github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc= +github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-msgpack v0.5.5/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= +github.com/hashicorp/go-msgpack v1.1.5 h1:9byZdVjKTe5mce63pRVNP1L7UAmdHOTEMGehn6KvJWs= +github.com/hashicorp/go-msgpack v1.1.5/go.mod h1:gWVc3sv/wbDmR3rQsj1CAktEZzoz1YNK9NfGLXJ69/4= +github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= +github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= +github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs= +github.com/hashicorp/go-uuid v1.0.0 h1:RS8zrF7PhGwyNPOtxSClXXj9HA8feRnJzgnI1RJCSnM= +github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= +github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/raft v1.1.2/go.mod h1:vPAJM8Asw6u8LxC3eJCUZmRP/E4QmUGE1R7g7k8sG/8= +github.com/hashicorp/raft v1.3.7/go.mod h1:4Ak7FSPnuvmb0GV6vgIAJ4vYT4bek9bb6Q+7HVbyzqM= +github.com/hashicorp/raft v1.5.0 h1:uNs9EfJ4FwiArZRxxfd/dQ5d33nV31/CdCHArH89hT8= +github.com/hashicorp/raft v1.5.0/go.mod h1:pKHB2mf/Y25u3AHNSXVRv+yT+WAnmeTX0BwVppVQV+M= +github.com/hashicorp/raft-boltdb v0.0.0-20171010151810-6e5ba93211ea/go.mod h1:pNv7Wc3ycL6F5oOWn+tPGo2gWD4a5X+yp/ntwdKLjRk= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= +github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= +github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= +github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM= +github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= +github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= +github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A= +github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= +go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= +go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.0.0-20210907225631-ff17edfbf26d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= +golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190523142557-0e01d883c5c5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190424220101-1e8e1cfdf96b/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/genproto v0.0.0-20210903162649-d08c68adba83/go.mod h1:eFjDcFEctNawg4eG61bRv87N7iHBWyVhJu7u1kqDUXY= +google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 h1:KpwkzHKEF7B9Zxg18WzOa7djJ+Ha5DzthMyZYQfEn2A= +google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.31.1/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= +google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= +google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= +google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= +google.golang.org/grpc v1.56.2 h1:fVRFRnXvU+x6C4IlHZewvJOVHoOv1TUuQyoRsYnB4bI= +google.golang.org/grpc v1.56.2/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/haproxy.cfg b/haproxy.cfg new file mode 100644 index 0000000..c0c892a --- /dev/null +++ b/haproxy.cfg @@ -0,0 +1,34 @@ +global + log 127.0.0.1 local0 + log 127.0.0.1 local1 debug + #log loghost local0 info + maxconn 4096 + #chroot /usr/share/haproxy + #daemon + #debug + #quiet + + defaults + log global + mode http + option httplog + option dontlognull + retries 3 + option redispatch + maxconn 2000 + timeout connect 5000 + timeout client 50000 + timeout server 50000 + +frontend grpc_frontend + bind *:8080 + mode tcp + default_backend grpc_servers + +backend grpc_servers + mode tcp + balance roundrobin + option tcp-check + server leader 127.0.0.1:3000 check + server follower1 127.0.0.1:5001 check + server follower2 127.0.0.1:5002 check diff --git a/main.go b/main.go index c161f9a..1bd5d81 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "flag" + "log" "github.com/ragoob/gCache/db" "github.com/ragoob/gCache/server" @@ -20,6 +21,9 @@ func main() { } s := server.NewServer(opts, db.New()) - s.Serve() + + if err := s.Serve(); err != nil { + log.Fatalf("Serve Error : %+v", err) + } } diff --git a/pkg/grpc/client/client.go b/pkg/grpc/client/client.go new file mode 100644 index 0000000..f6e66dc --- /dev/null +++ b/pkg/grpc/client/client.go @@ -0,0 +1,57 @@ +package client + +import ( + "context" + "fmt" + pb "github.com/ragoob/gCache/proto" + "google.golang.org/grpc" +) + +type Options struct { +} +type Client struct { + Grpc pb.GCacheServiceClient + Options + conn *grpc.ClientConn + IsReplicator bool +} + +func Connect(host string, opts Options) (*Client, error) { + conn, err := grpc.Dial(host, grpc.WithInsecure()) + if err != nil { + return nil, err + } + client := pb.NewGCacheServiceClient(conn) + + return &Client{ + Grpc: client, + Options: opts, + conn: conn, + }, nil +} + +func (c *Client) Get(ctx context.Context, key []byte) ([]byte, error) { + fmt.Printf("get key [%s] \n", string(key)) + + res, err := c.Grpc.Get(ctx, &pb.GetRequest{Key: key}) + + if err != nil { + return nil, err + } + return res.Value, nil +} + +func (c *Client) Set(ctx context.Context, key []byte, val []byte, duration int) error { + fmt.Printf("Set key [%s] \n", string(key)) + + _, err := c.Grpc.Set(ctx, &pb.SetRequest{Key: key, Value: val}) + + if err != nil { + return err + } + return nil +} + +func (c *Client) Close() error { + return c.conn.Close() +} diff --git a/proto/Makefile b/proto/Makefile new file mode 100755 index 0000000..3da2100 --- /dev/null +++ b/proto/Makefile @@ -0,0 +1,6 @@ +service.pb.go: + protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative service.proto + +force: + rm -f service.pb.go + make service.pb.gofo \ No newline at end of file diff --git a/proto/service.pb.go b/proto/service.pb.go new file mode 100644 index 0000000..c86cb1d --- /dev/null +++ b/proto/service.pb.go @@ -0,0 +1,350 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc v3.21.12 +// source: service.proto + +package proto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type SetRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` + Value []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` +} + +func (x *SetRequest) Reset() { + *x = SetRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_service_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SetRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SetRequest) ProtoMessage() {} + +func (x *SetRequest) ProtoReflect() protoreflect.Message { + mi := &file_service_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use SetRequest.ProtoReflect.Descriptor instead. +func (*SetRequest) Descriptor() ([]byte, []int) { + return file_service_proto_rawDescGZIP(), []int{0} +} + +func (x *SetRequest) GetKey() []byte { + if x != nil { + return x.Key + } + return nil +} + +func (x *SetRequest) GetValue() []byte { + if x != nil { + return x.Value + } + return nil +} + +type SetResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"` +} + +func (x *SetResponse) Reset() { + *x = SetResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_service_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SetResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SetResponse) ProtoMessage() {} + +func (x *SetResponse) ProtoReflect() protoreflect.Message { + mi := &file_service_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use SetResponse.ProtoReflect.Descriptor instead. +func (*SetResponse) Descriptor() ([]byte, []int) { + return file_service_proto_rawDescGZIP(), []int{1} +} + +func (x *SetResponse) GetSuccess() bool { + if x != nil { + return x.Success + } + return false +} + +type GetRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` +} + +func (x *GetRequest) Reset() { + *x = GetRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_service_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetRequest) ProtoMessage() {} + +func (x *GetRequest) ProtoReflect() protoreflect.Message { + mi := &file_service_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GetRequest.ProtoReflect.Descriptor instead. +func (*GetRequest) Descriptor() ([]byte, []int) { + return file_service_proto_rawDescGZIP(), []int{2} +} + +func (x *GetRequest) GetKey() []byte { + if x != nil { + return x.Key + } + return nil +} + +type GetResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Value []byte `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` +} + +func (x *GetResponse) Reset() { + *x = GetResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_service_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetResponse) ProtoMessage() {} + +func (x *GetResponse) ProtoReflect() protoreflect.Message { + mi := &file_service_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GetResponse.ProtoReflect.Descriptor instead. +func (*GetResponse) Descriptor() ([]byte, []int) { + return file_service_proto_rawDescGZIP(), []int{3} +} + +func (x *GetResponse) GetValue() []byte { + if x != nil { + return x.Value + } + return nil +} + +var File_service_proto protoreflect.FileDescriptor + +var file_service_proto_rawDesc = []byte{ + 0x0a, 0x0d, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, + 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x34, 0x0a, 0x0a, 0x53, 0x65, 0x74, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x0c, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x27, 0x0a, 0x0b, + 0x53, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, + 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x73, 0x75, + 0x63, 0x63, 0x65, 0x73, 0x73, 0x22, 0x1e, 0x0a, 0x0a, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, + 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x23, 0x0a, 0x0b, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x32, 0x6f, 0x0a, 0x0d, 0x47, 0x43, + 0x61, 0x63, 0x68, 0x65, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x2e, 0x0a, 0x03, 0x53, + 0x65, 0x74, 0x12, 0x11, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x65, 0x74, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x65, + 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x2e, 0x0a, 0x03, 0x47, + 0x65, 0x74, 0x12, 0x11, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x65, + 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x21, 0x5a, 0x1f, 0x20, + 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x72, 0x61, 0x67, 0x6f, 0x6f, + 0x62, 0x2f, 0x67, 0x43, 0x61, 0x63, 0x68, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_service_proto_rawDescOnce sync.Once + file_service_proto_rawDescData = file_service_proto_rawDesc +) + +func file_service_proto_rawDescGZIP() []byte { + file_service_proto_rawDescOnce.Do(func() { + file_service_proto_rawDescData = protoimpl.X.CompressGZIP(file_service_proto_rawDescData) + }) + return file_service_proto_rawDescData +} + +var file_service_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_service_proto_goTypes = []interface{}{ + (*SetRequest)(nil), // 0: proto.SetRequest + (*SetResponse)(nil), // 1: proto.SetResponse + (*GetRequest)(nil), // 2: proto.GetRequest + (*GetResponse)(nil), // 3: proto.GetResponse +} +var file_service_proto_depIdxs = []int32{ + 0, // 0: proto.GCacheService.Set:input_type -> proto.SetRequest + 2, // 1: proto.GCacheService.Get:input_type -> proto.GetRequest + 1, // 2: proto.GCacheService.Set:output_type -> proto.SetResponse + 3, // 3: proto.GCacheService.Get:output_type -> proto.GetResponse + 2, // [2:4] is the sub-list for method output_type + 0, // [0:2] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_service_proto_init() } +func file_service_proto_init() { + if File_service_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_service_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SetRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_service_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SetResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_service_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_service_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_service_proto_rawDesc, + NumEnums: 0, + NumMessages: 4, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_service_proto_goTypes, + DependencyIndexes: file_service_proto_depIdxs, + MessageInfos: file_service_proto_msgTypes, + }.Build() + File_service_proto = out.File + file_service_proto_rawDesc = nil + file_service_proto_goTypes = nil + file_service_proto_depIdxs = nil +} diff --git a/proto/service.proto b/proto/service.proto new file mode 100644 index 0000000..48f250f --- /dev/null +++ b/proto/service.proto @@ -0,0 +1,25 @@ +syntax = "proto3"; +package proto; +option go_package = " github.com/ragoob/gCache/proto"; + +service GCacheService { + rpc Set(SetRequest) returns (SetResponse) {} + rpc Get(GetRequest) returns (GetResponse) {} +} + +message SetRequest { + bytes key = 1; + bytes value = 2; +} + +message SetResponse { + bool success = 1; +} + +message GetRequest { + bytes key = 1; +} + +message GetResponse { + bytes value = 1; +} diff --git a/proto/service_grpc.pb.go b/proto/service_grpc.pb.go new file mode 100644 index 0000000..4a26b78 --- /dev/null +++ b/proto/service_grpc.pb.go @@ -0,0 +1,141 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v3.21.12 +// source: service.proto + +package proto + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// GCacheServiceClient is the client API for GCacheService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type GCacheServiceClient interface { + Set(ctx context.Context, in *SetRequest, opts ...grpc.CallOption) (*SetResponse, error) + Get(ctx context.Context, in *GetRequest, opts ...grpc.CallOption) (*GetResponse, error) +} + +type gCacheServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewGCacheServiceClient(cc grpc.ClientConnInterface) GCacheServiceClient { + return &gCacheServiceClient{cc} +} + +func (c *gCacheServiceClient) Set(ctx context.Context, in *SetRequest, opts ...grpc.CallOption) (*SetResponse, error) { + out := new(SetResponse) + err := c.cc.Invoke(ctx, "/proto.GCacheService/Set", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *gCacheServiceClient) Get(ctx context.Context, in *GetRequest, opts ...grpc.CallOption) (*GetResponse, error) { + out := new(GetResponse) + err := c.cc.Invoke(ctx, "/proto.GCacheService/Get", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// GCacheServiceServer is the server API for GCacheService service. +// All implementations must embed UnimplementedGCacheServiceServer +// for forward compatibility +type GCacheServiceServer interface { + Set(context.Context, *SetRequest) (*SetResponse, error) + Get(context.Context, *GetRequest) (*GetResponse, error) + mustEmbedUnimplementedGCacheServiceServer() +} + +// UnimplementedGCacheServiceServer must be embedded to have forward compatible implementations. +type UnimplementedGCacheServiceServer struct { +} + +func (UnimplementedGCacheServiceServer) Set(context.Context, *SetRequest) (*SetResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Set not implemented") +} +func (UnimplementedGCacheServiceServer) Get(context.Context, *GetRequest) (*GetResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Get not implemented") +} +func (UnimplementedGCacheServiceServer) mustEmbedUnimplementedGCacheServiceServer() {} + +// UnsafeGCacheServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to GCacheServiceServer will +// result in compilation errors. +type UnsafeGCacheServiceServer interface { + mustEmbedUnimplementedGCacheServiceServer() +} + +func RegisterGCacheServiceServer(s grpc.ServiceRegistrar, srv GCacheServiceServer) { + s.RegisterService(&GCacheService_ServiceDesc, srv) +} + +func _GCacheService_Set_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SetRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(GCacheServiceServer).Set(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/proto.GCacheService/Set", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(GCacheServiceServer).Set(ctx, req.(*SetRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _GCacheService_Get_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(GCacheServiceServer).Get(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/proto.GCacheService/Get", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(GCacheServiceServer).Get(ctx, req.(*GetRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// GCacheService_ServiceDesc is the grpc.ServiceDesc for GCacheService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var GCacheService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "proto.GCacheService", + HandlerType: (*GCacheServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Set", + Handler: _GCacheService_Set_Handler, + }, + { + MethodName: "Get", + Handler: _GCacheService_Get_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "service.proto", +} diff --git a/server/fsm.go b/server/fsm.go new file mode 100644 index 0000000..9ae89d6 --- /dev/null +++ b/server/fsm.go @@ -0,0 +1,141 @@ +package server + +import ( + "bytes" + "encoding/gob" + "fmt" + "github.com/hashicorp/raft" + "github.com/ragoob/gCache/cmd" + "io" + "log" + "time" +) + +// Apply applies a Raft log entry to the dummy FSM +func (s *Server) Apply(l *raft.Log) interface{} { + + // Extract the command data from the log entry + reader := bytes.NewReader(l.Data) + // Deserialize the command + command, err := cmd.ParseSetCommand(reader) + if err != nil { + return err + } + + if err := s.db.Set(command.Key, command.Val, time.Duration(command.Duration)); err != nil { + return err + } + + return nil +} + +// SetCmd represents a set command for a key-value pair +type SetCmd struct { + Key []byte + Val []byte + Replication bool + Duration int +} + +// ServerSnapshot represents a snapshot of the cache server's state +type ServerSnapshot struct { + Commands []SetCmd +} + +// Snapshot writes a snapshot of the cache server's state +func (s *Server) Snapshot() (raft.FSMSnapshot, error) { + // Create a new snapshot + snapshot := &ServerSnapshot{} + + // Acquire a read lock on the cache to ensure consistent snapshot + s.mtx.Lock() + defer s.mtx.Unlock() + + // Iterate over the cache and collect the set commands + for key, val := range s.db.Store() { + cmd := SetCmd{ + Key: []byte(key), + Val: val, + Replication: false, // Set the replication flag as needed + Duration: 0, // Set the duration as needed + } + snapshot.Commands = append(snapshot.Commands, cmd) + } + + log.Println("Snapshot created successfully") + + return snapshot, nil +} + +// Persist persists the snapshot data +func (s *ServerSnapshot) Persist(sink raft.SnapshotSink) error { + // Serialize the snapshot to a buffer + var buf bytes.Buffer + encoder := gob.NewEncoder(&buf) + err := encoder.Encode(s) + if err != nil { + sink.Cancel() + return fmt.Errorf("failed to encode snapshot: %v", err) + } + + // Write the serialized snapshot data to the sink + _, err = sink.Write(buf.Bytes()) + if err != nil { + sink.Cancel() + return fmt.Errorf("failed to write snapshot: %v", err) + } + + // Close the sink to finalize the snapshot + err = sink.Close() + if err != nil { + return fmt.Errorf("failed to close snapshot sink: %v", err) + } + + log.Println("Snapshot persisted successfully") + + return nil +} + +// Release releases any resources associated with the snapshot +func (s *ServerSnapshot) Release() { + // Perform any necessary cleanup or release of resources +} + +// Restore restores the cache server's state from a snapshot +func (s *Server) Restore(snapshot io.ReadCloser) error { + // Read the snapshot data from the provided io.ReadCloser + data, err := io.ReadAll(snapshot) + if err != nil { + return fmt.Errorf("failed to read snapshot data: %v", err) + } + + // Create a new buffer to decode the snapshot data + buf := bytes.NewBuffer(data) + + // Decode the snapshot data into a new ServerSnapshot instance + var restoredSnapshot ServerSnapshot + decoder := gob.NewDecoder(buf) + err = decoder.Decode(&restoredSnapshot) + if err != nil { + return fmt.Errorf("failed to decode snapshot data: %v", err) + } + + // Acquire a write lock on the cache to restore the state + s.mtx.Lock() + defer s.mtx.Unlock() + + // Clear the existing cache + s.db.Clear() + + // Restore the set commands from the snapshot + for _, cmd := range restoredSnapshot.Commands { + err := s.db.Set(cmd.Key, cmd.Val, time.Duration(cmd.Duration)) + if err != nil { + return fmt.Errorf("failed to restore command: %v", err) + } + } + + log.Println("Snapshot restored successfully") + + return nil +} diff --git a/server/raft.go b/server/raft.go new file mode 100644 index 0000000..6398381 --- /dev/null +++ b/server/raft.go @@ -0,0 +1,149 @@ +package server + +import ( + "errors" + "fmt" + "github.com/Jille/raft-grpc-leader-rpc/leaderhealth" + transport "github.com/Jille/raft-grpc-transport" + "github.com/Jille/raftadmin" + "github.com/hashicorp/raft" + pb "github.com/ragoob/gCache/proto" + "golang.org/x/net/context" + "google.golang.org/grpc" + "google.golang.org/grpc/reflection" + "log" + "net" + "os" + "path/filepath" + "time" +) + +type rpcInterface struct { + pb.GCacheServiceServer + server *Server + raft *raft.Raft +} + +func (s *Server) NewRaft(ctx context.Context, myID, myAddress string, fsm raft.FSM) (*raft.Raft, *transport.Manager, error) { + // Create a new Raft configuration + config := raft.DefaultConfig() + config.LocalID = raft.ServerID(myID) + + // Create the Raft log store (replace with your own implementation) + logStore := raft.NewInmemStore() + + // Create the Raft stable store (replace with your own implementation) + stableStore := raft.NewInmemStore() + + baseDir := filepath.Join("raft_data/", myID) + + fss, err := raft.NewFileSnapshotStore(baseDir, 3, os.Stderr) + if err != nil { + return nil, nil, fmt.Errorf(`raft.NewFileSnapshotStore(%q, ...): %v`, baseDir, err) + } + + // Create the transport layer (replace with your own implementation) + tm := transport.New(raft.ServerAddress(myAddress), []grpc.DialOption{grpc.WithInsecure()}) + + //timeout := time.Second * 5 + //tr, err := raft.NewTCPTransport(myAddress, nil, 10, timeout, os.Stdout) + //if err != nil { + // return nil, nil, fmt.Errorf("failed to create transport: %v", err) + //} + + // Create the Raft node + r, err := raft.NewRaft(config, fsm, logStore, stableStore, fss, tm.Transport()) + if err != nil { + return nil, nil, fmt.Errorf("failed to create Raft node: %v", err) + } + s1 := raft.Server{ + Suffrage: raft.Voter, + ID: config.LocalID, + Address: raft.ServerAddress("127.0.0.1:4000"), + } + // + //s2 := raft.Server{ + // Suffrage: raft.Voter, + // ID: raft.ServerID("node2"), + // Address: raft.ServerAddress("127.0.0.1:5002"), + //} + // + //s3 := raft.Server{ + // Suffrage: raft.Voter, + // ID: raft.ServerID("node3"), + // Address: raft.ServerAddress("127.0.0.1:5003"), + //} + + serverConfig := raft.Configuration{ + Servers: []raft.Server{s1}, + } + + if err := r.BootstrapCluster(serverConfig).Error(); err != nil { + log.Fatalf("failed to bootstrab the cluster : %+v ", err) + } + + return r, tm, nil +} + +func (s *Server) Serve() error { + // Create the Raft node + ctx := context.Background() + + bindAddr := fmt.Sprintf("127.0.0.1%s", s.ListenAddr) + + r, tm, err := s.NewRaft(ctx, s.NodeID, bindAddr, s) + if err != nil { + return err + } + + // Create the gRPC server + server := grpc.NewServer() + + // Register the Raft node as a gRPC service + //raftgrpc.RegisterRaftServer(server, raftNode) + + // Register your gRPC service with the server + pb.RegisterGCacheServiceServer(server, &rpcInterface{ + server: s, + raft: r, + }) + + // Register the Raft node's gRPC service with the Time Machine + tm.Register(server) + leaderhealth.Setup(r, server, []string{"Example"}) + raftadmin.Register(server, r) + reflection.Register(server) + + // Create a listener for the gRPC server + lis, err := net.Listen("tcp", s.ListenAddr) + if err != nil { + return fmt.Errorf("listen error: %v", err) + } + + // Start serving gRPC requests in a separate goroutine + go func() { + log.Printf("Server started [%s]", s.ListenAddr) + if err := server.Serve(lis); err != nil { + log.Fatalf("failed to serve: %v", err) + } + }() + + // Wait for the Raft node to become the leader + select { + case <-r.LeaderCh(): + log.Println("Raft node elected as leader") + case <-time.After(5 * time.Second): + return errors.New("timeout: Raft node did not become leader") + } + + // Wait for server shutdown + <-ctx.Done() + + // Stop the gRPC server + server.Stop() + + // Stop the Raft node + r.Shutdown() + + return nil +} diff --git a/server/server.go b/server/server.go index 8d67826..59a7382 100644 --- a/server/server.go +++ b/server/server.go @@ -1,18 +1,10 @@ package server import ( - "context" - "encoding/binary" - "fmt" + "github.com/google/uuid" + "github.com/ragoob/gCache/db" "github.com/ragoob/gCache/pkg/client" - "io" - "log" - "net" "sync" - "time" - - "github.com/ragoob/gCache/cmd" - "github.com/ragoob/gCache/db" ) type ServerOpts struct { @@ -23,143 +15,21 @@ type ServerOpts struct { type Server struct { ServerOpts + NodeID string followers map[*client.Client]struct{} db db.DB - mu sync.Mutex + mtx sync.Mutex +} + +func generateNodeID() string { + return uuid.New().String() } func NewServer(opts ServerOpts, db db.DB) *Server { return &Server{ + NodeID: generateNodeID(), ServerOpts: opts, db: db, followers: make(map[*client.Client]struct{}), } } - -func (s *Server) Serve() error { - ln, err := net.Listen("tcp", s.ListenAddr) - if err != nil { - return fmt.Errorf("listen error: [%v]", err) - } - - if !s.IsLeader && s.ListenAddr != "" { - go func() { - if err := s.dailLeader(); err != nil { - log.Println(err) - } - }() - } - - log.Printf("Server started [%s]", s.ListenAddr) - - for { - conn, err := ln.Accept() - if err != nil { - log.Printf("connection error [%v]", err) - continue - } - - go s.handleConn(conn) - } -} - -func (s *Server) dailLeader() error { - conn, err := net.Dial("tcp", s.LeaderAddr) - if err != nil { - return fmt.Errorf("failed to connect to leader [%v]", err) - } - - log.Println("Connected to leader") - joinCmdBytes := (&cmd.JoinCmd{ - Addr: []byte(s.ListenAddr), - }).GetBytes() - binary.Write(conn, binary.LittleEndian, joinCmdBytes) - - s.handleConn(conn) - - return nil -} -func (s *Server) handleConn(conn net.Conn) { - defer conn.Close() - - for { - command, err := cmd.ParseCmd(conn) - if err != nil { - if err == io.EOF { - break - } - - log.Println("command not defiend", err) - - break - } - - go s.handleCommand(conn, command) - } -} - -func (s *Server) handleCommand(conn net.Conn, command any) { - switch c := command.(type) { - case *cmd.SetCmd: - s.handleSetCommand(conn, c) - case *cmd.GetCmd: - s.handleGetCommand(conn, c) - case *cmd.JoinCmd: - s.handleJoinCommand(conn, c) - } -} - -func (s *Server) handleSetCommand(conn net.Conn, command *cmd.SetCmd) error { - - resp := cmd.SetRes{} - if !s.IsLeader && !command.Replication { - resp.Status = cmd.Error - _, err := conn.Write(resp.GetBytes()) - return err - } - if err := s.db.Set(command.Key, command.Val, time.Duration(command.Duration)); err != nil { - resp.Status = cmd.Error - _, err := conn.Write(resp.GetBytes()) - return err - } - resp.Status = cmd.OK - _, err := conn.Write(resp.GetBytes()) - go func() { - for peer := range s.followers { - if err := peer.Replicate(context.TODO(), command.Key, command.Val, command.Duration); err != nil { - log.Println("replicating to follower error:", err) - } - } - }() - return err -} - -func (s *Server) handleGetCommand(conn net.Conn, command *cmd.GetCmd) error { - resp := cmd.GetRes{} - - val, err := s.db.Get(command.Key) - if err != nil { - resp.Status = cmd.Error - _, err := conn.Write(resp.GetBytes()) - return err - } - resp.Status = cmd.OK - resp.Val = val - - _, err = conn.Write(resp.GetBytes()) - return err - -} - -func (s *Server) handleJoinCommand(conn net.Conn, command *cmd.JoinCmd) error { - log.Println("New follower joined: ", conn.RemoteAddr()) - s.mu.Lock() - defer s.mu.Unlock() - c, err := client.Connect(string(command.Addr), client.Options{}) - if err != nil { - log.Printf("error join cluster [%s]", string(command.Addr)) - return nil - } - s.followers[c] = struct{}{} - return nil -} diff --git a/server/service.go b/server/service.go new file mode 100644 index 0000000..737acb5 --- /dev/null +++ b/server/service.go @@ -0,0 +1,61 @@ +package server + +import ( + "fmt" + "github.com/Jille/raft-grpc-leader-rpc/rafterrors" + "github.com/ragoob/gCache/cmd" + pb "github.com/ragoob/gCache/proto" + "golang.org/x/net/context" + "time" +) + +func (r rpcInterface) Set(ctx context.Context, req *pb.SetRequest) (*pb.SetResponse, error) { + command := &cmd.SetCmd{ + Key: req.Key, + Val: req.Value, + Duration: 5, + } + cmdBytes := command.GetBytes() + + f := r.raft.Apply(cmdBytes, time.Second) + if err := f.Error(); err != nil { + return nil, rafterrors.MarkRetriable(err) + } + fmt.Printf("%+v :: CommitIndex: %+v \n", r.server.NodeID, f.Index()) + + // complete set value + return &pb.SetResponse{Success: true}, nil + + //// check if not leader don't write + // todo :: leader only can write + //if !s.IsLeader && !command.Replication { + // return &pb.SetResponse{Success: false}, errors.New("replica can't write") + //} + //// set the value to db + //if err := s.db.Set(command.Key, command.Val, time.Duration(command.Duration)); err != nil { + // return &pb.SetResponse{Success: false}, err + //} + //// set the value to the followers replicas + //go func() { + // for peer := range s.followers { + // if err := peer.Replicate(context.TODO(), command.Key, command.Val, command.Duration); err != nil { + // log.Println("replicating to follower error:", err) + // } + // } + //}() + //// complete set value + //return &pb.SetResponse{Success: true}, nil +} + +func (r rpcInterface) Get(ctx context.Context, req *pb.GetRequest) (*pb.GetResponse, error) { + val, err := r.server.db.Get(req.Key) + if err != nil { + return nil, err + } + + fmt.Printf("%s ::get command rpc : %s \n", r.server.NodeID, val) + + return &pb.GetResponse{ + Value: val, + }, nil +} diff --git a/server_new_old/server.go b/server_new_old/server.go new file mode 100644 index 0000000..00752fe --- /dev/null +++ b/server_new_old/server.go @@ -0,0 +1,96 @@ +package new + +import ( + "context" + "github.com/ragoob/gCache/cmd" + "github.com/ragoob/gCache/pkg/client" + "io" + "log" + "net" + "time" +) + +func (s *Server) handleConn(conn net.Conn) { + defer conn.Close() + + for { + command, err := cmd.ParseCmd(conn) + if err != nil { + if err == io.EOF { + break + } + + log.Println("command not defiend", err) + + break + } + + go s.handleCommand(conn, command) + } +} + +func (s *Server) handleCommand(conn net.Conn, command any) { + switch c := command.(type) { + case *cmd.SetCmd: + s.handleSetCommand(conn, c) + case *cmd.GetCmd: + s.handleGetCommand(conn, c) + case *cmd.JoinCmd: + s.handleJoinCommand(conn, c) + } +} + +func (s *Server) handleSetCommand(conn net.Conn, command *cmd.SetCmd) error { + + resp := cmd.SetRes{} + if !s.IsLeader && !command.Replication { + resp.Status = cmd.Error + _, err := conn.Write(resp.GetBytes()) + return err + } + if err := s.db.Set(command.Key, command.Val, time.Duration(command.Duration)); err != nil { + resp.Status = cmd.Error + _, err := conn.Write(resp.GetBytes()) + return err + } + resp.Status = cmd.OK + _, err := conn.Write(resp.GetBytes()) + go func() { + for peer := range s.followers { + if err := peer.Replicate(context.TODO(), command.Key, command.Val, command.Duration); err != nil { + log.Println("replicating to follower error:", err) + } + } + }() + return err +} + +func (s *Server) handleGetCommand(conn net.Conn, command *cmd.GetCmd) error { + resp := cmd.GetRes{} + + val, err := s.db.Get(command.Key) + if err != nil { + resp.Status = cmd.Error + _, err := conn.Write(resp.GetBytes()) + return err + } + resp.Status = cmd.OK + resp.Val = val + + _, err = conn.Write(resp.GetBytes()) + return err + +} + +func (s *Server) handleJoinCommand(conn net.Conn, command *cmd.JoinCmd) error { + log.Println("New follower joined: ", conn.RemoteAddr()) + s.mu.Lock() + defer s.mu.Unlock() + c, err := client.Connect(string(command.Addr), client.Options{}) + if err != nil { + log.Printf("error join cluster [%s]", string(command.Addr)) + return nil + } + s.followers[c] = struct{}{} + return nil +} diff --git a/server_new_old/service.go b/server_new_old/service.go new file mode 100644 index 0000000..cf44d9d --- /dev/null +++ b/server_new_old/service.go @@ -0,0 +1,100 @@ +package new + +import ( + "errors" + "fmt" + "github.com/ragoob/gCache/cmd" + "github.com/ragoob/gCache/db" + "github.com/ragoob/gCache/pkg/client" + pb "github.com/ragoob/gCache/proto" + "golang.org/x/net/context" + "google.golang.org/grpc" + "log" + "net" + "sync" + "time" +) + +type ServerOpts struct { + ListenAddr string + IsLeader bool + LeaderAddr string +} + +type Server struct { + pb.GCacheServiceServer + ServerOpts + followers map[*client.Client]struct{} + db db.DB + mu sync.Mutex +} + +func NewServer(opts ServerOpts, db db.DB) *Server { + return &Server{ + ServerOpts: opts, + db: db, + followers: make(map[*client.Client]struct{}), + } +} + +func (s *Server) Serve() error { + lis, err := net.Listen("tcp", s.ListenAddr) + if err != nil { + return fmt.Errorf("listen error: [%v]", err) + } + + server := grpc.NewServer() + pb.RegisterGCacheServiceServer(server, s) + + log.Printf("Server started [%s]", s.ListenAddr) + // Start the gRPC server in a separate goroutine + go func() { + if err := server.Serve(lis); err != nil { + log.Fatalf("failed to serve: %v", err) + } + }() + + if !s.IsLeader && s.ListenAddr != "" { + go func() { + if err := s.connectToLeader(); err != nil { + log.Println(err) + } + }() + } + return nil +} + +func (s *Server) Get(ctx context.Context, req *pb.GetRequest) (*pb.GetResponse, error) { + val, err := s.db.Get(req.Key) + if err != nil { + return nil, err + } + + return &pb.GetResponse{ + Value: val, + }, nil +} + +func (s *Server) Set(ctx context.Context, req *pb.SetRequest) (*pb.SetResponse, error) { + command := &cmd.SetCmd{ + Key: req.Key, + Val: req.Value, + Duration: 5, + } + + // check if not leader don't write + if !s.IsLeader && !command.Replication { + return &pb.SetResponse{Success: false}, errors.New("replica can't write") + } + // set the value to db + if err := s.db.Set(command.Key, command.Val, time.Duration(command.Duration)); err != nil { + return &pb.SetResponse{Success: false}, err + } + + // complete set value + return &pb.SetResponse{Success: true}, nil +} + +func (s *Server) connectToLeader() interface{} { + return nil +} diff --git a/server_old/server.go b/server_old/server.go new file mode 100644 index 0000000..eb93e40 --- /dev/null +++ b/server_old/server.go @@ -0,0 +1,165 @@ +package old + +import ( + "context" + "encoding/binary" + "fmt" + "github.com/ragoob/gCache/pkg/client" + "io" + "log" + "net" + "sync" + "time" + + "github.com/ragoob/gCache/cmd" + "github.com/ragoob/gCache/db" +) + +type ServerOpts struct { + ListenAddr string + IsLeader bool + LeaderAddr string +} + +type Server struct { + ServerOpts + followers map[*client.Client]struct{} + db db.DB + mu sync.Mutex +} + +func NewServer(opts ServerOpts, db db.DB) *Server { + return &Server{ + ServerOpts: opts, + db: db, + followers: make(map[*client.Client]struct{}), + } +} + +func (s *Server) Serve() error { + ln, err := net.Listen("tcp", s.ListenAddr) + if err != nil { + return fmt.Errorf("listen error: [%v]", err) + } + + if !s.IsLeader && s.ListenAddr != "" { + go func() { + if err := s.dailLeader(); err != nil { + log.Println(err) + } + }() + } + + log.Printf("Server started [%s]", s.ListenAddr) + + for { + conn, err := ln.Accept() + if err != nil { + log.Printf("connection error [%v]", err) + continue + } + + go s.handleConn(conn) + } +} + +func (s *Server) dailLeader() error { + conn, err := net.Dial("tcp", s.LeaderAddr) + if err != nil { + return fmt.Errorf("failed to connect to leader [%v]", err) + } + + log.Println("Connected to leader") + joinCmdBytes := (&cmd.JoinCmd{ + Addr: []byte(s.ListenAddr), + }).GetBytes() + binary.Write(conn, binary.LittleEndian, joinCmdBytes) + + s.handleConn(conn) + + return nil +} +func (s *Server) handleConn(conn net.Conn) { + defer conn.Close() + + for { + command, err := cmd.ParseCmd(conn) + if err != nil { + if err == io.EOF { + break + } + + log.Println("command not defiend", err) + + break + } + + go s.handleCommand(conn, command) + } +} + +func (s *Server) handleCommand(conn net.Conn, command any) { + switch c := command.(type) { + case *cmd.SetCmd: + s.handleSetCommand(conn, c) + case *cmd.GetCmd: + s.handleGetCommand(conn, c) + case *cmd.JoinCmd: + s.handleJoinCommand(conn, c) + } +} + +func (s *Server) handleSetCommand(conn net.Conn, command *cmd.SetCmd) error { + + resp := cmd.SetRes{} + if !s.IsLeader && !command.Replication { + resp.Status = cmd.Error + _, err := conn.Write(resp.GetBytes()) + return err + } + if err := s.db.Set(command.Key, command.Val, time.Duration(command.Duration)); err != nil { + resp.Status = cmd.Error + _, err := conn.Write(resp.GetBytes()) + return err + } + resp.Status = cmd.OK + _, err := conn.Write(resp.GetBytes()) + go func() { + for peer := range s.followers { + if err := peer.Replicate(context.TODO(), command.Key, command.Val, command.Duration); err != nil { + log.Println("replicating to follower error:", err) + } + } + }() + return err +} + +func (s *Server) handleGetCommand(conn net.Conn, command *cmd.GetCmd) error { + resp := cmd.GetRes{} + + val, err := s.db.Get(command.Key) + if err != nil { + resp.Status = cmd.Error + _, err := conn.Write(resp.GetBytes()) + return err + } + resp.Status = cmd.OK + resp.Val = val + + _, err = conn.Write(resp.GetBytes()) + return err + +} + +func (s *Server) handleJoinCommand(conn net.Conn, command *cmd.JoinCmd) error { + log.Println("New follower joined: ", conn.RemoteAddr()) + s.mu.Lock() + defer s.mu.Unlock() + c, err := client.Connect(string(command.Addr), client.Options{}) + if err != nil { + log.Printf("error join cluster [%s]", string(command.Addr)) + return nil + } + s.followers[c] = struct{}{} + return nil +}