|
| 1 | +/* |
| 2 | +/* |
| 3 | + * Copyright 2021 the original author or authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.couchbase.transactions; |
| 18 | + |
| 19 | +import com.couchbase.client.core.annotation.Stability; |
| 20 | +import com.couchbase.transactions.config.MergedTransactionConfig; |
| 21 | +import com.couchbase.transactions.config.PerTransactionConfig; |
| 22 | +import com.couchbase.transactions.config.PerTransactionConfigBuilder; |
| 23 | +import com.couchbase.transactions.config.TransactionConfig; |
| 24 | +import com.couchbase.transactions.forwards.Supported; |
| 25 | +import com.couchbase.transactions.log.TransactionLogger; |
| 26 | + |
| 27 | +import java.time.Duration; |
| 28 | +import java.time.temporal.ChronoUnit; |
| 29 | +import java.util.Optional; |
| 30 | +import java.util.UUID; |
| 31 | + |
| 32 | +/** |
| 33 | + * To access the AttemptContextReactive held by AttemptContext |
| 34 | + * |
| 35 | + * @author Michael Reiche |
| 36 | + */ |
| 37 | +public class AttemptContextReactiveAccessor { |
| 38 | + |
| 39 | + public static AttemptContextReactive getACR(AttemptContext attemptContext) { |
| 40 | + return attemptContext.ctx(); |
| 41 | + } |
| 42 | + |
| 43 | + public static AttemptContext from(AttemptContextReactive attemptContextReactive) { |
| 44 | + return new AttemptContext(attemptContextReactive); |
| 45 | + } |
| 46 | + |
| 47 | + public static TransactionLogger getLogger(AttemptContextReactive attemptContextReactive){ |
| 48 | + return attemptContextReactive.LOGGER; |
| 49 | + } |
| 50 | + @Stability.Internal |
| 51 | + public static AttemptContextReactive newAttemptContextReactive(TransactionsReactive transactions){ |
| 52 | + PerTransactionConfig perConfig = PerTransactionConfigBuilder.create().build(); |
| 53 | + MergedTransactionConfig merged = new MergedTransactionConfig(transactions.config(), Optional.of(perConfig)); |
| 54 | + |
| 55 | + TransactionContext overall = new TransactionContext( |
| 56 | + transactions.cleanup().clusterData().cluster().environment().requestTracer(), |
| 57 | + transactions.cleanup().clusterData().cluster().environment().eventBus(), |
| 58 | + UUID.randomUUID().toString(), now(), Duration.ZERO, merged); |
| 59 | + |
| 60 | + String txnId = UUID.randomUUID().toString(); |
| 61 | + overall.LOGGER.info(configDebug(transactions.config(), perConfig)); |
| 62 | + return transactions.createAttemptContext(overall, merged, txnId); |
| 63 | + } |
| 64 | + |
| 65 | + private static Duration now() { |
| 66 | + return Duration.of(System.nanoTime(), ChronoUnit.NANOS); |
| 67 | + } |
| 68 | + |
| 69 | + static private String configDebug(TransactionConfig config, PerTransactionConfig perConfig) { |
| 70 | + StringBuilder sb = new StringBuilder(); |
| 71 | + sb.append("library version: "); |
| 72 | + sb.append(TransactionsReactive.class.getPackage().getImplementationVersion()); |
| 73 | + sb.append(" config: "); |
| 74 | + sb.append("atrs="); |
| 75 | + sb.append(config.numAtrs()); |
| 76 | + sb.append(", metadataCollection="); |
| 77 | + sb.append(config.metadataCollection()); |
| 78 | + sb.append(", expiry="); |
| 79 | + sb.append(perConfig.expirationTime().orElse(config.transactionExpirationTime()).toMillis()); |
| 80 | + sb.append("msecs durability="); |
| 81 | + sb.append(config.durabilityLevel()); |
| 82 | + sb.append(" per-txn config="); |
| 83 | + sb.append(" durability="); |
| 84 | + sb.append(perConfig.durabilityLevel()); |
| 85 | + sb.append(", supported="); |
| 86 | + sb.append(Supported.SUPPORTED); |
| 87 | + return sb.toString(); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments