1313import org .slf4j .Logger ;
1414import org .slf4j .LoggerFactory ;
1515import org .slf4j .MDC ;
16- import tech .ydb .yoj .DeprecationWarnings ;
1716import tech .ydb .yoj .repository .db .cache .TransactionLog ;
17+ import tech .ydb .yoj .repository .db .exception .QueryInterruptedException ;
1818import tech .ydb .yoj .repository .db .exception .RetryableException ;
19- import tech .ydb .yoj .util .lang .CallStack ;
2019import tech .ydb .yoj .util .lang .Strings ;
2120
2221import javax .annotation .Nullable ;
2322import java .time .Duration ;
24- import java .util .Set ;
2523import java .util .concurrent .atomic .AtomicLong ;
2624import java .util .function .Supplier ;
2725
2826import static java .lang .String .format ;
2927import static java .util .Objects .requireNonNull ;
28+ import static java .util .concurrent .TimeUnit .MILLISECONDS ;
3029import static tech .ydb .yoj .repository .db .IsolationLevel .ONLINE_CONSISTENT_READ_ONLY ;
3130import static tech .ydb .yoj .repository .db .IsolationLevel .SERIALIZABLE_READ_WRITE ;
3231
4544 */
4645@ RequiredArgsConstructor (access = AccessLevel .PRIVATE )
4746public final class StdTxManager implements TxManager , TxManagerState {
48- /**
49- * @deprecated Please stop using the {@code StdTxManager.useNewTxNameGeneration} field.
50- * Changing this field has no effect as of YOJ 2.6.1, and it will be <strong>removed completely</strong> in YOJ 2.7.0.
51- */
52- @ Deprecated (forRemoval = true )
53- public static volatile boolean useNewTxNameGeneration = true ;
54-
5547 private static final Logger log = LoggerFactory .getLogger (StdTxManager .class );
5648
57- private static final CallStack callStack = new CallStack ();
58-
5949 private static final int DEFAULT_MAX_ATTEMPT_COUNT = 100 ;
6050 private static final double [] TX_ATTEMPTS_BUCKETS = new double []
6151 {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 12 , 14 , 16 , 18 , 20 , 25 , 35 , 40 , 45 , 50 , 60 , 70 , 80 , 90 , 100 };
@@ -92,19 +82,13 @@ public final class StdTxManager implements TxManager, TxManagerState {
9282 @ With (AccessLevel .PRIVATE )
9383 private final int maxAttemptCount ;
9484 @ With
95- private final String name ;
96- @ With (AccessLevel .PRIVATE )
97- private final Integer logLine ;
98- @ With
9985 @ Getter
10086 private final String logContext ;
10187 @ With (AccessLevel .PRIVATE )
10288 private final TxOptions options ;
10389 @ With (AccessLevel .PRIVATE )
10490 private final SeparatePolicy separatePolicy ;
10591 @ With
106- private final Set <String > skipCallerPackages ;
107- @ With
10892 private final TxNameGenerator txNameGenerator ;
10993
11094 private final long txLogId = txLogIdSeq .incrementAndGet ();
@@ -113,25 +97,16 @@ public StdTxManager(@NonNull Repository repository) {
11397 this (
11498 /* repository */ repository ,
11599 /* maxAttemptCount */ DEFAULT_MAX_ATTEMPT_COUNT ,
116- /* name */ null ,
117- /* logLine */ null ,
118100 /* logContext */ null ,
119101 /* options */ TxOptions .create (SERIALIZABLE_READ_WRITE ),
120102 /* separatePolicy */ SeparatePolicy .LOG ,
121- /* skipCallerPackages */ Set .of (),
122- /* txNameGenerator */ TxNameGenerator .SHORT
103+ /* txNameGenerator */ new TxNameGenerator .Default ()
123104 );
124105 }
125106
126- /**
127- * @deprecated Constructor is in YOJ 2.x for backwards compatibility, an will be removed in YOJ 3.0.0. Please construct
128- * {@link #StdTxManager(Repository)} and customize it using the {@code with<...>()} methods instead.
129- */
130- @ Deprecated (forRemoval = true )
131- public StdTxManager (Repository repository , int maxAttemptCount , String name , Integer logLine , String logContext , TxOptions options ) {
132- this (repository , maxAttemptCount , name , logLine , logContext , options , SeparatePolicy .LOG , Set .of (), TxNameGenerator .SHORT );
133- DeprecationWarnings .warnOnce ("StdTxManager(Repository, int, String, Integer, String, TxOptions)" ,
134- "Please use the recommended StdTxManager(Repository) constructor and customize the TxManager by using with<...>() methods" );
107+ @ Override
108+ public StdTxManager withName (String name ) {
109+ return withTxNameGenerator (new TxNameGenerator .Constant (name ));
135110 }
136111
137112 @ Override
@@ -215,25 +190,27 @@ public void tx(Runnable runnable) {
215190
216191 @ Override
217192 public <T > T tx (Supplier <T > supplier ) {
218- if (name == null ) {
219- return withGeneratedNameAndLine ().tx (supplier );
220- }
193+ TxName txName = txNameGenerator .generate ();
194+ String name = txName .name ();
221195
222- checkSeparatePolicy (separatePolicy , name );
223- return txImpl (supplier );
224- }
196+ checkSeparatePolicy (separatePolicy , txName .logName ());
225197
226- private <T > T txImpl (Supplier <T > supplier ) {
227198 RetryableException lastRetryableException = null ;
228199 TxImpl lastTx = null ;
229200 try (Timer ignored = totalDuration .labels (name ).startTimer ()) {
230201 for (int attempt = 1 ; attempt <= maxAttemptCount ; attempt ++) {
231202 try {
232203 attempts .labels (name ).observe (attempt );
233204 T result ;
234- try (var ignored1 = attemptDuration .labels (name ).startTimer ()) {
235- lastTx = new TxImpl (name , repository .startTransaction (options ), options );
236- result = runAttempt (supplier , lastTx );
205+ try (
206+ var ignored1 = attemptDuration .labels (name ).startTimer ();
207+ var ignored2 = MDC .putCloseable ("tx" , formatTx (txName ));
208+ var ignored3 = MDC .putCloseable ("tx-id" , formatTxId ());
209+ var ignored4 = MDC .putCloseable ("tx-name" , txName .logName ())
210+ ) {
211+ RepositoryTransaction transaction = repository .startTransaction (options );
212+ lastTx = new TxImpl (name , transaction , options );
213+ result = lastTx .run (supplier );
237214 }
238215
239216 if (options .isDryRun ()) {
@@ -247,7 +224,12 @@ private <T> T txImpl(Supplier<T> supplier) {
247224 retries .labels (name , getExceptionNameForMetric (e )).inc ();
248225 lastRetryableException = e ;
249226 if (attempt + 1 <= maxAttemptCount ) {
250- e .sleep (attempt );
227+ try {
228+ MILLISECONDS .sleep (e .getRetryPolicy ().calcDuration (attempt ).toMillis ());
229+ } catch (InterruptedException ex ) {
230+ Thread .currentThread ().interrupt ();
231+ throw new QueryInterruptedException ("DB query interrupted" , ex );
232+ }
251233 }
252234 } catch (Exception e ) {
253235 results .labels (name , "rollback" ).inc ();
@@ -284,49 +266,14 @@ private String getExceptionNameForMetric(RetryableException e) {
284266 return Strings .removeSuffix (e .getClass ().getSimpleName (), "Exception" );
285267 }
286268
287- private <T > T runAttempt (Supplier <T > supplier , TxImpl tx ) {
288- try (var ignored2 = MDC .putCloseable ("tx" , formatTx ());
289- var ignored3 = MDC .putCloseable ("tx-id" , formatTxId ());
290- var ignored4 = MDC .putCloseable ("tx-name" , formatTxName (false ))) {
291- return tx .run (supplier );
292- }
293- }
294-
295- private StdTxManager withGeneratedNameAndLine () {
296- record TxInfo (String name , int lineNumber ) {
297- }
298-
299- if (!useNewTxNameGeneration ) {
300- DeprecationWarnings .warnOnce ("StdTxManager.useNewTxNameGeneration" ,
301- "Setting StdTxManager.useNewTxNameGeneration has no effect. Please stop setting this field, it will be removed in YOJ 2.7.0" );
302- }
303-
304- var info = callStack .findCallingFrame ()
305- .skipPackage (StdTxManager .class .getPackageName ())
306- .skipPackages (skipCallerPackages )
307- .map (
308- f -> new TxInfo (
309- txNameGenerator .nameFor (f .getClassName (), f .getMethodName ()),
310- f .getLineNumber ()
311- ),
312- txNameGenerator
313- );
314-
315- return withName (info .name ).withLogLine (info .lineNumber );
316- }
317-
318- private String formatTx () {
319- return formatTxId () + " {" + formatTxName (true ) + "}" ;
269+ private String formatTx (TxName txName ) {
270+ return formatTxId () + " {" + txName .logName () + (logContext != null ? "/" + logContext : "" ) + "}" ;
320271 }
321272
322273 private String formatTxId () {
323274 return Strings .leftPad (Long .toUnsignedString (txLogId , 36 ), 6 , '0' ) + options .getIsolationLevel ().getTxIdSuffix ();
324275 }
325276
326- private String formatTxName (boolean withContext ) {
327- return name + (logLine != null ? ":" + logLine : "" ) + (withContext && logContext != null ? "/" + logContext : "" );
328- }
329-
330277 @ Override
331278 public TxManagerState getState () {
332279 return this ;
0 commit comments