Skip to content
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

bugfix: add restart empty sd protection #1494

Merged
merged 9 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions datasource/etcd/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ func (ds *MetadataManager) CountEnvironment(ctx context.Context, request *ev.Get
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
return &ev.GetEnvironmentCountResponse{
Count: all - preEnvNum,
}, nil
Expand Down
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ github.com/armon/go-metrics v0.3.10 h1:FR+drcQStOe+32sYyJYyZ7FIdgoGGBnwLl+flodp8
github.com/armon/go-metrics v0.3.10/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
github.com/armon/go-radix v1.0.0/go.mod h1:TsTFsXBVHVK4HQ+UrFSsQEhBXZGCDqoY+cr+sUq5ZmA=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4B6AGu/h5Sxe66HYVdqdGu2l9Iebqhi/AEoA=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/aws/aws-sdk-go v1.34.28 h1:sscPpn/Ns3i0F4HPEWAVcwdIRaZZCuL7llJ2/60yPIk=
Expand Down
73 changes: 73 additions & 0 deletions pkg/protect/protect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package protect

import (
"fmt"
"net/http"
"time"

"github.com/apache/servicecomb-service-center/server/config"

"github.com/apache/servicecomb-service-center/pkg/log"
)

/**
for restart service center, set a restartProtectInterval time window to return RestartProtectHttpCode on discovery apis,
indicating that sdk not need to clear cache
*/

var (
isWithinProtection bool
startupTimestamp int64
enableInstanceNullProtect bool
restartProtectInterval time.Duration
RestartProtectHttpCode int
validProtectCode = map[int]struct{}{http.StatusNotModified: {}, http.StatusUnprocessableEntity: {}, http.StatusInternalServerError: {}}
)

const (
maxInterval = 120 * time.Second
minInterval = 0 * time.Second
defaultRestartProtectInterval = 120 * time.Second
)

func Init() {
enableInstanceNullProtect = config.GetBool("instance_null_protect.enable", false)
if !enableInstanceNullProtect {
return
}
restartProtectInterval = time.Duration(config.GetInt("instance_null_protect.restart_protect_interval", 120)) * time.Second
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enable为false的时候,直接退出,不用做初始化的操作了

if restartProtectInterval > maxInterval || restartProtectInterval < minInterval {
log.Warn(fmt.Sprintf("invalid instance_null_protect.restart_protect_interval: %d,"+
" must between %d-%ds inclusively", restartProtectInterval, minInterval, maxInterval))
restartProtectInterval = defaultRestartProtectInterval
}
RestartProtectHttpCode = config.GetInt("instance_null_protect.http_status", http.StatusNotModified)
if _, ok := validProtectCode[RestartProtectHttpCode]; !ok {
log.Warn(fmt.Sprintf("invalid instance_null_protect.http_status: %d, must be %v", RestartProtectHttpCode, validProtectCode))
RestartProtectHttpCode = http.StatusNotModified
}

log.Info(fmt.Sprintf("instance_null_protect.enable: %t", enableInstanceNullProtect))
log.Info(fmt.Sprintf("instance_null_protect.restart_protect_interval: %d", restartProtectInterval))
log.Info(fmt.Sprintf("instance_null_protect.http_status: %d", RestartProtectHttpCode))
startupTimestamp = time.Now().UnixNano()
isWithinProtection = true
}

func IsWithinRestartProtection() bool {
if !enableInstanceNullProtect {
return false
}

if !isWithinProtection {
return false
}

if time.Now().Add(-restartProtectInterval).UnixNano() > startupTimestamp {
log.Info("restart protection stop")
isWithinProtection = false
return false
}
log.Info("within restart protection")
return true
}
30 changes: 30 additions & 0 deletions pkg/protect/protect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package protect

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestIsWithinRestartProtection(t *testing.T) {
restartProtectInterval = 2 * time.Minute

// protection switch off
enableInstanceNullProtect = false
assert.False(t, IsWithinRestartProtection())
// within protection
enableInstanceNullProtect = true
isWithinProtection = true
startupTimestamp = time.Now().Add(-1 * time.Minute).UnixNano()
assert.True(t, IsWithinRestartProtection())

// protection delay exceed
enableInstanceNullProtect = true
isWithinProtection = true
startupTimestamp = time.Now().Add(-2 * time.Minute).Unix()
assert.False(t, IsWithinRestartProtection())

// always false after exceed
assert.False(t, IsWithinRestartProtection())
}
5 changes: 5 additions & 0 deletions pkg/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ package rpc
import (
"crypto/tls"
"errors"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/resolver"
Expand Down Expand Up @@ -50,9 +52,12 @@ func GetPickFirstLbConn(config *Config) (*grpc.ClientConn, error) {
}

func GetRoundRobinLbConn(config *Config) (*grpc.ClientConn, error) {
connBackoff := backoff.DefaultConfig
connBackoff.MaxDelay = 30 * time.Second
return getLbConn(config, func() []grpc.DialOption {
return []grpc.DialOption{
grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`),
grpc.WithConnectParams(grpc.ConnectParams{Backoff: connBackoff}),
}
})
}
Expand Down
12 changes: 11 additions & 1 deletion server/resource/disco/instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ package disco

import (
"fmt"
"github.com/apache/servicecomb-service-center/pkg/protect"
"io"
"net/http"
"strings"

"github.com/go-chassis/go-chassis/v2/pkg/codec"

pb "github.com/go-chassis/cari/discovery"

"github.com/apache/servicecomb-service-center/datasource"
"github.com/apache/servicecomb-service-center/pkg/log"
"github.com/apache/servicecomb-service-center/pkg/rest"
"github.com/apache/servicecomb-service-center/pkg/util"
discosvc "github.com/apache/servicecomb-service-center/server/service/disco"
pb "github.com/go-chassis/cari/discovery"
)

type InstanceResource struct {
Expand Down Expand Up @@ -167,6 +169,10 @@ func (s *InstanceResource) FindInstances(w http.ResponseWriter, r *http.Request)
w.WriteHeader(http.StatusNotModified)
return
}
if len(resp.Instances) == 0 && protect.IsWithinRestartProtection() {
w.WriteHeader(protect.RestartProtectHttpCode)
return
}
rest.WriteResponse(w, r, nil, resp)
}

Expand Down Expand Up @@ -266,6 +272,10 @@ func (s *InstanceResource) ListInstance(w http.ResponseWriter, r *http.Request)
w.WriteHeader(http.StatusNotModified)
return
}
if len(resp.Instances) == 0 && protect.IsWithinRestartProtection() {
w.WriteHeader(protect.RestartProtectHttpCode)
return
}
rest.WriteResponse(w, r, nil, resp)
}

Expand Down
8 changes: 6 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ package server
import (
"context"
"crypto/tls"
"github.com/apache/servicecomb-service-center/pkg/protect"
"os"

"github.com/gofiber/fiber/v2"

"github.com/apache/servicecomb-service-center/server/middleware"
"github.com/apache/servicecomb-service-center/server/resource/disco"
"github.com/gofiber/fiber/v2"

"github.com/go-chassis/go-chassis/v2"
chassisServer "github.com/go-chassis/go-chassis/v2/core/server"

"github.com/go-chassis/foundation/gopool"

"github.com/apache/servicecomb-service-center/datasource"
nf "github.com/apache/servicecomb-service-center/pkg/event"
"github.com/apache/servicecomb-service-center/pkg/log"
Expand All @@ -41,7 +45,6 @@ import (
"github.com/apache/servicecomb-service-center/server/plugin/security/tlsconf"
"github.com/apache/servicecomb-service-center/server/service/grc"
"github.com/apache/servicecomb-service-center/server/service/rbac"
"github.com/go-chassis/foundation/gopool"
)

var sc ServiceCenterServer
Expand Down Expand Up @@ -211,6 +214,7 @@ func (s *ServiceCenterServer) startServices() {

func (s *ServiceCenterServer) startAPIService() {
s.APIServer.SetHostPort(s.Endpoint.Host, s.Endpoint.Port)
protect.Init()
s.APIServer.Start()
}

Expand Down
5 changes: 3 additions & 2 deletions server/service/disco/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (
"sync"
"time"

pb "github.com/go-chassis/cari/discovery"
"github.com/go-chassis/cari/pkg/errsvc"

"github.com/apache/servicecomb-service-center/datasource"
"github.com/apache/servicecomb-service-center/pkg/log"
"github.com/apache/servicecomb-service-center/pkg/util"
Expand All @@ -33,8 +36,6 @@ import (
"github.com/apache/servicecomb-service-center/server/health"
quotasvc "github.com/apache/servicecomb-service-center/server/service/quota"
"github.com/apache/servicecomb-service-center/server/service/validator"
pb "github.com/go-chassis/cari/discovery"
"github.com/go-chassis/cari/pkg/errsvc"
)

const (
Expand Down
Loading