Commit 27c2f5fb authored by k8s-merge-robot's avatar k8s-merge-robot

Merge pull request #17526 from yifan-gu/rkt_api_service

Auto commit by PR queue bot
parents acf1d57d 5b423dd4
...@@ -301,6 +301,11 @@ ...@@ -301,6 +301,11 @@
"Rev": "fa94270d4bac0d8ae5dc6b71894e251aada93f74" "Rev": "fa94270d4bac0d8ae5dc6b71894e251aada93f74"
}, },
{ {
"ImportPath": "github.com/coreos/rkt/api/v1alpha",
"Comment": "v0.11.0-22-g71d7331",
"Rev": "71d7331e46f20aaa41bed05e861bfeca92249968"
},
{
"ImportPath": "github.com/cpuguy83/go-md2man/md2man", "ImportPath": "github.com/cpuguy83/go-md2man/md2man",
"Comment": "v1.0.4", "Comment": "v1.0.4",
"Rev": "71acacd42f85e5e82f70a55327789582a5200a90" "Rev": "71acacd42f85e5e82f70a55327789582a5200a90"
...@@ -748,6 +753,14 @@ ...@@ -748,6 +753,14 @@
"Rev": "c2528b2dd8352441850638a8bb678c2ad056fd3e" "Rev": "c2528b2dd8352441850638a8bb678c2ad056fd3e"
}, },
{ {
"ImportPath": "golang.org/x/net/internal/timeseries",
"Rev": "c2528b2dd8352441850638a8bb678c2ad056fd3e"
},
{
"ImportPath": "golang.org/x/net/trace",
"Rev": "c2528b2dd8352441850638a8bb678c2ad056fd3e"
},
{
"ImportPath": "golang.org/x/net/websocket", "ImportPath": "golang.org/x/net/websocket",
"Rev": "c2528b2dd8352441850638a8bb678c2ad056fd3e" "Rev": "c2528b2dd8352441850638a8bb678c2ad056fd3e"
}, },
...@@ -789,7 +802,7 @@ ...@@ -789,7 +802,7 @@
}, },
{ {
"ImportPath": "google.golang.org/grpc", "ImportPath": "google.golang.org/grpc",
"Rev": "f5ebd86be717593ab029545492c93ddf8914832b" "Rev": "4bd040ce23a624ff9a1d07b0e729ee189bddd51c"
}, },
{ {
"ImportPath": "gopkg.in/natefinch/lumberjack.v2", "ImportPath": "gopkg.in/natefinch/lumberjack.v2",
......
# WARNING
The API defined here is proposed, experimental, and (for now) subject to change at any time.
**Do not use it.**
If you think you want to use it, or for any other queries, contact <rkt-dev@googlegroups.com> or file an [issue](https://github.com/coreos/rkt/issues/new)
For more information, see:
- #1208
- #1359
- #1468
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package trace
// This file implements histogramming for RPC statistics collection.
import (
"bytes"
"fmt"
"html/template"
"log"
"math"
"golang.org/x/net/internal/timeseries"
)
const (
bucketCount = 38
)
// histogram keeps counts of values in buckets that are spaced
// out in powers of 2: 0-1, 2-3, 4-7...
// histogram implements timeseries.Observable
type histogram struct {
sum int64 // running total of measurements
sumOfSquares float64 // square of running total
buckets []int64 // bucketed values for histogram
value int // holds a single value as an optimization
valueCount int64 // number of values recorded for single value
}
// AddMeasurement records a value measurement observation to the histogram.
func (h *histogram) addMeasurement(value int64) {
// TODO: assert invariant
h.sum += value
h.sumOfSquares += float64(value) * float64(value)
bucketIndex := getBucket(value)
if h.valueCount == 0 || (h.valueCount > 0 && h.value == bucketIndex) {
h.value = bucketIndex
h.valueCount++
} else {
h.allocateBuckets()
h.buckets[bucketIndex]++
}
}
func (h *histogram) allocateBuckets() {
if h.buckets == nil {
h.buckets = make([]int64, bucketCount)
h.buckets[h.value] = h.valueCount
h.value = 0
h.valueCount = -1
}
}
func log2(i int64) int {
n := 0
for ; i >= 0x100; i >>= 8 {
n += 8
}
for ; i > 0; i >>= 1 {
n += 1
}
return n
}
func getBucket(i int64) (index int) {
index = log2(i) - 1
if index < 0 {
index = 0
}
if index >= bucketCount {
index = bucketCount - 1
}
return
}
// Total returns the number of recorded observations.
func (h *histogram) total() (total int64) {
if h.valueCount >= 0 {
total = h.valueCount
}
for _, val := range h.buckets {
total += int64(val)
}
return
}
// Average returns the average value of recorded observations.
func (h *histogram) average() float64 {
t := h.total()
if t == 0 {
return 0
}
return float64(h.sum) / float64(t)
}
// Variance returns the variance of recorded observations.
func (h *histogram) variance() float64 {
t := float64(h.total())
if t == 0 {
return 0
}
s := float64(h.sum) / t
return h.sumOfSquares/t - s*s
}
// StandardDeviation returns the standard deviation of recorded observations.
func (h *histogram) standardDeviation() float64 {
return math.Sqrt(h.variance())
}
// PercentileBoundary estimates the value that the given fraction of recorded
// observations are less than.
func (h *histogram) percentileBoundary(percentile float64) int64 {
total := h.total()
// Corner cases (make sure result is strictly less than Total())
if total == 0 {
return 0
} else if total == 1 {
return int64(h.average())
}
percentOfTotal := round(float64(total) * percentile)
var runningTotal int64
for i := range h.buckets {
value := h.buckets[i]
runningTotal += value
if runningTotal == percentOfTotal {
// We hit an exact bucket boundary. If the next bucket has data, it is a
// good estimate of the value. If the bucket is empty, we interpolate the
// midpoint between the next bucket's boundary and the next non-zero
// bucket. If the remaining buckets are all empty, then we use the
// boundary for the next bucket as the estimate.
j := uint8(i + 1)
min := bucketBoundary(j)
if runningTotal < total {
for h.buckets[j] == 0 {
j++
}
}
max := bucketBoundary(j)
return min + round(float64(max-min)/2)
} else if runningTotal > percentOfTotal {
// The value is in this bucket. Interpolate the value.
delta := runningTotal - percentOfTotal
percentBucket := float64(value-delta) / float64(value)
bucketMin := bucketBoundary(uint8(i))
nextBucketMin := bucketBoundary(uint8(i + 1))
bucketSize := nextBucketMin - bucketMin
return bucketMin + round(percentBucket*float64(bucketSize))
}
}
return bucketBoundary(bucketCount - 1)
}
// Median returns the estimated median of the observed values.
func (h *histogram) median() int64 {
return h.percentileBoundary(0.5)
}
// Add adds other to h.
func (h *histogram) Add(other timeseries.Observable) {
o := other.(*histogram)
if o.valueCount == 0 {
// Other histogram is empty
} else if h.valueCount >= 0 && o.valueCount > 0 && h.value == o.value {
// Both have a single bucketed value, aggregate them
h.valueCount += o.valueCount
} else {
// Two different values necessitate buckets in this histogram
h.allocateBuckets()
if o.valueCount >= 0 {
h.buckets[o.value] += o.valueCount
} else {
for i := range h.buckets {
h.buckets[i] += o.buckets[i]
}
}
}
h.sumOfSquares += o.sumOfSquares
h.sum += o.sum
}
// Clear resets the histogram to an empty state, removing all observed values.
func (h *histogram) Clear() {
h.buckets = nil
h.value = 0
h.valueCount = 0
h.sum = 0
h.sumOfSquares = 0
}
// CopyFrom copies from other, which must be a *histogram, into h.
func (h *histogram) CopyFrom(other timeseries.Observable) {
o := other.(*histogram)
if o.valueCount == -1 {
h.allocateBuckets()
copy(h.buckets, o.buckets)
}
h.sum = o.sum
h.sumOfSquares = o.sumOfSquares
h.value = o.value
h.valueCount = o.valueCount
}
// Multiply scales the histogram by the specified ratio.
func (h *histogram) Multiply(ratio float64) {
if h.valueCount == -1 {
for i := range h.buckets {
h.buckets[i] = int64(float64(h.buckets[i]) * ratio)
}
} else {
h.valueCount = int64(float64(h.valueCount) * ratio)
}
h.sum = int64(float64(h.sum) * ratio)
h.sumOfSquares = h.sumOfSquares * ratio
}
// New creates a new histogram.
func (h *histogram) New() timeseries.Observable {
r := new(histogram)
r.Clear()
return r
}
func (h *histogram) String() string {
return fmt.Sprintf("%d, %f, %d, %d, %v",
h.sum, h.sumOfSquares, h.value, h.valueCount, h.buckets)
}
// round returns the closest int64 to the argument
func round(in float64) int64 {
return int64(math.Floor(in + 0.5))
}
// bucketBoundary returns the first value in the bucket.
func bucketBoundary(bucket uint8) int64 {
if bucket == 0 {
return 0
}
return 1 << bucket
}
// bucketData holds data about a specific bucket for use in distTmpl.
type bucketData struct {
Lower, Upper int64
N int64
Pct, CumulativePct float64
GraphWidth int
}
// data holds data about a Distribution for use in distTmpl.
type data struct {
Buckets []*bucketData
Count, Median int64
Mean, StandardDeviation float64
}
// maxHTMLBarWidth is the maximum width of the HTML bar for visualizing buckets.
const maxHTMLBarWidth = 350.0
// newData returns data representing h for use in distTmpl.
func (h *histogram) newData() *data {
// Force the allocation of buckets to simplify the rendering implementation
h.allocateBuckets()
// We scale the bars on the right so that the largest bar is
// maxHTMLBarWidth pixels in width.
maxBucket := int64(0)
for _, n := range h.buckets {
if n > maxBucket {
maxBucket = n
}
}
total := h.total()
barsizeMult := maxHTMLBarWidth / float64(maxBucket)
var pctMult float64
if total == 0 {
pctMult = 1.0
} else {
pctMult = 100.0 / float64(total)
}
buckets := make([]*bucketData, len(h.buckets))
runningTotal := int64(0)
for i, n := range h.buckets {
if n == 0 {
continue
}
runningTotal += n
var upperBound int64
if i < bucketCount-1 {
upperBound = bucketBoundary(uint8(i + 1))
} else {
upperBound = math.MaxInt64
}
buckets[i] = &bucketData{
Lower: bucketBoundary(uint8(i)),
Upper: upperBound,
N: n,
Pct: float64(n) * pctMult,
CumulativePct: float64(runningTotal) * pctMult,
GraphWidth: int(float64(n) * barsizeMult),
}
}
return &data{
Buckets: buckets,
Count: total,
Median: h.median(),
Mean: h.average(),
StandardDeviation: h.standardDeviation(),
}
}
func (h *histogram) html() template.HTML {
buf := new(bytes.Buffer)
if err := distTmpl.Execute(buf, h.newData()); err != nil {
buf.Reset()
log.Printf("net/trace: couldn't execute template: %v", err)
}
return template.HTML(buf.String())
}
// Input: data
var distTmpl = template.Must(template.New("distTmpl").Parse(`
<table>
<tr>
<td style="padding:0.25em">Count: {{.Count}}</td>
<td style="padding:0.25em">Mean: {{printf "%.0f" .Mean}}</td>
<td style="padding:0.25em">StdDev: {{printf "%.0f" .StandardDeviation}}</td>
<td style="padding:0.25em">Median: {{.Median}}</td>
</tr>
</table>
<hr>
<table>
{{range $b := .Buckets}}
{{if $b}}
<tr>
<td style="padding:0 0 0 0.25em">[</td>
<td style="text-align:right;padding:0 0.25em">{{.Lower}},</td>
<td style="text-align:right;padding:0 0.25em">{{.Upper}})</td>
<td style="text-align:right;padding:0 0.25em">{{.N}}</td>
<td style="text-align:right;padding:0 0.25em">{{printf "%#.3f" .Pct}}%</td>
<td style="text-align:right;padding:0 0.25em">{{printf "%#.3f" .CumulativePct}}%</td>
<td><div style="background-color: blue; height: 1em; width: {{.GraphWidth}};"></div></td>
</tr>
{{end}}
{{end}}
</table>
`))
sudo: false
language: go language: go
install:
- go get -v -t -d google.golang.org/grpc/...
script: script:
- go test -v -cpu 1,4 google.golang.org/grpc/... - make test testrace
- go test -v -race -cpu 1,4 google.golang.org/grpc/...
...@@ -20,8 +20,4 @@ When filing an issue, make sure to answer these five questions: ...@@ -20,8 +20,4 @@ When filing an issue, make sure to answer these five questions:
5. What did you see instead? 5. What did you see instead?
### Contributing code ### Contributing code
Please read the Contribution Guidelines before sending patches.
We will not accept GitHub pull requests once Gerrit is setup (we will use Gerrit instead for code review).
Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file. Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file.
.PHONY: \
all \
deps \
updatedeps \
testdeps \
updatetestdeps \
build \
proto \
test \
testrace \
clean \
all: test testrace
deps:
go get -d -v google.golang.org/grpc/...
updatedeps:
go get -d -v -u -f google.golang.org/grpc/...
testdeps:
go get -d -v -t google.golang.org/grpc/...
updatetestdeps:
go get -d -v -t -u -f google.golang.org/grpc/...
build: deps
go build google.golang.org/grpc/...
proto:
@ if ! which protoc > /dev/null; then \
echo "error: protoc not installed" >&2; \
exit 1; \
fi
go get -v github.com/golang/protobuf/protoc-gen-go
for file in $$(git ls-files '*.proto'); do \
protoc -I $$(dirname $$file) --go_out=plugins=grpc:$$(dirname $$file) $$file; \
done
test: testdeps
go test -v -cpu 1,4 google.golang.org/grpc/...
testrace: testdeps
go test -v -race -cpu 1,4 google.golang.org/grpc/...
clean:
go clean google.golang.org/grpc/...
...@@ -13,9 +13,14 @@ To install this package, you need to install Go 1.4 and setup your Go workspace ...@@ -13,9 +13,14 @@ To install this package, you need to install Go 1.4 and setup your Go workspace
$ go get google.golang.org/grpc $ go get google.golang.org/grpc
``` ```
Prerequisites
-------------
This requires Go 1.4 or above.
Documentation Documentation
------------- -------------
You can find more detailed documentation and examples in the [grpc-common repository](http://github.com/grpc/grpc-common). You can find more detailed documentation and examples in the [examples directory](examples/).
Status Status
------ ------
......
...@@ -40,13 +40,11 @@ import ( ...@@ -40,13 +40,11 @@ import (
"io" "io"
"math" "math"
"net" "net"
"time"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc" "google.golang.org/grpc"
testpb "google.golang.org/grpc/benchmark/grpc_testing"
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
testpb "google.golang.org/grpc/interop/grpc_testing"
) )
func newPayload(t testpb.PayloadType, size int) *testpb.Payload { func newPayload(t testpb.PayloadType, size int) *testpb.Payload {
...@@ -62,7 +60,7 @@ func newPayload(t testpb.PayloadType, size int) *testpb.Payload { ...@@ -62,7 +60,7 @@ func newPayload(t testpb.PayloadType, size int) *testpb.Payload {
grpclog.Fatalf("Unsupported payload type: %d", t) grpclog.Fatalf("Unsupported payload type: %d", t)
} }
return &testpb.Payload{ return &testpb.Payload{
Type: t.Enum(), Type: t,
Body: body, Body: body,
} }
} }
...@@ -70,49 +68,13 @@ func newPayload(t testpb.PayloadType, size int) *testpb.Payload { ...@@ -70,49 +68,13 @@ func newPayload(t testpb.PayloadType, size int) *testpb.Payload {
type testServer struct { type testServer struct {
} }
func (s *testServer) EmptyCall(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return new(testpb.Empty), nil
}
func (s *testServer) UnaryCall(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) { func (s *testServer) UnaryCall(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return &testpb.SimpleResponse{ return &testpb.SimpleResponse{
Payload: newPayload(in.GetResponseType(), int(in.GetResponseSize())), Payload: newPayload(in.ResponseType, int(in.ResponseSize)),
}, nil }, nil
} }
func (s *testServer) StreamingOutputCall(args *testpb.StreamingOutputCallRequest, stream testpb.TestService_StreamingOutputCallServer) error { func (s *testServer) StreamingCall(stream testpb.TestService_StreamingCallServer) error {
cs := args.GetResponseParameters()
for _, c := range cs {
if us := c.GetIntervalUs(); us > 0 {
time.Sleep(time.Duration(us) * time.Microsecond)
}
if err := stream.Send(&testpb.StreamingOutputCallResponse{
Payload: newPayload(args.GetResponseType(), int(c.GetSize())),
}); err != nil {
return err
}
}
return nil
}
func (s *testServer) StreamingInputCall(stream testpb.TestService_StreamingInputCallServer) error {
var sum int
for {
in, err := stream.Recv()
if err == io.EOF {
return stream.SendAndClose(&testpb.StreamingInputCallResponse{
AggregatedPayloadSize: proto.Int32(int32(sum)),
})
}
if err != nil {
return err
}
p := in.GetPayload().GetBody()
sum += len(p)
}
}
func (s *testServer) FullDuplexCall(stream testpb.TestService_FullDuplexCallServer) error {
for { for {
in, err := stream.Recv() in, err := stream.Recv()
if err == io.EOF { if err == io.EOF {
...@@ -122,53 +84,19 @@ func (s *testServer) FullDuplexCall(stream testpb.TestService_FullDuplexCallServ ...@@ -122,53 +84,19 @@ func (s *testServer) FullDuplexCall(stream testpb.TestService_FullDuplexCallServ
if err != nil { if err != nil {
return err return err
} }
cs := in.GetResponseParameters() if err := stream.Send(&testpb.SimpleResponse{
for _, c := range cs { Payload: newPayload(in.ResponseType, int(in.ResponseSize)),
if us := c.GetIntervalUs(); us > 0 {
time.Sleep(time.Duration(us) * time.Microsecond)
}
if err := stream.Send(&testpb.StreamingOutputCallResponse{
Payload: newPayload(in.GetResponseType(), int(c.GetSize())),
}); err != nil {
return err
}
}
}
}
func (s *testServer) HalfDuplexCall(stream testpb.TestService_HalfDuplexCallServer) error {
var msgBuf []*testpb.StreamingOutputCallRequest
for {
in, err := stream.Recv()
if err == io.EOF {
// read done.
break
}
if err != nil {
return err
}
msgBuf = append(msgBuf, in)
}
for _, m := range msgBuf {
cs := m.GetResponseParameters()
for _, c := range cs {
if us := c.GetIntervalUs(); us > 0 {
time.Sleep(time.Duration(us) * time.Microsecond)
}
if err := stream.Send(&testpb.StreamingOutputCallResponse{
Payload: newPayload(m.GetResponseType(), int(c.GetSize())),
}); err != nil { }); err != nil {
return err return err
} }
} }
}
return nil
} }
// StartServer starts a gRPC server serving a benchmark service. It returns its // StartServer starts a gRPC server serving a benchmark service on the given
// listen address and a function to stop the server. // address, which may be something like "localhost:0". It returns its listen
func StartServer() (string, func()) { // address and a function to stop the server.
lis, err := net.Listen("tcp", ":0") func StartServer(addr string) (string, func()) {
lis, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
grpclog.Fatalf("Failed to listen: %v", err) grpclog.Fatalf("Failed to listen: %v", err)
} }
...@@ -184,8 +112,8 @@ func StartServer() (string, func()) { ...@@ -184,8 +112,8 @@ func StartServer() (string, func()) {
func DoUnaryCall(tc testpb.TestServiceClient, reqSize, respSize int) { func DoUnaryCall(tc testpb.TestServiceClient, reqSize, respSize int) {
pl := newPayload(testpb.PayloadType_COMPRESSABLE, reqSize) pl := newPayload(testpb.PayloadType_COMPRESSABLE, reqSize)
req := &testpb.SimpleRequest{ req := &testpb.SimpleRequest{
ResponseType: testpb.PayloadType_COMPRESSABLE.Enum(), ResponseType: pl.Type,
ResponseSize: proto.Int32(int32(respSize)), ResponseSize: int32(respSize),
Payload: pl, Payload: pl,
} }
if _, err := tc.UnaryCall(context.Background(), req); err != nil { if _, err := tc.UnaryCall(context.Background(), req); err != nil {
...@@ -193,9 +121,25 @@ func DoUnaryCall(tc testpb.TestServiceClient, reqSize, respSize int) { ...@@ -193,9 +121,25 @@ func DoUnaryCall(tc testpb.TestServiceClient, reqSize, respSize int) {
} }
} }
// DoStreamingRoundTrip performs a round trip for a single streaming rpc.
func DoStreamingRoundTrip(tc testpb.TestServiceClient, stream testpb.TestService_StreamingCallClient, reqSize, respSize int) {
pl := newPayload(testpb.PayloadType_COMPRESSABLE, reqSize)
req := &testpb.SimpleRequest{
ResponseType: pl.Type,
ResponseSize: int32(respSize),
Payload: pl,
}
if err := stream.Send(req); err != nil {
grpclog.Fatalf("StreamingCall(_).Send: %v", err)
}
if _, err := stream.Recv(); err != nil {
grpclog.Fatalf("StreamingCall(_).Recv: %v", err)
}
}
// NewClientConn creates a gRPC client connection to addr. // NewClientConn creates a gRPC client connection to addr.
func NewClientConn(addr string) *grpc.ClientConn { func NewClientConn(addr string) *grpc.ClientConn {
conn, err := grpc.Dial(addr) conn, err := grpc.Dial(addr, grpc.WithInsecure())
if err != nil { if err != nil {
grpclog.Fatalf("NewClientConn(%q) failed to create a ClientConn %v", addr, err) grpclog.Fatalf("NewClientConn(%q) failed to create a ClientConn %v", addr, err)
} }
......
...@@ -9,29 +9,95 @@ import ( ...@@ -9,29 +9,95 @@ import (
"sync" "sync"
"time" "time"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/benchmark" "google.golang.org/grpc/benchmark"
testpb "google.golang.org/grpc/benchmark/grpc_testing"
"google.golang.org/grpc/benchmark/stats" "google.golang.org/grpc/benchmark/stats"
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
testpb "google.golang.org/grpc/interop/grpc_testing"
) )
var ( var (
server = flag.String("server", "", "The server address") server = flag.String("server", "", "The server address")
maxConcurrentRPCs = flag.Int("max_concurrent_rpcs", 1, "The max number of concurrent RPCs") maxConcurrentRPCs = flag.Int("max_concurrent_rpcs", 1, "The max number of concurrent RPCs")
duration = flag.Int("duration", math.MaxInt32, "The duration in seconds to run the benchmark client") duration = flag.Int("duration", math.MaxInt32, "The duration in seconds to run the benchmark client")
trace = flag.Bool("trace", true, "Whether tracing is on")
rpcType = flag.Int("rpc_type", 0,
`Configure different client rpc type. Valid options are:
0 : unary call;
1 : streaming call.`)
) )
func caller(client testpb.TestServiceClient) { func unaryCaller(client testpb.TestServiceClient) {
benchmark.DoUnaryCall(client, 1, 1) benchmark.DoUnaryCall(client, 1, 1)
} }
func closeLoop() { func streamCaller(client testpb.TestServiceClient, stream testpb.TestService_StreamingCallClient) {
s := stats.NewStats(256) benchmark.DoStreamingRoundTrip(client, stream, 1, 1)
conn := benchmark.NewClientConn(*server) }
tc := testpb.NewTestServiceClient(conn)
// Warm up connection. func buildConnection() (s *stats.Stats, conn *grpc.ClientConn, tc testpb.TestServiceClient) {
s = stats.NewStats(256)
conn = benchmark.NewClientConn(*server)
tc = testpb.NewTestServiceClient(conn)
return s, conn, tc
}
func closeLoopUnary() {
s, conn, tc := buildConnection()
for i := 0; i < 100; i++ {
unaryCaller(tc)
}
ch := make(chan int, *maxConcurrentRPCs*4)
var (
mu sync.Mutex
wg sync.WaitGroup
)
wg.Add(*maxConcurrentRPCs)
for i := 0; i < *maxConcurrentRPCs; i++ {
go func() {
for _ = range ch {
start := time.Now()
unaryCaller(tc)
elapse := time.Since(start)
mu.Lock()
s.Add(elapse)
mu.Unlock()
}
wg.Done()
}()
}
// Stop the client when time is up.
done := make(chan struct{})
go func() {
<-time.After(time.Duration(*duration) * time.Second)
close(done)
}()
ok := true
for ok {
select {
case ch <- 0:
case <-done:
ok = false
}
}
close(ch)
wg.Wait()
conn.Close()
grpclog.Println(s.String())
}
func closeLoopStream() {
s, conn, tc := buildConnection()
stream, err := tc.StreamingCall(context.Background())
if err != nil {
grpclog.Fatalf("%v.StreamingCall(_) = _, %v", tc, err)
}
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
caller(tc) streamCaller(tc, stream)
} }
ch := make(chan int, *maxConcurrentRPCs*4) ch := make(chan int, *maxConcurrentRPCs*4)
var ( var (
...@@ -44,7 +110,7 @@ func closeLoop() { ...@@ -44,7 +110,7 @@ func closeLoop() {
go func() { go func() {
for _ = range ch { for _ = range ch {
start := time.Now() start := time.Now()
caller(tc) streamCaller(tc, stream)
elapse := time.Since(start) elapse := time.Since(start)
mu.Lock() mu.Lock()
s.Add(elapse) s.Add(elapse)
...@@ -75,6 +141,7 @@ func closeLoop() { ...@@ -75,6 +141,7 @@ func closeLoop() {
func main() { func main() {
flag.Parse() flag.Parse()
grpc.EnableTracing = *trace
go func() { go func() {
lis, err := net.Listen("tcp", ":0") lis, err := net.Listen("tcp", ":0")
if err != nil { if err != nil {
...@@ -85,5 +152,10 @@ func main() { ...@@ -85,5 +152,10 @@ func main() {
grpclog.Fatalf("Failed to serve: %v", err) grpclog.Fatalf("Failed to serve: %v", err)
} }
}() }()
closeLoop() switch *rpcType {
case 0:
closeLoopUnary()
case 1:
closeLoopStream()
}
} }
// An integration test service that covers all the method signature permutations // An integration test service that covers all the method signature permutations
// of unary/streaming requests/responses. // of unary/streaming requests/responses.
syntax = "proto2"; syntax = "proto3";
package grpc.testing; package grpc.testing;
message Empty {}
// The type of payload that should be returned.
enum PayloadType { enum PayloadType {
// Compressable text format. // Compressable text format.
COMPRESSABLE = 0; COMPRESSABLE = 0;
...@@ -18,123 +15,134 @@ enum PayloadType { ...@@ -18,123 +15,134 @@ enum PayloadType {
RANDOM = 2; RANDOM = 2;
} }
// A block of data, to simply increase gRPC message size. message StatsRequest {
// run number
optional int32 test_num = 1;
}
message ServerStats {
// wall clock time
double time_elapsed = 1;
// user time used by the server process and threads
double time_user = 2;
// server time used by the server process and all threads
double time_system = 3;
}
message Payload { message Payload {
// The type of data in body. // The type of data in body.
optional PayloadType type = 1; PayloadType type = 1;
// Primary contents of payload. // Primary contents of payload.
optional bytes body = 2; bytes body = 2;
} }
// Unary request. message HistogramData {
message SimpleRequest { repeated uint32 bucket = 1;
// Desired payload type in the response from the server. double min_seen = 2;
// If response_type is RANDOM, server randomly chooses one from other formats. double max_seen = 3;
optional PayloadType response_type = 1; double sum = 4;
double sum_of_squares = 5;
// Desired payload size in the response from the server. double count = 6;
// If response_type is COMPRESSABLE, this denotes the size before compression. }
optional int32 response_size = 2;
// Optional input payload sent along with the request. enum ClientType {
optional Payload payload = 3; SYNCHRONOUS_CLIENT = 0;
ASYNC_CLIENT = 1;
}
// Whether SimpleResponse should include username. enum ServerType {
optional bool fill_username = 4; SYNCHRONOUS_SERVER = 0;
ASYNC_SERVER = 1;
}
// Whether SimpleResponse should include OAuth scope. enum RpcType {
optional bool fill_oauth_scope = 5; UNARY = 0;
STREAMING = 1;
} }
// Unary response, as configured by the request. message ClientConfig {
message SimpleResponse { repeated string server_targets = 1;
// Payload to increase message size. ClientType client_type = 2;
optional Payload payload = 1; bool enable_ssl = 3;
int32 outstanding_rpcs_per_channel = 4;
int32 client_channels = 5;
int32 payload_size = 6;
// only for async client:
int32 async_client_threads = 7;
RpcType rpc_type = 8;
}
// The user the request came from, for verifying authentication was // Request current stats
// successful when the client expected it. message Mark {}
optional string username = 2;
// OAuth scope. message ClientArgs {
optional string oauth_scope = 3; oneof argtype {
ClientConfig setup = 1;
Mark mark = 2;
}
} }
// Client-streaming request. message ClientStats {
message StreamingInputCallRequest { HistogramData latencies = 1;
// Optional input payload sent along with the request. double time_elapsed = 3;
optional Payload payload = 1; double time_user = 4;
double time_system = 5;
}
// Not expecting any payload from the response. message ClientStatus {
ClientStats stats = 1;
} }
// Client-streaming response. message ServerConfig {
message StreamingInputCallResponse { ServerType server_type = 1;
// Aggregated size of payloads received from the client. int32 threads = 2;
optional int32 aggregated_payload_size = 1; bool enable_ssl = 3;
} }
// Configuration for a particular response. message ServerArgs {
message ResponseParameters { oneof argtype {
// Desired payload sizes in responses from the server. ServerConfig setup = 1;
// If response_type is COMPRESSABLE, this denotes the size before compression. Mark mark = 2;
optional int32 size = 1; }
}
// Desired interval between consecutive responses in the response stream in message ServerStatus {
// microseconds. ServerStats stats = 1;
optional int32 interval_us = 2; int32 port = 2;
} }
// Server-streaming request. message SimpleRequest {
message StreamingOutputCallRequest {
// Desired payload type in the response from the server. // Desired payload type in the response from the server.
// If response_type is RANDOM, the payload from each response in the stream // If response_type is RANDOM, server randomly chooses one from other formats.
// might be of different types. This is to simulate a mixed type of payload PayloadType response_type = 1;
// stream.
optional PayloadType response_type = 1;
// Configuration for each expected response message. // Desired payload size in the response from the server.
repeated ResponseParameters response_parameters = 2; // If response_type is COMPRESSABLE, this denotes the size before compression.
int32 response_size = 2;
// Optional input payload sent along with the request. // Optional input payload sent along with the request.
optional Payload payload = 3; Payload payload = 3;
} }
// Server-streaming response, as configured by the request and parameters. message SimpleResponse {
message StreamingOutputCallResponse { Payload payload = 1;
// Payload to increase response size.
optional Payload payload = 1;
} }
// A simple service to test the various types of RPCs and experiment with
// performance with various types of payload.
service TestService { service TestService {
// One empty request followed by one empty response.
rpc EmptyCall(Empty) returns (Empty);
// One request followed by one response. // One request followed by one response.
// The server returns the client payload as-is. // The server returns the client payload as-is.
rpc UnaryCall(SimpleRequest) returns (SimpleResponse); rpc UnaryCall(SimpleRequest) returns (SimpleResponse);
// One request followed by a sequence of responses (streamed download). // One request followed by one response.
// The server returns the payload with client desired type and sizes. // The server returns the client payload as-is.
rpc StreamingOutputCall(StreamingOutputCallRequest) rpc StreamingCall(stream SimpleRequest) returns (stream SimpleResponse);
returns (stream StreamingOutputCallResponse); }
// A sequence of requests followed by one response (streamed upload). service Worker {
// The server returns the aggregated size of client payload as the result. // Start test with specified workload
rpc StreamingInputCall(stream StreamingInputCallRequest) rpc RunTest(stream ClientArgs) returns (stream ClientStatus);
returns (StreamingInputCallResponse); // Start test with specified workload
rpc RunServer(stream ServerArgs) returns (stream ServerStatus);
// A sequence of requests with each request served by the server immediately.
// As one request could lead to multiple responses, this interface
// demonstrates the idea of full duplexing.
rpc FullDuplexCall(stream StreamingOutputCallRequest)
returns (stream StreamingOutputCallResponse);
// A sequence of requests followed by a sequence of responses.
// The server buffers all the client requests and then serves them in order. A
// stream of responses are returned to the client when the server starts with
// first request.
rpc HalfDuplexCall(stream StreamingOutputCallRequest)
returns (stream StreamingOutputCallResponse);
} }
...@@ -28,7 +28,7 @@ func main() { ...@@ -28,7 +28,7 @@ func main() {
grpclog.Fatalf("Failed to serve: %v", err) grpclog.Fatalf("Failed to serve: %v", err)
} }
}() }()
addr, stopper := benchmark.StartServer() addr, stopper := benchmark.StartServer(":0") // listen on all interfaces
grpclog.Println("Server Address: ", addr) grpclog.Println("Server Address: ", addr)
<-time.After(time.Duration(*duration) * time.Second) <-time.After(time.Duration(*duration) * time.Second)
stopper() stopper()
......
...@@ -251,5 +251,5 @@ func (h *Histogram) findBucket(value int64) (int, error) { ...@@ -251,5 +251,5 @@ func (h *Histogram) findBucket(value int64) (int, error) {
} }
min = b + 1 min = b + 1
} }
return 0, fmt.Errorf("no bucket for value: %f", value) return 0, fmt.Errorf("no bucket for value: %d", value)
} }
...@@ -35,8 +35,10 @@ package grpc ...@@ -35,8 +35,10 @@ package grpc
import ( import (
"io" "io"
"time"
"golang.org/x/net/context" "golang.org/x/net/context"
"golang.org/x/net/trace"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
"google.golang.org/grpc/transport" "google.golang.org/grpc/transport"
...@@ -97,11 +99,12 @@ type callInfo struct { ...@@ -97,11 +99,12 @@ type callInfo struct {
failFast bool failFast bool
headerMD metadata.MD headerMD metadata.MD
trailerMD metadata.MD trailerMD metadata.MD
traceInfo traceInfo // in trace.go
} }
// Invoke is called by the generated code. It sends the RPC request on the // Invoke is called by the generated code. It sends the RPC request on the
// wire and returns after response is received. // wire and returns after response is received.
func Invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) error { func Invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) (err error) {
var c callInfo var c callInfo
for _, o := range opts { for _, o := range opts {
if err := o.before(&c); err != nil { if err := o.before(&c); err != nil {
...@@ -113,6 +116,23 @@ func Invoke(ctx context.Context, method string, args, reply interface{}, cc *Cli ...@@ -113,6 +116,23 @@ func Invoke(ctx context.Context, method string, args, reply interface{}, cc *Cli
o.after(&c) o.after(&c)
} }
}() }()
if EnableTracing {
c.traceInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method)
defer c.traceInfo.tr.Finish()
c.traceInfo.firstLine.client = true
if deadline, ok := ctx.Deadline(); ok {
c.traceInfo.firstLine.deadline = deadline.Sub(time.Now())
}
c.traceInfo.tr.LazyLog(&c.traceInfo.firstLine, false)
// TODO(dsymonds): Arrange for c.traceInfo.firstLine.remoteAddr to be set.
defer func() {
if err != nil {
c.traceInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
c.traceInfo.tr.SetError()
}
}()
}
callHdr := &transport.CallHdr{ callHdr := &transport.CallHdr{
Host: cc.authority, Host: cc.authority,
Method: method, Method: method,
...@@ -143,6 +163,9 @@ func Invoke(ctx context.Context, method string, args, reply interface{}, cc *Cli ...@@ -143,6 +163,9 @@ func Invoke(ctx context.Context, method string, args, reply interface{}, cc *Cli
} }
return toRPCErr(err) return toRPCErr(err)
} }
if c.traceInfo.tr != nil {
c.traceInfo.tr.LazyLog(&payload{sent: true, msg: args}, true)
}
stream, err = sendRequest(ctx, cc.dopts.codec, callHdr, t, args, topts) stream, err = sendRequest(ctx, cc.dopts.codec, callHdr, t, args, topts)
if err != nil { if err != nil {
if _, ok := err.(transport.ConnectionError); ok { if _, ok := err.(transport.ConnectionError); ok {
...@@ -159,6 +182,9 @@ func Invoke(ctx context.Context, method string, args, reply interface{}, cc *Cli ...@@ -159,6 +182,9 @@ func Invoke(ctx context.Context, method string, args, reply interface{}, cc *Cli
if _, ok := lastErr.(transport.ConnectionError); ok { if _, ok := lastErr.(transport.ConnectionError); ok {
continue continue
} }
if c.traceInfo.tr != nil {
c.traceInfo.tr.LazyLog(&payload{sent: false, msg: reply}, true)
}
t.CloseStream(stream, lastErr) t.CloseStream(stream, lastErr)
if lastErr != nil { if lastErr != nil {
return toRPCErr(lastErr) return toRPCErr(lastErr)
......
...@@ -35,6 +35,7 @@ package grpc ...@@ -35,6 +35,7 @@ package grpc
import ( import (
"errors" "errors"
"fmt"
"net" "net"
"strings" "strings"
"sync" "sync"
...@@ -49,18 +50,30 @@ import ( ...@@ -49,18 +50,30 @@ import (
var ( var (
// ErrUnspecTarget indicates that the target address is unspecified. // ErrUnspecTarget indicates that the target address is unspecified.
ErrUnspecTarget = errors.New("grpc: target is unspecified") ErrUnspecTarget = errors.New("grpc: target is unspecified")
// ErrNoTransportSecurity indicates that there is no transport security
// being set for ClientConn. Users should either set one or explicityly
// call WithInsecure DialOption to disable security.
ErrNoTransportSecurity = errors.New("grpc: no transport security set (use grpc.WithInsecure() explicitly or set credentials)")
// ErrCredentialsMisuse indicates that users want to transmit security infomation
// (e.g., oauth2 token) which requires secure connection on an insecure
// connection.
ErrCredentialsMisuse = errors.New("grpc: the credentials require transport level security (use grpc.WithTransportAuthenticator() to set)")
// ErrClientConnClosing indicates that the operation is illegal because // ErrClientConnClosing indicates that the operation is illegal because
// the session is closing. // the session is closing.
ErrClientConnClosing = errors.New("grpc: the client connection is closing") ErrClientConnClosing = errors.New("grpc: the client connection is closing")
// ErrClientConnTimeout indicates that the connection could not be // ErrClientConnTimeout indicates that the connection could not be
// established or re-established within the specified timeout. // established or re-established within the specified timeout.
ErrClientConnTimeout = errors.New("grpc: timed out trying to connect") ErrClientConnTimeout = errors.New("grpc: timed out trying to connect")
// minimum time to give a connection to complete
minConnectTimeout = 20 * time.Second
) )
// dialOptions configure a Dial call. dialOptions are set by the DialOption // dialOptions configure a Dial call. dialOptions are set by the DialOption
// values passed to Dial. // values passed to Dial.
type dialOptions struct { type dialOptions struct {
codec Codec codec Codec
block bool
insecure bool
copts transport.ConnectOptions copts transport.ConnectOptions
} }
...@@ -74,6 +87,21 @@ func WithCodec(c Codec) DialOption { ...@@ -74,6 +87,21 @@ func WithCodec(c Codec) DialOption {
} }
} }
// WithBlock returns a DialOption which makes caller of Dial blocks until the underlying
// connection is up. Without this, Dial returns immediately and connecting the server
// happens in background.
func WithBlock() DialOption {
return func(o *dialOptions) {
o.block = true
}
}
func WithInsecure() DialOption {
return func(o *dialOptions) {
o.insecure = true
}
}
// WithTransportCredentials returns a DialOption which configures a // WithTransportCredentials returns a DialOption which configures a
// connection level security credentials (e.g., TLS/SSL). // connection level security credentials (e.g., TLS/SSL).
func WithTransportCredentials(creds credentials.TransportAuthenticator) DialOption { func WithTransportCredentials(creds credentials.TransportAuthenticator) DialOption {
...@@ -104,19 +132,43 @@ func WithDialer(f func(addr string, timeout time.Duration) (net.Conn, error)) Di ...@@ -104,19 +132,43 @@ func WithDialer(f func(addr string, timeout time.Duration) (net.Conn, error)) Di
} }
} }
// WithUserAgent returns a DialOption that specifies a user agent string for all the RPCs.
func WithUserAgent(s string) DialOption {
return func(o *dialOptions) {
o.copts.UserAgent = s
}
}
// Dial creates a client connection the given target. // Dial creates a client connection the given target.
// TODO(zhaoq): Have an option to make Dial return immediately without waiting
// for connection to complete.
func Dial(target string, opts ...DialOption) (*ClientConn, error) { func Dial(target string, opts ...DialOption) (*ClientConn, error) {
if target == "" { if target == "" {
return nil, ErrUnspecTarget return nil, ErrUnspecTarget
} }
cc := &ClientConn{ cc := &ClientConn{
target: target, target: target,
shutdownChan: make(chan struct{}),
} }
for _, opt := range opts { for _, opt := range opts {
opt(&cc.dopts) opt(&cc.dopts)
} }
if !cc.dopts.insecure {
var ok bool
for _, c := range cc.dopts.copts.AuthOptions {
if _, ok := c.(credentials.TransportAuthenticator); !ok {
continue
}
ok = true
}
if !ok {
return nil, ErrNoTransportSecurity
}
} else {
for _, c := range cc.dopts.copts.AuthOptions {
if c.RequireTransportSecurity() {
return nil, ErrCredentialsMisuse
}
}
}
colonPos := strings.LastIndex(target, ":") colonPos := strings.LastIndex(target, ":")
if colonPos == -1 { if colonPos == -1 {
colonPos = len(target) colonPos = len(target)
...@@ -126,15 +178,61 @@ func Dial(target string, opts ...DialOption) (*ClientConn, error) { ...@@ -126,15 +178,61 @@ func Dial(target string, opts ...DialOption) (*ClientConn, error) {
// Set the default codec. // Set the default codec.
cc.dopts.codec = protoCodec{} cc.dopts.codec = protoCodec{}
} }
cc.stateCV = sync.NewCond(&cc.mu)
if cc.dopts.block {
if err := cc.resetTransport(false); err != nil { if err := cc.resetTransport(false); err != nil {
cc.Close()
return nil, err return nil, err
} }
cc.shutdownChan = make(chan struct{})
// Start to monitor the error status of transport. // Start to monitor the error status of transport.
go cc.transportMonitor() go cc.transportMonitor()
} else {
// Start a goroutine connecting to the server asynchronously.
go func() {
if err := cc.resetTransport(false); err != nil {
grpclog.Printf("Failed to dial %s: %v; please retry.", target, err)
cc.Close()
return
}
go cc.transportMonitor()
}()
}
return cc, nil return cc, nil
} }
// ConnectivityState indicates the state of a client connection.
type ConnectivityState int
const (
// Idle indicates the ClientConn is idle.
Idle ConnectivityState = iota
// Connecting indicates the ClienConn is connecting.
Connecting
// Ready indicates the ClientConn is ready for work.
Ready
// TransientFailure indicates the ClientConn has seen a failure but expects to recover.
TransientFailure
// Shutdown indicates the ClientConn has stated shutting down.
Shutdown
)
func (s ConnectivityState) String() string {
switch s {
case Idle:
return "IDLE"
case Connecting:
return "CONNECTING"
case Ready:
return "READY"
case TransientFailure:
return "TRANSIENT_FAILURE"
case Shutdown:
return "SHUTDOWN"
default:
panic(fmt.Sprintf("unknown connectivity state: %d", s))
}
}
// ClientConn represents a client connection to an RPC service. // ClientConn represents a client connection to an RPC service.
type ClientConn struct { type ClientConn struct {
target string target string
...@@ -143,11 +241,11 @@ type ClientConn struct { ...@@ -143,11 +241,11 @@ type ClientConn struct {
shutdownChan chan struct{} shutdownChan chan struct{}
mu sync.Mutex mu sync.Mutex
state ConnectivityState
stateCV *sync.Cond
// ready is closed and becomes nil when a new transport is up or failed // ready is closed and becomes nil when a new transport is up or failed
// due to timeout. // due to timeout.
ready chan struct{} ready chan struct{}
// Indicates the ClientConn is under destruction.
closing bool
// Every time a new transport is created, this is incremented by 1. Used // Every time a new transport is created, this is incremented by 1. Used
// to avoid trying to recreate a transport while the new one is already // to avoid trying to recreate a transport while the new one is already
// under construction. // under construction.
...@@ -155,16 +253,59 @@ type ClientConn struct { ...@@ -155,16 +253,59 @@ type ClientConn struct {
transport transport.ClientTransport transport transport.ClientTransport
} }
// State returns the connectivity state of the ClientConn
func (cc *ClientConn) State() ConnectivityState {
cc.mu.Lock()
defer cc.mu.Unlock()
return cc.state
}
// WaitForStateChange blocks until the state changes to something other than the sourceState
// or timeout fires. It returns false if timeout fires and true otherwise.
func (cc *ClientConn) WaitForStateChange(timeout time.Duration, sourceState ConnectivityState) bool {
start := time.Now()
cc.mu.Lock()
defer cc.mu.Unlock()
if sourceState != cc.state {
return true
}
expired := timeout <= time.Since(start)
if expired {
return false
}
done := make(chan struct{})
go func() {
select {
case <-time.After(timeout - time.Since(start)):
cc.mu.Lock()
expired = true
cc.stateCV.Broadcast()
cc.mu.Unlock()
case <-done:
}
}()
defer close(done)
for sourceState == cc.state {
cc.stateCV.Wait()
if expired {
return false
}
}
return true
}
func (cc *ClientConn) resetTransport(closeTransport bool) error { func (cc *ClientConn) resetTransport(closeTransport bool) error {
var retries int var retries int
start := time.Now() start := time.Now()
for { for {
cc.mu.Lock() cc.mu.Lock()
cc.state = Connecting
cc.stateCV.Broadcast()
t := cc.transport t := cc.transport
ts := cc.transportSeq ts := cc.transportSeq
// Avoid wait() picking up a dying transport unnecessarily. // Avoid wait() picking up a dying transport unnecessarily.
cc.transportSeq = 0 cc.transportSeq = 0
if cc.closing { if cc.state == Shutdown {
cc.mu.Unlock() cc.mu.Unlock()
return ErrClientConnClosing return ErrClientConnClosing
} }
...@@ -185,9 +326,25 @@ func (cc *ClientConn) resetTransport(closeTransport bool) error { ...@@ -185,9 +326,25 @@ func (cc *ClientConn) resetTransport(closeTransport bool) error {
return ErrClientConnTimeout return ErrClientConnTimeout
} }
} }
sleepTime := backoff(retries)
timeout := sleepTime
if timeout < minConnectTimeout {
timeout = minConnectTimeout
}
if copts.Timeout == 0 || copts.Timeout > timeout {
copts.Timeout = timeout
}
connectTime := time.Now()
newTransport, err := transport.NewClientTransport(cc.target, &copts) newTransport, err := transport.NewClientTransport(cc.target, &copts)
if err != nil { if err != nil {
sleepTime := backoff(retries) cc.mu.Lock()
cc.state = TransientFailure
cc.stateCV.Broadcast()
cc.mu.Unlock()
sleepTime -= time.Since(connectTime)
if sleepTime < 0 {
sleepTime = 0
}
// Fail early before falling into sleep. // Fail early before falling into sleep.
if cc.dopts.copts.Timeout > 0 && cc.dopts.copts.Timeout < sleepTime+time.Since(start) { if cc.dopts.copts.Timeout > 0 && cc.dopts.copts.Timeout < sleepTime+time.Since(start) {
cc.Close() cc.Close()
...@@ -200,12 +357,14 @@ func (cc *ClientConn) resetTransport(closeTransport bool) error { ...@@ -200,12 +357,14 @@ func (cc *ClientConn) resetTransport(closeTransport bool) error {
continue continue
} }
cc.mu.Lock() cc.mu.Lock()
if cc.closing { if cc.state == Shutdown {
// cc.Close() has been invoked. // cc.Close() has been invoked.
cc.mu.Unlock() cc.mu.Unlock()
newTransport.Close() newTransport.Close()
return ErrClientConnClosing return ErrClientConnClosing
} }
cc.state = Ready
cc.stateCV.Broadcast()
cc.transport = newTransport cc.transport = newTransport
cc.transportSeq = ts + 1 cc.transportSeq = ts + 1
if cc.ready != nil { if cc.ready != nil {
...@@ -222,13 +381,17 @@ func (cc *ClientConn) resetTransport(closeTransport bool) error { ...@@ -222,13 +381,17 @@ func (cc *ClientConn) resetTransport(closeTransport bool) error {
func (cc *ClientConn) transportMonitor() { func (cc *ClientConn) transportMonitor() {
for { for {
select { select {
// shutdownChan is needed to detect the channel teardown when // shutdownChan is needed to detect the teardown when
// the ClientConn is idle (i.e., no RPC in flight). // the ClientConn is idle (i.e., no RPC in flight).
case <-cc.shutdownChan: case <-cc.shutdownChan:
return return
case <-cc.transport.Error(): case <-cc.transport.Error():
cc.mu.Lock()
cc.state = TransientFailure
cc.stateCV.Broadcast()
cc.mu.Unlock()
if err := cc.resetTransport(true); err != nil { if err := cc.resetTransport(true); err != nil {
// The channel is closing. // The ClientConn is closing.
grpclog.Printf("grpc: ClientConn.transportMonitor exits due to: %v", err) grpclog.Printf("grpc: ClientConn.transportMonitor exits due to: %v", err)
return return
} }
...@@ -244,7 +407,7 @@ func (cc *ClientConn) wait(ctx context.Context, ts int) (transport.ClientTranspo ...@@ -244,7 +407,7 @@ func (cc *ClientConn) wait(ctx context.Context, ts int) (transport.ClientTranspo
for { for {
cc.mu.Lock() cc.mu.Lock()
switch { switch {
case cc.closing: case cc.state == Shutdown:
cc.mu.Unlock() cc.mu.Unlock()
return nil, 0, ErrClientConnClosing return nil, 0, ErrClientConnClosing
case ts < cc.transportSeq: case ts < cc.transportSeq:
...@@ -276,10 +439,11 @@ func (cc *ClientConn) wait(ctx context.Context, ts int) (transport.ClientTranspo ...@@ -276,10 +439,11 @@ func (cc *ClientConn) wait(ctx context.Context, ts int) (transport.ClientTranspo
func (cc *ClientConn) Close() error { func (cc *ClientConn) Close() error {
cc.mu.Lock() cc.mu.Lock()
defer cc.mu.Unlock() defer cc.mu.Unlock()
if cc.closing { if cc.state == Shutdown {
return ErrClientConnClosing return ErrClientConnClosing
} }
cc.closing = true cc.state = Shutdown
cc.stateCV.Broadcast()
if cc.ready != nil { if cc.ready != nil {
close(cc.ready) close(cc.ready)
cc.ready = nil cc.ready = nil
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
# plugin at https://github.com/golang/protobuf (after 2015-02-20). If you have # plugin at https://github.com/golang/protobuf (after 2015-02-20). If you have
# not, please install them first. # not, please install them first.
# #
# We recommend running this script at $GOPATH or $GOPATH/src. # We recommend running this script at $GOPATH/src.
# #
# If this is not what you need, feel free to make your own scripts. Again, this # If this is not what you need, feel free to make your own scripts. Again, this
# script is for demonstration purpose. # script is for demonstration purpose.
......
...@@ -47,14 +47,11 @@ import ( ...@@ -47,14 +47,11 @@ import (
"time" "time"
"golang.org/x/net/context" "golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
) )
var ( var (
// alpnProtoStr are the specified application level protocols for gRPC. // alpnProtoStr are the specified application level protocols for gRPC.
alpnProtoStr = []string{"h2-14", "h2-15", "h2-16"} alpnProtoStr = []string{"h2"}
) )
// Credentials defines the common interface all supported credentials must // Credentials defines the common interface all supported credentials must
...@@ -63,11 +60,15 @@ type Credentials interface { ...@@ -63,11 +60,15 @@ type Credentials interface {
// GetRequestMetadata gets the current request metadata, refreshing // GetRequestMetadata gets the current request metadata, refreshing
// tokens if required. This should be called by the transport layer on // tokens if required. This should be called by the transport layer on
// each request, and the data should be populated in headers or other // each request, and the data should be populated in headers or other
// context. When supported by the underlying implementation, ctx can // context. uri is the URI of the entry point for the request. When
// be used for timeout and cancellation. // supported by the underlying implementation, ctx can be used for
// timeout and cancellation.
// TODO(zhaoq): Define the set of the qualified keys instead of leaving // TODO(zhaoq): Define the set of the qualified keys instead of leaving
// it as an arbitrary string. // it as an arbitrary string.
GetRequestMetadata(ctx context.Context) (map[string]string, error) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error)
// RequireTransportSecurity indicates whether the credentails requires
// transport security.
RequireTransportSecurity() bool
} }
// ProtocolInfo provides information regarding the gRPC wire protocol version, // ProtocolInfo provides information regarding the gRPC wire protocol version,
...@@ -81,26 +82,57 @@ type ProtocolInfo struct { ...@@ -81,26 +82,57 @@ type ProtocolInfo struct {
SecurityVersion string SecurityVersion string
} }
// AuthInfo defines the common interface for the auth information the users are interested in.
type AuthInfo interface {
AuthType() string
}
type authInfoKey struct{}
// NewContext creates a new context with authInfo attached.
func NewContext(ctx context.Context, authInfo AuthInfo) context.Context {
return context.WithValue(ctx, authInfoKey{}, authInfo)
}
// FromContext returns the authInfo in ctx if it exists.
func FromContext(ctx context.Context) (authInfo AuthInfo, ok bool) {
authInfo, ok = ctx.Value(authInfoKey{}).(AuthInfo)
return
}
// TransportAuthenticator defines the common interface for all the live gRPC wire // TransportAuthenticator defines the common interface for all the live gRPC wire
// protocols and supported transport security protocols (e.g., TLS, SSL). // protocols and supported transport security protocols (e.g., TLS, SSL).
type TransportAuthenticator interface { type TransportAuthenticator interface {
// ClientHandshake does the authentication handshake specified by the corresponding // ClientHandshake does the authentication handshake specified by the corresponding
// authentication protocol on rawConn for clients. // authentication protocol on rawConn for clients. It returns the authenticated
ClientHandshake(addr string, rawConn net.Conn, timeout time.Duration) (net.Conn, error) // connection and the corresponding auth information about the connection.
// ServerHandshake does the authentication handshake for servers. ClientHandshake(addr string, rawConn net.Conn, timeout time.Duration) (net.Conn, AuthInfo, error)
ServerHandshake(rawConn net.Conn) (net.Conn, error) // ServerHandshake does the authentication handshake for servers. It returns
// the authenticated connection and the corresponding auth information about
// the connection.
ServerHandshake(rawConn net.Conn) (net.Conn, AuthInfo, error)
// Info provides the ProtocolInfo of this TransportAuthenticator. // Info provides the ProtocolInfo of this TransportAuthenticator.
Info() ProtocolInfo Info() ProtocolInfo
Credentials Credentials
} }
// TLSInfo contains the auth information for a TLS authenticated connection.
// It implements the AuthInfo interface.
type TLSInfo struct {
state tls.ConnectionState
}
func (t TLSInfo) AuthType() string {
return "tls"
}
// tlsCreds is the credentials required for authenticating a connection using TLS. // tlsCreds is the credentials required for authenticating a connection using TLS.
type tlsCreds struct { type tlsCreds struct {
// TLS configuration // TLS configuration
config tls.Config config tls.Config
} }
func (c *tlsCreds) Info() ProtocolInfo { func (c tlsCreds) Info() ProtocolInfo {
return ProtocolInfo{ return ProtocolInfo{
SecurityProtocol: "tls", SecurityProtocol: "tls",
SecurityVersion: "1.2", SecurityVersion: "1.2",
...@@ -109,17 +141,21 @@ func (c *tlsCreds) Info() ProtocolInfo { ...@@ -109,17 +141,21 @@ func (c *tlsCreds) Info() ProtocolInfo {
// GetRequestMetadata returns nil, nil since TLS credentials does not have // GetRequestMetadata returns nil, nil since TLS credentials does not have
// metadata. // metadata.
func (c *tlsCreds) GetRequestMetadata(ctx context.Context) (map[string]string, error) { func (c *tlsCreds) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return nil, nil return nil, nil
} }
func (c *tlsCreds) RequireTransportSecurity() bool {
return true
}
type timeoutError struct{} type timeoutError struct{}
func (timeoutError) Error() string { return "credentials: Dial timed out" } func (timeoutError) Error() string { return "credentials: Dial timed out" }
func (timeoutError) Timeout() bool { return true } func (timeoutError) Timeout() bool { return true }
func (timeoutError) Temporary() bool { return true } func (timeoutError) Temporary() bool { return true }
func (c *tlsCreds) ClientHandshake(addr string, rawConn net.Conn, timeout time.Duration) (_ net.Conn, err error) { func (c *tlsCreds) ClientHandshake(addr string, rawConn net.Conn, timeout time.Duration) (_ net.Conn, _ AuthInfo, err error) {
// borrow some code from tls.DialWithDialer // borrow some code from tls.DialWithDialer
var errChannel chan error var errChannel chan error
if timeout != 0 { if timeout != 0 {
...@@ -146,18 +182,20 @@ func (c *tlsCreds) ClientHandshake(addr string, rawConn net.Conn, timeout time.D ...@@ -146,18 +182,20 @@ func (c *tlsCreds) ClientHandshake(addr string, rawConn net.Conn, timeout time.D
} }
if err != nil { if err != nil {
rawConn.Close() rawConn.Close()
return nil, err return nil, nil, err
} }
return conn, nil // TODO(zhaoq): Omit the auth info for client now. It is more for
// information than anything else.
return conn, nil, nil
} }
func (c *tlsCreds) ServerHandshake(rawConn net.Conn) (net.Conn, error) { func (c *tlsCreds) ServerHandshake(rawConn net.Conn) (net.Conn, AuthInfo, error) {
conn := tls.Server(rawConn, &c.config) conn := tls.Server(rawConn, &c.config)
if err := conn.Handshake(); err != nil { if err := conn.Handshake(); err != nil {
rawConn.Close() rawConn.Close()
return nil, err return nil, nil, err
} }
return conn, nil return conn, TLSInfo{conn.ConnectionState()}, nil
} }
// NewTLS uses c to construct a TransportAuthenticator based on TLS. // NewTLS uses c to construct a TransportAuthenticator based on TLS.
...@@ -199,72 +237,3 @@ func NewServerTLSFromFile(certFile, keyFile string) (TransportAuthenticator, err ...@@ -199,72 +237,3 @@ func NewServerTLSFromFile(certFile, keyFile string) (TransportAuthenticator, err
} }
return NewTLS(&tls.Config{Certificates: []tls.Certificate{cert}}), nil return NewTLS(&tls.Config{Certificates: []tls.Certificate{cert}}), nil
} }
// TokenSource supplies credentials from an oauth2.TokenSource.
type TokenSource struct {
oauth2.TokenSource
}
// GetRequestMetadata gets the request metadata as a map from a TokenSource.
func (ts TokenSource) GetRequestMetadata(ctx context.Context) (map[string]string, error) {
token, err := ts.Token()
if err != nil {
return nil, err
}
return map[string]string{
"authorization": token.TokenType + " " + token.AccessToken,
}, nil
}
// NewComputeEngine constructs the credentials that fetches access tokens from
// Google Compute Engine (GCE)'s metadata server. It is only valid to use this
// if your program is running on a GCE instance.
// TODO(dsymonds): Deprecate and remove this.
func NewComputeEngine() Credentials {
return TokenSource{google.ComputeTokenSource("")}
}
// serviceAccount represents credentials via JWT signing key.
type serviceAccount struct {
config *jwt.Config
}
func (s serviceAccount) GetRequestMetadata(ctx context.Context) (map[string]string, error) {
token, err := s.config.TokenSource(ctx).Token()
if err != nil {
return nil, err
}
return map[string]string{
"authorization": token.TokenType + " " + token.AccessToken,
}, nil
}
// NewServiceAccountFromKey constructs the credentials using the JSON key slice
// from a Google Developers service account.
func NewServiceAccountFromKey(jsonKey []byte, scope ...string) (Credentials, error) {
config, err := google.JWTConfigFromJSON(jsonKey, scope...)
if err != nil {
return nil, err
}
return serviceAccount{config: config}, nil
}
// NewServiceAccountFromFile constructs the credentials using the JSON key file
// of a Google Developers service account.
func NewServiceAccountFromFile(keyFile string, scope ...string) (Credentials, error) {
jsonKey, err := ioutil.ReadFile(keyFile)
if err != nil {
return nil, fmt.Errorf("credentials: failed to read the service account key file: %v", err)
}
return NewServiceAccountFromKey(jsonKey, scope...)
}
// NewApplicationDefault returns "Application Default Credentials". For more
// detail, see https://developers.google.com/accounts/docs/application-default-credentials.
func NewApplicationDefault(ctx context.Context, scope ...string) (Credentials, error) {
t, err := google.DefaultTokenSource(ctx, scope...)
if err != nil {
return nil, err
}
return TokenSource{t}, nil
}
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
// Package oauth implements gRPC credentials using OAuth.
package oauth
import (
"fmt"
"io/ioutil"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
"google.golang.org/grpc/credentials"
)
// TokenSource supplies credentials from an oauth2.TokenSource.
type TokenSource struct {
oauth2.TokenSource
}
// GetRequestMetadata gets the request metadata as a map from a TokenSource.
func (ts TokenSource) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
token, err := ts.Token()
if err != nil {
return nil, err
}
return map[string]string{
"authorization": token.TokenType + " " + token.AccessToken,
}, nil
}
func (ts TokenSource) RequireTransportSecurity() bool {
return true
}
type jwtAccess struct {
jsonKey []byte
}
func NewJWTAccessFromFile(keyFile string) (credentials.Credentials, error) {
jsonKey, err := ioutil.ReadFile(keyFile)
if err != nil {
return nil, fmt.Errorf("credentials: failed to read the service account key file: %v", err)
}
return NewJWTAccessFromKey(jsonKey)
}
func NewJWTAccessFromKey(jsonKey []byte) (credentials.Credentials, error) {
return jwtAccess{jsonKey}, nil
}
func (j jwtAccess) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
ts, err := google.JWTAccessTokenSourceFromJSON(j.jsonKey, uri[0])
if err != nil {
return nil, err
}
token, err := ts.Token()
if err != nil {
return nil, err
}
return map[string]string{
"authorization": token.TokenType + " " + token.AccessToken,
}, nil
}
func (j jwtAccess) RequireTransportSecurity() bool {
return true
}
// oauthAccess supplies credentials from a given token.
type oauthAccess struct {
token oauth2.Token
}
// NewOauthAccess constructs the credentials using a given token.
func NewOauthAccess(token *oauth2.Token) credentials.Credentials {
return oauthAccess{token: *token}
}
func (oa oauthAccess) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{
"authorization": oa.token.TokenType + " " + oa.token.AccessToken,
}, nil
}
func (oa oauthAccess) RequireTransportSecurity() bool {
return true
}
// NewComputeEngine constructs the credentials that fetches access tokens from
// Google Compute Engine (GCE)'s metadata server. It is only valid to use this
// if your program is running on a GCE instance.
// TODO(dsymonds): Deprecate and remove this.
func NewComputeEngine() credentials.Credentials {
return TokenSource{google.ComputeTokenSource("")}
}
// serviceAccount represents credentials via JWT signing key.
type serviceAccount struct {
config *jwt.Config
}
func (s serviceAccount) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
token, err := s.config.TokenSource(ctx).Token()
if err != nil {
return nil, err
}
return map[string]string{
"authorization": token.TokenType + " " + token.AccessToken,
}, nil
}
func (s serviceAccount) RequireTransportSecurity() bool {
return true
}
// NewServiceAccountFromKey constructs the credentials using the JSON key slice
// from a Google Developers service account.
func NewServiceAccountFromKey(jsonKey []byte, scope ...string) (credentials.Credentials, error) {
config, err := google.JWTConfigFromJSON(jsonKey, scope...)
if err != nil {
return nil, err
}
return serviceAccount{config: config}, nil
}
// NewServiceAccountFromFile constructs the credentials using the JSON key file
// of a Google Developers service account.
func NewServiceAccountFromFile(keyFile string, scope ...string) (credentials.Credentials, error) {
jsonKey, err := ioutil.ReadFile(keyFile)
if err != nil {
return nil, fmt.Errorf("credentials: failed to read the service account key file: %v", err)
}
return NewServiceAccountFromKey(jsonKey, scope...)
}
// NewApplicationDefault returns "Application Default Credentials". For more
// detail, see https://developers.google.com/accounts/docs/application-default-credentials.
func NewApplicationDefault(ctx context.Context, scope ...string) (credentials.Credentials, error) {
t, err := google.DefaultTokenSource(ctx, scope...)
if err != nil {
return nil, err
}
return TokenSource{t}, nil
}
gRPC in 3 minutes (Go)
======================
BACKGROUND
-------------
For this sample, we've already generated the server and client stubs from [helloworld.proto](examples/helloworld/proto/helloworld.proto).
PREREQUISITES
-------------
- This requires Go 1.4
- Requires that [GOPATH is set](https://golang.org/doc/code.html#GOPATH)
```sh
$ go help gopath
$ # ensure the PATH contains $GOPATH/bin
$ export PATH=$PATH:$GOPATH/bin
```
INSTALL
-------
```sh
$ go get -u github.com/grpc/grpc-go/examples/greeter_client
$ go get -u github.com/grpc/grpc-go/examples/greeter_server
```
TRY IT!
-------
- Run the server
```sh
$ greeter_server &
```
- Run the client
```sh
$ greeter_client
```
OPTIONAL - Rebuilding the generated code
----------------------------------------
1 First [install protoc](https://github.com/google/protobuf/blob/master/INSTALL.txt)
- For now, this needs to be installed from source
- This is will change once proto3 is officially released
2 Install the protoc Go plugin.
```sh
$ go get -a github.com/golang/protobuf/protoc-gen-go
$
$ # from this dir; invoke protoc
$ protoc -I ./helloworld/proto/ ./helloworld/proto/helloworld.proto --go_out=plugins=grpc:helloworld
```
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package main
import (
"log"
"os"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
const (
address = "localhost:50051"
defaultName = "world"
)
func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.Message)
}
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package main
import (
"log"
"net"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
const (
port = ":50051"
)
// server is used to implement hellowrld.GreeterServer.
type server struct{}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
}
func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
s.Serve(lis)
}
// Code generated by protoc-gen-go.
// source: helloworld.proto
// DO NOT EDIT!
/*
Package helloworld is a generated protocol buffer package.
It is generated from these files:
helloworld.proto
It has these top-level messages:
HelloRequest
HelloReply
*/
package helloworld
import proto "github.com/golang/protobuf/proto"
import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
// The request message containing the user's name.
type HelloRequest struct {
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
}
func (m *HelloRequest) Reset() { *m = HelloRequest{} }
func (m *HelloRequest) String() string { return proto.CompactTextString(m) }
func (*HelloRequest) ProtoMessage() {}
// The response message containing the greetings
type HelloReply struct {
Message string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"`
}
func (m *HelloReply) Reset() { *m = HelloReply{} }
func (m *HelloReply) String() string { return proto.CompactTextString(m) }
func (*HelloReply) ProtoMessage() {}
func init() {
}
// Client API for Greeter service
type GreeterClient interface {
// Sends a greeting
SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error)
}
type greeterClient struct {
cc *grpc.ClientConn
}
func NewGreeterClient(cc *grpc.ClientConn) GreeterClient {
return &greeterClient{cc}
}
func (c *greeterClient) SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) {
out := new(HelloReply)
err := grpc.Invoke(ctx, "/helloworld.Greeter/SayHello", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Greeter service
type GreeterServer interface {
// Sends a greeting
SayHello(context.Context, *HelloRequest) (*HelloReply, error)
}
func RegisterGreeterServer(s *grpc.Server, srv GreeterServer) {
s.RegisterService(&_Greeter_serviceDesc, srv)
}
func _Greeter_SayHello_Handler(srv interface{}, ctx context.Context, codec grpc.Codec, buf []byte) (interface{}, error) {
in := new(HelloRequest)
if err := codec.Unmarshal(buf, in); err != nil {
return nil, err
}
out, err := srv.(GreeterServer).SayHello(ctx, in)
if err != nil {
return nil, err
}
return out, nil
}
var _Greeter_serviceDesc = grpc.ServiceDesc{
ServiceName: "helloworld.Greeter",
HandlerType: (*GreeterServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "SayHello",
Handler: _Greeter_SayHello_Handler,
},
},
Streams: []grpc.StreamDesc{},
}
// Copyright 2015, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
syntax = "proto3";
option java_package = "io.grpc.examples";
option objc_class_prefix = "HLW";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
The route guide server and client demonstrate how to use grpc go libraries to The route guide server and client demonstrate how to use grpc go libraries to
perform unary, client streaming, server streaming and full duplex RPCs. perform unary, client streaming, server streaming and full duplex RPCs.
Please refer to [Getting Started Guide for Go] (https://github.com/grpc/grpc-common/blob/master/go/gotutorial.md) for more information. Please refer to [Getting Started Guide for Go] (examples/gotutorial.md) for more information.
See the definition of the route guide service in proto/route_guide.proto. See the definition of the route guide service in proto/route_guide.proto.
......
...@@ -46,7 +46,7 @@ import ( ...@@ -46,7 +46,7 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials"
pb "google.golang.org/grpc/examples/route_guide/proto" pb "google.golang.org/grpc/examples/route_guide/routeguide"
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
) )
...@@ -175,6 +175,8 @@ func main() { ...@@ -175,6 +175,8 @@ func main() {
creds = credentials.NewClientTLSFromCert(nil, sn) creds = credentials.NewClientTLSFromCert(nil, sn)
} }
opts = append(opts, grpc.WithTransportCredentials(creds)) opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithInsecure())
} }
conn, err := grpc.Dial(*serverAddr, opts...) conn, err := grpc.Dial(*serverAddr, opts...)
if err != nil { if err != nil {
......
...@@ -15,7 +15,7 @@ It has these top-level messages: ...@@ -15,7 +15,7 @@ It has these top-level messages:
RouteNote RouteNote
RouteSummary RouteSummary
*/ */
package proto package routeguide
import proto1 "github.com/golang/protobuf/proto" import proto1 "github.com/golang/protobuf/proto"
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
syntax = "proto3"; syntax = "proto3";
package proto; package routeguide;
// Interface exported by the server. // Interface exported by the server.
service RouteGuide { service RouteGuide {
......
...@@ -53,9 +53,9 @@ import ( ...@@ -53,9 +53,9 @@ import (
"google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
proto "github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
pb "google.golang.org/grpc/examples/route_guide/proto" pb "google.golang.org/grpc/examples/route_guide/routeguide"
) )
var ( var (
......
# Authentication # Authentication
As outlined <a href="https://github.com/grpc/grpc-common/blob/master/grpc-auth-support.md">here</a> gRPC supports a number of different mechanisms for asserting identity between an client and server. We'll present some code-samples here demonstrating how to provide TLS support encryption and identity assertions as well as passing OAuth2 tokens to services that support it. As outlined <a href="https://github.com/grpc/grpc/blob/master/doc/grpc-auth-support.md">here</a> gRPC supports a number of different mechanisms for asserting identity between an client and server. We'll present some code-samples here demonstrating how to provide TLS support encryption and identity assertions as well as passing OAuth2 tokens to services that support it.
# Enabling TLS on a gRPC client # Enabling TLS on a gRPC client
...@@ -26,13 +26,13 @@ server.Serve(lis) ...@@ -26,13 +26,13 @@ server.Serve(lis)
## Google Compute Engine (GCE) ## Google Compute Engine (GCE)
```Go ```Go
conn, err := grpc.Dial(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, ""), grpc.WithPerRPCCredentials(credentials.NewComputeEngine()))) conn, err := grpc.Dial(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, ""), grpc.WithPerRPCCredentials(oauth.NewComputeEngine())))
``` ```
## JWT ## JWT
```Go ```Go
jwtCreds, err := credentials.NewServiceAccountFromFile(*serviceAccountKeyFile, *oauthScope) jwtCreds, err := oauth.NewServiceAccountFromFile(*serviceAccountKeyFile, *oauthScope)
if err != nil { if err != nil {
log.Fatalf("Failed to create JWT credentials: %v", err) log.Fatalf("Failed to create JWT credentials: %v", err)
} }
......
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
Package glogger defines glog-based logging for grpc.
*/
package glogger
import (
"github.com/golang/glog"
"google.golang.org/grpc/grpclog"
)
func init() {
grpclog.SetLogger(&glogger{})
}
type glogger struct{}
func (g *glogger) Fatal(args ...interface{}) {
glog.Fatal(args...)
}
func (g *glogger) Fatalf(format string, args ...interface{}) {
glog.Fatalf(format, args...)
}
func (g *glogger) Fatalln(args ...interface{}) {
glog.Fatalln(args...)
}
func (g *glogger) Print(args ...interface{}) {
glog.Info(args...)
}
func (g *glogger) Printf(format string, args ...interface{}) {
glog.Infof(format, args...)
}
func (g *glogger) Println(args ...interface{}) {
glog.Infoln(args...)
}
...@@ -32,26 +32,17 @@ ...@@ -32,26 +32,17 @@
*/ */
/* /*
Package log defines logging for grpc. Package grpclog defines logging for grpc.
*/ */
package grpclog package grpclog
import ( import (
"log" "log"
"os" "os"
"github.com/golang/glog"
) )
var ( // Use golang's standard logger by default.
// GLogger is a Logger that uses glog. This is the default logger. var logger Logger = log.New(os.Stderr, "", log.LstdFlags)
GLogger Logger = &glogger{}
// StdLogger is a Logger that uses golang's standard logger.
StdLogger Logger = log.New(os.Stderr, "", log.LstdFlags)
logger = GLogger
)
// Logger mimics golang's standard Logger as an interface. // Logger mimics golang's standard Logger as an interface.
type Logger interface { type Logger interface {
...@@ -73,12 +64,12 @@ func Fatal(args ...interface{}) { ...@@ -73,12 +64,12 @@ func Fatal(args ...interface{}) {
logger.Fatal(args...) logger.Fatal(args...)
} }
// Fatal is equivalent to Printf() followed by a call to os.Exit() with a non-zero exit code. // Fatalf is equivalent to Printf() followed by a call to os.Exit() with a non-zero exit code.
func Fatalf(format string, args ...interface{}) { func Fatalf(format string, args ...interface{}) {
logger.Fatalf(format, args...) logger.Fatalf(format, args...)
} }
// Fatal is equivalent to Println() followed by a call to os.Exit()) with a non-zero exit code. // Fatalln is equivalent to Println() followed by a call to os.Exit()) with a non-zero exit code.
func Fatalln(args ...interface{}) { func Fatalln(args ...interface{}) {
logger.Fatalln(args...) logger.Fatalln(args...)
} }
...@@ -97,29 +88,3 @@ func Printf(format string, args ...interface{}) { ...@@ -97,29 +88,3 @@ func Printf(format string, args ...interface{}) {
func Println(args ...interface{}) { func Println(args ...interface{}) {
logger.Println(args...) logger.Println(args...)
} }
type glogger struct{}
func (g *glogger) Fatal(args ...interface{}) {
glog.Fatal(args...)
}
func (g *glogger) Fatalf(format string, args ...interface{}) {
glog.Fatalf(format, args...)
}
func (g *glogger) Fatalln(args ...interface{}) {
glog.Fatalln(args...)
}
func (g *glogger) Print(args ...interface{}) {
glog.Info(args...)
}
func (g *glogger) Printf(format string, args ...interface{}) {
glog.Infof(format, args...)
}
func (g *glogger) Println(args ...interface{}) {
glog.Infoln(args...)
}
// Code generated by protoc-gen-go.
// source: health.proto
// DO NOT EDIT!
/*
Package grpc_health_v1alpha is a generated protocol buffer package.
It is generated from these files:
health.proto
It has these top-level messages:
HealthCheckRequest
HealthCheckResponse
*/
package grpc_health_v1alpha
import proto "github.com/golang/protobuf/proto"
import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
type HealthCheckResponse_ServingStatus int32
const (
HealthCheckResponse_UNKNOWN HealthCheckResponse_ServingStatus = 0
HealthCheckResponse_SERVING HealthCheckResponse_ServingStatus = 1
HealthCheckResponse_NOT_SERVING HealthCheckResponse_ServingStatus = 2
)
var HealthCheckResponse_ServingStatus_name = map[int32]string{
0: "UNKNOWN",
1: "SERVING",
2: "NOT_SERVING",
}
var HealthCheckResponse_ServingStatus_value = map[string]int32{
"UNKNOWN": 0,
"SERVING": 1,
"NOT_SERVING": 2,
}
func (x HealthCheckResponse_ServingStatus) String() string {
return proto.EnumName(HealthCheckResponse_ServingStatus_name, int32(x))
}
type HealthCheckRequest struct {
Service string `protobuf:"bytes,2,opt,name=service" json:"service,omitempty"`
}
func (m *HealthCheckRequest) Reset() { *m = HealthCheckRequest{} }
func (m *HealthCheckRequest) String() string { return proto.CompactTextString(m) }
func (*HealthCheckRequest) ProtoMessage() {}
type HealthCheckResponse struct {
Status HealthCheckResponse_ServingStatus `protobuf:"varint,1,opt,name=status,enum=grpc.health.v1alpha.HealthCheckResponse_ServingStatus" json:"status,omitempty"`
}
func (m *HealthCheckResponse) Reset() { *m = HealthCheckResponse{} }
func (m *HealthCheckResponse) String() string { return proto.CompactTextString(m) }
func (*HealthCheckResponse) ProtoMessage() {}
func init() {
proto.RegisterEnum("grpc.health.v1alpha.HealthCheckResponse_ServingStatus", HealthCheckResponse_ServingStatus_name, HealthCheckResponse_ServingStatus_value)
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
// Client API for HealthCheck service
type HealthCheckClient interface {
Check(ctx context.Context, in *HealthCheckRequest, opts ...grpc.CallOption) (*HealthCheckResponse, error)
}
type healthCheckClient struct {
cc *grpc.ClientConn
}
func NewHealthCheckClient(cc *grpc.ClientConn) HealthCheckClient {
return &healthCheckClient{cc}
}
func (c *healthCheckClient) Check(ctx context.Context, in *HealthCheckRequest, opts ...grpc.CallOption) (*HealthCheckResponse, error) {
out := new(HealthCheckResponse)
err := grpc.Invoke(ctx, "/grpc.health.v1alpha.HealthCheck/Check", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for HealthCheck service
type HealthCheckServer interface {
Check(context.Context, *HealthCheckRequest) (*HealthCheckResponse, error)
}
func RegisterHealthCheckServer(s *grpc.Server, srv HealthCheckServer) {
s.RegisterService(&_HealthCheck_serviceDesc, srv)
}
func _HealthCheck_Check_Handler(srv interface{}, ctx context.Context, codec grpc.Codec, buf []byte) (interface{}, error) {
in := new(HealthCheckRequest)
if err := codec.Unmarshal(buf, in); err != nil {
return nil, err
}
out, err := srv.(HealthCheckServer).Check(ctx, in)
if err != nil {
return nil, err
}
return out, nil
}
var _HealthCheck_serviceDesc = grpc.ServiceDesc{
ServiceName: "grpc.health.v1alpha.HealthCheck",
HandlerType: (*HealthCheckServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Check",
Handler: _HealthCheck_Check_Handler,
},
},
Streams: []grpc.StreamDesc{},
}
syntax = "proto3";
package grpc.health.v1alpha;
message HealthCheckRequest {
string service = 2;
}
message HealthCheckResponse {
enum ServingStatus {
UNKNOWN = 0;
SERVING = 1;
NOT_SERVING = 2;
}
ServingStatus status = 1;
}
service HealthCheck{
rpc Check(HealthCheckRequest) returns (HealthCheckResponse);
}
// Package health provides some utility functions to health-check a server. The implementation
// is based on protobuf. Users need to write their own implementations if other IDLs are used.
package health
import (
"sync"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
healthpb "google.golang.org/grpc/health/grpc_health_v1alpha"
)
type HealthServer struct {
mu sync.Mutex
// statusMap stores the serving status of the services this HealthServer monitors.
statusMap map[string]healthpb.HealthCheckResponse_ServingStatus
}
func NewHealthServer() *HealthServer {
return &HealthServer{
statusMap: make(map[string]healthpb.HealthCheckResponse_ServingStatus),
}
}
func (s *HealthServer) Check(ctx context.Context, in *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) {
s.mu.Lock()
defer s.mu.Unlock()
if in.Service == "" {
// check the server overall health status.
return &healthpb.HealthCheckResponse{
Status: healthpb.HealthCheckResponse_SERVING,
}, nil
}
if status, ok := s.statusMap[in.Service]; ok {
return &healthpb.HealthCheckResponse{
Status: status,
}, nil
}
return nil, grpc.Errorf(codes.NotFound, "unknown service")
}
// SetServingStatus is called when need to reset the serving status of a service
// or insert a new service entry into the statusMap.
func (s *HealthServer) SetServingStatus(service string, status healthpb.HealthCheckResponse_ServingStatus) {
s.mu.Lock()
s.statusMap[service] = status
s.mu.Unlock()
}
...@@ -40,12 +40,16 @@ import ( ...@@ -40,12 +40,16 @@ import (
"net" "net"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"golang.org/x/net/context" "golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/oauth"
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
testpb "google.golang.org/grpc/interop/grpc_testing" testpb "google.golang.org/grpc/interop/grpc_testing"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
...@@ -67,8 +71,13 @@ var ( ...@@ -67,8 +71,13 @@ var (
client_streaming : request streaming with single response; client_streaming : request streaming with single response;
server_streaming : single request with response streaming; server_streaming : single request with response streaming;
ping_pong : full-duplex streaming; ping_pong : full-duplex streaming;
empty_stream : full-duplex streaming with zero message;
timeout_on_sleeping_server: fullduplex streaming;
compute_engine_creds: large_unary with compute engine auth; compute_engine_creds: large_unary with compute engine auth;
service_account_creds: large_unary with service account auth; service_account_creds: large_unary with service account auth;
jwt_token_creds: large_unary with jwt token auth;
per_rpc_creds: large_unary with per rpc token;
oauth2_auth_token: large_unary with oauth2 token auth;
cancel_after_begin: cancellation after metadata has been sent but before payloads are sent; cancel_after_begin: cancellation after metadata has been sent but before payloads are sent;
cancel_after_first_response: cancellation after receiving 1st message from the server.`) cancel_after_first_response: cancellation after receiving 1st message from the server.`)
) )
...@@ -244,6 +253,44 @@ func doPingPong(tc testpb.TestServiceClient) { ...@@ -244,6 +253,44 @@ func doPingPong(tc testpb.TestServiceClient) {
grpclog.Println("Pingpong done") grpclog.Println("Pingpong done")
} }
func doEmptyStream(tc testpb.TestServiceClient) {
stream, err := tc.FullDuplexCall(context.Background())
if err != nil {
grpclog.Fatalf("%v.FullDuplexCall(_) = _, %v", tc, err)
}
if err := stream.CloseSend(); err != nil {
grpclog.Fatalf("%v.CloseSend() got %v, want %v", stream, err, nil)
}
if _, err := stream.Recv(); err != io.EOF {
grpclog.Fatalf("%v failed to complete the empty stream test: %v", stream, err)
}
grpclog.Println("Emptystream done")
}
func doTimeoutOnSleepingServer(tc testpb.TestServiceClient) {
ctx, _ := context.WithTimeout(context.Background(), 1*time.Millisecond)
stream, err := tc.FullDuplexCall(ctx)
if err != nil {
if grpc.Code(err) == codes.DeadlineExceeded {
grpclog.Println("TimeoutOnSleepingServer done")
return
}
grpclog.Fatalf("%v.FullDuplexCall(_) = _, %v", tc, err)
}
pl := newPayload(testpb.PayloadType_COMPRESSABLE, 27182)
req := &testpb.StreamingOutputCallRequest{
ResponseType: testpb.PayloadType_COMPRESSABLE.Enum(),
Payload: pl,
}
if err := stream.Send(req); err != nil {
grpclog.Fatalf("%v.Send(%v) = %v", stream, req, err)
}
if _, err := stream.Recv(); grpc.Code(err) != codes.DeadlineExceeded {
grpclog.Fatalf("%v.Recv() = _, %v, want error code %d", stream, err, codes.DeadlineExceeded)
}
grpclog.Println("TimeoutOnSleepingServer done")
}
func doComputeEngineCreds(tc testpb.TestServiceClient) { func doComputeEngineCreds(tc testpb.TestServiceClient) {
pl := newPayload(testpb.PayloadType_COMPRESSABLE, largeReqSize) pl := newPayload(testpb.PayloadType_COMPRESSABLE, largeReqSize)
req := &testpb.SimpleRequest{ req := &testpb.SimpleRequest{
...@@ -301,10 +348,96 @@ func doServiceAccountCreds(tc testpb.TestServiceClient) { ...@@ -301,10 +348,96 @@ func doServiceAccountCreds(tc testpb.TestServiceClient) {
grpclog.Println("ServiceAccountCreds done") grpclog.Println("ServiceAccountCreds done")
} }
func doJWTTokenCreds(tc testpb.TestServiceClient) {
pl := newPayload(testpb.PayloadType_COMPRESSABLE, largeReqSize)
req := &testpb.SimpleRequest{
ResponseType: testpb.PayloadType_COMPRESSABLE.Enum(),
ResponseSize: proto.Int32(int32(largeRespSize)),
Payload: pl,
FillUsername: proto.Bool(true),
}
reply, err := tc.UnaryCall(context.Background(), req)
if err != nil {
grpclog.Fatal("/TestService/UnaryCall RPC failed: ", err)
}
jsonKey := getServiceAccountJSONKey()
user := reply.GetUsername()
if !strings.Contains(string(jsonKey), user) {
grpclog.Fatalf("Got user name %q which is NOT a substring of %q.", user, jsonKey)
}
grpclog.Println("JWTtokenCreds done")
}
func getToken() *oauth2.Token {
jsonKey := getServiceAccountJSONKey()
config, err := google.JWTConfigFromJSON(jsonKey, *oauthScope)
if err != nil {
grpclog.Fatalf("Failed to get the config: %v", err)
}
token, err := config.TokenSource(context.Background()).Token()
if err != nil {
grpclog.Fatalf("Failed to get the token: %v", err)
}
return token
}
func doOauth2TokenCreds(tc testpb.TestServiceClient) {
pl := newPayload(testpb.PayloadType_COMPRESSABLE, largeReqSize)
req := &testpb.SimpleRequest{
ResponseType: testpb.PayloadType_COMPRESSABLE.Enum(),
ResponseSize: proto.Int32(int32(largeRespSize)),
Payload: pl,
FillUsername: proto.Bool(true),
FillOauthScope: proto.Bool(true),
}
reply, err := tc.UnaryCall(context.Background(), req)
if err != nil {
grpclog.Fatal("/TestService/UnaryCall RPC failed: ", err)
}
jsonKey := getServiceAccountJSONKey()
user := reply.GetUsername()
scope := reply.GetOauthScope()
if !strings.Contains(string(jsonKey), user) {
grpclog.Fatalf("Got user name %q which is NOT a substring of %q.", user, jsonKey)
}
if !strings.Contains(*oauthScope, scope) {
grpclog.Fatalf("Got OAuth scope %q which is NOT a substring of %q.", scope, *oauthScope)
}
grpclog.Println("Oauth2TokenCreds done")
}
func doPerRPCCreds(tc testpb.TestServiceClient) {
jsonKey := getServiceAccountJSONKey()
pl := newPayload(testpb.PayloadType_COMPRESSABLE, largeReqSize)
req := &testpb.SimpleRequest{
ResponseType: testpb.PayloadType_COMPRESSABLE.Enum(),
ResponseSize: proto.Int32(int32(largeRespSize)),
Payload: pl,
FillUsername: proto.Bool(true),
FillOauthScope: proto.Bool(true),
}
token := getToken()
kv := map[string]string{"authorization": token.TokenType + " " + token.AccessToken}
ctx := metadata.NewContext(context.Background(), metadata.MD{"authorization": []string{kv["authorization"]}})
reply, err := tc.UnaryCall(ctx, req)
if err != nil {
grpclog.Fatal("/TestService/UnaryCall RPC failed: ", err)
}
user := reply.GetUsername()
scope := reply.GetOauthScope()
if !strings.Contains(string(jsonKey), user) {
grpclog.Fatalf("Got user name %q which is NOT a substring of %q.", user, jsonKey)
}
if !strings.Contains(*oauthScope, scope) {
grpclog.Fatalf("Got OAuth scope %q which is NOT a substring of %q.", scope, *oauthScope)
}
grpclog.Println("PerRPCCreds done")
}
var ( var (
testMetadata = metadata.MD{ testMetadata = metadata.MD{
"key1": "value1", "key1": []string{"value1"},
"key2": "value2", "key2": []string{"value2"},
} }
) )
...@@ -373,14 +506,24 @@ func main() { ...@@ -373,14 +506,24 @@ func main() {
} }
opts = append(opts, grpc.WithTransportCredentials(creds)) opts = append(opts, grpc.WithTransportCredentials(creds))
if *testCase == "compute_engine_creds" { if *testCase == "compute_engine_creds" {
opts = append(opts, grpc.WithPerRPCCredentials(credentials.NewComputeEngine())) opts = append(opts, grpc.WithPerRPCCredentials(oauth.NewComputeEngine()))
} else if *testCase == "service_account_creds" { } else if *testCase == "service_account_creds" {
jwtCreds, err := credentials.NewServiceAccountFromFile(*serviceAccountKeyFile, *oauthScope) jwtCreds, err := oauth.NewServiceAccountFromFile(*serviceAccountKeyFile, *oauthScope)
if err != nil { if err != nil {
grpclog.Fatalf("Failed to create JWT credentials: %v", err) grpclog.Fatalf("Failed to create JWT credentials: %v", err)
} }
opts = append(opts, grpc.WithPerRPCCredentials(jwtCreds)) opts = append(opts, grpc.WithPerRPCCredentials(jwtCreds))
} else if *testCase == "jwt_token_creds" {
jwtCreds, err := oauth.NewJWTAccessFromFile(*serviceAccountKeyFile)
if err != nil {
grpclog.Fatalf("Failed to create JWT credentials: %v", err)
} }
opts = append(opts, grpc.WithPerRPCCredentials(jwtCreds))
} else if *testCase == "oauth2_auth_token" {
opts = append(opts, grpc.WithPerRPCCredentials(oauth.NewOauthAccess(getToken())))
}
} else {
opts = append(opts, grpc.WithInsecure())
} }
conn, err := grpc.Dial(serverAddr, opts...) conn, err := grpc.Dial(serverAddr, opts...)
if err != nil { if err != nil {
...@@ -399,6 +542,10 @@ func main() { ...@@ -399,6 +542,10 @@ func main() {
doServerStreaming(tc) doServerStreaming(tc)
case "ping_pong": case "ping_pong":
doPingPong(tc) doPingPong(tc)
case "empty_stream":
doEmptyStream(tc)
case "timeout_on_sleeping_server":
doTimeoutOnSleepingServer(tc)
case "compute_engine_creds": case "compute_engine_creds":
if !*useTLS { if !*useTLS {
grpclog.Fatalf("TLS is not enabled. TLS is required to execute compute_engine_creds test case.") grpclog.Fatalf("TLS is not enabled. TLS is required to execute compute_engine_creds test case.")
...@@ -409,6 +556,21 @@ func main() { ...@@ -409,6 +556,21 @@ func main() {
grpclog.Fatalf("TLS is not enabled. TLS is required to execute service_account_creds test case.") grpclog.Fatalf("TLS is not enabled. TLS is required to execute service_account_creds test case.")
} }
doServiceAccountCreds(tc) doServiceAccountCreds(tc)
case "jwt_token_creds":
if !*useTLS {
grpclog.Fatalf("TLS is not enabled. TLS is required to execute jwt_token_creds test case.")
}
doJWTTokenCreds(tc)
case "per_rpc_creds":
if !*useTLS {
grpclog.Fatalf("TLS is not enabled. TLS is required to execute per_rpc_creds test case.")
}
doPerRPCCreds(tc)
case "oauth2_auth_token":
if !*useTLS {
grpclog.Fatalf("TLS is not enabled. TLS is required to execute oauth2_auth_token test case.")
}
doOauth2TokenCreds(tc)
case "cancel_after_begin": case "cancel_after_begin":
doCancelAfterBegin(tc) doCancelAfterBegin(tc)
case "cancel_after_first_response": case "cancel_after_first_response":
......
// Code generated by protoc-gen-go. // Code generated by protoc-gen-go.
// source: src/google.golang.org/grpc/test/grpc_testing/test.proto // source: test.proto
// DO NOT EDIT! // DO NOT EDIT!
/* /*
Package grpc_testing is a generated protocol buffer package. Package grpc_testing is a generated protocol buffer package.
It is generated from these files: It is generated from these files:
src/google.golang.org/grpc/test/grpc_testing/test.proto test.proto
It has these top-level messages: It has these top-level messages:
Empty Empty
......
...@@ -64,7 +64,7 @@ func encodeKeyValue(k, v string) (string, string) { ...@@ -64,7 +64,7 @@ func encodeKeyValue(k, v string) (string, string) {
if isASCII(v) { if isASCII(v) {
return k, v return k, v
} }
key := k + binHdrSuffix key := strings.ToLower(k + binHdrSuffix)
val := base64.StdEncoding.EncodeToString([]byte(v)) val := base64.StdEncoding.EncodeToString([]byte(v))
return key, string(val) return key, string(val)
} }
...@@ -85,14 +85,14 @@ func DecodeKeyValue(k, v string) (string, string, error) { ...@@ -85,14 +85,14 @@ func DecodeKeyValue(k, v string) (string, string, error) {
// MD is a mapping from metadata keys to values. Users should use the following // MD is a mapping from metadata keys to values. Users should use the following
// two convenience functions New and Pairs to generate MD. // two convenience functions New and Pairs to generate MD.
type MD map[string]string type MD map[string][]string
// New creates a MD from given key-value map. // New creates a MD from given key-value map.
func New(m map[string]string) MD { func New(m map[string]string) MD {
md := MD{} md := MD{}
for k, v := range m { for k, v := range m {
key, val := encodeKeyValue(k, v) key, val := encodeKeyValue(k, v)
md[key] = val md[key] = append(md[key], val)
} }
return md return md
} }
...@@ -111,7 +111,7 @@ func Pairs(kv ...string) MD { ...@@ -111,7 +111,7 @@ func Pairs(kv ...string) MD {
continue continue
} }
key, val := encodeKeyValue(k, s) key, val := encodeKeyValue(k, s)
md[key] = val md[key] = append(md[key], val)
} }
return md return md
} }
...@@ -125,7 +125,9 @@ func (md MD) Len() int { ...@@ -125,7 +125,9 @@ func (md MD) Len() int {
func (md MD) Copy() MD { func (md MD) Copy() MD {
out := MD{} out := MD{}
for k, v := range md { for k, v := range md {
out[k] = v for _, i := range v {
out[k] = append(out[k], i)
}
} }
return out return out
} }
......
package etcd
import (
"log"
"sync"
etcdcl "github.com/coreos/etcd/client"
"golang.org/x/net/context"
"google.golang.org/grpc/naming"
)
type kv struct {
key, value string
}
// recvBuffer is an unbounded channel of *kv to record all the pending changes from etcd server.
type recvBuffer struct {
c chan *kv
mu sync.Mutex
stopping bool
backlog []*kv
}
func newRecvBuffer() *recvBuffer {
b := &recvBuffer{
c: make(chan *kv, 1),
}
return b
}
func (b *recvBuffer) put(r *kv) {
b.mu.Lock()
defer b.mu.Unlock()
if b.stopping {
return
}
b.backlog = append(b.backlog, r)
select {
case b.c <- b.backlog[0]:
b.backlog = b.backlog[1:]
default:
}
}
func (b *recvBuffer) load() {
b.mu.Lock()
defer b.mu.Unlock()
if b.stopping || len(b.backlog) == 0 {
return
}
select {
case b.c <- b.backlog[0]:
b.backlog = b.backlog[1:]
default:
}
}
func (b *recvBuffer) get() <-chan *kv {
return b.c
}
// stop terminates the recvBuffer. After it is called, the recvBuffer is not usable any more.
func (b *recvBuffer) stop() {
b.mu.Lock()
b.stopping = true
close(b.c)
b.mu.Unlock()
}
type etcdNR struct {
kAPI etcdcl.KeysAPI
recv *recvBuffer
ctx context.Context
cancel context.CancelFunc
}
// NewETCDNR creates an etcd NameResolver.
func NewETCDNR(cfg etcdcl.Config) (naming.Resolver, error) {
c, err := etcdcl.New(cfg)
if err != nil {
return nil, err
}
kAPI := etcdcl.NewKeysAPI(c)
ctx, cancel := context.WithCancel(context.Background())
return &etcdNR{
kAPI: kAPI,
recv: newRecvBuffer(),
ctx: ctx,
cancel: cancel,
}, nil
}
// getNode builds the resulting key-value map starting from node recursively.
func getNode(node *etcdcl.Node, res map[string]string) {
if !node.Dir {
res[node.Key] = node.Value
return
}
for _, val := range node.Nodes {
getNode(val, res)
}
}
func (nr *etcdNR) Get(target string) map[string]string {
resp, err := nr.kAPI.Get(nr.ctx, target, &etcdcl.GetOptions{Recursive: true, Sort: true})
if err != nil {
log.Printf("etcdNR.Get(_) stopped: %v", err)
return nil
}
res := make(map[string]string)
getNode(resp.Node, res)
return res
}
func (nr *etcdNR) Watch(target string) {
watcher := nr.kAPI.Watcher(target, &etcdcl.WatcherOptions{Recursive: true})
for {
resp, err := watcher.Next(nr.ctx)
if err != nil {
log.Printf("etcdNR.Watch(_) stopped: %v", err)
break
}
if resp.Node.Dir {
continue
}
entry := &kv{key: resp.Node.Key, value: resp.Node.Value}
nr.recv.put(entry)
}
}
func (nr *etcdNR) GetUpdate() (string, string) {
i := <-nr.recv.get()
nr.recv.load()
if i == nil {
return "", ""
}
// returns key and the corresponding value of the updated kv pair
return i.key, i.value
}
func (nr *etcdNR) Stop() {
nr.recv.stop()
nr.cancel()
}
package naming
// Resolver dose name resolution and watches for the resolution changes.
type Resolver interface {
// Get gets a snapshot of the current name resolution results for target.
Get(target string) map[string]string
// Watch watches for the name resolution changes on target. It blocks until Stop() is invoked. The watch results are obtained via GetUpdate().
Watch(target string)
// GetUpdate returns a name resolution change when watch is triggered. It blocks until it observes a change. The caller needs to call it again to get the next change.
GetUpdate() (string, string)
// Stop shuts down the NameResolver.
Stop()
}
...@@ -277,28 +277,29 @@ func convertCode(err error) codes.Code { ...@@ -277,28 +277,29 @@ func convertCode(err error) codes.Code {
const ( const (
// how long to wait after the first failure before retrying // how long to wait after the first failure before retrying
baseDelay = 1.0 * time.Second baseDelay = 1.0 * time.Second
// upper bound on backoff delay // upper bound of backoff delay
maxDelay = 120 * time.Second maxDelay = 120 * time.Second
backoffFactor = 2.0 // backoff increases by this factor on each retry // backoff increases by this factor on each retry
backoffRange = 0.4 // backoff is randomized downwards by this factor backoffFactor = 1.6
// backoff is randomized downwards by this factor
backoffJitter = 0.2
) )
// backoff returns a value in [0, maxDelay] that increases exponentially with func backoff(retries int) (t time.Duration) {
// retries, starting from baseDelay. if retries == 0 {
func backoff(retries int) time.Duration { return baseDelay
}
backoff, max := float64(baseDelay), float64(maxDelay) backoff, max := float64(baseDelay), float64(maxDelay)
for backoff < max && retries > 0 { for backoff < max && retries > 0 {
backoff = backoff * backoffFactor backoff *= backoffFactor
retries-- retries--
} }
if backoff > max { if backoff > max {
backoff = max backoff = max
} }
// Randomize backoff delays so that if a cluster of requests start at // Randomize backoff delays so that if a cluster of requests start at
// the same time, they won't operate in lockstep. We just subtract up // the same time, they won't operate in lockstep.
// to 40% so that we obey maxDelay. backoff *= 1 + backoffJitter*(rand.Float64()*2-1)
backoff -= backoff * backoffRange * rand.Float64()
if backoff < 0 { if backoff < 0 {
return 0 return 0
} }
......
...@@ -43,6 +43,7 @@ import ( ...@@ -43,6 +43,7 @@ import (
"sync" "sync"
"golang.org/x/net/context" "golang.org/x/net/context"
"golang.org/x/net/trace"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
...@@ -139,17 +140,20 @@ func NewServer(opt ...ServerOption) *Server { ...@@ -139,17 +140,20 @@ func NewServer(opt ...ServerOption) *Server {
// server. Called from the IDL generated code. This must be called before // server. Called from the IDL generated code. This must be called before
// invoking Serve. // invoking Serve.
func (s *Server) RegisterService(sd *ServiceDesc, ss interface{}) { func (s *Server) RegisterService(sd *ServiceDesc, ss interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
// Does some sanity checks.
if _, ok := s.m[sd.ServiceName]; ok {
grpclog.Fatalf("grpc: Server.RegisterService found duplicate service registration for %q", sd.ServiceName)
}
ht := reflect.TypeOf(sd.HandlerType).Elem() ht := reflect.TypeOf(sd.HandlerType).Elem()
st := reflect.TypeOf(ss) st := reflect.TypeOf(ss)
if !st.Implements(ht) { if !st.Implements(ht) {
grpclog.Fatalf("grpc: Server.RegisterService found the handler of type %v that does not satisfy %v", st, ht) grpclog.Fatalf("grpc: Server.RegisterService found the handler of type %v that does not satisfy %v", st, ht)
} }
s.register(sd, ss)
}
func (s *Server) register(sd *ServiceDesc, ss interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.m[sd.ServiceName]; ok {
grpclog.Fatalf("grpc: Server.RegisterService found duplicate service registration for %q", sd.ServiceName)
}
srv := &service{ srv := &service{
server: ss, server: ss,
md: make(map[string]*MethodDesc), md: make(map[string]*MethodDesc),
...@@ -195,8 +199,9 @@ func (s *Server) Serve(lis net.Listener) error { ...@@ -195,8 +199,9 @@ func (s *Server) Serve(lis net.Listener) error {
if err != nil { if err != nil {
return err return err
} }
var authInfo credentials.AuthInfo
if creds, ok := s.opts.creds.(credentials.TransportAuthenticator); ok { if creds, ok := s.opts.creds.(credentials.TransportAuthenticator); ok {
c, err = creds.ServerHandshake(c) c, authInfo, err = creds.ServerHandshake(c)
if err != nil { if err != nil {
grpclog.Println("grpc: Server.Serve failed to complete security handshake.") grpclog.Println("grpc: Server.Serve failed to complete security handshake.")
continue continue
...@@ -208,7 +213,7 @@ func (s *Server) Serve(lis net.Listener) error { ...@@ -208,7 +213,7 @@ func (s *Server) Serve(lis net.Listener) error {
c.Close() c.Close()
return nil return nil
} }
st, err := transport.NewServerTransport("http2", c, s.opts.maxConcurrentStreams) st, err := transport.NewServerTransport("http2", c, s.opts.maxConcurrentStreams, authInfo)
if err != nil { if err != nil {
s.mu.Unlock() s.mu.Unlock()
c.Close() c.Close()
...@@ -244,13 +249,26 @@ func (s *Server) sendResponse(t transport.ServerTransport, stream *transport.Str ...@@ -244,13 +249,26 @@ func (s *Server) sendResponse(t transport.ServerTransport, stream *transport.Str
return t.Write(stream, p, opts) return t.Write(stream, p, opts)
} }
func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport.Stream, srv *service, md *MethodDesc) { func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport.Stream, srv *service, md *MethodDesc) (err error) {
var traceInfo traceInfo
if EnableTracing {
traceInfo.tr = trace.New("grpc.Recv."+methodFamily(stream.Method()), stream.Method())
defer traceInfo.tr.Finish()
traceInfo.firstLine.client = false
traceInfo.tr.LazyLog(&traceInfo.firstLine, false)
defer func() {
if err != nil && err != io.EOF {
traceInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
traceInfo.tr.SetError()
}
}()
}
p := &parser{s: stream} p := &parser{s: stream}
for { for {
pf, req, err := p.recvMsg() pf, req, err := p.recvMsg()
if err == io.EOF { if err == io.EOF {
// The entire stream is done (for unary RPC only). // The entire stream is done (for unary RPC only).
return return err
} }
if err != nil { if err != nil {
switch err := err.(type) { switch err := err.(type) {
...@@ -263,7 +281,10 @@ func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport. ...@@ -263,7 +281,10 @@ func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport.
default: default:
panic(fmt.Sprintf("grpc: Unexpected error (%T) from recvMsg: %v", err, err)) panic(fmt.Sprintf("grpc: Unexpected error (%T) from recvMsg: %v", err, err))
} }
return return err
}
if traceInfo.tr != nil {
traceInfo.tr.LazyLog(&payload{sent: false, msg: req}, true)
} }
switch pf { switch pf {
case compressionNone: case compressionNone:
...@@ -280,38 +301,59 @@ func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport. ...@@ -280,38 +301,59 @@ func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport.
} }
if err := t.WriteStatus(stream, statusCode, statusDesc); err != nil { if err := t.WriteStatus(stream, statusCode, statusDesc); err != nil {
grpclog.Printf("grpc: Server.processUnaryRPC failed to write status: %v", err) grpclog.Printf("grpc: Server.processUnaryRPC failed to write status: %v", err)
return err
} }
return return nil
} }
opts := &transport.Options{ opts := &transport.Options{
Last: true, Last: true,
Delay: false, Delay: false,
} }
if err := s.sendResponse(t, stream, reply, compressionNone, opts); err != nil { if err := s.sendResponse(t, stream, reply, compressionNone, opts); err != nil {
if _, ok := err.(transport.ConnectionError); ok { switch err := err.(type) {
return case transport.ConnectionError:
} // Nothing to do here.
if e, ok := err.(transport.StreamError); ok { case transport.StreamError:
statusCode = e.Code statusCode = err.Code
statusDesc = e.Desc statusDesc = err.Desc
} else { default:
statusCode = codes.Unknown statusCode = codes.Unknown
statusDesc = err.Error() statusDesc = err.Error()
} }
return err
}
if traceInfo.tr != nil {
traceInfo.tr.LazyLog(&payload{sent: true, msg: reply}, true)
} }
t.WriteStatus(stream, statusCode, statusDesc) return t.WriteStatus(stream, statusCode, statusDesc)
default: default:
panic(fmt.Sprintf("payload format to be supported: %d", pf)) panic(fmt.Sprintf("payload format to be supported: %d", pf))
} }
} }
} }
func (s *Server) processStreamingRPC(t transport.ServerTransport, stream *transport.Stream, srv *service, sd *StreamDesc) { func (s *Server) processStreamingRPC(t transport.ServerTransport, stream *transport.Stream, srv *service, sd *StreamDesc) (err error) {
ss := &serverStream{ ss := &serverStream{
t: t, t: t,
s: stream, s: stream,
p: &parser{s: stream}, p: &parser{s: stream},
codec: s.opts.codec, codec: s.opts.codec,
tracing: EnableTracing,
}
if ss.tracing {
ss.traceInfo.tr = trace.New("grpc.Recv."+methodFamily(stream.Method()), stream.Method())
ss.traceInfo.firstLine.client = false
ss.traceInfo.tr.LazyLog(&ss.traceInfo.firstLine, false)
defer func() {
ss.mu.Lock()
if err != nil && err != io.EOF {
ss.traceInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
ss.traceInfo.tr.SetError()
}
ss.traceInfo.tr.Finish()
ss.traceInfo.tr = nil
ss.mu.Unlock()
}()
} }
if appErr := sd.Handler(srv.server, ss); appErr != nil { if appErr := sd.Handler(srv.server, ss); appErr != nil {
if err, ok := appErr.(rpcError); ok { if err, ok := appErr.(rpcError); ok {
...@@ -322,7 +364,8 @@ func (s *Server) processStreamingRPC(t transport.ServerTransport, stream *transp ...@@ -322,7 +364,8 @@ func (s *Server) processStreamingRPC(t transport.ServerTransport, stream *transp
ss.statusDesc = appErr.Error() ss.statusDesc = appErr.Error()
} }
} }
t.WriteStatus(ss.s, ss.statusCode, ss.statusDesc) return t.WriteStatus(ss.s, ss.statusCode, ss.statusDesc)
} }
func (s *Server) handleStream(t transport.ServerTransport, stream *transport.Stream) { func (s *Server) handleStream(t transport.ServerTransport, stream *transport.Stream) {
......
...@@ -36,8 +36,11 @@ package grpc ...@@ -36,8 +36,11 @@ package grpc
import ( import (
"errors" "errors"
"io" "io"
"sync"
"time"
"golang.org/x/net/context" "golang.org/x/net/context"
"golang.org/x/net/trace"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
"google.golang.org/grpc/transport" "google.golang.org/grpc/transport"
...@@ -98,6 +101,19 @@ func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth ...@@ -98,6 +101,19 @@ func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
Host: cc.authority, Host: cc.authority,
Method: method, Method: method,
} }
cs := &clientStream{
desc: desc,
codec: cc.dopts.codec,
tracing: EnableTracing,
}
if cs.tracing {
cs.traceInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method)
cs.traceInfo.firstLine.client = true
if deadline, ok := ctx.Deadline(); ok {
cs.traceInfo.firstLine.deadline = deadline.Sub(time.Now())
}
cs.traceInfo.tr.LazyLog(&cs.traceInfo.firstLine, false)
}
t, _, err := cc.wait(ctx, 0) t, _, err := cc.wait(ctx, 0)
if err != nil { if err != nil {
return nil, toRPCErr(err) return nil, toRPCErr(err)
...@@ -106,13 +122,10 @@ func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth ...@@ -106,13 +122,10 @@ func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
if err != nil { if err != nil {
return nil, toRPCErr(err) return nil, toRPCErr(err)
} }
return &clientStream{ cs.t = t
t: t, cs.s = s
s: s, cs.p = &parser{s: s}
p: &parser{s: s}, return cs, nil
desc: desc,
codec: cc.dopts.codec,
}, nil
} }
// clientStream implements a client side Stream. // clientStream implements a client side Stream.
...@@ -122,6 +135,13 @@ type clientStream struct { ...@@ -122,6 +135,13 @@ type clientStream struct {
p *parser p *parser
desc *StreamDesc desc *StreamDesc
codec Codec codec Codec
tracing bool // set to EnableTracing when the clientStream is created.
mu sync.Mutex // protects traceInfo
// traceInfo.tr is set when the clientStream is created (if EnableTracing is true),
// and is set to nil when the clientStream's finish method is called.
traceInfo traceInfo
} }
func (cs *clientStream) Context() context.Context { func (cs *clientStream) Context() context.Context {
...@@ -143,6 +163,13 @@ func (cs *clientStream) Trailer() metadata.MD { ...@@ -143,6 +163,13 @@ func (cs *clientStream) Trailer() metadata.MD {
} }
func (cs *clientStream) SendMsg(m interface{}) (err error) { func (cs *clientStream) SendMsg(m interface{}) (err error) {
if cs.tracing {
cs.mu.Lock()
if cs.traceInfo.tr != nil {
cs.traceInfo.tr.LazyLog(&payload{sent: true, msg: m}, true)
}
cs.mu.Unlock()
}
defer func() { defer func() {
if err == nil || err == io.EOF { if err == nil || err == io.EOF {
return return
...@@ -161,7 +188,20 @@ func (cs *clientStream) SendMsg(m interface{}) (err error) { ...@@ -161,7 +188,20 @@ func (cs *clientStream) SendMsg(m interface{}) (err error) {
func (cs *clientStream) RecvMsg(m interface{}) (err error) { func (cs *clientStream) RecvMsg(m interface{}) (err error) {
err = recv(cs.p, cs.codec, m) err = recv(cs.p, cs.codec, m)
defer func() {
// err != nil indicates the termination of the stream.
if err != nil {
cs.finish(err)
}
}()
if err == nil { if err == nil {
if cs.tracing {
cs.mu.Lock()
if cs.traceInfo.tr != nil {
cs.traceInfo.tr.LazyLog(&payload{sent: false, msg: m}, true)
}
cs.mu.Unlock()
}
if !cs.desc.ClientStreams || cs.desc.ServerStreams { if !cs.desc.ClientStreams || cs.desc.ServerStreams {
return return
} }
...@@ -204,6 +244,24 @@ func (cs *clientStream) CloseSend() (err error) { ...@@ -204,6 +244,24 @@ func (cs *clientStream) CloseSend() (err error) {
return return
} }
func (cs *clientStream) finish(err error) {
if !cs.tracing {
return
}
cs.mu.Lock()
defer cs.mu.Unlock()
if cs.traceInfo.tr != nil {
if err == nil || err == io.EOF {
cs.traceInfo.tr.LazyPrintf("RPC: [OK]")
} else {
cs.traceInfo.tr.LazyPrintf("RPC: [%v]", err)
cs.traceInfo.tr.SetError()
}
cs.traceInfo.tr.Finish()
cs.traceInfo.tr = nil
}
}
// ServerStream defines the interface a server stream has to satisfy. // ServerStream defines the interface a server stream has to satisfy.
type ServerStream interface { type ServerStream interface {
// SendHeader sends the header metadata. It should not be called // SendHeader sends the header metadata. It should not be called
...@@ -224,6 +282,13 @@ type serverStream struct { ...@@ -224,6 +282,13 @@ type serverStream struct {
codec Codec codec Codec
statusCode codes.Code statusCode codes.Code
statusDesc string statusDesc string
tracing bool // set to EnableTracing when the serverStream is created.
mu sync.Mutex // protects traceInfo
// traceInfo.tr is set when the serverStream is created (if EnableTracing is true),
// and is set to nil when the serverStream's finish method is called.
traceInfo traceInfo
} }
func (ss *serverStream) Context() context.Context { func (ss *serverStream) Context() context.Context {
...@@ -242,7 +307,20 @@ func (ss *serverStream) SetTrailer(md metadata.MD) { ...@@ -242,7 +307,20 @@ func (ss *serverStream) SetTrailer(md metadata.MD) {
return return
} }
func (ss *serverStream) SendMsg(m interface{}) error { func (ss *serverStream) SendMsg(m interface{}) (err error) {
defer func() {
if ss.tracing {
ss.mu.Lock()
if err == nil {
ss.traceInfo.tr.LazyLog(&payload{sent: true, msg: m}, true)
} else {
ss.traceInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
ss.traceInfo.tr.SetError()
}
ss.mu.Unlock()
}
}()
out, err := encode(ss.codec, m, compressionNone) out, err := encode(ss.codec, m, compressionNone)
if err != nil { if err != nil {
err = transport.StreamErrorf(codes.Internal, "grpc: %v", err) err = transport.StreamErrorf(codes.Internal, "grpc: %v", err)
...@@ -251,6 +329,18 @@ func (ss *serverStream) SendMsg(m interface{}) error { ...@@ -251,6 +329,18 @@ func (ss *serverStream) SendMsg(m interface{}) error {
return ss.t.Write(ss.s, out, &transport.Options{Last: false}) return ss.t.Write(ss.s, out, &transport.Options{Last: false})
} }
func (ss *serverStream) RecvMsg(m interface{}) error { func (ss *serverStream) RecvMsg(m interface{}) (err error) {
defer func() {
if ss.tracing {
ss.mu.Lock()
if err == nil {
ss.traceInfo.tr.LazyLog(&payload{sent: false, msg: m}, true)
} else if err != io.EOF {
ss.traceInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
ss.traceInfo.tr.SetError()
}
ss.mu.Unlock()
}
}()
return recv(ss.p, ss.codec, m) return recv(ss.p, ss.codec, m)
} }
// Code generated by protoc-gen-go. // Code generated by protoc-gen-go.
// source: src/google.golang.org/grpc/test/grpc_testing/test.proto // source: test.proto
// DO NOT EDIT! // DO NOT EDIT!
/* /*
Package grpc_testing is a generated protocol buffer package. Package grpc_testing is a generated protocol buffer package.
It is generated from these files: It is generated from these files:
src/google.golang.org/grpc/test/grpc_testing/test.proto test.proto
It has these top-level messages: It has these top-level messages:
Empty Empty
......
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package grpc
import (
"bytes"
"fmt"
"io"
"net"
"strings"
"time"
"golang.org/x/net/trace"
)
// EnableTracing controls whether to trace RPCs using the golang.org/x/net/trace package.
// This should only be set before any RPCs are sent or received by this program.
var EnableTracing = true
// methodFamily returns the trace family for the given method.
// It turns "/pkg.Service/GetFoo" into "pkg.Service".
func methodFamily(m string) string {
m = strings.TrimPrefix(m, "/") // remove leading slash
if i := strings.Index(m, "/"); i >= 0 {
m = m[:i] // remove everything from second slash
}
if i := strings.LastIndex(m, "."); i >= 0 {
m = m[i+1:] // cut down to last dotted component
}
return m
}
// traceInfo contains tracing information for an RPC.
type traceInfo struct {
tr trace.Trace
firstLine firstLine
}
// firstLine is the first line of an RPC trace.
type firstLine struct {
client bool // whether this is a client (outgoing) RPC
remoteAddr net.Addr
deadline time.Duration // may be zero
}
func (f *firstLine) String() string {
var line bytes.Buffer
io.WriteString(&line, "RPC: ")
if f.client {
io.WriteString(&line, "to")
} else {
io.WriteString(&line, "from")
}
fmt.Fprintf(&line, " %v deadline:", f.remoteAddr)
if f.deadline != 0 {
fmt.Fprint(&line, f.deadline)
} else {
io.WriteString(&line, "none")
}
return line.String()
}
// payload represents an RPC request or response payload.
type payload struct {
sent bool // whether this is an outgoing payload
msg interface{} // e.g. a proto.Message
// TODO(dsymonds): add stringifying info to codec, and limit how much we hold here?
}
func (p payload) String() string {
if p.sent {
return fmt.Sprintf("sent: %v", p.msg)
} else {
return fmt.Sprintf("recv: %v", p.msg)
}
}
type fmtStringer struct {
format string
a []interface{}
}
func (f *fmtStringer) String() string {
return fmt.Sprintf(f.format, f.a...)
}
...@@ -62,7 +62,7 @@ func (windowUpdate) isItem() bool { ...@@ -62,7 +62,7 @@ func (windowUpdate) isItem() bool {
type settings struct { type settings struct {
ack bool ack bool
setting []http2.Setting ss []http2.Setting
} }
func (settings) isItem() bool { func (settings) isItem() bool {
...@@ -104,8 +104,14 @@ type quotaPool struct { ...@@ -104,8 +104,14 @@ type quotaPool struct {
// newQuotaPool creates a quotaPool which has quota q available to consume. // newQuotaPool creates a quotaPool which has quota q available to consume.
func newQuotaPool(q int) *quotaPool { func newQuotaPool(q int) *quotaPool {
qb := &quotaPool{c: make(chan int, 1)} qb := &quotaPool{
c: make(chan int, 1),
}
if q > 0 {
qb.c <- q qb.c <- q
} else {
qb.quota = q
}
return qb return qb
} }
......
...@@ -46,6 +46,7 @@ import ( ...@@ -46,6 +46,7 @@ import (
"github.com/bradfitz/http2/hpack" "github.com/bradfitz/http2/hpack"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
) )
...@@ -58,6 +59,7 @@ var ErrIllegalHeaderWrite = errors.New("transport: the stream is done or WriteHe ...@@ -58,6 +59,7 @@ var ErrIllegalHeaderWrite = errors.New("transport: the stream is done or WriteHe
type http2Server struct { type http2Server struct {
conn net.Conn conn net.Conn
maxStreamID uint32 // max stream ID ever seen maxStreamID uint32 // max stream ID ever seen
authInfo credentials.AuthInfo // auth info about the connection
// writableChan synchronizes write access to the transport. // writableChan synchronizes write access to the transport.
// A writer acquires the write lock by sending a value on writableChan // A writer acquires the write lock by sending a value on writableChan
// and releases it by receiving from writableChan. // and releases it by receiving from writableChan.
...@@ -88,11 +90,9 @@ type http2Server struct { ...@@ -88,11 +90,9 @@ type http2Server struct {
// newHTTP2Server constructs a ServerTransport based on HTTP2. ConnectionError is // newHTTP2Server constructs a ServerTransport based on HTTP2. ConnectionError is
// returned if something goes wrong. // returned if something goes wrong.
func newHTTP2Server(conn net.Conn, maxStreams uint32) (_ ServerTransport, err error) { func newHTTP2Server(conn net.Conn, maxStreams uint32, authInfo credentials.AuthInfo) (_ ServerTransport, err error) {
framer := newFramer(conn) framer := newFramer(conn)
// Send initial settings as connection preface to client. // Send initial settings as connection preface to client.
// TODO(zhaoq): Have a better way to signal "no limit" because 0 is
// permitted in the HTTP2 spec.
var settings []http2.Setting var settings []http2.Setting
// TODO(zhaoq): Have a better way to signal "no limit" because 0 is // TODO(zhaoq): Have a better way to signal "no limit" because 0 is
// permitted in the HTTP2 spec. // permitted in the HTTP2 spec.
...@@ -116,6 +116,7 @@ func newHTTP2Server(conn net.Conn, maxStreams uint32) (_ ServerTransport, err er ...@@ -116,6 +116,7 @@ func newHTTP2Server(conn net.Conn, maxStreams uint32) (_ ServerTransport, err er
var buf bytes.Buffer var buf bytes.Buffer
t := &http2Server{ t := &http2Server{
conn: conn, conn: conn,
authInfo: authInfo,
framer: framer, framer: framer,
hBuf: &buf, hBuf: &buf,
hEnc: hpack.NewEncoder(&buf), hEnc: hpack.NewEncoder(&buf),
...@@ -183,6 +184,10 @@ func (t *http2Server) operateHeaders(hDec *hpackDecoder, s *Stream, frame header ...@@ -183,6 +184,10 @@ func (t *http2Server) operateHeaders(hDec *hpackDecoder, s *Stream, frame header
} else { } else {
s.ctx, s.cancel = context.WithCancel(context.TODO()) s.ctx, s.cancel = context.WithCancel(context.TODO())
} }
// Attach Auth info if there is any.
if t.authInfo != nil {
s.ctx = credentials.NewContext(s.ctx, t.authInfo)
}
// Cache the current stream to the context so that the server application // Cache the current stream to the context so that the server application
// can find out. Required when the server wants to send some metadata // can find out. Required when the server wants to send some metadata
// back to the client (unary call only). // back to the client (unary call only).
...@@ -324,6 +329,7 @@ func (t *http2Server) handleData(f *http2.DataFrame) { ...@@ -324,6 +329,7 @@ func (t *http2Server) handleData(f *http2.DataFrame) {
return return
} }
size := len(f.Data()) size := len(f.Data())
if size > 0 {
if err := s.fc.onData(uint32(size)); err != nil { if err := s.fc.onData(uint32(size)); err != nil {
if _, ok := err.(ConnectionError); ok { if _, ok := err.(ConnectionError); ok {
grpclog.Printf("transport: http2Server %v", err) grpclog.Printf("transport: http2Server %v", err)
...@@ -340,6 +346,7 @@ func (t *http2Server) handleData(f *http2.DataFrame) { ...@@ -340,6 +346,7 @@ func (t *http2Server) handleData(f *http2.DataFrame) {
data := make([]byte, size) data := make([]byte, size)
copy(data, f.Data()) copy(data, f.Data())
s.write(recvMsg{data: data}) s.write(recvMsg{data: data})
}
if f.Header().Flags.Has(http2.FlagDataEndStream) { if f.Header().Flags.Has(http2.FlagDataEndStream) {
// Received the end of stream from the client. // Received the end of stream from the client.
s.mu.Lock() s.mu.Lock()
...@@ -367,18 +374,13 @@ func (t *http2Server) handleSettings(f *http2.SettingsFrame) { ...@@ -367,18 +374,13 @@ func (t *http2Server) handleSettings(f *http2.SettingsFrame) {
if f.IsAck() { if f.IsAck() {
return return
} }
var ss []http2.Setting
f.ForeachSetting(func(s http2.Setting) error { f.ForeachSetting(func(s http2.Setting) error {
if v, ok := f.Value(http2.SettingInitialWindowSize); ok { ss = append(ss, s)
t.mu.Lock()
defer t.mu.Unlock()
for _, s := range t.activeStreams {
s.sendQuotaPool.reset(int(v - t.streamSendQuota))
}
t.streamSendQuota = v
}
return nil return nil
}) })
t.controlBuf.put(&settings{ack: true}) // The settings will be applied once the ack is sent.
t.controlBuf.put(&settings{ack: true, ss: ss})
} }
func (t *http2Server) handlePing(f *http2.PingFrame) { func (t *http2Server) handlePing(f *http2.PingFrame) {
...@@ -445,7 +447,9 @@ func (t *http2Server) WriteHeader(s *Stream, md metadata.MD) error { ...@@ -445,7 +447,9 @@ func (t *http2Server) WriteHeader(s *Stream, md metadata.MD) error {
t.hEnc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"}) t.hEnc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
t.hEnc.WriteField(hpack.HeaderField{Name: "content-type", Value: "application/grpc"}) t.hEnc.WriteField(hpack.HeaderField{Name: "content-type", Value: "application/grpc"})
for k, v := range md { for k, v := range md {
t.hEnc.WriteField(hpack.HeaderField{Name: k, Value: v}) for _, entry := range v {
t.hEnc.WriteField(hpack.HeaderField{Name: k, Value: entry})
}
} }
if err := t.writeHeaders(s, t.hBuf, false); err != nil { if err := t.writeHeaders(s, t.hBuf, false); err != nil {
return err return err
...@@ -478,7 +482,9 @@ func (t *http2Server) WriteStatus(s *Stream, statusCode codes.Code, statusDesc s ...@@ -478,7 +482,9 @@ func (t *http2Server) WriteStatus(s *Stream, statusCode codes.Code, statusDesc s
t.hEnc.WriteField(hpack.HeaderField{Name: "grpc-message", Value: statusDesc}) t.hEnc.WriteField(hpack.HeaderField{Name: "grpc-message", Value: statusDesc})
// Attach the trailer metadata. // Attach the trailer metadata.
for k, v := range s.trailer { for k, v := range s.trailer {
t.hEnc.WriteField(hpack.HeaderField{Name: k, Value: v}) for _, entry := range v {
t.hEnc.WriteField(hpack.HeaderField{Name: k, Value: entry})
}
} }
if err := t.writeHeaders(s, t.hBuf, true); err != nil { if err := t.writeHeaders(s, t.hBuf, true); err != nil {
t.Close() t.Close()
...@@ -584,6 +590,20 @@ func (t *http2Server) Write(s *Stream, data []byte, opts *Options) error { ...@@ -584,6 +590,20 @@ func (t *http2Server) Write(s *Stream, data []byte, opts *Options) error {
} }
func (t *http2Server) applySettings(ss []http2.Setting) {
for _, s := range ss {
if s.ID == http2.SettingInitialWindowSize {
t.mu.Lock()
defer t.mu.Unlock()
for _, stream := range t.activeStreams {
stream.sendQuotaPool.reset(int(s.Val - t.streamSendQuota))
}
t.streamSendQuota = s.Val
}
}
}
// controller running in a separate goroutine takes charge of sending control // controller running in a separate goroutine takes charge of sending control
// frames (e.g., window update, reset stream, setting, etc.) to the server. // frames (e.g., window update, reset stream, setting, etc.) to the server.
func (t *http2Server) controller() { func (t *http2Server) controller() {
...@@ -599,8 +619,9 @@ func (t *http2Server) controller() { ...@@ -599,8 +619,9 @@ func (t *http2Server) controller() {
case *settings: case *settings:
if i.ack { if i.ack {
t.framer.writeSettingsAck(true) t.framer.writeSettingsAck(true)
t.applySettings(i.ss)
} else { } else {
t.framer.writeSettings(true, i.setting...) t.framer.writeSettings(true, i.ss...)
} }
case *resetStream: case *resetStream:
t.framer.writeRSTStream(true, i.streamID, i.code) t.framer.writeRSTStream(true, i.streamID, i.code)
......
...@@ -39,6 +39,7 @@ import ( ...@@ -39,6 +39,7 @@ import (
"io" "io"
"net" "net"
"strconv" "strconv"
"strings"
"sync/atomic" "sync/atomic"
"time" "time"
...@@ -50,6 +51,8 @@ import ( ...@@ -50,6 +51,8 @@ import (
) )
const ( const (
// The primary user agent
primaryUA = "grpc-go/0.7"
// http2MaxFrameLen specifies the max length of a HTTP2 frame. // http2MaxFrameLen specifies the max length of a HTTP2 frame.
http2MaxFrameLen = 16384 // 16KB frame http2MaxFrameLen = 16384 // 16KB frame
// http://http2.github.io/http2-spec/#SettingValues // http://http2.github.io/http2-spec/#SettingValues
...@@ -60,13 +63,11 @@ const ( ...@@ -60,13 +63,11 @@ const (
var ( var (
clientPreface = []byte(http2.ClientPreface) clientPreface = []byte(http2.ClientPreface)
) http2RSTErrConvTab = map[http2.ErrCode]codes.Code{
var http2RSTErrConvTab = map[http2.ErrCode]codes.Code{
http2.ErrCodeNo: codes.Internal, http2.ErrCodeNo: codes.Internal,
http2.ErrCodeProtocol: codes.Internal, http2.ErrCodeProtocol: codes.Internal,
http2.ErrCodeInternal: codes.Internal, http2.ErrCodeInternal: codes.Internal,
http2.ErrCodeFlowControl: codes.Internal, http2.ErrCodeFlowControl: codes.ResourceExhausted,
http2.ErrCodeSettingsTimeout: codes.Internal, http2.ErrCodeSettingsTimeout: codes.Internal,
http2.ErrCodeFrameSize: codes.Internal, http2.ErrCodeFrameSize: codes.Internal,
http2.ErrCodeRefusedStream: codes.Unavailable, http2.ErrCodeRefusedStream: codes.Unavailable,
...@@ -75,15 +76,15 @@ var http2RSTErrConvTab = map[http2.ErrCode]codes.Code{ ...@@ -75,15 +76,15 @@ var http2RSTErrConvTab = map[http2.ErrCode]codes.Code{
http2.ErrCodeConnect: codes.Internal, http2.ErrCodeConnect: codes.Internal,
http2.ErrCodeEnhanceYourCalm: codes.ResourceExhausted, http2.ErrCodeEnhanceYourCalm: codes.ResourceExhausted,
http2.ErrCodeInadequateSecurity: codes.PermissionDenied, http2.ErrCodeInadequateSecurity: codes.PermissionDenied,
} }
statusCodeConvTab = map[codes.Code]http2.ErrCode{
var statusCodeConvTab = map[codes.Code]http2.ErrCode{ codes.Internal: http2.ErrCodeInternal,
codes.Internal: http2.ErrCodeInternal, // pick an arbitrary one which is matched.
codes.Canceled: http2.ErrCodeCancel, codes.Canceled: http2.ErrCodeCancel,
codes.Unavailable: http2.ErrCodeRefusedStream, codes.Unavailable: http2.ErrCodeRefusedStream,
codes.ResourceExhausted: http2.ErrCodeEnhanceYourCalm, codes.ResourceExhausted: http2.ErrCodeEnhanceYourCalm,
codes.PermissionDenied: http2.ErrCodeInadequateSecurity, codes.PermissionDenied: http2.ErrCodeInadequateSecurity,
} }
)
// Records the states during HPACK decoding. Must be reset once the // Records the states during HPACK decoding. Must be reset once the
// decoding of the entire headers are finished. // decoding of the entire headers are finished.
...@@ -97,7 +98,7 @@ type decodeState struct { ...@@ -97,7 +98,7 @@ type decodeState struct {
timeout time.Duration timeout time.Duration
method string method string
// key-value metadata map from the peer. // key-value metadata map from the peer.
mdata map[string]string mdata map[string][]string
} }
// An hpackDecoder decodes HTTP2 headers which may span multiple frames. // An hpackDecoder decodes HTTP2 headers which may span multiple frames.
...@@ -128,8 +129,7 @@ func isReservedHeader(hdr string) bool { ...@@ -128,8 +129,7 @@ func isReservedHeader(hdr string) bool {
"grpc-message", "grpc-message",
"grpc-status", "grpc-status",
"grpc-timeout", "grpc-timeout",
"te", "te":
"user-agent":
return true return true
default: default:
return false return false
...@@ -161,15 +161,24 @@ func newHPACKDecoder() *hpackDecoder { ...@@ -161,15 +161,24 @@ func newHPACKDecoder() *hpackDecoder {
d.state.method = f.Value d.state.method = f.Value
default: default:
if !isReservedHeader(f.Name) { if !isReservedHeader(f.Name) {
if f.Name == "user-agent" {
i := strings.LastIndex(f.Value, " ")
if i == -1 {
// There is no application user agent string being set.
return
}
// Extract the application user agent string.
f.Value = f.Value[:i]
}
if d.state.mdata == nil { if d.state.mdata == nil {
d.state.mdata = make(map[string]string) d.state.mdata = make(map[string][]string)
} }
k, v, err := metadata.DecodeKeyValue(f.Name, f.Value) k, v, err := metadata.DecodeKeyValue(f.Name, f.Value)
if err != nil { if err != nil {
grpclog.Printf("Failed to decode (%q, %q): %v", f.Name, f.Value, err) grpclog.Printf("Failed to decode (%q, %q): %v", f.Name, f.Value, err)
return return
} }
d.state.mdata[k] = v d.state.mdata[k] = append(d.state.mdata[k], v)
} }
} }
}) })
......
...@@ -172,7 +172,6 @@ type Stream struct { ...@@ -172,7 +172,6 @@ type Stream struct {
method string method string
buf *recvBuffer buf *recvBuffer
dec io.Reader dec io.Reader
fc *inFlow fc *inFlow
recvQuota uint32 recvQuota uint32
// The accumulated inbound quota pending for window update. // The accumulated inbound quota pending for window update.
...@@ -309,14 +308,19 @@ const ( ...@@ -309,14 +308,19 @@ const (
// NewServerTransport creates a ServerTransport with conn or non-nil error // NewServerTransport creates a ServerTransport with conn or non-nil error
// if it fails. // if it fails.
func NewServerTransport(protocol string, conn net.Conn, maxStreams uint32) (ServerTransport, error) { func NewServerTransport(protocol string, conn net.Conn, maxStreams uint32, authInfo credentials.AuthInfo) (ServerTransport, error) {
return newHTTP2Server(conn, maxStreams) return newHTTP2Server(conn, maxStreams, authInfo)
} }
// ConnectOptions covers all relevant options for dialing a server. // ConnectOptions covers all relevant options for dialing a server.
type ConnectOptions struct { type ConnectOptions struct {
// UserAgent is the application user agent.
UserAgent string
// Dialer specifies how to dial a network address.
Dialer func(string, time.Duration) (net.Conn, error) Dialer func(string, time.Duration) (net.Conn, error)
// AuthOptions stores the credentials required to setup a client connection and/or issue RPCs.
AuthOptions []credentials.Credentials AuthOptions []credentials.Credentials
// Timeout specifies the timeout for dialing a client connection.
Timeout time.Duration Timeout time.Duration
} }
......
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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 rkt
import (
"fmt"
"strconv"
"sync"
"github.com/coreos/go-systemd/dbus"
rktapi "github.com/coreos/rkt/api/v1alpha"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// fakeRktInterface mocks the rktapi.PublicAPIClient interface for testing purpose.
type fakeRktInterface struct {
sync.Mutex
info rktapi.Info
called []string
err error
}
func newFakeRktInterface() *fakeRktInterface {
return &fakeRktInterface{}
}
func (f *fakeRktInterface) CleanCalls() {
f.Lock()
defer f.Unlock()
f.called = nil
}
func (f *fakeRktInterface) GetInfo(ctx context.Context, in *rktapi.GetInfoRequest, opts ...grpc.CallOption) (*rktapi.GetInfoResponse, error) {
f.Lock()
defer f.Unlock()
f.called = append(f.called, "GetInfo")
return &rktapi.GetInfoResponse{&f.info}, f.err
}
func (f *fakeRktInterface) ListPods(ctx context.Context, in *rktapi.ListPodsRequest, opts ...grpc.CallOption) (*rktapi.ListPodsResponse, error) {
return nil, fmt.Errorf("Not implemented")
}
func (f *fakeRktInterface) InspectPod(ctx context.Context, in *rktapi.InspectPodRequest, opts ...grpc.CallOption) (*rktapi.InspectPodResponse, error) {
return nil, fmt.Errorf("Not implemented")
}
func (f *fakeRktInterface) ListImages(ctx context.Context, in *rktapi.ListImagesRequest, opts ...grpc.CallOption) (*rktapi.ListImagesResponse, error) {
return nil, fmt.Errorf("Not implemented")
}
func (f *fakeRktInterface) InspectImage(ctx context.Context, in *rktapi.InspectImageRequest, opts ...grpc.CallOption) (*rktapi.InspectImageResponse, error) {
return nil, fmt.Errorf("Not implemented")
}
func (f *fakeRktInterface) ListenEvents(ctx context.Context, in *rktapi.ListenEventsRequest, opts ...grpc.CallOption) (rktapi.PublicAPI_ListenEventsClient, error) {
return nil, fmt.Errorf("Not implemented")
}
func (f *fakeRktInterface) GetLogs(ctx context.Context, in *rktapi.GetLogsRequest, opts ...grpc.CallOption) (rktapi.PublicAPI_GetLogsClient, error) {
return nil, fmt.Errorf("Not implemented")
}
// fakeSystemd mocks the systemdInterface for testing purpose.
// TODO(yifan): Remove this once we have a package for launching rkt pods.
// See https://github.com/coreos/rkt/issues/1769.
type fakeSystemd struct {
sync.Mutex
called []string
version string
err error
}
func newFakeSystemd() *fakeSystemd {
return &fakeSystemd{}
}
func (f *fakeSystemd) CleanCalls() {
f.Lock()
defer f.Unlock()
f.called = nil
}
func (f *fakeSystemd) Version() (systemdVersion, error) {
f.Lock()
defer f.Unlock()
f.called = append(f.called, "Version")
v, _ := strconv.Atoi(f.version)
return systemdVersion(v), f.err
}
func (f *fakeSystemd) ListUnits() ([]dbus.UnitStatus, error) {
return nil, fmt.Errorf("Not implemented")
}
func (f *fakeSystemd) StopUnit(name, mode string) (string, error) {
return "", fmt.Errorf("Not implemented")
}
func (f *fakeSystemd) RestartUnit(name, mode string) (string, error) {
return "", fmt.Errorf("Not implemented")
}
func (f *fakeSystemd) Reload() error {
return fmt.Errorf("Not implemented")
}
...@@ -30,10 +30,12 @@ import ( ...@@ -30,10 +30,12 @@ import (
"syscall" "syscall"
"time" "time"
"google.golang.org/grpc"
appcschema "github.com/appc/spec/schema" appcschema "github.com/appc/spec/schema"
appctypes "github.com/appc/spec/schema/types" appctypes "github.com/appc/spec/schema/types"
"github.com/coreos/go-systemd/dbus"
"github.com/coreos/go-systemd/unit" "github.com/coreos/go-systemd/unit"
rktapi "github.com/coreos/rkt/api/v1alpha"
"github.com/docker/docker/pkg/parsers" "github.com/docker/docker/pkg/parsers"
docker "github.com/fsouza/go-dockerclient" docker "github.com/fsouza/go-dockerclient"
"github.com/golang/glog" "github.com/golang/glog"
...@@ -53,10 +55,11 @@ import ( ...@@ -53,10 +55,11 @@ import (
const ( const (
RktType = "rkt" RktType = "rkt"
acVersion = "0.7.1" minimumAppcVersion = "0.7.1"
minimumRktVersion = "0.9.0" minimumRktBinVersion = "0.9.0"
recommendRktVersion = "0.9.0" recommendedRktBinVersion = "0.9.0"
systemdMinimumVersion = "219" minimumRktApiVersion = "1.0.0-alpha"
minimumSystemdVersion = "219"
systemdServiceDir = "/run/systemd/system" systemdServiceDir = "/run/systemd/system"
rktDataDir = "/var/lib/rkt" rktDataDir = "/var/lib/rkt"
...@@ -74,13 +77,17 @@ const ( ...@@ -74,13 +77,17 @@ const (
dockerAuthTemplate = `{"rktKind":"dockerAuth","rktVersion":"v1","registries":[%q],"credentials":{"user":%q,"password":%q}}` dockerAuthTemplate = `{"rktKind":"dockerAuth","rktVersion":"v1","registries":[%q],"credentials":{"user":%q,"password":%q}}`
defaultImageTag = "latest" defaultImageTag = "latest"
defaultRktAPIServiceAddr = "localhost:15441"
) )
// Runtime implements the Containerruntime for rkt. The implementation // Runtime implements the Containerruntime for rkt. The implementation
// uses systemd, so in order to run this runtime, systemd must be installed // uses systemd, so in order to run this runtime, systemd must be installed
// on the machine. // on the machine.
type Runtime struct { type Runtime struct {
systemd *dbus.Conn systemd systemdInterface
// The grpc client for rkt api-service.
apisvcConn *grpc.ClientConn
apisvc rktapi.PublicAPIClient
// The absolute path to rkt binary. // The absolute path to rkt binary.
rktBinAbsPath string rktBinAbsPath string
config *Config config *Config
...@@ -93,6 +100,12 @@ type Runtime struct { ...@@ -93,6 +100,12 @@ type Runtime struct {
livenessManager proberesults.Manager livenessManager proberesults.Manager
volumeGetter volumeGetter volumeGetter volumeGetter
imagePuller kubecontainer.ImagePuller imagePuller kubecontainer.ImagePuller
// Versions
binVersion rktVersion
apiVersion rktVersion
appcVersion rktVersion
systemdVersion systemdVersion
} }
var _ kubecontainer.Runtime = &Runtime{} var _ kubecontainer.Runtime = &Runtime{}
...@@ -114,21 +127,16 @@ func New(config *Config, ...@@ -114,21 +127,16 @@ func New(config *Config,
imageBackOff *util.Backoff, imageBackOff *util.Backoff,
serializeImagePulls bool, serializeImagePulls bool,
) (*Runtime, error) { ) (*Runtime, error) {
systemdVersion, err := getSystemdVersion() // Create dbus connection.
if err != nil { systemd, err := newSystemd()
return nil, err
}
result, err := systemdVersion.Compare(systemdMinimumVersion)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("rkt: cannot create systemd interface: %v", err)
}
if result < 0 {
return nil, fmt.Errorf("rkt: systemd version is too old, requires at least %v", systemdMinimumVersion)
} }
systemd, err := dbus.New() // TODO(yifan): Use secure connection.
apisvcConn, err := grpc.Dial(defaultRktAPIServiceAddr, grpc.WithInsecure())
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot connect to dbus: %v", err) return nil, fmt.Errorf("rkt: cannot connect to rkt api service: %v", err)
} }
rktBinAbsPath := config.Path rktBinAbsPath := config.Path
...@@ -144,6 +152,8 @@ func New(config *Config, ...@@ -144,6 +152,8 @@ func New(config *Config,
rkt := &Runtime{ rkt := &Runtime{
systemd: systemd, systemd: systemd,
rktBinAbsPath: rktBinAbsPath, rktBinAbsPath: rktBinAbsPath,
apisvcConn: apisvcConn,
apisvc: rktapi.NewPublicAPIClient(apisvcConn),
config: config, config: config,
dockerKeyring: credentialprovider.NewDockerKeyring(), dockerKeyring: credentialprovider.NewDockerKeyring(),
containerRefManager: containerRefManager, containerRefManager: containerRefManager,
...@@ -158,28 +168,13 @@ func New(config *Config, ...@@ -158,28 +168,13 @@ func New(config *Config,
rkt.imagePuller = kubecontainer.NewImagePuller(recorder, rkt, imageBackOff) rkt.imagePuller = kubecontainer.NewImagePuller(recorder, rkt, imageBackOff)
} }
// Test the rkt version. if err := rkt.checkVersion(minimumRktBinVersion, recommendedRktBinVersion, minimumAppcVersion, minimumRktApiVersion, minimumSystemdVersion); err != nil {
version, err := rkt.Version() // TODO(yifan): Latest go-systemd version have the ability to close the
if err != nil { // dbus connection. However the 'docker/libcontainer' package is using
// the older go-systemd version, so we can't update the go-systemd version.
rkt.apisvcConn.Close()
return nil, err return nil, err
} }
result, err = version.Compare(minimumRktVersion)
if err != nil {
return nil, err
}
if result < 0 {
return nil, fmt.Errorf("rkt: version is too old, requires at least %v", minimumRktVersion)
}
result, err = version.Compare(recommendRktVersion)
if err != nil {
return nil, err
}
if result != 0 {
// TODO(yifan): Record an event to expose the information.
glog.Warningf("rkt: current version %q is not recommended (recommended version %q)", version, recommendRktVersion)
}
return rkt, nil return rkt, nil
} }
...@@ -880,33 +875,8 @@ func (r *Runtime) Type() string { ...@@ -880,33 +875,8 @@ func (r *Runtime) Type() string {
return RktType return RktType
} }
// Version invokes 'rkt version' to get the version information of the rkt
// runtime on the machine.
// The return values are an int array containers the version number.
//
// Example:
// rkt:0.3.2+git --> []int{0, 3, 2}.
//
func (r *Runtime) Version() (kubecontainer.Version, error) { func (r *Runtime) Version() (kubecontainer.Version, error) {
output, err := r.runCommand("version") return r.binVersion, nil
if err != nil {
return nil, err
}
// Example output for 'rkt version':
// rkt version 0.3.2+git
// appc version 0.3.0+git
for _, line := range output {
tuples := strings.Split(strings.TrimSpace(line), " ")
if len(tuples) != 3 {
glog.Warningf("rkt: cannot parse the output: %q.", line)
continue
}
if tuples[0] == "rkt" {
return parseVersion(tuples[2])
}
}
return nil, fmt.Errorf("rkt: cannot determine the version")
} }
// TODO(yifan): This is very racy, unefficient, and unsafe, we need to provide // TODO(yifan): This is very racy, unefficient, and unsafe, we need to provide
......
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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 rkt
import (
"fmt"
"testing"
rktapi "github.com/coreos/rkt/api/v1alpha"
"github.com/stretchr/testify/assert"
)
func TestCheckVersion(t *testing.T) {
fr := newFakeRktInterface()
fs := newFakeSystemd()
r := &Runtime{apisvc: fr, systemd: fs}
fr.info = rktapi.Info{
RktVersion: "1.2.3+git",
AppcVersion: "1.2.4+git",
ApiVersion: "1.2.6-alpha",
}
fs.version = "100"
tests := []struct {
minimumRktBinVersion string
recommendedRktBinVersion string
minimumAppcVersion string
minimumRktApiVersion string
minimumSystemdVersion string
err error
calledGetInfo bool
calledSystemVersion bool
}{
// Good versions.
{
"1.2.3",
"1.2.3",
"1.2.4",
"1.2.5",
"99",
nil,
true,
true,
},
// Good versions.
{
"1.2.3+git",
"1.2.3+git",
"1.2.4+git",
"1.2.6-alpha",
"100",
nil,
true,
true,
},
// Requires greater binary version.
{
"1.2.4",
"1.2.4",
"1.2.4",
"1.2.6-alpha",
"100",
fmt.Errorf("rkt: binary version is too old(%v), requires at least %v", fr.info.RktVersion, "1.2.4"),
true,
true,
},
// Requires greater Appc version.
{
"1.2.3",
"1.2.3",
"1.2.5",
"1.2.6-alpha",
"100",
fmt.Errorf("rkt: appc version is too old(%v), requires at least %v", fr.info.AppcVersion, "1.2.5"),
true,
true,
},
// Requires greater API version.
{
"1.2.3",
"1.2.3",
"1.2.4",
"1.2.6",
"100",
fmt.Errorf("rkt: API version is too old(%v), requires at least %v", fr.info.ApiVersion, "1.2.6"),
true,
true,
},
// Requires greater API version.
{
"1.2.3",
"1.2.3",
"1.2.4",
"1.2.7",
"100",
fmt.Errorf("rkt: API version is too old(%v), requires at least %v", fr.info.ApiVersion, "1.2.7"),
true,
true,
},
// Requires greater systemd version.
{
"1.2.3",
"1.2.3",
"1.2.4",
"1.2.7",
"101",
fmt.Errorf("rkt: systemd version(%v) is too old, requires at least %v", fs.version, "101"),
false,
true,
},
}
for i, tt := range tests {
testCaseHint := fmt.Sprintf("test case #%d", i)
err := r.checkVersion(tt.minimumRktBinVersion, tt.recommendedRktBinVersion, tt.minimumAppcVersion, tt.minimumRktApiVersion, tt.minimumSystemdVersion)
assert.Equal(t, err, tt.err, testCaseHint)
if tt.calledGetInfo {
assert.Equal(t, fr.called, []string{"GetInfo"}, testCaseHint)
}
if tt.calledSystemVersion {
assert.Equal(t, fs.called, []string{"Version"}, testCaseHint)
}
if err == nil {
assert.Equal(t, r.binVersion.String(), fr.info.RktVersion, testCaseHint)
assert.Equal(t, r.appcVersion.String(), fr.info.AppcVersion, testCaseHint)
assert.Equal(t, r.apiVersion.String(), fr.info.ApiVersion, testCaseHint)
}
fr.CleanCalls()
fs.CleanCalls()
}
}
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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 rkt
import (
"fmt"
"os/exec"
"strconv"
"strings"
"github.com/coreos/go-systemd/dbus"
)
// systemdVersion is a type wraps the int to implement kubecontainer.Version interface.
type systemdVersion int
func (s systemdVersion) String() string {
return fmt.Sprintf("%d", s)
}
func (s systemdVersion) Compare(other string) (int, error) {
v, err := strconv.Atoi(other)
if err != nil {
return -1, err
}
if int(s) < v {
return -1, nil
} else if int(s) > v {
return 1, nil
}
return 0, nil
}
// systemdInterface is an abstraction of the go-systemd/dbus to make
// it mockable for testing.
// TODO(yifan): Eventually we should move these functionalities to:
// 1. a package for launching/stopping rkt pods.
// 2. rkt api-service interface for listing pods.
// See https://github.com/coreos/rkt/issues/1769.
type systemdInterface interface {
// Version returns the version of the systemd.
Version() (systemdVersion, error)
// ListUnits lists all the loaded units.
ListUnits() ([]dbus.UnitStatus, error)
// StopUnits stops the unit with the given name.
StopUnit(name, mode string) (string, error)
// StopUnits restarts the unit with the given name.
RestartUnit(name, mode string) (string, error)
// Reload is equivalent to 'systemctl daemon-reload'.
Reload() error
}
// systemd implements the systemdInterface using dbus and systemctl.
// All the functions other then Version() are already implemented by go-systemd/dbus.
type systemd struct {
*dbus.Conn
}
// newSystemd creates a systemd object that implements systemdInterface.
func newSystemd() (*systemd, error) {
dbusConn, err := dbus.New()
if err != nil {
return nil, err
}
return &systemd{dbusConn}, nil
}
// Version returns the version of the systemd.
func (s *systemd) Version() (systemdVersion, error) {
output, err := exec.Command("systemctl", "--version").Output()
if err != nil {
return -1, err
}
// Example output of 'systemctl --version':
//
// systemd 215
// +PAM +AUDIT +SELINUX +IMA +SYSVINIT +LIBCRYPTSETUP +GCRYPT +ACL +XZ -SECCOMP -APPARMOR
//
lines := strings.Split(string(output), "\n")
tuples := strings.Split(lines[0], " ")
if len(tuples) != 2 {
return -1, fmt.Errorf("rkt: Failed to parse version %v", lines)
}
result, err := strconv.Atoi(string(tuples[1]))
if err != nil {
return -1, err
}
return systemdVersion(result), nil
}
...@@ -18,93 +18,111 @@ package rkt ...@@ -18,93 +18,111 @@ package rkt
import ( import (
"fmt" "fmt"
"os/exec"
"strconv"
"strings"
appctypes "github.com/appc/spec/schema/types" "github.com/coreos/go-semver/semver"
rktapi "github.com/coreos/rkt/api/v1alpha"
"github.com/golang/glog"
"golang.org/x/net/context"
) )
type rktVersion []int // rktVersion implementes kubecontainer.Version interface by implementing
// Compare() and String() (which is implemented by the underlying semver.Version)
type rktVersion struct {
*semver.Version
}
func parseVersion(input string) (rktVersion, error) { func newRktVersion(version string) (rktVersion, error) {
nsv, err := appctypes.NewSemVer(input) sem, err := semver.NewVersion(version)
if err != nil { if err != nil {
return nil, err return rktVersion{}, err
} }
return rktVersion{int(nsv.Major), int(nsv.Minor), int(nsv.Patch)}, nil return rktVersion{sem}, nil
} }
func (r rktVersion) Compare(other string) (int, error) { func (r rktVersion) Compare(other string) (int, error) {
v, err := parseVersion(other) v, err := semver.NewVersion(other)
if err != nil { if err != nil {
return -1, err return -1, err
} }
for i := range r { if r.LessThan(*v) {
if i > len(v)-1 {
return 1, nil
}
if r[i] < v[i] {
return -1, nil return -1, nil
} }
if r[i] > v[i] { if v.LessThan(*r.Version) {
return 1, nil return 1, nil
} }
}
// When loop ends, len(r) is <= len(v).
if len(r) < len(v) {
return -1, nil
}
return 0, nil return 0, nil
} }
func (r rktVersion) String() string { // checkVersion tests whether the rkt/systemd/rkt-api-service that meet the version requirement.
var version []string // If all version requirements are met, it returns nil.
for _, v := range r { func (r *Runtime) checkVersion(minimumRktBinVersion, recommendedRktBinVersion, minimumAppcVersion, minimumRktApiVersion, minimumSystemdVersion string) error {
version = append(version, fmt.Sprintf("%d", v)) // Check systemd version.
var err error
r.systemdVersion, err = r.systemd.Version()
if err != nil {
return err
}
result, err := r.systemdVersion.Compare(minimumSystemdVersion)
if err != nil {
return err
}
if result < 0 {
return fmt.Errorf("rkt: systemd version(%v) is too old, requires at least %v", r.systemdVersion, minimumSystemdVersion)
} }
return strings.Join(version, ".")
}
type systemdVersion int // Example for the version strings returned by GetInfo():
// RktVersion:"0.10.0+gitb7349b1" AppcVersion:"0.7.1" ApiVersion:"1.0.0-alpha"
resp, err := r.apisvc.GetInfo(context.Background(), &rktapi.GetInfoRequest{})
if err != nil {
return err
}
func (s systemdVersion) String() string { // Check rkt binary version.
return fmt.Sprintf("%d", s) r.binVersion, err = newRktVersion(resp.Info.RktVersion)
} if err != nil {
return err
}
result, err = r.binVersion.Compare(minimumRktBinVersion)
if err != nil {
return err
}
if result < 0 {
return fmt.Errorf("rkt: binary version is too old(%v), requires at least %v", resp.Info.RktVersion, minimumRktBinVersion)
}
result, err = r.binVersion.Compare(recommendedRktBinVersion)
if err != nil {
return err
}
if result != 0 {
// TODO(yifan): Record an event to expose the information.
glog.Warningf("rkt: current binary version %q is not recommended (recommended version %q)", resp.Info.RktVersion, recommendedRktBinVersion)
}
func (s systemdVersion) Compare(other string) (int, error) { // Check Appc version.
v, err := strconv.Atoi(other) r.appcVersion, err = newRktVersion(resp.Info.AppcVersion)
if err != nil { if err != nil {
return -1, err return err
} }
if int(s) < v { result, err = r.appcVersion.Compare(minimumAppcVersion)
return -1, nil if err != nil {
} else if int(s) > v { return err
return 1, nil }
if result < 0 {
return fmt.Errorf("rkt: appc version is too old(%v), requires at least %v", resp.Info.AppcVersion, minimumAppcVersion)
} }
return 0, nil
}
func getSystemdVersion() (systemdVersion, error) { // Check rkt API version.
output, err := exec.Command("systemctl", "--version").Output() r.apiVersion, err = newRktVersion(resp.Info.ApiVersion)
if err != nil { if err != nil {
return -1, err return err
} }
// Example output of 'systemctl --version': result, err = r.apiVersion.Compare(minimumRktApiVersion)
//
// systemd 215
// +PAM +AUDIT +SELINUX +IMA +SYSVINIT +LIBCRYPTSETUP +GCRYPT +ACL +XZ -SECCOMP -APPARMOR
//
lines := strings.Split(string(output), "\n")
tuples := strings.Split(lines[0], " ")
if len(tuples) != 2 {
return -1, fmt.Errorf("rkt: Failed to parse version %v", lines)
}
result, err := strconv.Atoi(string(tuples[1]))
if err != nil { if err != nil {
return -1, err return err
}
if result < 0 {
return fmt.Errorf("rkt: API version is too old(%v), requires at least %v", resp.Info.ApiVersion, minimumRktApiVersion)
} }
return systemdVersion(result), nil return nil
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment