本文整理匯總了Golang中github.com/cockroachdb/cockroach/pkg/internal/client.NewDB函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewDB函數的具體用法?Golang NewDB怎麽用?Golang NewDB使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewDB函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: MakeDBClient
// MakeDBClient creates a kv client for use in cli tools.
func MakeDBClient() (*client.DB, *stop.Stopper, error) {
conn, stopper, err := getGRPCConn()
if err != nil {
return nil, nil, err
}
return client.NewDB(client.NewSender(conn)), stopper, nil
}
示例2: TestTxnAbandonCount
func TestTxnAbandonCount(t *testing.T) {
defer leaktest.AfterTest(t)()
manual, sender, cleanupFn := setupMetricsTest(t)
defer cleanupFn()
value := []byte("value")
db := client.NewDB(sender)
// Test abandoned transaction by making the client timeout ridiculously short. We also set
// the sender to heartbeat very frequently, because the heartbeat detects and tears down
// abandoned transactions.
sender.heartbeatInterval = 2 * time.Millisecond
sender.clientTimeout = 1 * time.Millisecond
if err := db.Txn(context.TODO(), func(txn *client.Txn) error {
key := []byte("key-abandon")
if err := txn.SetIsolation(enginepb.SNAPSHOT); err != nil {
return err
}
if err := txn.Put(key, value); err != nil {
return err
}
manual.Increment(int64(sender.clientTimeout + sender.heartbeatInterval*2))
checkTxnMetrics(t, sender, "abandon txn", 0, 0, 1, 0, 0)
return nil
}); !testutils.IsError(err, "writing transaction timed out") {
t.Fatalf("unexpected error: %v", err)
}
}
示例3: TestTxnAbortCount
func TestTxnAbortCount(t *testing.T) {
defer leaktest.AfterTest(t)()
_, sender, cleanupFn := setupMetricsTest(t)
defer cleanupFn()
value := []byte("value")
db := client.NewDB(sender)
intentionalErrText := "intentional error to cause abort"
// Test aborted transaction.
if err := db.Txn(context.TODO(), func(txn *client.Txn) error {
key := []byte("key-abort")
if err := txn.SetIsolation(enginepb.SNAPSHOT); err != nil {
return err
}
if err := txn.Put(key, value); err != nil {
t.Fatal(err)
}
return errors.New(intentionalErrText)
}); !testutils.IsError(err, intentionalErrText) {
t.Fatalf("unexpected error: %v", err)
}
teardownHeartbeats(sender)
checkTxnMetrics(t, sender, "abort txn", 0, 0, 0, 1, 0)
}
示例4: NewClient
// NewClient implements the Cluster interface.
func (f *Farmer) NewClient(ctx context.Context, t testing.TB, i int) *client.DB {
conn, err := f.RPCContext.GRPCDial(f.Addr(ctx, i, base.DefaultPort))
if err != nil {
t.Fatal(err)
}
return client.NewDB(client.NewSender(conn))
}
示例5: createTestNode
// createTestNode creates an rpc server using the specified address,
// gossip instance, KV database and a node using the specified slice
// of engines. The server, clock and node are returned. If gossipBS is
// not nil, the gossip bootstrap address is set to gossipBS.
func createTestNode(
addr net.Addr, engines []engine.Engine, gossipBS net.Addr, t *testing.T,
) (*grpc.Server, net.Addr, *hlc.Clock, *Node, *stop.Stopper) {
cfg := storage.StoreConfig{}
stopper := stop.NewStopper()
cfg.Clock = hlc.NewClock(hlc.UnixNano)
nodeRPCContext := rpc.NewContext(log.AmbientContext{}, nodeTestBaseContext, cfg.Clock, stopper)
cfg.ScanInterval = 10 * time.Hour
cfg.ConsistencyCheckInterval = 10 * time.Hour
grpcServer := rpc.NewServer(nodeRPCContext)
serverCfg := makeTestConfig()
cfg.Gossip = gossip.NewTest(
0,
nodeRPCContext,
grpcServer,
serverCfg.GossipBootstrapResolvers,
stopper,
metric.NewRegistry(),
)
ln, err := netutil.ListenAndServeGRPC(stopper, grpcServer, addr)
if err != nil {
t.Fatal(err)
}
if gossipBS != nil {
// Handle possibility of a :0 port specification.
if gossipBS.Network() == addr.Network() && gossipBS.String() == addr.String() {
gossipBS = ln.Addr()
}
r, err := resolver.NewResolverFromAddress(gossipBS)
if err != nil {
t.Fatalf("bad gossip address %s: %s", gossipBS, err)
}
cfg.Gossip.SetResolvers([]resolver.Resolver{r})
cfg.Gossip.Start(ln.Addr())
}
retryOpts := base.DefaultRetryOptions()
retryOpts.Closer = stopper.ShouldQuiesce()
distSender := kv.NewDistSender(kv.DistSenderConfig{
Clock: cfg.Clock,
RPCContext: nodeRPCContext,
RPCRetryOptions: &retryOpts,
}, cfg.Gossip)
cfg.AmbientCtx.Tracer = tracing.NewTracer()
sender := kv.NewTxnCoordSender(
cfg.AmbientCtx,
distSender,
cfg.Clock,
false,
stopper,
kv.MakeTxnMetrics(metric.TestSampleInterval),
)
cfg.DB = client.NewDB(sender)
cfg.Transport = storage.NewDummyRaftTransport()
cfg.MetricsSampleInterval = metric.TestSampleInterval
node := NewNode(cfg, status.NewMetricsRecorder(cfg.Clock), metric.NewRegistry(), stopper,
kv.MakeTxnMetrics(metric.TestSampleInterval), sql.MakeEventLogger(nil))
roachpb.RegisterInternalServer(grpcServer, node)
return grpcServer, ln.Addr(), cfg.Clock, node, stopper
}
示例6: NewClient
// NewClient implements the Cluster interface.
func (f *Farmer) NewClient(ctx context.Context, i int) (*client.DB, error) {
conn, err := f.RPCContext.GRPCDial(f.Addr(ctx, i, base.DefaultPort))
if err != nil {
return nil, err
}
return client.NewDB(client.NewSender(conn)), nil
}
示例7: TestTxnCommit
// Test a normal transaction. This and the other metrics tests below use real KV operations,
// because it took far too much mucking with TxnCoordSender internals to mock out the sender
// function as other tests do.
func TestTxnCommit(t *testing.T) {
defer leaktest.AfterTest(t)()
_, sender, cleanupFn := setupMetricsTest(t)
defer cleanupFn()
value := []byte("value")
db := client.NewDB(sender)
// Test normal commit.
if err := db.Txn(context.TODO(), func(txn *client.Txn) error {
key := []byte("key-commit")
if err := txn.SetIsolation(enginepb.SNAPSHOT); err != nil {
return err
}
if err := txn.Put(key, value); err != nil {
return err
}
if err := txn.CommitOrCleanup(); err != nil {
return err
}
return nil
}); err != nil {
t.Fatal(err)
}
teardownHeartbeats(sender)
checkTxnMetrics(t, sender, "commit txn", 1, 0 /* not 1PC */, 0, 0, 0)
}
示例8: newKVNative
func newKVNative(b *testing.B) kvInterface {
enableTracing := tracing.Disable()
s, _, _ := serverutils.StartServer(b, base.TestServerArgs{})
// TestServer.DB() returns the TxnCoordSender wrapped client. But that isn't
// a fair comparison with SQL as we want these client requests to be sent
// over the network.
sender, err := client.NewSender(
rpc.NewContext(log.AmbientContext{}, &base.Config{
User: security.NodeUser,
SSLCA: filepath.Join(security.EmbeddedCertsDir, security.EmbeddedCACert),
SSLCert: filepath.Join(security.EmbeddedCertsDir, "node.crt"),
SSLCertKey: filepath.Join(security.EmbeddedCertsDir, "node.key"),
}, nil, s.Stopper()),
s.ServingAddr())
if err != nil {
b.Fatal(err)
}
return &kvNative{
db: client.NewDB(sender),
doneFn: func() {
s.Stopper().Stop()
enableTracing()
},
}
}
示例9: makeClient
func (c *Cluster) makeClient(nodeIdx int) *client.DB {
sender, err := client.NewSender(c.rpcCtx, c.RPCAddr(nodeIdx))
if err != nil {
log.Fatalf(context.Background(), "failed to initialize KV client: %s", err)
}
return client.NewDB(sender)
}
示例10: NewClient
// NewClient implements the Cluster interface.
func (f *Farmer) NewClient(t *testing.T, i int) (*client.DB, *stop.Stopper) {
stopper := stop.NewStopper()
rpcContext := rpc.NewContext(log.AmbientContext{}, &base.Config{
Insecure: true,
User: security.NodeUser,
}, nil, stopper)
sender, err := client.NewSender(rpcContext, f.Addr(i, base.DefaultPort))
if err != nil {
t.Fatal(err)
}
return client.NewDB(sender), stopper
}
示例11: TestInconsistentReads
// TestInconsistentReads tests that the methods that generate inconsistent reads
// generate outgoing requests with an INCONSISTENT read consistency.
func TestInconsistentReads(t *testing.T) {
defer leaktest.AfterTest(t)()
// Mock out DistSender's sender function to check the read consistency for
// outgoing BatchRequests and return an empty reply.
var senderFn client.SenderFunc
senderFn = func(_ context.Context, ba roachpb.BatchRequest) (*roachpb.BatchResponse, *roachpb.Error) {
if ba.ReadConsistency != roachpb.INCONSISTENT {
return nil, roachpb.NewErrorf("BatchRequest has unexpected ReadConsistency %s",
ba.ReadConsistency)
}
return ba.CreateReply(), nil
}
db := client.NewDB(senderFn)
ctx := context.TODO()
prepInconsistent := func() *client.Batch {
b := &client.Batch{}
b.Header.ReadConsistency = roachpb.INCONSISTENT
return b
}
// Perform inconsistent reads through the mocked sender function.
{
key := roachpb.Key([]byte("key"))
b := prepInconsistent()
b.Get(key)
if err := db.Run(ctx, b); err != nil {
t.Fatal(err)
}
}
{
b := prepInconsistent()
key1 := roachpb.Key([]byte("key1"))
key2 := roachpb.Key([]byte("key2"))
b.Scan(key1, key2)
if err := db.Run(ctx, b); err != nil {
t.Fatal(err)
}
}
{
key := roachpb.Key([]byte("key"))
b := &client.Batch{}
b.Header.ReadConsistency = roachpb.INCONSISTENT
b.Get(key)
if err := db.Run(ctx, b); err != nil {
t.Fatal(err)
}
}
}
示例12: createTestClientForUser
func createTestClientForUser(t *testing.T, stopper *stop.Stopper, addr, user string) *client.DB {
var ctx base.Config
ctx.InitDefaults()
ctx.User = user
ctx.SSLCA = filepath.Join(security.EmbeddedCertsDir, security.EmbeddedCACert)
ctx.SSLCert = filepath.Join(security.EmbeddedCertsDir, fmt.Sprintf("%s.crt", user))
ctx.SSLCertKey = filepath.Join(security.EmbeddedCertsDir, fmt.Sprintf("%s.key", user))
sender, err := client.NewSender(rpc.NewContext(log.AmbientContext{}, &ctx, nil, stopper), addr)
if err != nil {
t.Fatal(err)
}
return client.NewDB(sender)
}
示例13: NewClient
// NewClient implements the Cluster interface.
func (l *LocalCluster) NewClient(t *testing.T, i int) (*roachClient.DB, *stop.Stopper) {
stopper := stop.NewStopper()
rpcContext := rpc.NewContext(log.AmbientContext{}, &base.Config{
User: security.NodeUser,
SSLCA: filepath.Join(l.CertsDir, security.EmbeddedCACert),
SSLCert: filepath.Join(l.CertsDir, security.EmbeddedNodeCert),
SSLCertKey: filepath.Join(l.CertsDir, security.EmbeddedNodeKey),
}, hlc.NewClock(hlc.UnixNano, 0), stopper)
conn, err := rpcContext.GRPCDial(l.Nodes[i].Addr(DefaultTCP).String())
if err != nil {
t.Fatal(err)
}
return roachClient.NewDB(roachClient.NewSender(conn)), stopper
}
示例14: createTestClientForUser
func createTestClientForUser(
t *testing.T, s serverutils.TestServerInterface, user string,
) *client.DB {
var ctx base.Config
ctx.InitDefaults()
ctx.User = user
ctx.SSLCA = filepath.Join(security.EmbeddedCertsDir, security.EmbeddedCACert)
ctx.SSLCert = filepath.Join(security.EmbeddedCertsDir, fmt.Sprintf("%s.crt", user))
ctx.SSLCertKey = filepath.Join(security.EmbeddedCertsDir, fmt.Sprintf("%s.key", user))
conn, err := rpc.NewContext(log.AmbientContext{}, &ctx, s.Clock(), s.Stopper()).GRPCDial(s.ServingAddr())
if err != nil {
t.Fatal(err)
}
return client.NewDB(client.NewSender(conn))
}
示例15: TestTxnOnePhaseCommit
// TestTxnOnePhaseCommit verifies that 1PC metric tracking works.
func TestTxnOnePhaseCommit(t *testing.T) {
defer leaktest.AfterTest(t)()
_, sender, cleanupFn := setupMetricsTest(t)
defer cleanupFn()
value := []byte("value")
db := client.NewDB(sender)
if err := db.Txn(context.TODO(), func(txn *client.Txn) error {
key := []byte("key-commit")
b := txn.NewBatch()
b.Put(key, value)
return txn.CommitInBatch(b)
}); err != nil {
t.Fatal(err)
}
teardownHeartbeats(sender)
checkTxnMetrics(t, sender, "commit 1PC txn", 1, 1 /* 1PC */, 0, 0, 0)
}