Browse code

libcontainerd: use healthcheck to track containerd conn

Signed-off-by: Alexander Morozov <lk4d4@docker.com>

Alexander Morozov authored on 2016/09/13 02:38:55
Showing 10 changed files
... ...
@@ -243,7 +243,7 @@ RUN set -x \
243 243
 	&& rm -rf "$GOPATH"
244 244
 
245 245
 # Install containerd
246
-ENV CONTAINERD_COMMIT 35a736c471ccd3ebfc7b80ceeb0ee303129acd61
246
+ENV CONTAINERD_COMMIT 4c21ad662f71af56c0e6b29c0afef72df441d1ff
247 247
 RUN set -x \
248 248
 	&& export GOPATH="$(mktemp -d)" \
249 249
 	&& git clone https://github.com/docker/containerd.git "$GOPATH/src/github.com/docker/containerd" \
... ...
@@ -186,7 +186,7 @@ RUN set -x \
186 186
 	&& rm -rf "$GOPATH"
187 187
 
188 188
 # Install containerd
189
-ENV CONTAINERD_COMMIT 35a736c471ccd3ebfc7b80ceeb0ee303129acd61
189
+ENV CONTAINERD_COMMIT 4c21ad662f71af56c0e6b29c0afef72df441d1ff
190 190
 RUN set -x \
191 191
 	&& export GOPATH="$(mktemp -d)" \
192 192
 	&& git clone https://github.com/docker/containerd.git "$GOPATH/src/github.com/docker/containerd" \
... ...
@@ -184,7 +184,7 @@ RUN set -x \
184 184
 	&& rm -rf "$GOPATH"
185 185
 
186 186
 # Install containerd
187
-ENV CONTAINERD_COMMIT 35a736c471ccd3ebfc7b80ceeb0ee303129acd61
187
+ENV CONTAINERD_COMMIT 4c21ad662f71af56c0e6b29c0afef72df441d1ff
188 188
 RUN set -x \
189 189
 	&& export GOPATH="$(mktemp -d)" \
190 190
 	&& git clone https://github.com/docker/containerd.git "$GOPATH/src/github.com/docker/containerd" \
... ...
@@ -204,7 +204,7 @@ RUN set -x \
204 204
 	&& rm -rf "$GOPATH"
205 205
 
206 206
 # Install containerd
207
-ENV CONTAINERD_COMMIT 35a736c471ccd3ebfc7b80ceeb0ee303129acd61
207
+ENV CONTAINERD_COMMIT 4c21ad662f71af56c0e6b29c0afef72df441d1ff
208 208
 RUN set -x \
209 209
 	&& export GOPATH="$(mktemp -d)" \
210 210
 	&& git clone https://github.com/docker/containerd.git "$GOPATH/src/github.com/docker/containerd" \
... ...
@@ -196,7 +196,7 @@ RUN set -x \
196 196
 	&& rm -rf "$GOPATH"
197 197
 
198 198
 # Install containerd
199
-ENV CONTAINERD_COMMIT 35a736c471ccd3ebfc7b80ceeb0ee303129acd61
199
+ENV CONTAINERD_COMMIT 4c21ad662f71af56c0e6b29c0afef72df441d1ff
200 200
 RUN set -x \
201 201
 	&& export GOPATH="$(mktemp -d)" \
202 202
 	&& git clone https://github.com/docker/containerd.git "$GOPATH/src/github.com/docker/containerd" \
... ...
@@ -68,7 +68,7 @@ RUN set -x \
68 68
 	&& rm -rf "$GOPATH"
69 69
 
70 70
 # Install containerd
71
-ENV CONTAINERD_COMMIT 35a736c471ccd3ebfc7b80ceeb0ee303129acd61
71
+ENV CONTAINERD_COMMIT 4c21ad662f71af56c0e6b29c0afef72df441d1ff
72 72
 RUN set -x \
