-
Notifications
You must be signed in to change notification settings - Fork 9.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[3.5] gRPC health server sets serving status to NOT_SERVING on defrag #17914
Merged
serathius
merged 2 commits into
etcd-io:release-3.5
from
tjungblu:backport_35_grpc_defragserv
May 7, 2024
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2023 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package v3rpc | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc/health" | ||
healthpb "google.golang.org/grpc/health/grpc_health_v1" | ||
) | ||
|
||
const ( | ||
allGRPCServices = "" | ||
) | ||
|
||
type HealthNotifier interface { | ||
StartServe() | ||
StopServe(reason string) | ||
} | ||
|
||
func NewHealthNotifier(hs *health.Server, lg *zap.Logger) HealthNotifier { | ||
if hs == nil { | ||
panic("unexpected nil gRPC health server") | ||
} | ||
if lg == nil { | ||
lg = zap.NewNop() | ||
} | ||
hc := &healthChecker{hs: hs, lg: lg} | ||
// set grpc health server as serving status blindly since | ||
// the grpc server will serve iff s.ReadyNotify() is closed. | ||
hc.StartServe() | ||
return hc | ||
} | ||
|
||
type healthChecker struct { | ||
hs *health.Server | ||
lg *zap.Logger | ||
} | ||
|
||
func (hc *healthChecker) StartServe() { | ||
hc.lg.Info( | ||
"grpc service status changed", | ||
zap.String("service", allGRPCServices), | ||
zap.String("status", healthpb.HealthCheckResponse_SERVING.String()), | ||
) | ||
hc.hs.SetServingStatus(allGRPCServices, healthpb.HealthCheckResponse_SERVING) | ||
} | ||
|
||
func (hc *healthChecker) StopServe(reason string) { | ||
hc.lg.Warn( | ||
"grpc service status changed", | ||
zap.String("service", allGRPCServices), | ||
zap.String("status", healthpb.HealthCheckResponse_NOT_SERVING.String()), | ||
zap.String("reason", reason), | ||
) | ||
hc.hs.SetServingStatus(allGRPCServices, healthpb.HealthCheckResponse_NOT_SERVING) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// Copyright 2023 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build !cluster_proxy | ||
|
||
package e2e | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"golang.org/x/sync/errgroup" | ||
"google.golang.org/grpc" | ||
_ "google.golang.org/grpc/health" | ||
|
||
clientv3 "go.etcd.io/etcd/client/v3" | ||
"go.etcd.io/etcd/tests/v3/framework/e2e" | ||
) | ||
|
||
const ( | ||
// in sync with how kubernetes uses etcd | ||
// https://github.com/kubernetes/kubernetes/blob/release-1.28/staging/src/k8s.io/apiserver/pkg/storage/storagebackend/factory/etcd3.go#L59-L71 | ||
keepaliveTime = 30 * time.Second | ||
keepaliveTimeout = 10 * time.Second | ||
dialTimeout = 20 * time.Second | ||
|
||
clientRuntime = 10 * time.Second | ||
requestTimeout = 100 * time.Millisecond | ||
) | ||
|
||
func TestFailoverOnDefrag(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
|
||
experimentalStopGRPCServiceOnDefragEnabled bool | ||
gRPCDialOptions []grpc.DialOption | ||
|
||
// common assertion | ||
expectedMinTotalRequestsCount int | ||
// happy case assertion | ||
expectedMaxFailedRequestsCount int | ||
// negative case assertion | ||
expectedMinFailedRequestsCount int | ||
}{ | ||
{ | ||
name: "defrag failover happy case", | ||
experimentalStopGRPCServiceOnDefragEnabled: true, | ||
gRPCDialOptions: []grpc.DialOption{ | ||
grpc.WithDisableServiceConfig(), | ||
grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin", "healthCheckConfig": {"serviceName": ""}}`), | ||
}, | ||
expectedMinTotalRequestsCount: 300, | ||
expectedMaxFailedRequestsCount: 5, | ||
}, | ||
{ | ||
name: "defrag blocks one-third of requests with stopGRPCServiceOnDefrag set to false", | ||
experimentalStopGRPCServiceOnDefragEnabled: false, | ||
gRPCDialOptions: []grpc.DialOption{ | ||
grpc.WithDisableServiceConfig(), | ||
grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin", "healthCheckConfig": {"serviceName": ""}}`), | ||
}, | ||
expectedMinTotalRequestsCount: 300, | ||
expectedMinFailedRequestsCount: 90, | ||
}, | ||
{ | ||
name: "defrag blocks one-third of requests with stopGRPCServiceOnDefrag set to true and client health check disabled", | ||
experimentalStopGRPCServiceOnDefragEnabled: true, | ||
expectedMinTotalRequestsCount: 300, | ||
expectedMinFailedRequestsCount: 90, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
e2e.BeforeTest(t) | ||
cfg := e2e.EtcdProcessClusterConfig{ | ||
ClusterSize: 3, | ||
GoFailEnabled: true, | ||
ExperimentalStopGRPCServiceOnDefrag: tc.experimentalStopGRPCServiceOnDefragEnabled, | ||
} | ||
clus, err := e2e.NewEtcdProcessCluster(t, &cfg) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { clus.Stop() }) | ||
|
||
endpoints := clus.EndpointsGRPC() | ||
|
||
requestVolume, successfulRequestCount := 0, 0 | ||
g := new(errgroup.Group) | ||
g.Go(func() (lastErr error) { | ||
clusterClient, cerr := clientv3.New(clientv3.Config{ | ||
DialTimeout: dialTimeout, | ||
DialKeepAliveTime: keepaliveTime, | ||
DialKeepAliveTimeout: keepaliveTimeout, | ||
Endpoints: endpoints, | ||
DialOptions: tc.gRPCDialOptions, | ||
}) | ||
if cerr != nil { | ||
return cerr | ||
} | ||
defer clusterClient.Close() | ||
|
||
timeout := time.After(clientRuntime) | ||
for { | ||
select { | ||
case <-timeout: | ||
return lastErr | ||
default: | ||
} | ||
getContext, cancel := context.WithTimeout(context.Background(), requestTimeout) | ||
_, err := clusterClient.Get(getContext, "health") | ||
cancel() | ||
requestVolume++ | ||
if err != nil { | ||
lastErr = err | ||
continue | ||
} | ||
successfulRequestCount++ | ||
} | ||
}) | ||
|
||
triggerDefrag(t, clus.Procs[0]) | ||
|
||
err = g.Wait() | ||
if err != nil { | ||
t.Logf("etcd client failed to fail over, error (%v)", err) | ||
} | ||
t.Logf("request failure rate is %.2f%%, traffic volume successfulRequestCount %d requests, total %d requests", (1-float64(successfulRequestCount)/float64(requestVolume))*100, successfulRequestCount, requestVolume) | ||
|
||
require.GreaterOrEqual(t, requestVolume, tc.expectedMinTotalRequestsCount) | ||
failedRequestCount := requestVolume - successfulRequestCount | ||
if tc.expectedMaxFailedRequestsCount != 0 { | ||
require.LessOrEqual(t, failedRequestCount, tc.expectedMaxFailedRequestsCount) | ||
} | ||
if tc.expectedMinFailedRequestsCount != 0 { | ||
require.GreaterOrEqual(t, failedRequestCount, tc.expectedMinFailedRequestsCount) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func triggerDefrag(t *testing.T, member e2e.EtcdProcess) { | ||
require.NoError(t, member.Failpoints().SetupHTTP(context.Background(), "defragBeforeCopy", `sleep("10s")`)) | ||
require.NoError(t, member.Etcdctl(e2e.ClientNonTLS, false, false).Defragment(time.Minute)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding
experimental
prefix and removing it in future is a breaking change as we discussed in #17657. Also see #17657Are we still happy to add prefix
experimental
for an flag?I know this PR just backports changes from main to 3.5, so the suggestion (adding
experimental
in description only) is actually for the main branch (update main firstly). Do we want to follow the suggestion for now? Or do we have an agreement on #17657 and #17657 before we add any new flags? @fuweid @jmhbnz @serathius @siyuanfoundation @spzalaThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As part of #17657 we will still need to support migration from the old flag naming scheme (with prefix) to the new one. One more flag should not change much as we already have other flags to migrate. So I don't think we need to block on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy for this to proceed using older approach. We are still working on the KEP for feature flags so it will be a while off and agree with @serathius above we don't want to block this work.