2626import tech .ydb .core .grpc .GrpcRequestSettings ;
2727import tech .ydb .core .operation .StatusExtractor ;
2828import tech .ydb .core .settings .BaseRequestSettings ;
29+ import tech .ydb .core .tracing .Scope ;
2930import tech .ydb .core .tracing .Span ;
3031import tech .ydb .core .utils .URITools ;
3132import tech .ydb .core .utils .UpdatableOptional ;
@@ -126,10 +127,14 @@ public CompletableFuture<Result<QueryTransaction>> beginTransaction(TxMode tx, B
126127 .setTxSettings (TxControl .txSettings (tx ))
127128 .build ();
128129
129- return rpc .beginTransaction (request , makeOptions (settings ).build ()).thenApply (result -> {
130- updateSessionState (result .getStatus ());
131- return result .map (resp -> updateTransaction (new TransactionImpl (tx , resp .getTxMeta ().getId ())));
132- });
130+ Span span = startSpan ("ydb.BeginTransaction" );
131+ try (Scope ignored = span .makeCurrent ()) {
132+ return Span .endOnResult (span , rpc .beginTransaction (request , makeOptions (settings , span ).build ()))
133+ .thenApply (result -> {
134+ updateSessionState (result .getStatus ());
135+ return result .map (resp -> updateTransaction (new TransactionImpl (tx , resp .getTxMeta ().getId ())));
136+ });
137+ }
133138 }
134139
135140 private QueryTransaction updateTransaction (TransactionImpl newTx ) {
@@ -331,14 +336,16 @@ GrpcReadStream<YdbQuery.ExecuteQueryResponsePart> createGrpcStream(
331336 public QueryStream createQuery (String query , TxMode tx , Params prms , ExecuteQuerySettings settings ) {
332337 YdbQuery .TransactionControl tc = TxControl .txModeCtrl (tx , true );
333338 Span span = startSpan ("ydb.ExecuteQuery" );
334- return new StreamImpl (createGrpcStream (query , tc , prms , settings , span ), span ) {
335- @ Override
336- void handleTxMeta (String txID ) {
337- if (txID != null && !txID .isEmpty ()) {
338- logger .warn ("{} got unexpected transaction id {}" , SessionImpl .this , txID );
339+ try (Scope ignored = span .makeCurrent ()) {
340+ return new StreamImpl (createGrpcStream (query , tc , prms , settings , span ), span ) {
341+ @ Override
342+ void handleTxMeta (String txID ) {
343+ if (txID != null && !txID .isEmpty ()) {
344+ logger .warn ("{} got unexpected transaction id {}" , SessionImpl .this , txID );
345+ }
339346 }
340- }
341- };
347+ };
348+ }
342349 }
343350
344351 public CompletableFuture <Result <YdbQuery .DeleteSessionResponse >> delete (DeleteSessionSettings settings ) {
@@ -478,44 +485,46 @@ public QueryStream createQuery(String query, boolean commitAtEnd, Params prms, E
478485 : TxControl .txModeCtrl (txMode , commitAtEnd );
479486
480487 Span span = startSpan ("ydb.ExecuteQuery" );
481- return new StreamImpl (createGrpcStream (query , tc , prms , settings , span ), span ) {
482- @ Override
483- void handleTxMeta (String txID ) {
484- String newId = txID == null || txID .isEmpty () ? null : txID ;
485- if (!txId .compareAndSet (currentId , newId )) {
486- logger .warn ("{} lost transaction meta id {}" , SessionImpl .this , newId );
488+ try (Scope ignored = span .makeCurrent ()) {
489+ return new StreamImpl (createGrpcStream (query , tc , prms , settings , span ), span ) {
490+ @ Override
491+ void handleTxMeta (String txID ) {
492+ String newId = txID == null || txID .isEmpty () ? null : txID ;
493+ if (!txId .compareAndSet (currentId , newId )) {
494+ logger .warn ("{} lost transaction meta id {}" , SessionImpl .this , newId );
495+ }
487496 }
488- }
489497
490- @ Override
491- void handleCompletion (Status status , Throwable th ) {
492- if (th != null ) {
493- currentStatusFuture .completeExceptionally (
494- new RuntimeException ("Query on transaction failed with exception " , th ));
495- }
496- if (status .isSuccess ()) {
497- if (commitAtEnd ) {
498- currentStatusFuture .complete (Status .SUCCESS );
498+ @ Override
499+ void handleCompletion (Status status , Throwable th ) {
500+ if (th != null ) {
501+ currentStatusFuture .completeExceptionally (
502+ new RuntimeException ("Query on transaction failed with exception " , th ));
499503 }
500- } else {
501- if (txId .compareAndSet (currentId , null )) {
502- logger .warn ("{} transaction with id {} was failed" , SessionImpl .this , currentId );
504+ if (status .isSuccess ()) {
505+ if (commitAtEnd ) {
506+ currentStatusFuture .complete (Status .SUCCESS );
507+ }
508+ } else {
509+ if (txId .compareAndSet (currentId , null )) {
510+ logger .warn ("{} transaction with id {} was failed" , SessionImpl .this , currentId );
511+ }
512+ currentStatusFuture .complete (Status
513+ .of (StatusCode .ABORTED )
514+ .withIssues (Issue .of ("Query on transaction failed with status "
515+ + status , Issue .Severity .ERROR )));
503516 }
504- currentStatusFuture .complete (Status
505- .of (StatusCode .ABORTED )
506- .withIssues (Issue .of ("Query on transaction failed with status "
507- + status , Issue .Severity .ERROR )));
508517 }
509- }
510518
511- @ Override
512- public void cancel () {
513- super .cancel ();
514- if (txId .compareAndSet (currentId , null )) {
515- logger .warn ("{} transaction with id {} was cancelled" , SessionImpl .this , currentId );
519+ @ Override
520+ public void cancel () {
521+ super .cancel ();
522+ if (txId .compareAndSet (currentId , null )) {
523+ logger .warn ("{} transaction with id {} was cancelled" , SessionImpl .this , currentId );
524+ }
516525 }
517- }
518- };
526+ };
527+ }
519528 }
520529
521530 @ Override
@@ -534,22 +543,25 @@ public CompletableFuture<Result<QueryInfo>> commit(CommitTransactionSettings set
534543 .setTxId (transactionId )
535544 .build ();
536545
537- return Span .endOnResult (span , rpc .commitTransaction (request , makeOptions (settings , span ).build ()))
538- .thenApply (res -> {
539- Status status = res .getStatus ();
540- currentStatusFuture .complete (status );
541- updateSessionState (status );
542- if (!txId .compareAndSet (transactionId , null )) {
543- logger .warn ("{} lost commit response for transaction {}" , SessionImpl .this , transactionId );
544- }
545- // TODO: CommitTransactionResponse must contain exec_stats
546- return res .map (resp -> new QueryInfo (null ));
547- }).whenComplete (((status , th ) -> {
548- if (th != null ) {
549- currentStatusFuture .completeExceptionally (
550- new RuntimeException ("Transaction commit failed with exception" , th ));
551- }
552- }));
546+ try (Scope ignored = span .makeCurrent ()) {
547+ return Span .endOnResult (span , rpc .commitTransaction (request , makeOptions (settings , span ).build ()))
548+ .thenApply (res -> {
549+ Status status = res .getStatus ();
550+ currentStatusFuture .complete (status );
551+ updateSessionState (status );
552+ if (!txId .compareAndSet (transactionId , null )) {
553+ logger .warn ("{} lost commit response for transaction {}" , SessionImpl .this ,
554+ transactionId );
555+ }
556+ // TODO: CommitTransactionResponse must contain exec_stats
557+ return res .map (resp -> new QueryInfo (null ));
558+ }).whenComplete (((status , th ) -> {
559+ if (th != null ) {
560+ currentStatusFuture .completeExceptionally (
561+ new RuntimeException ("Transaction commit failed with exception" , th ));
562+ }
563+ }));
564+ }
553565 }
554566
555567 @ Override
@@ -568,20 +580,22 @@ public CompletableFuture<Status> rollback(RollbackTransactionSettings settings)
568580 .setSessionId (sessionId )
569581 .setTxId (transactionId )
570582 .build ();
571- return Span .endOnResult (span , rpc .rollbackTransaction (request , makeOptions (settings , span ).build ()))
572- .thenApply (result -> {
573- updateSessionState (result .getStatus ());
574- if (!txId .compareAndSet (transactionId , null )) {
575- logger .warn ("{} lost rollback response for transaction {}" , SessionImpl .this ,
576- transactionId );
577- }
578- return result .getStatus ();
579- })
580- .whenComplete ((status , th ) -> {
581- currentStatusFuture .complete (Status
582- .of (StatusCode .ABORTED )
583- .withIssues (Issue .of ("Transaction was rolled back" , Issue .Severity .ERROR )));
584- });
583+ try (Scope ignored = span .makeCurrent ()) {
584+ return Span .endOnResult (span , rpc .rollbackTransaction (request , makeOptions (settings , span ).build ()))
585+ .thenApply (result -> {
586+ updateSessionState (result .getStatus ());
587+ if (!txId .compareAndSet (transactionId , null )) {
588+ logger .warn ("{} lost rollback response for transaction {}" , SessionImpl .this ,
589+ transactionId );
590+ }
591+ return result .getStatus ();
592+ })
593+ .whenComplete ((status , th ) -> {
594+ currentStatusFuture .complete (Status
595+ .of (StatusCode .ABORTED )
596+ .withIssues (Issue .of ("Transaction was rolled back" , Issue .Severity .ERROR )));
597+ });
598+ }
585599 }
586600 }
587601}
0 commit comments