Browse code

Merge pull request #31142 from dongluochen/network_converge_delay

add 2 seconds delay to allow gossip converge

Aaron Lehmann authored on 2017/05/12 08:50:44
Showing 1 changed files
... ...
@@ -21,6 +21,8 @@ import (
21 21
 	"golang.org/x/time/rate"
22 22
 )
23 23
 
24
+const defaultGossipConvergeDelay = 2 * time.Second
25
+
24 26
 // controller implements agent.Controller against docker's API.
25 27
 //
26 28
 // Most operations against docker's API are done through the container name,
... ...
@@ -301,6 +303,24 @@ func (r *controller) Wait(pctx context.Context) error {
301 301
 	return nil
302 302
 }
303 303
 
304
+func (r *controller) hasServiceBinding() bool {
305
+	if r.task == nil {
306
+		return false
307
+	}
308
+
309
+	// service is attached to a network besides the default bridge
310
+	for _, na := range r.task.Networks {
311
+		if na.Network == nil ||
312
+			na.Network.DriverState == nil ||
313
+			na.Network.DriverState.Name == "bridge" && na.Network.Spec.Annotations.Name == "bridge" {
314
+			continue
315
+		}
316
+		return true
317
+	}
318
+
319
+	return false
320
+}
321
+
304 322
 // Shutdown the container cleanly.
305 323
 func (r *controller) Shutdown(ctx context.Context) error {
306 324
 	if err := r.checkClosed(); err != nil {
... ...
@@ -311,12 +331,18 @@ func (r *controller) Shutdown(ctx context.Context) error {
311 311
 		r.cancelPull()
312 312
 	}
313 313
 
314
-	// remove container from service binding
315
-	if err := r.adapter.deactivateServiceBinding(); err != nil {
316
-		log.G(ctx).WithError(err).Warningf("failed to deactivate service binding for container %s", r.adapter.container.name())
317
-		// Don't return an error here, because failure to deactivate
318
-		// the service binding is expected if the container was never
319
-		// started.
314
+	if r.hasServiceBinding() {
315
+		// remove container from service binding
316
+		if err := r.adapter.deactivateServiceBinding(); err != nil {
317
+			log.G(ctx).WithError(err).Warningf("failed to deactivate service binding for container %s", r.adapter.container.name())
318
+			// Don't return an error here, because failure to deactivate
319
+			// the service binding is expected if the container was never
320
+			// started.
321
+		}
322
+
323
+		// add a delay for gossip converge
324
+		// TODO(dongluochen): this delay shoud be configurable to fit different cluster size and network delay.
325
+		time.Sleep(defaultGossipConvergeDelay)
320 326
 	}
321 327
 
322 328
 	if err := r.adapter.shutdown(ctx); err != nil {