From 8e946a32e37388c49c9b43b93010c0c605a8021e Mon Sep 17 00:00:00 2001 From: ykadowak Date: Wed, 13 Dec 2023 05:44:46 +0000 Subject: [PATCH 01/14] [RESET THIS] --- .../vald/templates/agent/readreplica/svc.yaml | 1 + .../vald/templates/gateway/lb/configmap.yaml | 19 +++++++ .../client/v1/client/discoverer/discover.go | 50 +++++++++++++++++++ .../client/v1/client/discoverer/option.go | 14 ++++++ internal/config/lb.go | 29 +++++++++++ pkg/gateway/lb/handler/grpc/aggregation.go | 3 +- pkg/gateway/lb/handler/grpc/handler.go | 12 ++--- pkg/gateway/lb/service/gateway.go | 28 +++++++++-- pkg/gateway/lb/usecase/vald.go | 12 +++++ 9 files changed, 158 insertions(+), 10 deletions(-) diff --git a/charts/vald/templates/agent/readreplica/svc.yaml b/charts/vald/templates/agent/readreplica/svc.yaml index 441c00c5cb..e4d94b78d4 100644 --- a/charts/vald/templates/agent/readreplica/svc.yaml +++ b/charts/vald/templates/agent/readreplica/svc.yaml @@ -50,6 +50,7 @@ spec: app: {{ $readreplica.name }}-{{ $id }} app.kubernetes.io/name: {{ $valdname }} type: ClusterIP + clusterIP: None {{- if $agent.externalTrafficPolicy }} externalTrafficPolicy: {{ $agent.externalTrafficPolicy }} {{- end }} diff --git a/charts/vald/templates/gateway/lb/configmap.yaml b/charts/vald/templates/gateway/lb/configmap.yaml index 430040b929..6be0a7a506 100644 --- a/charts/vald/templates/gateway/lb/configmap.yaml +++ b/charts/vald/templates/gateway/lb/configmap.yaml @@ -16,6 +16,8 @@ {{- $gateway := .Values.gateway.lb -}} {{- $agent := .Values.agent -}} {{- $discoverer := .Values.discoverer -}} +{{- $readreplica := .Values.agent.readreplica -}} +{{- $release := .Release -}} {{- if $gateway.enabled }} apiVersion: v1 kind: ConfigMap @@ -49,6 +51,7 @@ data: agent_namespace: {{ $gateway.gateway_config.agent_namespace | quote }} node_name: {{ $gateway.gateway_config.node_name | quote }} index_replica: {{ $gateway.gateway_config.index_replica }} + readreplica_replicas: {{ $readreplica.replica }} discoverer: duration: {{ $gateway.gateway_config.discoverer.duration }} client: @@ -64,4 +67,20 @@ data: agent_client_options: {{- include "vald.grpc.client.addrs" (dict "Valued" $gateway.gateway_config.discoverer.agent_client_options.addrs) | nindent 10 }} {{- include "vald.grpc.client" (dict "Values" $gateway.gateway_config.discoverer.agent_client_options "default" .Values.defaults.grpc.client) | nindent 10 }} + {{- if $readreplica.enabled }} + read_replica_client: + client: + {{- $discovererClient := $gateway.gateway_config.discoverer.client }} + {{- $readReplicaPort := $agent.server_config.servers.grpc.port }} + {{- $defaultReadReplicaPort := default .Values.defaults.server_config.servers.grpc.port $readReplicaPort }} + {{- $readReplicaAddrs := list }} + {{- range $i := until (int $agent.minReplicas) }} + {{- $addr := printf "%s-%d.%s.svc.cluster.local:%d" $readreplica.name $i $release.Namespace (int64 $defaultReadReplicaPort) }} + {{- $readReplicaAddrs = append $readReplicaAddrs $addr }} + {{- end }} + {{- $readReplicaAddrs := dict "Values" $discovererClient.addrs "default" $readReplicaAddrs }} + {{- include "vald.grpc.client.addrs" $readReplicaAddrs | nindent 10 }} + {{- $readReplicaGRPCclient := dict "Values" $discovererClient "default" .Values.defaults.grpc.client }} + {{- include "vald.grpc.client" $readReplicaGRPCclient | nindent 10 }} + {{- end }} {{- end }} diff --git a/internal/client/v1/client/discoverer/discover.go b/internal/client/v1/client/discoverer/discover.go index 89a43f2a8c..43fd6f526b 100644 --- a/internal/client/v1/client/discoverer/discover.go +++ b/internal/client/v1/client/discoverer/discover.go @@ -37,7 +37,9 @@ import ( type Client interface { Start(ctx context.Context) (<-chan error, error) GetAddrs(ctx context.Context) []string + GetReadAddrs(ctx context.Context) []string GetClient() grpc.Client + GetReadClient() grpc.Client } type client struct { @@ -56,6 +58,11 @@ type client struct { name string namespace string nodeName string + // read replica related below + readClient grpc.Client + readAddrs atomic.Pointer[[]string] + readReplicaReplicas uint64 + roundRobin atomic.Uint64 } func New(opts ...Option) (d Client, err error) { @@ -74,6 +81,14 @@ func (c *client) Start(ctx context.Context) (<-chan error, error) { return nil, err } + var rrech <-chan error + if c.readClient != nil { + rrech, err = c.readClient.StartConnectionMonitor(ctx) + if err != nil { + return nil, err + } + } + ech := make(chan error, 100) addrs, err := c.dnsDiscovery(ctx, ech) if err != nil { @@ -134,6 +149,7 @@ func (c *client) Start(ctx context.Context) (<-chan error, error) { return finalize() case err = <-dech: case err = <-aech: + case err = <-rrech: case <-dt.C: err = c.discover(ctx, ech) } @@ -168,12 +184,45 @@ func (c *client) GetAddrs(ctx context.Context) (addrs []string) { return addrs } +func (c *client) GetReadAddrs(ctx context.Context) []string { + a := c.readAddrs.Load() + if a == nil { + return nil + } + return *a +} + func (c *client) GetClient() grpc.Client { return c.client } +func (c *client) GetReadClient() grpc.Client { + // read replica + // 1. primary + svc cluster IP *n でここで比率を見て呼び分ける + // read replicaのreplica数がHPAなどで高速に変動する場合に、実装がシンプルそうなのでこちらが良さそう + // 2. primary + ipsで単純にラウンドロビン + + // round robin with c.client and c.readClient everytime it's called + // with a ratio of primary + read replica deployment replicas + // TODO: is this atomic operation really worth it? + var new uint64 + for { + cur := c.roundRobin.Load() + new = (cur + 1) % (c.readReplicaReplicas + 1) + if c.roundRobin.CompareAndSwap(cur, new) { + break + } + } + if new == 0 || c.readClient == nil { + return c.client + // return c.readClient // DEBUG: + } + return c.readClient +} + func (c *client) connect(ctx context.Context, addr string) (err error) { if c.autoconn && c.client != nil { + // FIXME: この中で重複管理されている _, err = c.client.Connect(ctx, addr) if err != nil { return err @@ -323,6 +372,7 @@ func (c *client) discoverAddrs(ctx context.Context, nodes *payload.Info_Nodes, e len(node.GetPods().GetPods()) > i && len(node.GetPods().GetPods()[i].GetIp()) != 0 { addr := net.JoinHostPort(node.GetPods().GetPods()[i].GetIp(), uint16(c.port)) + // FIXME: ここで実際にgrpcサーバーへ接続する if err = c.connect(ctx, addr); err != nil { select { case <-ctx.Done(): diff --git a/internal/client/v1/client/discoverer/option.go b/internal/client/v1/client/discoverer/option.go index aca62c61f2..23bdff6229 100644 --- a/internal/client/v1/client/discoverer/option.go +++ b/internal/client/v1/client/discoverer/option.go @@ -68,6 +68,13 @@ func WithDiscovererClient(gc grpc.Client) Option { } } +func WithReadReplicaClient(gc grpc.Client) Option { + return func(c *client) error { + c.readClient = gc + return nil + } +} + func WithDiscoverDuration(dur string) Option { return func(c *client) error { d, err := timeutil.Parse(dur) @@ -142,3 +149,10 @@ func WithErrGroup(eg errgroup.Group) Option { return nil } } + +func WithReadReplicaReplicas(num uint64) Option { + return func(c *client) error { + c.readReplicaReplicas = num + return nil + } +} diff --git a/internal/config/lb.go b/internal/config/lb.go index 0e6a429f2f..467098e43a 100644 --- a/internal/config/lb.go +++ b/internal/config/lb.go @@ -37,6 +37,12 @@ type LB struct { // IndexReplica represents index replication count IndexReplica int `json:"index_replica" yaml:"index_replica"` + // ReadReplicaReplicas represents replica count of read replica Deployment + ReadReplicaReplicas uint64 `json:"read_replica_replicas" yaml:"read_replica_replicas"` + + // ReadReplicaClient represents read replica client configuration + ReadReplicaClient ReadReplicaClient `json:"read_replica_client" yaml:"read_replica_client"` + // Discoverer represent agent discoverer service configuration Discoverer *DiscovererClient `json:"discoverer" yaml:"discoverer"` @@ -56,3 +62,26 @@ func (g *LB) Bind() *LB { } return g } + +// ReadReplicaClient +type ReadReplicaClient struct { + Duration string `json:"duration" yaml:"duration"` + Client *GRPCClient `json:"client" yaml:"client"` + AgentClientOptions *GRPCClient `json:"agent_client_options" yaml:"agent_client_options"` +} + +// Bind binds the actual data from the ReadReplicaClient receiver field. +func (d *ReadReplicaClient) Bind() *ReadReplicaClient { + d.Duration = GetActualValue(d.Duration) + if d.Client != nil { + d.Client.Bind() + } else { + d.Client = newGRPCClientConfig() + } + if d.AgentClientOptions != nil { + d.AgentClientOptions.Bind() + } else { + d.AgentClientOptions = newGRPCClientConfig() + } + return d +} diff --git a/pkg/gateway/lb/handler/grpc/aggregation.go b/pkg/gateway/lb/handler/grpc/aggregation.go index 58f749eee0..26b51bf547 100644 --- a/pkg/gateway/lb/handler/grpc/aggregation.go +++ b/pkg/gateway/lb/handler/grpc/aggregation.go @@ -33,6 +33,7 @@ import ( "github.com/vdaas/vald/internal/net/grpc/status" "github.com/vdaas/vald/internal/observability/trace" "github.com/vdaas/vald/internal/sync" + "github.com/vdaas/vald/pkg/gateway/lb/service" ) type Aggregator interface { @@ -68,7 +69,7 @@ func (s *server) aggregationSearch(ctx context.Context, aggr Aggregator, cfg *pa ctx, cancel := context.WithTimeout(ctx, timeout) aggr.Start(ctx) - err = s.gateway.BroadCast(ctx, func(ctx context.Context, target string, vc vald.Client, copts ...grpc.CallOption) error { + err = s.gateway.BroadCast(ctx, service.READ, func(ctx context.Context, target string, vc vald.Client, copts ...grpc.CallOption) error { sctx, sspan := trace.StartSpan(grpc.WrapGRPCMethod(ctx, "BroadCast/"+target), apiName+"/aggregationSearch/"+target) defer func() { if sspan != nil { diff --git a/pkg/gateway/lb/handler/grpc/handler.go b/pkg/gateway/lb/handler/grpc/handler.go index e8fde265a6..95fa86b1dc 100644 --- a/pkg/gateway/lb/handler/grpc/handler.go +++ b/pkg/gateway/lb/handler/grpc/handler.go @@ -95,7 +95,7 @@ func (s *server) exists(ctx context.Context, uuid string) (id *payload.Object_ID defer close(ich) defer close(ech) var once sync.Once - ech <- s.gateway.BroadCast(ctx, func(ctx context.Context, target string, vc vald.Client, copts ...grpc.CallOption) error { + ech <- s.gateway.BroadCast(ctx, service.READ, func(ctx context.Context, target string, vc vald.Client, copts ...grpc.CallOption) error { sctx, sspan := trace.StartSpan(grpc.WrapGRPCMethod(ctx, "BroadCast/"+target), apiName+"/exists/BroadCast/"+target) defer func() { if sspan != nil { @@ -1653,7 +1653,7 @@ func (s *server) Update(ctx context.Context, req *payload.Update_Request) (res * Ips: make([]string, 0, s.replica), } ) - err = s.gateway.BroadCast(ctx, func(ctx context.Context, target string, vc vald.Client, copts ...grpc.CallOption) (err error) { + err = s.gateway.BroadCast(ctx, service.WRITE, func(ctx context.Context, target string, vc vald.Client, copts ...grpc.CallOption) (err error) { ctx, span := trace.StartSpan(grpc.WrapGRPCMethod(ctx, "BroadCast/"+target), apiName+"/"+vald.UpdateRPCName+"/"+target) defer func() { if span != nil { @@ -2562,7 +2562,7 @@ func (s *server) Remove(ctx context.Context, req *payload.Remove_Request) (locs Ips: make([]string, 0, s.replica), } ls := make([]string, 0, s.replica) - err = s.gateway.BroadCast(ctx, func(ctx context.Context, target string, vc vald.Client, copts ...grpc.CallOption) (err error) { + err = s.gateway.BroadCast(ctx, service.WRITE, func(ctx context.Context, target string, vc vald.Client, copts ...grpc.CallOption) (err error) { ctx, span := trace.StartSpan(grpc.WrapGRPCMethod(ctx, "BroadCast/"+target), apiName+"/"+vald.RemoveRPCName+"/"+target) defer func() { if span != nil { @@ -2778,7 +2778,7 @@ func (s *server) RemoveByTimestamp(ctx context.Context, req *payload.Remove_Time visited := make(map[string]int) // map[uuid: position of locs] locs = new(payload.Object_Locations) - err := s.gateway.BroadCast(ctx, func(ctx context.Context, target string, vc vald.Client, copts ...grpc.CallOption) (err error) { + err := s.gateway.BroadCast(ctx, service.WRITE, func(ctx context.Context, target string, vc vald.Client, copts ...grpc.CallOption) (err error) { sctx, sspan := trace.StartSpan(grpc.WithGRPCMethod(ctx, "BroadCast/"+target), apiName+"/removeByTimestamp/BroadCast/"+target) defer func() { if sspan != nil { @@ -2905,7 +2905,7 @@ func (s *server) getObject(ctx context.Context, uuid string) (vec *payload.Objec defer close(vch) defer close(ech) var once sync.Once - ech <- s.gateway.BroadCast(ctx, func(ctx context.Context, target string, vc vald.Client, copts ...grpc.CallOption) error { + ech <- s.gateway.BroadCast(ctx, service.READ, func(ctx context.Context, target string, vc vald.Client, copts ...grpc.CallOption) error { sctx, sspan := trace.StartSpan(grpc.WrapGRPCMethod(ctx, "BroadCast/"+target), apiName+"/getObject/BroadCast/"+target) defer func() { if sspan != nil { @@ -3143,7 +3143,7 @@ func (s *server) StreamListObject(req *payload.Object_List_Request, stream vald. defer cancel() var rmu, smu sync.Mutex - err := s.gateway.BroadCast(ctx, func(ctx context.Context, target string, vc vald.Client, copts ...grpc.CallOption) error { + err := s.gateway.BroadCast(ctx, service.READ, func(ctx context.Context, target string, vc vald.Client, copts ...grpc.CallOption) error { ctx, sspan := trace.StartSpan(grpc.WrapGRPCMethod(ctx, "BroadCast/"+target), apiName+"/"+vald.StreamListObjectRPCName+"/"+target) defer func() { if sspan != nil { diff --git a/pkg/gateway/lb/service/gateway.go b/pkg/gateway/lb/service/gateway.go index 7d32364df0..b3ffdf5a9a 100644 --- a/pkg/gateway/lb/service/gateway.go +++ b/pkg/gateway/lb/service/gateway.go @@ -37,10 +37,16 @@ type Gateway interface { Addrs(ctx context.Context) []string DoMulti(ctx context.Context, num int, f func(ctx context.Context, target string, ac vald.Client, copts ...grpc.CallOption) error) error - BroadCast(ctx context.Context, + BroadCast(ctx context.Context, kind BroadCastKind, f func(ctx context.Context, target string, ac vald.Client, copts ...grpc.CallOption) error) error } +type BroadCastKind int +const ( + READ BroadCastKind = iota + WRITE +) + type gateway struct { client discoverer.Client eg errgroup.Group @@ -60,7 +66,9 @@ func (g *gateway) Start(ctx context.Context) (<-chan error, error) { return g.client.Start(ctx) } -func (g *gateway) BroadCast(ctx context.Context, +// FIXME: そもそも呼ばれているメソッドがread系かwrite系かをどこかで判断する必要があるが、 +// 今後もメソッドは増えていくのだからBroadCastとBroadCastReadみたいに分けて、呼び出し側で判断するべき +func (g *gateway) BroadCast(ctx context.Context, kind BroadCastKind, f func(ctx context.Context, target string, ac vald.Client, copts ...grpc.CallOption) error, ) (err error) { fctx, span := trace.StartSpan(ctx, "vald/gateway-lb/service/Gateway.BroadCast") @@ -69,7 +77,20 @@ func (g *gateway) BroadCast(ctx context.Context, span.End() } }() - return g.client.GetClient().RangeConcurrent(fctx, -1, func(ictx context.Context, + + // select read or write clinet which is connected to primary agent or read replica agent + // GetReadClient includes the client to the primary agent so + // it works even when there is no read replica + var client grpc.Client + switch kind { + case READ: + client = g.client.GetReadClient() + case WRITE: + client = g.client.GetClient() + } + + // FIXME: broadcastはsvc経由でロードバランスされてるっぽい + return client.RangeConcurrent(fctx, -1, func(ictx context.Context, addr string, conn *grpc.ClientConn, copts ...grpc.CallOption, ) (err error) { select { @@ -85,6 +106,7 @@ func (g *gateway) BroadCast(ctx context.Context, }) } +// DoMultiはWrite系しか使用しないので、普通にGetClientしていれば良い。現状Update or Insertのみ func (g *gateway) DoMulti(ctx context.Context, num int, f func(ctx context.Context, target string, ac vald.Client, copts ...grpc.CallOption) error, ) (err error) { diff --git a/pkg/gateway/lb/usecase/vald.go b/pkg/gateway/lb/usecase/vald.go index 4b2eadef88..27bf60f39b 100644 --- a/pkg/gateway/lb/usecase/vald.go +++ b/pkg/gateway/lb/usecase/vald.go @@ -55,6 +55,16 @@ func New(cfg *config.Data) (r runner.Runner, err error) { if err != nil { return nil, err } + + rrOpts, err := cfg.Gateway.ReadReplicaClient.Client.Opts() + if err != nil { + return nil, err + } + rrOpts = append(rrOpts, + grpc.WithErrGroup(eg), + grpc.WithConnectionPoolSize(int(cfg.Gateway.ReadReplicaReplicas)), + ) + // skipcq: CRT-D0001 dopts := append( cOpts, @@ -75,9 +85,11 @@ func New(cfg *config.Data) (r runner.Runner, err error) { discoverer.WithPort(cfg.Gateway.AgentPort), discoverer.WithServiceDNSARecord(cfg.Gateway.AgentDNS), discoverer.WithDiscovererClient(grpc.New(dopts...)), + discoverer.WithReadReplicaClient(grpc.New(rrOpts...)), discoverer.WithDiscoverDuration(cfg.Gateway.Discoverer.Duration), discoverer.WithOptions(aopts...), discoverer.WithNodeName(cfg.Gateway.NodeName), + discoverer.WithReadReplicaReplicas(cfg.Gateway.ReadReplicaReplicas), ) if err != nil { return nil, err From fb50dbb3db52d73a3c46e3e38cd8cccd2cb79ead Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Wed, 20 Dec 2023 06:22:40 +0000 Subject: [PATCH 02/14] style: format code with Gofumpt and Prettier This commit fixes the style issues introduced in 0e3b1ea according to the output from Gofumpt and Prettier. Details: https://github.com/vdaas/vald/pull/2276 --- internal/client/v1/client/discoverer/discover.go | 10 ---------- pkg/gateway/lb/service/gateway.go | 4 +--- pkg/index/job/correction/service/corrector_test.go | 4 ++++ 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/internal/client/v1/client/discoverer/discover.go b/internal/client/v1/client/discoverer/discover.go index 43fd6f526b..be0c365610 100644 --- a/internal/client/v1/client/discoverer/discover.go +++ b/internal/client/v1/client/discoverer/discover.go @@ -37,7 +37,6 @@ import ( type Client interface { Start(ctx context.Context) (<-chan error, error) GetAddrs(ctx context.Context) []string - GetReadAddrs(ctx context.Context) []string GetClient() grpc.Client GetReadClient() grpc.Client } @@ -60,7 +59,6 @@ type client struct { nodeName string // read replica related below readClient grpc.Client - readAddrs atomic.Pointer[[]string] readReplicaReplicas uint64 roundRobin atomic.Uint64 } @@ -184,14 +182,6 @@ func (c *client) GetAddrs(ctx context.Context) (addrs []string) { return addrs } -func (c *client) GetReadAddrs(ctx context.Context) []string { - a := c.readAddrs.Load() - if a == nil { - return nil - } - return *a -} - func (c *client) GetClient() grpc.Client { return c.client } diff --git a/pkg/gateway/lb/service/gateway.go b/pkg/gateway/lb/service/gateway.go index b3ffdf5a9a..d0f2754725 100644 --- a/pkg/gateway/lb/service/gateway.go +++ b/pkg/gateway/lb/service/gateway.go @@ -42,6 +42,7 @@ type Gateway interface { } type BroadCastKind int + const ( READ BroadCastKind = iota WRITE @@ -66,8 +67,6 @@ func (g *gateway) Start(ctx context.Context) (<-chan error, error) { return g.client.Start(ctx) } -// FIXME: そもそも呼ばれているメソッドがread系かwrite系かをどこかで判断する必要があるが、 -// 今後もメソッドは増えていくのだからBroadCastとBroadCastReadみたいに分けて、呼び出し側で判断するべき func (g *gateway) BroadCast(ctx context.Context, kind BroadCastKind, f func(ctx context.Context, target string, ac vald.Client, copts ...grpc.CallOption) error, ) (err error) { @@ -89,7 +88,6 @@ func (g *gateway) BroadCast(ctx context.Context, kind BroadCastKind, client = g.client.GetClient() } - // FIXME: broadcastはsvc経由でロードバランスされてるっぽい return client.RangeConcurrent(fctx, -1, func(ictx context.Context, addr string, conn *grpc.ClientConn, copts ...grpc.CallOption, ) (err error) { diff --git a/pkg/index/job/correction/service/corrector_test.go b/pkg/index/job/correction/service/corrector_test.go index eab80e6fab..391bd3cfc9 100644 --- a/pkg/index/job/correction/service/corrector_test.go +++ b/pkg/index/job/correction/service/corrector_test.go @@ -41,6 +41,10 @@ func (m *mockDiscovererClient) GetClient() grpc.Client { return &m.client } +func (m *mockDiscovererClient) GetReadClient() grpc.Client { + return &m.client +} + func Test_correct_correctTimestamp(t *testing.T) { t.Parallel() From 51c9f8d56d94ed503e2d7b8e3bdfe2469a5b36a5 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Thu, 21 Dec 2023 02:01:06 +0000 Subject: [PATCH 03/14] Fix configmap --- charts/vald/templates/gateway/lb/configmap.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/vald/templates/gateway/lb/configmap.yaml b/charts/vald/templates/gateway/lb/configmap.yaml index 6be0a7a506..428437f4bb 100644 --- a/charts/vald/templates/gateway/lb/configmap.yaml +++ b/charts/vald/templates/gateway/lb/configmap.yaml @@ -51,7 +51,7 @@ data: agent_namespace: {{ $gateway.gateway_config.agent_namespace | quote }} node_name: {{ $gateway.gateway_config.node_name | quote }} index_replica: {{ $gateway.gateway_config.index_replica }} - readreplica_replicas: {{ $readreplica.replica }} + read_replica_replicas: {{ $readreplica.replica }} discoverer: duration: {{ $gateway.gateway_config.discoverer.duration }} client: From 16468de9da11c7266ace009d041923aaed0c4a16 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Thu, 21 Dec 2023 06:05:48 +0000 Subject: [PATCH 04/14] Update read replica rotator enable condition --- charts/vald/templates/gateway/lb/configmap.yaml | 2 +- .../templates/index/job/readreplica/rotate/clusterrole.yaml | 3 ++- .../index/job/readreplica/rotate/clusterrolebinding.yaml | 3 ++- .../vald/templates/index/job/readreplica/rotate/configmap.yaml | 3 ++- .../vald/templates/index/job/readreplica/rotate/cronjob.yaml | 3 ++- .../templates/index/job/readreplica/rotate/networkpolicy.yaml | 3 ++- .../templates/index/job/readreplica/rotate/serviceaccount.yaml | 3 ++- 7 files changed, 13 insertions(+), 7 deletions(-) diff --git a/charts/vald/templates/gateway/lb/configmap.yaml b/charts/vald/templates/gateway/lb/configmap.yaml index 428437f4bb..348371132c 100644 --- a/charts/vald/templates/gateway/lb/configmap.yaml +++ b/charts/vald/templates/gateway/lb/configmap.yaml @@ -70,7 +70,7 @@ data: {{- if $readreplica.enabled }} read_replica_client: client: - {{- $discovererClient := $gateway.gateway_config.discoverer.client }} + {{- $discovererClient := $gateway.gateway_config.read_replica_client.client }} {{- $readReplicaPort := $agent.server_config.servers.grpc.port }} {{- $defaultReadReplicaPort := default .Values.defaults.server_config.servers.grpc.port $readReplicaPort }} {{- $readReplicaAddrs := list }} diff --git a/charts/vald/templates/index/job/readreplica/rotate/clusterrole.yaml b/charts/vald/templates/index/job/readreplica/rotate/clusterrole.yaml index 921d78f061..c1a6e5b09b 100644 --- a/charts/vald/templates/index/job/readreplica/rotate/clusterrole.yaml +++ b/charts/vald/templates/index/job/readreplica/rotate/clusterrole.yaml @@ -14,7 +14,8 @@ # limitations under the License. # {{- $rotator := .Values.manager.index.readreplica.rotator -}} -{{- if and $rotator.enabled $rotator.clusterRole.enabled }} +{{- $readreplica := .Values.agent.readreplica -}} +{{- if and $readreplica.enabled $rotator.clusterRole.enabled }} apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: diff --git a/charts/vald/templates/index/job/readreplica/rotate/clusterrolebinding.yaml b/charts/vald/templates/index/job/readreplica/rotate/clusterrolebinding.yaml index e69382eae5..19883367ba 100644 --- a/charts/vald/templates/index/job/readreplica/rotate/clusterrolebinding.yaml +++ b/charts/vald/templates/index/job/readreplica/rotate/clusterrolebinding.yaml @@ -14,7 +14,8 @@ # limitations under the License. # {{- $rotator := .Values.manager.index.readreplica.rotator -}} -{{- if and $rotator.enabled $rotator.clusterRoleBinding.enabled }} +{{- $readreplica := .Values.agent.readreplica -}} +{{- if and $readreplica.enabled $rotator.clusterRoleBinding.enabled }} apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: diff --git a/charts/vald/templates/index/job/readreplica/rotate/configmap.yaml b/charts/vald/templates/index/job/readreplica/rotate/configmap.yaml index 31e67ab0a2..baabf858f2 100644 --- a/charts/vald/templates/index/job/readreplica/rotate/configmap.yaml +++ b/charts/vald/templates/index/job/readreplica/rotate/configmap.yaml @@ -14,11 +14,12 @@ # limitations under the License. # {{- $rotator := .Values.manager.index.readreplica.rotator -}} +{{- $readreplica := .Values.agent.readreplica -}} {{- $gateway := .Values.gateway.lb -}} {{- $index := .Values.manager.index -}} {{- $agent := .Values.agent -}} {{- $discoverer := .Values.discoverer -}} -{{- if $rotator.enabled }} +{{- if $readreplica.enabled }} apiVersion: v1 kind: ConfigMap metadata: diff --git a/charts/vald/templates/index/job/readreplica/rotate/cronjob.yaml b/charts/vald/templates/index/job/readreplica/rotate/cronjob.yaml index a3bf6941b3..78c3238a57 100644 --- a/charts/vald/templates/index/job/readreplica/rotate/cronjob.yaml +++ b/charts/vald/templates/index/job/readreplica/rotate/cronjob.yaml @@ -14,7 +14,8 @@ # limitations under the License. # {{- $rotator := .Values.manager.index.readreplica.rotator -}} -{{- if $rotator.enabled }} +{{- $readreplica := .Values.agent.readreplica -}} +{{- if $readreplica.enabled }} apiVersion: batch/v1 kind: CronJob metadata: diff --git a/charts/vald/templates/index/job/readreplica/rotate/networkpolicy.yaml b/charts/vald/templates/index/job/readreplica/rotate/networkpolicy.yaml index adb7f7ae3f..062ef37243 100644 --- a/charts/vald/templates/index/job/readreplica/rotate/networkpolicy.yaml +++ b/charts/vald/templates/index/job/readreplica/rotate/networkpolicy.yaml @@ -15,7 +15,8 @@ # {{- $rotator := .Values.manager.index.readreplica.rotator -}} -{{- if and .Values.defaults.networkPolicy.enabled $rotator.enabled }} +{{- $readreplica := .Values.agent.readreplica -}} +{{- if and .Values.defaults.networkPolicy.enabled $readreplica.enabled }} apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: diff --git a/charts/vald/templates/index/job/readreplica/rotate/serviceaccount.yaml b/charts/vald/templates/index/job/readreplica/rotate/serviceaccount.yaml index 5afae0eeab..b9d3ac302f 100644 --- a/charts/vald/templates/index/job/readreplica/rotate/serviceaccount.yaml +++ b/charts/vald/templates/index/job/readreplica/rotate/serviceaccount.yaml @@ -14,7 +14,8 @@ # limitations under the License. # {{- $rotator := .Values.manager.index.readreplica.rotator -}} -{{- if and $rotator.enabled $rotator.serviceAccount.enabled }} +{{- $readreplica := .Values.agent.readreplica -}} +{{- if and $readreplica.enabled $rotator.serviceAccount.enabled }} apiVersion: v1 kind: ServiceAccount metadata: From 434eee84d4d3f87cd4cfb11c1eaca6c0e6542f87 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Thu, 21 Dec 2023 07:44:24 +0000 Subject: [PATCH 05/14] Fix helm template --- .../templates/clusterrole.yaml | 21 ++++++++++++++++++- .../vald/templates/gateway/lb/configmap.yaml | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/charts/vald-helm-operator/templates/clusterrole.yaml b/charts/vald-helm-operator/templates/clusterrole.yaml index b027ea16a4..3555620617 100644 --- a/charts/vald-helm-operator/templates/clusterrole.yaml +++ b/charts/vald-helm-operator/templates/clusterrole.yaml @@ -187,5 +187,24 @@ rules: - patch - update - watch - + - apiGroups: + - snapshot.storage.k8s.io + resources: + - volumesnapshots + verbs: + - get + - list + - watch + - create + - delete + - apiGroups: + - "" + resources: + - persistentvolumeclaims + verbs: + - get + - list + - watch + - create + - delete {{- end }} diff --git a/charts/vald/templates/gateway/lb/configmap.yaml b/charts/vald/templates/gateway/lb/configmap.yaml index 348371132c..428437f4bb 100644 --- a/charts/vald/templates/gateway/lb/configmap.yaml +++ b/charts/vald/templates/gateway/lb/configmap.yaml @@ -70,7 +70,7 @@ data: {{- if $readreplica.enabled }} read_replica_client: client: - {{- $discovererClient := $gateway.gateway_config.read_replica_client.client }} + {{- $discovererClient := $gateway.gateway_config.discoverer.client }} {{- $readReplicaPort := $agent.server_config.servers.grpc.port }} {{- $defaultReadReplicaPort := default .Values.defaults.server_config.servers.grpc.port $readReplicaPort }} {{- $readReplicaAddrs := list }} From c2c2a1a501bdc66feff62cd108b8c67c23365143 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Fri, 22 Dec 2023 01:50:37 +0000 Subject: [PATCH 06/14] Set default replica replicas to 1 --- charts/vald/values.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/vald/values.yaml b/charts/vald/values.yaml index f97f20b34d..aa7c1956e4 100644 --- a/charts/vald/values.yaml +++ b/charts/vald/values.yaml @@ -1656,7 +1656,7 @@ agent: snapshot_classname: "" # @schema {"name": "agent.readreplica.replica", "type": "integer"} # agent.readreplica.replica -- replica number of read replica - replica: 2 + replica: 1 # @schema {"name": "agent.readreplica.service", "type": "object"} # agent.readreplica.service -- service settings for read replica service resources service: From 57d1b99b17c41108152a35fdca4eac5dd9798bee Mon Sep 17 00:00:00 2001 From: ykadowak Date: Fri, 22 Dec 2023 02:04:06 +0000 Subject: [PATCH 07/14] Fix deployment template --- charts/vald/templates/agent/readreplica/deployment.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/charts/vald/templates/agent/readreplica/deployment.yaml b/charts/vald/templates/agent/readreplica/deployment.yaml index 876182dbb2..dd45d1950c 100644 --- a/charts/vald/templates/agent/readreplica/deployment.yaml +++ b/charts/vald/templates/agent/readreplica/deployment.yaml @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # +{{- $values := .Values -}} {{- $agent := .Values.agent -}} {{- $readreplica := .Values.agent.readreplica -}} {{- $defaults := .Values.defaults -}} @@ -63,7 +64,7 @@ spec: {{- if $agent.initContainers }} initContainers: {{- if $agent.initContainers }} - {{- $initContainers := dict "initContainers" $agent.initContainers "Values" .Values "namespace" .Release.Namespace -}} + {{- $initContainers := dict "initContainers" $agent.initContainers "Values" $values "namespace" $release.Namespace -}} {{- include "vald.initContainers" $initContainers | trim | nindent 8 }} {{- end }} {{- end }} From b12f711fdfd8f267257038a0d38c96719f5e4019 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Fri, 22 Dec 2023 02:16:52 +0000 Subject: [PATCH 08/14] Add patch permission to cluster role --- charts/vald-helm-operator/templates/clusterrole.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/charts/vald-helm-operator/templates/clusterrole.yaml b/charts/vald-helm-operator/templates/clusterrole.yaml index 3555620617..ffd7cb4a86 100644 --- a/charts/vald-helm-operator/templates/clusterrole.yaml +++ b/charts/vald-helm-operator/templates/clusterrole.yaml @@ -197,6 +197,7 @@ rules: - watch - create - delete + - patch - apiGroups: - "" resources: From 202fdd8ac7ca9b89f82f02c38fb328056efd7be6 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Fri, 22 Dec 2023 04:40:39 +0000 Subject: [PATCH 09/14] Remove some comments --- internal/client/v1/client/discoverer/discover.go | 3 --- pkg/gateway/lb/service/gateway.go | 1 - 2 files changed, 4 deletions(-) diff --git a/internal/client/v1/client/discoverer/discover.go b/internal/client/v1/client/discoverer/discover.go index be0c365610..0656552971 100644 --- a/internal/client/v1/client/discoverer/discover.go +++ b/internal/client/v1/client/discoverer/discover.go @@ -205,14 +205,12 @@ func (c *client) GetReadClient() grpc.Client { } if new == 0 || c.readClient == nil { return c.client - // return c.readClient // DEBUG: } return c.readClient } func (c *client) connect(ctx context.Context, addr string) (err error) { if c.autoconn && c.client != nil { - // FIXME: この中で重複管理されている _, err = c.client.Connect(ctx, addr) if err != nil { return err @@ -362,7 +360,6 @@ func (c *client) discoverAddrs(ctx context.Context, nodes *payload.Info_Nodes, e len(node.GetPods().GetPods()) > i && len(node.GetPods().GetPods()[i].GetIp()) != 0 { addr := net.JoinHostPort(node.GetPods().GetPods()[i].GetIp(), uint16(c.port)) - // FIXME: ここで実際にgrpcサーバーへ接続する if err = c.connect(ctx, addr); err != nil { select { case <-ctx.Done(): diff --git a/pkg/gateway/lb/service/gateway.go b/pkg/gateway/lb/service/gateway.go index d0f2754725..70f24487c0 100644 --- a/pkg/gateway/lb/service/gateway.go +++ b/pkg/gateway/lb/service/gateway.go @@ -104,7 +104,6 @@ func (g *gateway) BroadCast(ctx context.Context, kind BroadCastKind, }) } -// DoMultiはWrite系しか使用しないので、普通にGetClientしていれば良い。現状Update or Insertのみ func (g *gateway) DoMulti(ctx context.Context, num int, f func(ctx context.Context, target string, ac vald.Client, copts ...grpc.CallOption) error, ) (err error) { From c77de67587773ff1df4c66250f7e75802294d9c4 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Fri, 22 Dec 2023 05:43:33 +0000 Subject: [PATCH 10/14] Add support for agent volumes in read replica deployment --- charts/vald/templates/agent/readreplica/deployment.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/charts/vald/templates/agent/readreplica/deployment.yaml b/charts/vald/templates/agent/readreplica/deployment.yaml index dd45d1950c..5a8ec76a0f 100644 --- a/charts/vald/templates/agent/readreplica/deployment.yaml +++ b/charts/vald/templates/agent/readreplica/deployment.yaml @@ -136,6 +136,9 @@ spec: - name: {{ $readreplica.volume_name }} persistentVolumeClaim: claimName: {{ $readreplica.name }}-pvc-{{ $id }} + {{- if $agent.volumes }} + {{- toYaml $agent.volumes | nindent 8 }} + {{- end }} {{- if $agent.nodeName }} nodeName: {{ $agent.nodeName }} {{- end }} From dca6ae2bda1be8951c7feeb09c9fbc9da0da30e7 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Fri, 22 Dec 2023 06:00:56 +0000 Subject: [PATCH 11/14] Update volumeSnapshotClassName in snapshot.yaml --- charts/vald/templates/agent/readreplica/snapshot.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/vald/templates/agent/readreplica/snapshot.yaml b/charts/vald/templates/agent/readreplica/snapshot.yaml index 6e3b8f1007..624eb27fd2 100644 --- a/charts/vald/templates/agent/readreplica/snapshot.yaml +++ b/charts/vald/templates/agent/readreplica/snapshot.yaml @@ -25,7 +25,7 @@ metadata: {{ $readreplica.label_key }}: "{{ $id }}" name: {{ $readreplica.name }}-snapshot-{{ $id }} spec: - volumeSnapshotClassName: csi-hostpath-snapclass + volumeSnapshotClassName: {{ $readreplica.snapshot_classname }} source: # Specify the PVCs for which you want to take snapshots persistentVolumeClaimName: {{ $agent.name }}-pvc-{{ $agent.name }}-{{ $id }} From 2fad0730dd5087f22ed8f3ea878042fd8ab6713a Mon Sep 17 00:00:00 2001 From: ykadowak Date: Fri, 22 Dec 2023 08:11:44 +0000 Subject: [PATCH 12/14] Add storageClassName to PVC.yaml --- charts/vald/templates/agent/readreplica/pvc.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/charts/vald/templates/agent/readreplica/pvc.yaml b/charts/vald/templates/agent/readreplica/pvc.yaml index c578e005d6..32182f0b4a 100644 --- a/charts/vald/templates/agent/readreplica/pvc.yaml +++ b/charts/vald/templates/agent/readreplica/pvc.yaml @@ -35,5 +35,6 @@ spec: name: {{ $readreplica.name }}-snapshot-{{ $id }} kind: VolumeSnapshot apiGroup: snapshot.storage.k8s.io + storageClassName: {{ $agent.persistentVolume.storageClass }} {{- end }} {{- end }} From 35208a9711cece4110d660c8235ea12ec312f66e Mon Sep 17 00:00:00 2001 From: ykadowak Date: Fri, 22 Dec 2023 08:24:28 +0000 Subject: [PATCH 13/14] Update storage size in read replica PVC template --- charts/vald/templates/agent/readreplica/pvc.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/vald/templates/agent/readreplica/pvc.yaml b/charts/vald/templates/agent/readreplica/pvc.yaml index 32182f0b4a..985ccb55b9 100644 --- a/charts/vald/templates/agent/readreplica/pvc.yaml +++ b/charts/vald/templates/agent/readreplica/pvc.yaml @@ -30,7 +30,7 @@ spec: - ReadOnlyMany resources: requests: - storage: 1Gi + storage: {{ $agent.persistentVolume.size }} dataSource: name: {{ $readreplica.name }}-snapshot-{{ $id }} kind: VolumeSnapshot From b04bc23d3be34c09ae829393ab0c06d024d09ae8 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Thu, 28 Dec 2023 03:06:35 +0000 Subject: [PATCH 14/14] Separate readreplica chart --- charts/vald-readreplica/.helmignore | 23 ++++++++++++++++++ charts/vald-readreplica/Chart.yaml | 24 +++++++++++++++++++ charts/vald-readreplica/templates/NOTES.txt | 4 ++++ .../vald-readreplica/templates/_helpers.tpl | 1 + .../templates}/deployment.yaml | 0 .../templates}/networkpolicy.yaml | 0 .../templates}/pvc.yaml | 0 .../templates}/snapshot.yaml | 0 .../templates}/svc.yaml | 0 charts/vald-readreplica/values.yaml | 1 + 10 files changed, 53 insertions(+) create mode 100644 charts/vald-readreplica/.helmignore create mode 100644 charts/vald-readreplica/Chart.yaml create mode 100644 charts/vald-readreplica/templates/NOTES.txt create mode 120000 charts/vald-readreplica/templates/_helpers.tpl rename charts/{vald/templates/agent/readreplica => vald-readreplica/templates}/deployment.yaml (100%) rename charts/{vald/templates/agent/readreplica => vald-readreplica/templates}/networkpolicy.yaml (100%) rename charts/{vald/templates/agent/readreplica => vald-readreplica/templates}/pvc.yaml (100%) rename charts/{vald/templates/agent/readreplica => vald-readreplica/templates}/snapshot.yaml (100%) rename charts/{vald/templates/agent/readreplica => vald-readreplica/templates}/svc.yaml (100%) create mode 120000 charts/vald-readreplica/values.yaml diff --git a/charts/vald-readreplica/.helmignore b/charts/vald-readreplica/.helmignore new file mode 100644 index 0000000000..0e8a0eb36f --- /dev/null +++ b/charts/vald-readreplica/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/charts/vald-readreplica/Chart.yaml b/charts/vald-readreplica/Chart.yaml new file mode 100644 index 0000000000..1b3c1c135c --- /dev/null +++ b/charts/vald-readreplica/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: vald-readreplica +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" diff --git a/charts/vald-readreplica/templates/NOTES.txt b/charts/vald-readreplica/templates/NOTES.txt new file mode 100644 index 0000000000..0563a59e9d --- /dev/null +++ b/charts/vald-readreplica/templates/NOTES.txt @@ -0,0 +1,4 @@ +Release {{ .Release.Name }} is created. + +Readreplica feature is intended to be used with the main vald chart. +Please look at the document of the main chart for more details. diff --git a/charts/vald-readreplica/templates/_helpers.tpl b/charts/vald-readreplica/templates/_helpers.tpl new file mode 120000 index 0000000000..edf15b30ba --- /dev/null +++ b/charts/vald-readreplica/templates/_helpers.tpl @@ -0,0 +1 @@ +../../vald/templates/_helpers.tpl \ No newline at end of file diff --git a/charts/vald/templates/agent/readreplica/deployment.yaml b/charts/vald-readreplica/templates/deployment.yaml similarity index 100% rename from charts/vald/templates/agent/readreplica/deployment.yaml rename to charts/vald-readreplica/templates/deployment.yaml diff --git a/charts/vald/templates/agent/readreplica/networkpolicy.yaml b/charts/vald-readreplica/templates/networkpolicy.yaml similarity index 100% rename from charts/vald/templates/agent/readreplica/networkpolicy.yaml rename to charts/vald-readreplica/templates/networkpolicy.yaml diff --git a/charts/vald/templates/agent/readreplica/pvc.yaml b/charts/vald-readreplica/templates/pvc.yaml similarity index 100% rename from charts/vald/templates/agent/readreplica/pvc.yaml rename to charts/vald-readreplica/templates/pvc.yaml diff --git a/charts/vald/templates/agent/readreplica/snapshot.yaml b/charts/vald-readreplica/templates/snapshot.yaml similarity index 100% rename from charts/vald/templates/agent/readreplica/snapshot.yaml rename to charts/vald-readreplica/templates/snapshot.yaml diff --git a/charts/vald/templates/agent/readreplica/svc.yaml b/charts/vald-readreplica/templates/svc.yaml similarity index 100% rename from charts/vald/templates/agent/readreplica/svc.yaml rename to charts/vald-readreplica/templates/svc.yaml diff --git a/charts/vald-readreplica/values.yaml b/charts/vald-readreplica/values.yaml new file mode 120000 index 0000000000..fd7949917f --- /dev/null +++ b/charts/vald-readreplica/values.yaml @@ -0,0 +1 @@ +../vald/values.yaml \ No newline at end of file