com.hazelcast.transaction.impl.xa.XATransaction Maven / Gradle / Ivy
Show all versions of hazelcast-all Show documentation
/*
* Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.transaction.impl.xa;
import com.hazelcast.core.ExecutionCallback;
import com.hazelcast.logging.ILogger;
import com.hazelcast.spi.InternalCompletableFuture;
import com.hazelcast.spi.NodeEngine;
import com.hazelcast.spi.OperationService;
import com.hazelcast.spi.partition.IPartitionService;
import com.hazelcast.transaction.TransactionException;
import com.hazelcast.transaction.TransactionNotActiveException;
import com.hazelcast.transaction.TransactionOptions.TransactionType;
import com.hazelcast.transaction.impl.Transaction;
import com.hazelcast.transaction.impl.TransactionLog;
import com.hazelcast.transaction.impl.TransactionLogRecord;
import com.hazelcast.transaction.impl.xa.operations.PutRemoteTransactionOperation;
import com.hazelcast.util.Clock;
import com.hazelcast.util.ExceptionUtil;
import com.hazelcast.util.FutureUtil;
import com.hazelcast.util.UuidUtil;
import javax.transaction.xa.XAException;
import javax.transaction.xa.Xid;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.logging.Level;
import static com.hazelcast.transaction.impl.Transaction.State.ACTIVE;
import static com.hazelcast.transaction.impl.Transaction.State.COMMITTED;
import static com.hazelcast.transaction.impl.Transaction.State.COMMITTING;
import static com.hazelcast.transaction.impl.Transaction.State.COMMIT_FAILED;
import static com.hazelcast.transaction.impl.Transaction.State.NO_TXN;
import static com.hazelcast.transaction.impl.Transaction.State.PREPARED;
import static com.hazelcast.transaction.impl.Transaction.State.PREPARING;
import static com.hazelcast.transaction.impl.Transaction.State.ROLLED_BACK;
import static com.hazelcast.transaction.impl.Transaction.State.ROLLING_BACK;
import static com.hazelcast.transaction.impl.xa.XAService.SERVICE_NAME;
import static com.hazelcast.util.FutureUtil.RETHROW_TRANSACTION_EXCEPTION;
import static com.hazelcast.util.FutureUtil.logAllExceptions;
import static com.hazelcast.util.FutureUtil.waitWithDeadline;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
/**
* XA {@link Transaction} implementation.
*
* This class does not need to be thread-safe, it is only used via XAResource
* All visibility guarantees handled by XAResource
*/
public final class XATransaction implements Transaction {
private static final int ROLLBACK_TIMEOUT_MINUTES = 5;
private static final int COMMIT_TIMEOUT_MINUTES = 5;
private final FutureUtil.ExceptionHandler commitExceptionHandler;
private final FutureUtil.ExceptionHandler rollbackExceptionHandler;
private final NodeEngine nodeEngine;
private final long timeoutMillis;
private final String txnId;
private final SerializableXID xid;
private final String txOwnerUuid;
private final TransactionLog transactionLog;
private State state = NO_TXN;
private long startTime;
private boolean originatedFromClient;
public XATransaction(NodeEngine nodeEngine, Xid xid, String txOwnerUuid, int timeout, boolean originatedFromClient) {
this.nodeEngine = nodeEngine;
this.transactionLog = new TransactionLog();
this.timeoutMillis = SECONDS.toMillis(timeout);
this.txnId = UuidUtil.newUnsecureUuidString();
this.xid = new SerializableXID(xid.getFormatId(), xid.getGlobalTransactionId(), xid.getBranchQualifier());
this.txOwnerUuid = txOwnerUuid == null ? nodeEngine.getLocalMember().getUuid() : txOwnerUuid;
ILogger logger = nodeEngine.getLogger(getClass());
this.commitExceptionHandler = logAllExceptions(logger, "Error during commit!", Level.WARNING);
this.rollbackExceptionHandler = logAllExceptions(logger, "Error during rollback!", Level.WARNING);
this.originatedFromClient = originatedFromClient;
}
public XATransaction(NodeEngine nodeEngine, Collection logs,
String txnId, SerializableXID xid, String txOwnerUuid, long timeoutMillis, long startTime) {
this.nodeEngine = nodeEngine;
this.transactionLog = new TransactionLog(logs);
this.timeoutMillis = timeoutMillis;
this.txnId = txnId;
this.xid = xid;
this.txOwnerUuid = txOwnerUuid;
ILogger logger = nodeEngine.getLogger(getClass());
this.commitExceptionHandler = logAllExceptions(logger, "Error during commit!", Level.WARNING);
this.rollbackExceptionHandler = logAllExceptions(logger, "Error during rollback!", Level.WARNING);
this.startTime = startTime;
state = PREPARED;
}
@Override
public void begin() throws IllegalStateException {
if (state == ACTIVE) {
throw new IllegalStateException("Transaction is already active");
}
startTime = Clock.currentTimeMillis();
state = ACTIVE;
}
@Override
public void prepare() throws TransactionException {
if (state != ACTIVE) {
throw new TransactionNotActiveException("Transaction is not active");
}
checkTimeout();
try {
state = PREPARING;
List futures = transactionLog.prepare(nodeEngine);
waitWithDeadline(futures, timeoutMillis, MILLISECONDS, RETHROW_TRANSACTION_EXCEPTION);
futures.clear();
putTransactionInfoRemote();
state = PREPARED;
} catch (Throwable e) {
throw ExceptionUtil.rethrow(e, TransactionException.class);
}
}
private void putTransactionInfoRemote() throws ExecutionException, InterruptedException {
PutRemoteTransactionOperation operation = new PutRemoteTransactionOperation(
transactionLog.getRecords(), txnId, xid, txOwnerUuid, timeoutMillis, startTime);
OperationService operationService = nodeEngine.getOperationService();
IPartitionService partitionService = nodeEngine.getPartitionService();
int partitionId = partitionService.getPartitionId(xid);
InternalCompletableFuture