Skip to content

Commit

Permalink
Finish Test
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash committed Nov 15, 2019
1 parent a993531 commit af9741a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 29 deletions.
1 change: 1 addition & 0 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ const (
ProtocolConfigPrefix = "dubbo.protocols."
ProviderConfigPrefix = "dubbo.provider."
ConsumerConfigPrefix = "dubbo.consumer."
ShutdownConfigPrefix = "dubbo.shutdown."
)

const (
Expand Down
8 changes: 8 additions & 0 deletions config/base_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func Test_refresh(t *testing.T) {
mockMap["dubbo.com.MockService.MockService.GetUser.retries"] = "10"
mockMap["dubbo.consumer.check"] = "false"
mockMap["dubbo.application.name"] = "dubbo"
mockMap["dubbo.shutdown.timeout"] = "12s"

config.GetEnvInstance().UpdateExternalConfigMap(mockMap)

Expand Down Expand Up @@ -113,6 +114,13 @@ func Test_refresh(t *testing.T) {
},
},
},
ShutdownConfig: &ShutdownConfig{
Timeout: "12s",
StepTimeout: "2s",
RejectRequestHandler: "mock",
RejectRequest: false,
RequestsFinished: false,
},
}

c.SetFatherConfig(father)
Expand Down
41 changes: 21 additions & 20 deletions config/graceful_shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,31 +79,14 @@ func GracefulShutdownInit() {
}()
}

func totalTimeout() time.Duration {
var providerShutdown time.Duration = 0
if providerConfig != nil && providerConfig.ShutdownConfig != nil {
providerShutdown = providerConfig.ShutdownConfig.GetTimeout()
}

var consumerShutdown time.Duration = 0
if consumerConfig != nil && consumerConfig.ShutdownConfig != nil {
consumerShutdown = consumerConfig.ShutdownConfig.GetTimeout()
}

var timeout = providerShutdown
if consumerShutdown > providerShutdown {
timeout = consumerShutdown
}
return timeout
}

func BeforeShutdown() {

destroyAllRegistries()
// waiting for a short time so that the clients have enough time to get the notification that server shutdowns
// The value of configuration depends on how long the clients will get notification.
waitAndAcceptNewRequests()

time.Sleep(1 * time.Minute)
// reject the new request, but keeping waiting for accepting requests
waitForReceivingRequests()

Expand All @@ -116,15 +99,15 @@ func BeforeShutdown() {
// If this application is not the consumer, it will do nothing
destroyConsumerProtocols()

logger.Infof("Graceful shutdown --- Execute the custom callbacks.")
logger.Info("Graceful shutdown --- Execute the custom callbacks.")
customCallbacks := extension.GetAllCustomShutdownCallbacks()
for callback := customCallbacks.Front(); callback != nil; callback = callback.Next() {
callback.Value.(func())()
}
}

func destroyAllRegistries() {
logger.Infof("Graceful shutdown --- Destroy all registries. ")
logger.Info("Graceful shutdown --- Destroy all registries. ")
registryProtocol := extension.GetProtocol(constant.REGISTRY_KEY)
registryProtocol.Destroy()
}
Expand Down Expand Up @@ -216,3 +199,21 @@ func waitingProcessedTimeout(shutdownConfig *ShutdownConfig) {
time.Sleep(10 * time.Millisecond)
}
}

func totalTimeout() time.Duration {
var providerShutdown time.Duration = 0
if providerConfig != nil && providerConfig.ShutdownConfig != nil {
providerShutdown = providerConfig.ShutdownConfig.GetTimeout()
}

var consumerShutdown time.Duration = 0
if consumerConfig != nil && consumerConfig.ShutdownConfig != nil {
consumerShutdown = consumerConfig.ShutdownConfig.GetTimeout()
}

var timeout = providerShutdown
if consumerShutdown > providerShutdown {
timeout = consumerShutdown
}
return timeout
}
5 changes: 5 additions & 0 deletions config/graceful_shutdown_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"
)
import (
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/logger"
)

Expand Down Expand Up @@ -55,6 +56,10 @@ type ShutdownConfig struct {
RequestsFinished bool
}

func (config *ShutdownConfig) Prefix() string {
return constant.ShutdownConfigPrefix
}

func (config *ShutdownConfig) GetTimeout() time.Duration {
result, err := time.ParseDuration(config.Timeout)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion registry/zookeeper/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ func (l *RegistryConfigurationListener) Next() (*registry.ServiceEvent, error) {
}
}
func (l *RegistryConfigurationListener) Close() {
l.registry.wg.Done()
if l.registry.IsAvailable() {
/**
* if the registry is not available, it means that the registry has been destroy
* so we don't need to call Done(), or it will cause the negative count panic for registry.wg
*/
l.registry.wg.Done()
}
}

func (l *RegistryConfigurationListener) valid() bool {
Expand Down
11 changes: 3 additions & 8 deletions registry/zookeeper/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,9 @@ func (r *zkRegistry) GetUrl() common.URL {
}

func (r *zkRegistry) Destroy() {
/**
* Don't r.listener.Close()
* here we don't close the listener because
* the listener will be close in Subscribe().
* We can not close it here. If we do that,
* a negative count error of r.wg will occur because
* we close the listener twice.
*/
if r.configListener != nil {
r.configListener.Close()
}
close(r.done)
r.wg.Wait()
r.closeRegisters()
Expand Down

0 comments on commit af9741a

Please sign in to comment.