use of org.apache.ignite.internal.tx.Timestamp in project ignite-3 by apache.
the class ItInternalTableScanTest method setUp.
/**
* Prepare test environment.
* <ol>
* <li>Start network node.</li>
* <li>Start raft server.</li>
* <li>Prepare partitioned raft group.</li>
* <li>Prepare partitioned raft group service.</li>
* <li>Prepare internal table as a test object.</li>
* </ol>
*
* @throws Exception If any.
*/
@BeforeEach
public void setUp(TestInfo testInfo) throws Exception {
NetworkAddress nodeNetworkAddress = new NetworkAddress("localhost", 20_000);
network = ClusterServiceTestUtils.clusterService(testInfo, 20_000, new StaticNodeFinder(List.of(nodeNetworkAddress)), NETWORK_FACTORY);
network.start();
raftSrv = new RaftServerImpl(network, FACTORY);
raftSrv.start();
String grpName = "test_part_grp";
List<Peer> conf = List.of(new Peer(nodeNetworkAddress));
mockStorage = mock(PartitionStorage.class);
txManager = new TxManagerImpl(network, new HeapLockManager());
txManager.start();
UUID tblId = UUID.randomUUID();
raftSrv.startRaftGroup(grpName, new PartitionListener(tblId, new VersionedRowStore(mockStorage, txManager) {
@Override
protected Pair<BinaryRow, BinaryRow> versionedRow(@Nullable DataRow row, Timestamp timestamp) {
// Return as is.
return new Pair<>(new ByteBufferRow(row.valueBytes()), null);
}
}), conf);
executor = new ScheduledThreadPoolExecutor(20, new NamedThreadFactory(Loza.CLIENT_POOL_NAME));
RaftGroupService raftGrpSvc = RaftGroupServiceImpl.start(RAFT_GRP_ID, network, FACTORY, 10_000, conf, true, 200, executor).get(3, TimeUnit.SECONDS);
internalTbl = new InternalTableImpl(TEST_TABLE_NAME, tblId, Int2ObjectMaps.singleton(0, raftGrpSvc), 1, NetworkAddress::toString, txManager, mock(TableStorage.class));
}
use of org.apache.ignite.internal.tx.Timestamp in project ignite-3 by apache.
the class VersionedRowStore method unpack.
/**
* Unpacks a raw value into (cur, old, ts) triplet. TODO asch IGNITE-15934 not very efficient.
*
* @param row The row.
* @return The value.
*/
private static Value unpack(@Nullable DataRow row) {
if (row == null) {
return new Value(null, null, null);
}
ByteBuffer buf = row.value();
BinaryRow newVal = null;
BinaryRow oldVal = null;
int l1 = buf.asIntBuffer().get();
int pos = 4;
buf.position(pos);
if (l1 != 0) {
// TODO asch IGNITE-15934 get rid of copying
byte[] tmp = new byte[l1];
buf.get(tmp);
newVal = new ByteBufferRow(tmp);
pos += l1;
}
buf.position(pos);
int l2 = buf.asIntBuffer().get();
pos += 4;
buf.position(pos);
if (l2 != 0) {
// TODO asch get rid of copying
byte[] tmp = new byte[l2];
buf.get(tmp);
oldVal = new ByteBufferRow(tmp);
pos += l2;
}
buf.position(pos);
long ts = buf.getLong();
long nodeId = buf.getLong();
return new Value(newVal, oldVal, new Timestamp(ts, nodeId));
}
use of org.apache.ignite.internal.tx.Timestamp in project ignite-3 by apache.
the class TransactionImpl method finish.
/**
* Finishes a transaction.
*
* @param commit {@code true} to commit, false to rollback.
* @return The future.
*/
private CompletableFuture<Void> finish(boolean commit) {
Map<NetworkAddress, Set<String>> tmp = new HashMap<>();
// Group by common leader addresses.
for (RaftGroupService svc : enlisted) {
NetworkAddress addr = svc.leader().address();
tmp.computeIfAbsent(addr, k -> new HashSet<>()).add(svc.groupId());
}
CompletableFuture[] futs = new CompletableFuture[tmp.size() + 1];
int i = 0;
for (Map.Entry<NetworkAddress, Set<String>> entry : tmp.entrySet()) {
boolean local = address.equals(entry.getKey());
futs[i++] = txManager.finishRemote(entry.getKey(), timestamp, commit, entry.getValue());
LOG.debug("finish [addr={}, commit={}, ts={}, local={}, groupIds={}", address, commit, timestamp, local, entry.getValue());
}
// Handle coordinator's tx.
futs[i] = tmp.containsKey(address) ? CompletableFuture.completedFuture(null) : commit ? txManager.commitAsync(timestamp) : txManager.rollbackAsync(timestamp);
return CompletableFuture.allOf(futs);
}
use of org.apache.ignite.internal.tx.Timestamp in project ignite-3 by apache.
the class TxManagerImpl method finishRemote.
/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<Void> finishRemote(NetworkAddress addr, Timestamp ts, boolean commit, Set<String> groups) {
assert groups != null && !groups.isEmpty();
TxFinishRequest req = FACTORY.txFinishRequest().timestamp(ts).groups(groups).commit(commit).build();
CompletableFuture<NetworkMessage> fut = clusterService.messagingService().invoke(addr, req, TIMEOUT);
// Submit response to a dedicated pool to avoid deadlocks. TODO: IGNITE-15389
return fut.thenApplyAsync(resp -> ((TxFinishResponse) resp).errorMessage()).thenCompose(msg -> msg == null ? completedFuture(null) : failedFuture(new TransactionException(msg)));
}
use of org.apache.ignite.internal.tx.Timestamp in project ignite-3 by apache.
the class PartitionListener method tryEnlistIntoTransaction.
/**
* Attempts to enlist a command into a transaction.
*
* @param command The command.
* @param clo The closure.
* @return {@code true} if a command is compatible with a transaction state or a command is not transactional.
*/
private boolean tryEnlistIntoTransaction(Command command, CommandClosure<?> clo) {
if (command instanceof TransactionalCommand) {
Timestamp ts = ((TransactionalCommand) command).getTimestamp();
TxState state = txManager.getOrCreateTransaction(ts);
if (state != null && state != TxState.PENDING) {
clo.result(new TransactionException(format("Failed to enlist a key into a transaction, state={}", state)));
return false;
}
}
return true;
}
Aggregations