private void writeFile(final TransactionRecover transaction) { makeDir(); String file = getFullFileName(transaction.getId()); RandomAccessFile raf; try { raf = new RandomAccessFile(file, "rw"); try (FileChannel channel = raf.getChannel()) { byte[] content = TransactionRecoverUtils.convert(transaction, serializer); ByteBuffer buffer = ByteBuffer.allocate(content.length); buffer.put(content); buffer.flip(); while (buffer.hasRemaining()) { channel.write(buffer); } channel.force(true); } } catch (Exception e) { e.printStackTrace(); } }
private TransactionRecover readTransaction(final File file) throws Exception { try (FileInputStream fis = new FileInputStream(file)) { byte[] content = new byte[(int) file.length()]; fis.read(content); return TransactionRecoverUtils.transformBean(content, serializer); } }
@Override public int create(final TransactionRecover transactionRecover) { try { zooKeeper.create(getRootPath(transactionRecover.getId()), TransactionRecoverUtils.convert(transactionRecover, objectSerializer), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return ROWS; } catch (Exception e) { throw new TransactionIoException(e); } }
@Override public TransactionRecover findById(final String id) { try { Stat stat = new Stat(); byte[] contents = zooKeeper.getData(getRootPath(id), false, stat); return TransactionRecoverUtils.transformBean(contents, objectSerializer); } catch (Exception e) { throw new TransactionIoException(e); } }
@Override public int create(final TransactionRecover transactionRecover) { try { final String redisKey = RedisHelper.buildRecoverKey(keyName, transactionRecover.getId()); jedisClient.set(redisKey, TransactionRecoverUtils.convert(transactionRecover, objectSerializer)); return ROWS; } catch (Exception e) { throw new TransactionIoException(e); } }
@Override public List<TransactionRecover> listAll() { List<TransactionRecover> transactionRecovers = Lists.newArrayList(); List<String> zNodePaths; try { zNodePaths = zooKeeper.getChildren(rootPath, false); } catch (Exception e) { throw new TransactionIoException(e); } if (CollectionUtils.isNotEmpty(zNodePaths)) { transactionRecovers = zNodePaths.stream() .filter(StringUtils::isNoneBlank) .map(zNodePath -> { try { byte[] contents = zooKeeper.getData(getRootPath(zNodePath), false, new Stat()); return TransactionRecoverUtils.transformBean(contents, objectSerializer); } catch (KeeperException | InterruptedException | TransactionException e) { e.printStackTrace(); } return null; }).collect(Collectors.toList()); } return transactionRecovers; }
@Override public int update(final TransactionRecover transactionRecover) throws TransactionRuntimeException { try { final String redisKey = RedisHelper.buildRecoverKey(keyName, transactionRecover.getId()); if (CompensationOperationTypeEnum.TASK_EXECUTE.getCode() == transactionRecover.getOperation()) { TransactionRecover recover = findById(transactionRecover.getId()); recover.setCompleteFlag(CommonConstant.TX_TRANSACTION_COMPLETE_FLAG_OK); jedisClient.set(redisKey, TransactionRecoverUtils.convert(recover, objectSerializer)); return ROWS; } transactionRecover.setVersion(transactionRecover.getVersion() + 1); transactionRecover.setLastTime(new Date()); transactionRecover.setRetriedCount(transactionRecover.getRetriedCount() + 1); jedisClient.set(redisKey, TransactionRecoverUtils.convert(transactionRecover, objectSerializer)); return ROWS; } catch (Exception e) { throw new TransactionRuntimeException(e); } }
@Override public List<TransactionRecover> listAll() { try { List<TransactionRecover> transactions = Lists.newArrayList(); Set<byte[]> keys = jedisClient.keys((keyName + "*").getBytes()); for (final byte[] key : keys) { byte[] contents = jedisClient.get(key); if (contents != null) { transactions.add(TransactionRecoverUtils.transformBean(contents, objectSerializer)); } } return transactions; } catch (Exception e) { throw new TransactionIoException(e); } }
@Override public int update(final TransactionRecover transactionRecover) throws TransactionRuntimeException { try { if (CompensationOperationTypeEnum.TASK_EXECUTE.getCode() == transactionRecover.getOperation()) { TransactionRecover recover = findById(transactionRecover.getId()); recover.setCompleteFlag(CommonConstant.TX_TRANSACTION_COMPLETE_FLAG_OK); zooKeeper.setData(getRootPath(recover.getId()), TransactionRecoverUtils.convert(recover, objectSerializer), recover.getVersion() - 2); return ROWS; } transactionRecover.setLastTime(new Date()); transactionRecover.setVersion(transactionRecover.getVersion() + 1); transactionRecover.setRetriedCount(transactionRecover.getRetriedCount() + 1); zooKeeper.setData(getRootPath(transactionRecover.getId()), TransactionRecoverUtils.convert(transactionRecover, objectSerializer), transactionRecover.getVersion() - 2); return ROWS; } catch (Exception e) { throw new TransactionIoException(e); } }
@Override public TransactionRecover findById(final String id) { try { final String redisKey = RedisHelper.buildRecoverKey(keyName, id); byte[] contents = jedisClient.get(redisKey.getBytes()); return TransactionRecoverUtils.transformBean(contents, objectSerializer); } catch (Exception e) { throw new TransactionIoException(e); } }