Search in sources :

Example 1 with DalTransaction

use of com.ctrip.xpipe.redis.console.annotation.DalTransaction in project x-pipe by ctripcorp.

the class DcClusterShardDao method deleteDcClusterShardsBatch.

@DalTransaction
public void deleteDcClusterShardsBatch(List<DcClusterShardTbl> dcClusterShards) throws DalException {
    if (null == dcClusterShards || dcClusterShards.isEmpty()) {
        logger.warn("[deleteDcClusterShardsBatch] Empty list: {}", dcClusterShards);
        return;
    }
    List<RedisTbl> redises = new LinkedList<RedisTbl>();
    for (final DcClusterShardTbl dcClusterShard : dcClusterShards) {
        List<RedisTbl> relatedRedises = redisDao.findAllByDcClusterShard(dcClusterShard.getDcClusterShardId());
        if (null != relatedRedises && !relatedRedises.isEmpty()) {
            for (RedisTbl redis : relatedRedises) {
                redis.setRunId(generateDeletedName(redis.getRunId()));
            }
            redises.addAll(relatedRedises);
        }
    }
    redisDao.deleteRedisesBatch(redises);
    queryHandler.handleBatchDelete(new DalQuery<int[]>() {

        @Override
        public int[] doQuery() throws DalException {
            return dcClusterShardTblDao.deleteDcClusterShardsBatch(dcClusterShards.toArray(new DcClusterShardTbl[dcClusterShards.size()]), DcClusterShardTblEntity.UPDATESET_FULL);
        }
    }, true);
}
Also used : RedisTbl(com.ctrip.xpipe.redis.console.model.RedisTbl) DalException(org.unidal.dal.jdbc.DalException) DcClusterShardTbl(com.ctrip.xpipe.redis.console.model.DcClusterShardTbl) LinkedList(java.util.LinkedList) DalTransaction(com.ctrip.xpipe.redis.console.annotation.DalTransaction)

Example 2 with DalTransaction

use of com.ctrip.xpipe.redis.console.annotation.DalTransaction in project x-pipe by ctripcorp.

the class DefaultMigrationCluster method updateStat.

@Override
@DalTransaction
public void updateStat(MigrationState stat) {
    logger.info("[updateStat]{}-{}, {} -> {}", migrationCluster.getMigrationEventId(), clusterName(), this.currentState.getStatus(), stat.getStatus());
    this.currentState = stat;
    try {
        tryUpdateStartTime(stat.getStatus());
        updateStorageClusterStatus();
        updateStorageMigrationClusterStatus();
    } catch (Exception e) {
        logger.error("[updateStat] ", e);
        throw new ServerException(e.getMessage());
    }
}
Also used : ServerException(com.ctrip.xpipe.redis.console.exception.ServerException) ServerException(com.ctrip.xpipe.redis.console.exception.ServerException) TimeoutException(java.util.concurrent.TimeoutException) DalTransaction(com.ctrip.xpipe.redis.console.annotation.DalTransaction)

Example 3 with DalTransaction

use of com.ctrip.xpipe.redis.console.annotation.DalTransaction in project x-pipe by ctripcorp.

the class ClusterDao method createCluster.

@DalTransaction
public ClusterTbl createCluster(final ClusterTbl cluster) throws DalException {
    // check for unique cluster name
    ClusterTbl clusterWithSameName = queryHandler.handleQuery(new DalQuery<ClusterTbl>() {

        @Override
        public ClusterTbl doQuery() throws DalException {
            return clusterTblDao.findClusterByClusterName(cluster.getClusterName(), ClusterTblEntity.READSET_FULL);
        }
    });
    if (null != clusterWithSameName)
        throw new BadRequestException("Duplicated cluster name");
    cluster.setCreateTime(new Date());
    // cluster meta
    queryHandler.handleInsert(new DalQuery<Integer>() {

        @Override
        public Integer doQuery() throws DalException {
            return clusterTblDao.insert(cluster);
        }
    });
    // related dc-cluster
    ClusterTbl newCluster = clusterTblDao.findClusterByClusterName(cluster.getClusterName(), ClusterTblEntity.READSET_FULL);
    DcTbl activeDc = dcTblDao.findByPK(cluster.getActivedcId(), DcTblEntity.READSET_FULL);
    DcClusterTbl protoDcCluster = dcClusterTblDao.createLocal();
    protoDcCluster.setDcId(activeDc.getId()).setClusterId(newCluster.getId());
    queryHandler.handleInsert(new DalQuery<Integer>() {

        @Override
        public Integer doQuery() throws DalException {
            return dcClusterTblDao.insert(protoDcCluster);
        }
    });
    return newCluster;
}
Also used : DalException(org.unidal.dal.jdbc.DalException) BadRequestException(com.ctrip.xpipe.redis.console.exception.BadRequestException) Date(java.util.Date) DalTransaction(com.ctrip.xpipe.redis.console.annotation.DalTransaction)

Example 4 with DalTransaction

use of com.ctrip.xpipe.redis.console.annotation.DalTransaction in project x-pipe by ctripcorp.

the class ClusterServiceImpl method balanceCluster.

// Add transaction for one cluster update, rollback if one 'DcClusterShard' update fails
@VisibleForTesting
@DalTransaction
protected void balanceCluster(Map<String, List<SetinelTbl>> dcToSentinels, final String cluster) {
    for (String dcName : dcToSentinels.keySet()) {
        List<DcClusterShardTbl> dcClusterShards = dcClusterShardService.findAllByDcCluster(dcName, cluster);
        List<SetinelTbl> sentinels = dcToSentinels.get(dcName);
        if (dcClusterShards == null || sentinels == null) {
            throw new XpipeRuntimeException("DcClusterShard | Sentinels should not be null");
        }
        long randomlySelectedSentinelId = randomlyChoseSentinels(sentinels);
        dcClusterShards.forEach(dcClusterShard -> {
            dcClusterShard.setSetinelId(randomlySelectedSentinelId);
            try {
                dcClusterShardService.updateDcClusterShard(dcClusterShard);
            } catch (DalException e) {
                throw new XpipeRuntimeException(e.getMessage());
            }
        });
    }
}
Also used : DalException(org.unidal.dal.jdbc.DalException) XpipeRuntimeException(com.ctrip.xpipe.exception.XpipeRuntimeException) VisibleForTesting(com.ctrip.xpipe.utils.VisibleForTesting) DalTransaction(com.ctrip.xpipe.redis.console.annotation.DalTransaction)

Example 5 with DalTransaction

use of com.ctrip.xpipe.redis.console.annotation.DalTransaction in project x-pipe by ctripcorp.

the class DcClusterDao method deleteDcClustersBatch.

@DalTransaction
public void deleteDcClustersBatch(final DcClusterTbl dcCluster) throws DalException {
    if (null == dcCluster)
        throw new DalException("Null cannot be deleted.");
    List<DcClusterShardTbl> dcClusterShards = queryHandler.handleQuery(new DalQuery<List<DcClusterShardTbl>>() {

        @Override
        public List<DcClusterShardTbl> doQuery() throws DalException {
            return dcClusterShardTblDao.findAllByDcClusterId(dcCluster.getDcClusterId(), DcClusterShardTblEntity.READSET_FULL);
        }
    });
    if (null != dcClusterShards) {
        dcClusterShardDao.deleteDcClusterShardsBatch(dcClusterShards);
    }
    queryHandler.handleDelete(new DalQuery<Integer>() {

        @Override
        public Integer doQuery() throws DalException {
            return dcClusterTblDao.deleteBatch(dcCluster, DcClusterTblEntity.UPDATESET_FULL);
        }
    }, true);
}
Also used : DalException(org.unidal.dal.jdbc.DalException) List(java.util.List) LinkedList(java.util.LinkedList) DalTransaction(com.ctrip.xpipe.redis.console.annotation.DalTransaction)

Aggregations

DalTransaction (com.ctrip.xpipe.redis.console.annotation.DalTransaction)6 DalException (org.unidal.dal.jdbc.DalException)5 LinkedList (java.util.LinkedList)3 List (java.util.List)2 XpipeRuntimeException (com.ctrip.xpipe.exception.XpipeRuntimeException)1 BadRequestException (com.ctrip.xpipe.redis.console.exception.BadRequestException)1 ServerException (com.ctrip.xpipe.redis.console.exception.ServerException)1 DcClusterShardTbl (com.ctrip.xpipe.redis.console.model.DcClusterShardTbl)1 RedisTbl (com.ctrip.xpipe.redis.console.model.RedisTbl)1 VisibleForTesting (com.ctrip.xpipe.utils.VisibleForTesting)1 Date (java.util.Date)1 TimeoutException (java.util.concurrent.TimeoutException)1