// Dial establishes a connection for a given endpoint using the client's config func (c *Client) Dial(endpoint string) (*grpc.ClientConn, error) { opts := []grpc.DialOption{ grpc.WithBlock(), grpc.WithTimeout(c.cfg.DialTimeout), } if c.creds != nil { opts = append(opts, grpc.WithTransportCredentials(*c.creds)) } else { opts = append(opts, grpc.WithInsecure()) } if url, uerr := url.Parse(endpoint); uerr == nil && url.Scheme == "unix" { f := func(a string, t time.Duration) (net.Conn, error) { return net.DialTimeout("unix", a, t) } // strip unix:// prefix so certs work endpoint = url.Host opts = append(opts, grpc.WithDialer(f)) } conn, err := grpc.Dial(endpoint, opts...) if err != nil { return nil, err } return conn, nil }
// NewClientConn creates a gRPC client connection to addr. func NewClientConn(addr string) *grpc.ClientConn { conn, err := grpc.Dial(addr, grpc.WithInsecure()) if err != nil { grpclog.Fatalf("NewClientConn(%q) failed to create a ClientConn %v", addr, err) } return conn }
// deleteRangeCommandFunc executes the "delegeRange" command. func deleteRangeCommandFunc(c *cli.Context) { if len(c.Args()) == 0 { panic("bad arg") } var rangeEnd []byte key := []byte(c.Args()[0]) if len(c.Args()) > 1 { rangeEnd = []byte(c.Args()[1]) } conn, err := grpc.Dial(c.GlobalString("endpoint"), grpc.WithInsecure()) if err != nil { panic(err) } etcd := pb.NewEtcdClient(conn) req := &pb.DeleteRangeRequest{Key: key, RangeEnd: rangeEnd} etcd.DeleteRange(context.Background(), req) if rangeEnd != nil { fmt.Printf("range [%s, %s) is deleted\n", string(key), string(rangeEnd)) } else { fmt.Printf("key %s is deleted\n", string(key)) } }
// checkConsistency stops the cluster for a moment and get the hashes of KV storages. func (c *cluster) checkConsistency() error { hashes := make(map[string]uint32) for _, u := range c.GRPCURLs { conn, err := grpc.Dial(u, grpc.WithInsecure(), grpc.WithTimeout(5*time.Second)) if err != nil { return err } kvc := pb.NewKVClient(conn) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) resp, err := kvc.Hash(ctx, &pb.HashRequest{}) hv := resp.Hash if resp != nil && err != nil { return err } cancel() hashes[u] = hv } if !checkConsistency(hashes) { return fmt.Errorf("check consistency fails: %v", hashes) } return nil }
// NewClientV3 creates a new grpc client connection to the member func NewClientV3(m *member) (*clientv3.Client, error) { if m.grpcAddr == "" { return nil, fmt.Errorf("member not configured for grpc") } f := func(a string, t time.Duration) (net.Conn, error) { return net.Dial("unix", a) } unixdialer := grpc.WithDialer(f) opts := []grpc.DialOption{ unixdialer, grpc.WithBlock(), grpc.WithTimeout(5 * time.Second)} if m.ClientTLSInfo != nil { tlscfg, err := m.ClientTLSInfo.ClientConfig() if err != nil { return nil, err } creds := credentials.NewTLS(tlscfg) opts = append(opts, grpc.WithTransportCredentials(creds)) } else { opts = append(opts, grpc.WithInsecure()) } conn, err := grpc.Dial(m.grpcAddr, opts...) if err != nil { return nil, err } return clientv3.NewFromConn(conn), nil }
// Dial establishes a connection for a given endpoint using the client's config func (c *Client) Dial(endpoint string) (*grpc.ClientConn, error) { opts := []grpc.DialOption{ grpc.WithBlock(), grpc.WithTimeout(c.cfg.DialTimeout), } if c.creds != nil { opts = append(opts, grpc.WithTransportCredentials(*c.creds)) } else { opts = append(opts, grpc.WithInsecure()) } proto := "tcp" if url, uerr := url.Parse(endpoint); uerr == nil && url.Scheme == "unix" { proto = "unix" // strip unix:// prefix so certs work endpoint = url.Host } f := func(a string, t time.Duration) (net.Conn, error) { select { case <-c.ctx.Done(): return nil, c.ctx.Err() default: } return net.DialTimeout(proto, a, t) } opts = append(opts, grpc.WithDialer(f)) conn, err := grpc.Dial(endpoint, opts...) if err != nil { return nil, err } return conn, nil }
// txnCommandFunc executes the "txn" command. func txnCommandFunc(c *cli.Context) { if len(c.Args()) != 0 { panic("unexpected args") } reader := bufio.NewReader(os.Stdin) next := compareState txn := &pb.TxnRequest{} for next != nil { next = next(txn, reader) } conn, err := grpc.Dial(c.GlobalString("endpoint"), grpc.WithInsecure()) if err != nil { panic(err) } etcd := pb.NewEtcdClient(conn) resp, err := etcd.Txn(context.Background(), txn) if err != nil { fmt.Println(err) } if resp.Succeeded { fmt.Println("executed success request list") } else { fmt.Println("executed failure request list") } }
// newGrpcClient creates a new grpc client connection to the member func NewGRPCClient(m *member) (*grpc.ClientConn, error) { if m.grpcAddr == "" { return nil, fmt.Errorf("member not configured for grpc") } f := func(a string, t time.Duration) (net.Conn, error) { return net.Dial("unix", a) } unixdialer := grpc.WithDialer(f) return grpc.Dial(m.grpcAddr, grpc.WithInsecure(), unixdialer) }
func (s *stresser) Stress() error { conn, err := grpc.Dial(s.Endpoint, grpc.WithInsecure(), grpc.WithTimeout(5*time.Second)) if err != nil { return fmt.Errorf("%v (%s)", err, s.Endpoint) } defer conn.Close() ctx, cancel := context.WithCancel(context.Background()) wg := &sync.WaitGroup{} wg.Add(s.N) s.mu.Lock() s.conn = conn s.cancel = cancel s.wg = wg s.mu.Unlock() kvc := pb.NewKVClient(conn) for i := 0; i < s.N; i++ { go func(i int) { defer wg.Done() for { // TODO: 10-second is enough timeout to cover leader failure // and immediate leader election. Find out what other cases this // could be timed out. putctx, putcancel := context.WithTimeout(ctx, 10*time.Second) _, err := kvc.Put(putctx, &pb.PutRequest{ Key: []byte(fmt.Sprintf("foo%d", rand.Intn(s.KeySuffixRange))), Value: []byte(randStr(s.KeySize)), }) putcancel() if err != nil { if grpc.ErrorDesc(err) == context.DeadlineExceeded.Error() { // This retries when request is triggered at the same time as // leader failure. When we terminate the leader, the request to // that leader cannot be processed, and times out. Also requests // to followers cannot be forwarded to the old leader, so timing out // as well. We want to keep stressing until the cluster elects a // new leader and start processing requests again. continue } return } s.mu.Lock() s.success++ s.mu.Unlock() } }(i) } <-ctx.Done() return nil }
// Dial establishes a connection for a given endpoint using the client's config func (c *Client) Dial(endpoint string) (*grpc.ClientConn, error) { // TODO: enable grpc.WithTransportCredentials(creds) conn, err := grpc.Dial( endpoint, grpc.WithBlock(), grpc.WithTimeout(c.cfg.DialTimeout), grpc.WithInsecure()) if err != nil { return nil, err } return conn, nil }
// NewClientV3 creates a new grpc client connection to the member func NewClientV3(m *member) (*clientv3.Client, error) { if m.grpcAddr == "" { return nil, fmt.Errorf("member not configured for grpc") } f := func(a string, t time.Duration) (net.Conn, error) { return net.Dial("unix", a) } unixdialer := grpc.WithDialer(f) conn, err := grpc.Dial(m.grpcAddr, grpc.WithInsecure(), unixdialer) if err != nil { return nil, err } return clientv3.NewFromConn(conn), nil }
func clientSetUp(t *testing.T, addr string, cg grpc.CompressorGenerator, dg grpc.DecompressorGenerator, ua string, e env) (cc *grpc.ClientConn) { var derr error if e.security == "tls" { creds, err := credentials.NewClientTLSFromFile(tlsDir+"ca.pem", "x.test.youtube.com") if err != nil { t.Fatalf("Failed to create credentials %v", err) } cc, derr = grpc.Dial(addr, grpc.WithTransportCredentials(creds), grpc.WithDialer(e.dialer), grpc.WithUserAgent(ua), grpc.WithCompressor(cg), grpc.WithDecompressor(dg)) } else { cc, derr = grpc.Dial(addr, grpc.WithDialer(e.dialer), grpc.WithInsecure(), grpc.WithUserAgent(ua), grpc.WithCompressor(cg), grpc.WithDecompressor(dg)) } if derr != nil { t.Fatalf("Dial(%q) = %v", addr, derr) } return }
// setHealthKey sets health key on all given urls. func setHealthKey(us []string) error { for _, u := range us { conn, err := grpc.Dial(u, grpc.WithInsecure(), grpc.WithTimeout(5*time.Second)) if err != nil { return fmt.Errorf("%v (%s)", err, u) } ctx, cancel := context.WithTimeout(context.Background(), time.Second) kvc := pb.NewKVClient(conn) _, err = kvc.Put(ctx, &pb.PutRequest{Key: []byte("health"), Value: []byte("good")}) cancel() if err != nil { return err } } return nil }
// Dial establishes a connection for a given endpoint using the client's config func (c *Client) Dial(endpoint string) (*grpc.ClientConn, error) { opts := []grpc.DialOption{ grpc.WithBlock(), grpc.WithTimeout(c.cfg.DialTimeout), } if c.creds != nil { opts = append(opts, grpc.WithTransportCredentials(*c.creds)) } else { opts = append(opts, grpc.WithInsecure()) } conn, err := grpc.Dial(endpoint, opts...) if err != nil { return nil, err } return conn, nil }
// putCommandFunc executes the "put" command. func putCommandFunc(c *cli.Context) { if len(c.Args()) != 2 { panic("bad arg") } key := []byte(c.Args()[0]) value := []byte(c.Args()[1]) conn, err := grpc.Dial(c.GlobalString("endpoint"), grpc.WithInsecure()) if err != nil { panic(err) } etcd := pb.NewEtcdClient(conn) req := &pb.PutRequest{Key: key, Value: value} etcd.Put(context.Background(), req) fmt.Printf("%s %s\n", key, value) }
func (c *cluster) getRevision() (map[string]int64, error) { revs := make(map[string]int64) for _, u := range c.GRPCURLs { conn, err := grpc.Dial(u, grpc.WithInsecure(), grpc.WithTimeout(5*time.Second)) if err != nil { return nil, err } kvc := pb.NewKVClient(conn) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) resp, err := kvc.Range(ctx, &pb.RangeRequest{Key: []byte("foo")}) if err != nil { return nil, err } cancel() revs[u] = resp.Header.Revision } return revs, nil }
func (c *cluster) getKVHash() (map[string]int64, error) { hashes := make(map[string]int64) for _, u := range c.GRPCURLs { conn, err := grpc.Dial(u, grpc.WithInsecure(), grpc.WithTimeout(5*time.Second)) if err != nil { return nil, err } kvc := pb.NewKVClient(conn) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) resp, err := kvc.Hash(ctx, &pb.HashRequest{}) if resp != nil && err != nil { return nil, err } cancel() hashes[u] = int64(resp.Hash) } return hashes, nil }
func main() { flag.Parse() var opts []grpc.DialOption if *tls { var sn string if *serverHostOverride != "" { sn = *serverHostOverride } var creds credentials.TransportAuthenticator if *caFile != "" { var err error creds, err = credentials.NewClientTLSFromFile(*caFile, sn) if err != nil { grpclog.Fatalf("Failed to create TLS credentials %v", err) } } else { creds = credentials.NewClientTLSFromCert(nil, sn) } opts = append(opts, grpc.WithTransportCredentials(creds)) } else { opts = append(opts, grpc.WithInsecure()) } conn, err := grpc.Dial(*serverAddr, opts...) if err != nil { grpclog.Fatalf("fail to dial: %v", err) } defer conn.Close() client := pb.NewRouteGuideClient(conn) // Looking for a valid feature printFeature(client, &pb.Point{409146138, -746188906}) // Feature missing. printFeature(client, &pb.Point{0, 0}) // Looking for features between 40, -75 and 42, -73. printFeatures(client, &pb.Rectangle{&pb.Point{400000000, -750000000}, &pb.Point{420000000, -730000000}}) // RecordRoute runRecordRoute(client) // RouteChat runRouteChat(client) }
func TestReconnectTimeout(t *testing.T) { lis, err := net.Listen("tcp", ":0") if err != nil { t.Fatalf("Failed to listen: %v", err) } _, port, err := net.SplitHostPort(lis.Addr().String()) if err != nil { t.Fatalf("Failed to parse listener address: %v", err) } addr := "localhost:" + port conn, err := grpc.Dial(addr, grpc.WithTimeout(5*time.Second), grpc.WithBlock(), grpc.WithInsecure()) if err != nil { t.Fatalf("Failed to dial to the server %q: %v", addr, err) } // Close unaccepted connection (i.e., conn). lis.Close() tc := testpb.NewTestServiceClient(conn) waitC := make(chan struct{}) go func() { defer close(waitC) argSize := 271828 respSize := 314159 payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, int32(argSize)) if err != nil { t.Fatal(err) } req := &testpb.SimpleRequest{ ResponseType: testpb.PayloadType_COMPRESSABLE.Enum(), ResponseSize: proto.Int32(int32(respSize)), Payload: payload, } if _, err := tc.UnaryCall(context.Background(), req); err == nil { t.Fatalf("TestService/UnaryCall(_, _) = _, <nil>, want _, non-nil") } }() // Block untill reconnect times out. <-waitC if err := conn.Close(); err != grpc.ErrClientConnClosing { t.Fatalf("%v.Close() = %v, want %v", conn, err, grpc.ErrClientConnClosing) } }
// compactionCommandFunc executes the "compaction" command. func compactionCommandFunc(c *cli.Context) { if len(c.Args()) != 1 { panic("bad arg") } rev, err := strconv.ParseInt(c.Args()[0], 10, 64) if err != nil { panic("bad arg") } conn, err := grpc.Dial(c.GlobalString("endpoint"), grpc.WithInsecure()) if err != nil { panic(err) } etcd := pb.NewEtcdClient(conn) req := &pb.CompactionRequest{Revision: rev} etcd.Compact(context.Background(), req) }
func (s *stresser) Stress() error { conn, err := grpc.Dial(s.Endpoint, grpc.WithInsecure(), grpc.WithTimeout(5*time.Second)) if err != nil { return fmt.Errorf("%v (%s)", err, s.Endpoint) } defer conn.Close() ctx, cancel := context.WithCancel(context.Background()) wg := &sync.WaitGroup{} wg.Add(s.N) s.mu.Lock() s.conn = conn s.cancel = cancel s.wg = wg s.mu.Unlock() kvc := pb.NewKVClient(conn) for i := 0; i < s.N; i++ { go func(i int) { defer wg.Done() for { putctx, putcancel := context.WithTimeout(ctx, 5*time.Second) _, err := kvc.Put(putctx, &pb.PutRequest{ Key: []byte(fmt.Sprintf("foo%d", rand.Intn(s.KeySuffixRange))), Value: []byte(randStr(s.KeySize)), }) putcancel() if err != nil { return } s.mu.Lock() s.success++ s.mu.Unlock() } }(i) } <-ctx.Done() return nil }
func (c *cluster) compactKV(rev int64) error { var ( conn *grpc.ClientConn err error ) for _, u := range c.GRPCURLs { conn, err = grpc.Dial(u, grpc.WithInsecure(), grpc.WithTimeout(5*time.Second)) if err != nil { continue } kvc := pb.NewKVClient(conn) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) _, err = kvc.Compact(ctx, &pb.CompactionRequest{Revision: rev}) cancel() if err == nil { return nil } } return err }
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) }
func (s *stresser) Stress() error { conn, err := grpc.Dial(s.Endpoint, grpc.WithInsecure(), grpc.WithTimeout(5*time.Second)) if err != nil { return fmt.Errorf("%v (%s)", err, s.Endpoint) } ctx, cancel := context.WithCancel(context.Background()) s.mu.Lock() s.conn = conn s.cancel = cancel s.mu.Unlock() kvc := pb.NewKVClient(conn) for i := 0; i < s.N; i++ { go func(i int) { for { putctx, putcancel := context.WithTimeout(ctx, 5*time.Second) _, err := kvc.Put(putctx, &pb.PutRequest{ Key: []byte(fmt.Sprintf("foo%d", rand.Intn(s.KeySuffixRange))), Value: []byte(randStr(s.KeySize)), }) putcancel() if grpc.ErrorDesc(err) == context.Canceled.Error() { return } s.mu.Lock() if err != nil { s.failure++ } else { s.success++ } s.mu.Unlock() } }(i) } <-ctx.Done() return nil }
// rangeCommandFunc executes the "range" command. func rangeCommandFunc(c *cli.Context) { if len(c.Args()) == 0 { panic("bad arg") } var rangeEnd []byte key := []byte(c.Args()[0]) if len(c.Args()) > 1 { rangeEnd = []byte(c.Args()[1]) } conn, err := grpc.Dial(c.GlobalString("endpoint"), grpc.WithInsecure()) if err != nil { panic(err) } etcd := pb.NewEtcdClient(conn) req := &pb.RangeRequest{Key: key, RangeEnd: rangeEnd} resp, err := etcd.Range(context.Background(), req) for _, kv := range resp.Kvs { fmt.Printf("%s %s\n", string(kv.Key), string(kv.Value)) } }
func main() { flag.Parse() serverAddr := net.JoinHostPort(*serverHost, strconv.Itoa(*serverPort)) var opts []grpc.DialOption if *useTLS { var sn string if *tlsServerName != "" { sn = *tlsServerName } var creds credentials.TransportAuthenticator if *testCA { var err error creds, err = credentials.NewClientTLSFromFile(testCAFile, sn) if err != nil { grpclog.Fatalf("Failed to create TLS credentials %v", err) } } else { creds = credentials.NewClientTLSFromCert(nil, sn) } opts = append(opts, grpc.WithTransportCredentials(creds)) if *testCase == "compute_engine_creds" { opts = append(opts, grpc.WithPerRPCCredentials(oauth.NewComputeEngine())) } else if *testCase == "service_account_creds" { jwtCreds, err := oauth.NewServiceAccountFromFile(*serviceAccountKeyFile, *oauthScope) if err != nil { grpclog.Fatalf("Failed to create JWT credentials: %v", err) } 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(interop.GetToken(*serviceAccountKeyFile, *oauthScope)))) } } else { opts = append(opts, grpc.WithInsecure()) } conn, err := grpc.Dial(serverAddr, opts...) if err != nil { grpclog.Fatalf("Fail to dial: %v", err) } defer conn.Close() tc := testpb.NewTestServiceClient(conn) switch *testCase { case "empty_unary": interop.DoEmptyUnaryCall(tc) case "large_unary": interop.DoLargeUnaryCall(tc) case "client_streaming": interop.DoClientStreaming(tc) case "server_streaming": interop.DoServerStreaming(tc) case "ping_pong": interop.DoPingPong(tc) case "empty_stream": interop.DoEmptyStream(tc) case "timeout_on_sleeping_server": interop.DoTimeoutOnSleepingServer(tc) case "compute_engine_creds": if !*useTLS { grpclog.Fatalf("TLS is not enabled. TLS is required to execute compute_engine_creds test case.") } interop.DoComputeEngineCreds(tc, *defaultServiceAccount, *oauthScope) case "service_account_creds": if !*useTLS { grpclog.Fatalf("TLS is not enabled. TLS is required to execute service_account_creds test case.") } interop.DoServiceAccountCreds(tc, *serviceAccountKeyFile, *oauthScope) case "jwt_token_creds": if !*useTLS { grpclog.Fatalf("TLS is not enabled. TLS is required to execute jwt_token_creds test case.") } interop.DoJWTTokenCreds(tc, *serviceAccountKeyFile) case "per_rpc_creds": if !*useTLS { grpclog.Fatalf("TLS is not enabled. TLS is required to execute per_rpc_creds test case.") } interop.DoPerRPCCreds(tc, *serviceAccountKeyFile, *oauthScope) case "oauth2_auth_token": if !*useTLS { grpclog.Fatalf("TLS is not enabled. TLS is required to execute oauth2_auth_token test case.") } interop.DoOauth2TokenCreds(tc, *serviceAccountKeyFile, *oauthScope) case "cancel_after_begin": interop.DoCancelAfterBegin(tc) case "cancel_after_first_response": interop.DoCancelAfterFirstResponse(tc) default: grpclog.Fatal("Unsupported test case: ", *testCase) } }
// Dial establishes a connection for a given endpoint using the client's config func (c *Client) Dial(endpoint string) (*grpc.ClientConn, error) { // TODO: enable grpc.WithTransportCredentials(creds) return grpc.Dial(endpoint, grpc.WithInsecure()) }
func main() { var c, n int var url string flag.IntVar(&c, "c", 50, "number of connections") flag.IntVar(&n, "n", 200, "number of requests") // TODO: config the number of concurrency in each connection flag.StringVar(&url, "u", "127.0.0.1:12379", "etcd server endpoint") flag.Parse() if flag.NArg() < 1 { flag.Usage() os.Exit(1) } if act := flag.Args()[0]; act != "get" { fmt.Errorf("unsupported action %v", act) os.Exit(1) } var rangeEnd []byte key := []byte(flag.Args()[1]) if len(flag.Args()) > 2 { rangeEnd = []byte(flag.Args()[2]) } results := make(chan *result, n) bar := pb.New(n) bar.Format("Bom !") bar.Start() start := time.Now() defer func() { bar.Finish() printReport(n, results, time.Now().Sub(start)) }() var wg sync.WaitGroup wg.Add(c) jobs := make(chan struct{}, n) for i := 0; i < c; i++ { go func() { defer wg.Done() conn, err := grpc.Dial(url, grpc.WithInsecure()) if err != nil { fmt.Errorf("dial error: %v", err) os.Exit(1) } etcd := etcdserverpb.NewEtcdClient(conn) req := &etcdserverpb.RangeRequest{Key: key, RangeEnd: rangeEnd} for _ = range jobs { st := time.Now() resp, err := etcd.Range(context.Background(), req) var errStr string if err != nil { errStr = err.Error() } else { errStr = resp.Header.Error } results <- &result{ errStr: errStr, duration: time.Now().Sub(st), } bar.Increment() } }() } for i := 0; i < n; i++ { jobs <- struct{}{} } close(jobs) wg.Wait() }