73 73
 	&& export GOPATH="$(mktemp -d)" \
74 74
 	&& git clone https://github.com/docker/containerd.git "$GOPATH/src/github.com/docker/containerd" \
... ...
@@ -141,7 +141,7 @@ clone git google.golang.org/cloud dae7e3d993bc3812a2185af60552bb6b847e52a0 https
141 141
 clone git github.com/docker/docker-credential-helpers v0.3.0
142 142
 
143 143
 # containerd
144
-clone git github.com/docker/containerd 35a736c471ccd3ebfc7b80ceeb0ee303129acd61
144
+clone git github.com/docker/containerd 4c21ad662f71af56c0e6b29c0afef72df441d1ff
145 145
 
146 146
 # cluster
147 147
 clone git github.com/docker/swarmkit 27fbaef4ceed648bb575969ccc9083a6e104a719
... ...
@@ -25,18 +25,20 @@ import (
25 25
 	"golang.org/x/net/context"
26 26
 	"google.golang.org/grpc"
27 27
 	"google.golang.org/grpc/grpclog"
28
+	"google.golang.org/grpc/health/grpc_health_v1"
28 29
 	"google.golang.org/grpc/transport"
29 30
 )
30 31
 
31 32
 const (
32
-	maxConnectionRetryCount   = 3
33
-	connectionRetryDelay      = 3 * time.Second
34
-	containerdShutdownTimeout = 15 * time.Second
35
-	containerdBinary          = "docker-containerd"
36
-	containerdPidFilename     = "docker-containerd.pid"
37
-	containerdSockFilename    = "docker-containerd.sock"
38
-	containerdStateDir        = "containerd"
39
-	eventTimestampFilename    = "event.ts"
33
+	maxConnectionRetryCount      = 3
34
+	connectionRetryDelay         = 3 * time.Second
35
+	containerdHealthCheckTimeout = 3 * time.Second
36
+	containerdShutdownTimeout    = 15 * time.Second
37
+	containerdBinary             = "docker-containerd"
38
+	containerdPidFilename        = "docker-containerd.pid"
39
+	containerdSockFilename       = "docker-containerd.sock"
40
+	containerdStateDir           = "containerd"
41
+	eventTimestampFilename       = "event.ts"
40 42
 )
41 43
 
42 44
 type remote struct {
... ...
@@ -134,37 +136,41 @@ func (r *remote) UpdateOptions(options ...RemoteOption) error {
134 134
 
135 135
 func (r *remote) handleConnectionChange() {
136 136
 	var transientFailureCount = 0
137
-	state := grpc.Idle
137
+
138
+	ticker := time.NewTicker(500 * time.Millisecond)
139
+	defer ticker.Stop()
140
+	healthClient := grpc_health_v1.NewHealthClient(r.rpcConn)
141
+
138 142
 	for {
139
-		s, err := r.rpcConn.WaitForStateChange(context.Background(), state)
140
-		if err != nil {
141
-			break
143
+		<-ticker.C
144
+		ctx, cancel := context.WithTimeout(context.Background(), containerdHealthCheckTimeout)
145
+		_, err := healthClient.Check(ctx, &grpc_health_v1.HealthCheckRequest{})
146
+		cancel()
147
+		if err == nil {
148
+			continue
142 149
 		}
143
-		state = s
144
-		logrus.Debugf("libcontainerd: containerd connection state change: %v", s)
150
+
151
+		logrus.Debugf("libcontainerd: containerd health check returned error: %v", err)
145 152
 
146 153
 		if r.daemonPid != -1 {
147
-			switch state {
148
-			case grpc.TransientFailure:
149
-				// Reset state to be notified of next failure
150
-				transientFailureCount++
151
-				if transientFailureCount >= maxConnectionRetryCount {
152
-					transientFailureCount = 0
153
-					if utils.IsProcessAlive(r.daemonPid) {
154
-						utils.KillProcess(r.daemonPid)
155
-					}
156
-					<-r.daemonWaitCh
157
-					if err := r.runContainerdDaemon(); err != nil { //FIXME: Handle error
158
-						logrus.Errorf("libcontainerd: error restarting containerd: %v", err)
159
-					}
160
-				} else {
161
-					state = grpc.Idle
162
-					time.Sleep(connectionRetryDelay)
163
-				}
164
-			case grpc.Shutdown:
154
+			if strings.Contains(err.Error(), "is closing") {
165 155
 				// Well, we asked for it to stop, just return
166 156
 				return
167 157
 			}
158
+			// all other errors are transient
159
+			// Reset state to be notified of next failure
160
+			transientFailureCount++
161
+			if transientFailureCount >= maxConnectionRetryCount {
162
+				transientFailureCount = 0
163
+				if utils.IsProcessAlive(r.daemonPid) {
164
+					utils.KillProcess(r.daemonPid)
165
+				}
166
+				<-r.daemonWaitCh
167
+				if err := r.runContainerdDaemon(); err != nil { //FIXME: Handle error
168
+					logrus.Errorf("libcontainerd: error restarting containerd: %v", err)
169
+				}
170
+				continue
171
+			}
168 172
 		}
169 173
 	}
170 174
 }
171 175
new file mode 100644
... ...
@@ -0,0 +1,172 @@
0
+// Code generated by protoc-gen-go.
1
+// source: health/grpc_health_v1/health.proto
2
+// DO NOT EDIT!
3
+
4
+/*
5
+Package grpc_health_v1 is a generated protocol buffer package.
6
+
7
+It is generated from these files:
8
+	health/grpc_health_v1/health.proto
9
+
10
+It has these top-level messages:
11
+	HealthCheckRequest
12
+	HealthCheckResponse
13
+*/
14
+package grpc_health_v1
15
+
16
+import proto "github.com/golang/protobuf/proto"
17
+import fmt "fmt"
18
+import math "math"
19
+
20
+import (
21
+	context "golang.org/x/net/context"
22
+	grpc "google.golang.org/grpc"
23
+)
24
+
25
+// Reference imports to suppress errors if they are not otherwise used.
26
+var _ = proto.Marshal
27
+var _ = fmt.Errorf
28
+var _ = math.Inf
29
+
30
+// This is a compile-time assertion to ensure that this generated file
31
+// is compatible with the proto package it is being compiled against.
32
+const _ = proto.ProtoPackageIsVersion1
33
+
34
+type HealthCheckResponse_ServingStatus int32
35
+
36
+const (
37
+	HealthCheckResponse_UNKNOWN     HealthCheckResponse_ServingStatus = 0
38
+	HealthCheckResponse_SERVING     HealthCheckResponse_ServingStatus = 1
39
+	HealthCheckResponse_NOT_SERVING HealthCheckResponse_ServingStatus = 2
40
+)
41
+
42
+var HealthCheckResponse_ServingStatus_name = map[int32]string{
43
+	0: "UNKNOWN",
44
+	1: "SERVING",
45
+	2: "NOT_SERVING",
46
+}
47
+var HealthCheckResponse_ServingStatus_value = map[string]int32{
48
+	"UNKNOWN":     0,
49
+	"SERVING":     1,
50
+	"NOT_SERVING": 2,
51
+}
52
+
53
+func (x HealthCheckResponse_ServingStatus) String() string {
54
+	return proto.EnumName(HealthCheckResponse_ServingStatus_name, int32(x))
55
+}
56
+func (HealthCheckResponse_ServingStatus) EnumDescriptor() ([]byte, []int) {
57
+	return fileDescriptor0, []int{1, 0}
58
+}
59
+
60
+type HealthCheckRequest struct {
61
+	Service string `protobuf:"bytes,1,opt,name=service" json:"service,omitempty"`
62
+}
63
+
64
+func (m *HealthCheckRequest) Reset()                    { *m = HealthCheckRequest{} }
65
+func (m *HealthCheckRequest) String() string            { return proto.CompactTextString(m) }
66
+func (*HealthCheckRequest) ProtoMessage()               {}
67
+func (*HealthCheckRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
68
+
69
+type HealthCheckResponse struct {
70
+	Status HealthCheckResponse_ServingStatus `protobuf:"varint,1,opt,name=status,enum=grpc.health.v1.HealthCheckResponse_ServingStatus" json:"status,omitempty"`
71
+}
72
+
73
+func (m *HealthCheckResponse) Reset()                    { *m = HealthCheckResponse{} }
74
+func (m *HealthCheckResponse) String() string            { return proto.CompactTextString(m) }
75
+func (*HealthCheckResponse) ProtoMessage()               {}
76
+func (*HealthCheckResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
77
+
78
+func init() {
79
+	proto.RegisterType((*HealthCheckRequest)(nil), "grpc.health.v1.HealthCheckRequest")
80
+	proto.RegisterType((*HealthCheckResponse)(nil), "grpc.health.v1.HealthCheckResponse")
81
+	proto.RegisterEnum("grpc.health.v1.HealthCheckResponse_ServingStatus", HealthCheckResponse_ServingStatus_name, HealthCheckResponse_ServingStatus_value)
82
+}
83
+
84
+// Reference imports to suppress errors if they are not otherwise used.
85
+var _ context.Context
86
+var _ grpc.ClientConn
87
+
88
+// This is a compile-time assertion to ensure that this generated file
89
+// is compatible with the grpc package it is being compiled against.
90
+const _ = grpc.SupportPackageIsVersion2
91
+
92
+// Client API for Health service
93
+
94
+type HealthClient interface {
95
+	Check(ctx context.Context, in *HealthCheckRequest, opts ...grpc.CallOption) (*HealthCheckResponse, error)
96
+}
97
+
98
+type healthClient struct {
99
+	cc *grpc.ClientConn
100
+}
101
+
102
+func NewHealthClient(cc *grpc.ClientConn) HealthClient {
103
+	return &healthClient{cc}
104
+}
105
+
106
+func (c *healthClient) Check(ctx context.Context, in *HealthCheckRequest, opts ...grpc.CallOption) (*HealthCheckResponse, error) {
107
+	out := new(HealthCheckResponse)
108
+	err := grpc.Invoke(ctx, "/grpc.health.v1.Health/Check", in, out, c.cc, opts...)
109
+	if err != nil {
110
+		return nil, err
111
+	}
112
+	return out, nil
113
+}
114
+
115
+// Server API for Health service
116
+
117
+type HealthServer interface {
118
+	Check(context.Context, *HealthCheckRequest) (*HealthCheckResponse, error)
119
+}
120
+
121
+func RegisterHealthServer(s *grpc.Server, srv HealthServer) {
122
+	s.RegisterService(&_Health_serviceDesc, srv)
123
+}
124
+
125
+func _Health_Check_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
126
+	in := new(HealthCheckRequest)
127
+	if err := dec(in); err != nil {
128
+		return nil, err
129
+	}
130
+	if interceptor == nil {
131
+		return srv.(HealthServer).Check(ctx, in)
132
+	}
133
+	info := &grpc.UnaryServerInfo{
134
+		Server:     srv,
135
+		FullMethod: "/grpc.health.v1.Health/Check",
136
+	}
137
+	handler := func(ctx context.Context, req interface{}) (interface{}, error) {
138
+		return srv.(HealthServer).Check(ctx, req.(*HealthCheckRequest))
139
+	}
140
+	return interceptor(ctx, in, info, handler)
141
+}
142
+
143
+var _Health_serviceDesc = grpc.ServiceDesc{
144
+	ServiceName: "grpc.health.v1.Health",
145
+	HandlerType: (*HealthServer)(nil),
146
+	Methods: []grpc.MethodDesc{
147
+		{
148
+			MethodName: "Check",
149
+			Handler:    _Health_Check_Handler,
150
+		},
151
+	},
152
+	Streams: []grpc.StreamDesc{},
153
+}
154
+
155
+var fileDescriptor0 = []byte{
156
+	// 209 bytes of a gzipped FileDescriptorProto
157
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x52, 0xca, 0x48, 0x4d, 0xcc,
158
+	0x29, 0xc9, 0xd0, 0x4f, 0x2f, 0x2a, 0x48, 0x8e, 0x87, 0xb0, 0xe3, 0xcb, 0x0c, 0xf5, 0x21, 0x2c,
159
+	0xbd, 0x82, 0xa2, 0xfc, 0x92, 0x7c, 0x21, 0x3e, 0x90, 0xa4, 0x1e, 0x54, 0xa8, 0xcc, 0x50, 0x49,
160
+	0x95, 0x4b, 0xc8, 0x03, 0xcc, 0x71, 0xce, 0x48, 0x4d, 0xce, 0x0e, 0x4a, 0x2d, 0x2c, 0x4d, 0x2d,
161
+	0x2e, 0x11, 0xe2, 0xe7, 0x62, 0x2f, 0x4e, 0x2d, 0x2a, 0xcb, 0x4c, 0x4e, 0x95, 0x60, 0x54, 0x60,
162
+	0xd4, 0xe0, 0x54, 0x9a, 0xc2, 0xc8, 0x25, 0x8c, 0xa2, 0xae, 0xb8, 0x20, 0x3f, 0xaf, 0x38, 0x55,
163
+	0xc8, 0x91, 0x8b, 0xad, 0xb8, 0x24, 0xb1, 0xa4, 0xb4, 0x18, 0xac, 0x8e, 0xcf, 0xc8, 0x50, 0x0f,
164
+	0xd5, 0x7c, 0x3d, 0x2c, 0x9a, 0xf4, 0x82, 0x41, 0x46, 0xe7, 0xa5, 0x07, 0x83, 0x35, 0x2a, 0x59,
165
+	0x71, 0xf1, 0xa2, 0x08, 0x08, 0x71, 0x73, 0xb1, 0x87, 0xfa, 0x79, 0xfb, 0xf9, 0x87, 0xfb, 0x09,
166
+	0x30, 0x80, 0x38, 0xc1, 0xae, 0x41, 0x61, 0x9e, 0x7e, 0xee, 0x02, 0x8c, 0x40, 0x67, 0x71, 0xfb,
167
+	0xf9, 0x87, 0xc4, 0xc3, 0x04, 0x98, 0x8c, 0xa2, 0xb8, 0xd8, 0x20, 0x16, 0x08, 0x05, 0x70, 0xb1,
168
+	0x82, 0x2d, 0x11, 0x52, 0xc2, 0xeb, 0x02, 0xb0, 0xf7, 0xa4, 0x94, 0x89, 0x70, 0x65, 0x12, 0x1b,
169
+	0x38, 0xc0, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xa9, 0xf7, 0x2e, 0x1f, 0x56, 0x01, 0x00,
170
+	0x00,
171
+}
0 172
new file mode 100644
... ...
@@ -0,0 +1,20 @@
0
+syntax = "proto3";
1
+
2
+package grpc.health.v1;
3
+
4
+message HealthCheckRequest {
5
+  string service = 1;
6
+}
7
+
8
+message HealthCheckResponse {
9
+  enum ServingStatus {
10
+ 	UNKNOWN = 0;
11
+	SERVING = 1;
12
+	NOT_SERVING = 2;
13
+  }
14
+  ServingStatus status = 1;
15
+}
16
+
17
+service Health{
18
+  rpc Check(HealthCheckRequest) returns (HealthCheckResponse);
19
+}