Search in sources :

Example 11 with DistributedLock

use of com.actionworks.flashsale.lock.DistributedLock in project flash-sale by ThoughtsBeta.

the class DefaultActivityAppService method offlineFlashActivity.

@Override
public AppResult offlineFlashActivity(Long userId, Long activityId) {
    logger.info("activityOffline|下线活动|{},{}", userId, activityId);
    if (userId == null || activityId == null) {
        throw new BizException(INVALID_PARAMS);
    }
    AuthResult authResult = authorizationService.auth(userId, FLASH_ACTIVITY_MODIFICATION);
    if (!authResult.isSuccess()) {
        throw new AuthException(UNAUTHORIZED_ACCESS);
    }
    DistributedLock activityModificationLock = lockFactoryService.getDistributedLock(getActivityModificationLockKey(activityId));
    try {
        boolean isLockSuccess = activityModificationLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
        if (!isLockSuccess) {
            throw new BizException(FREQUENTLY_ERROR);
        }
        flashActivityDomainService.offlineActivity(userId, activityId);
        logger.info("activityOffline|活动已下线");
        return AppResult.buildSuccess();
    } catch (Exception e) {
        logger.error("activityModification|活动修改失败|{},{}", userId, activityId, e);
        throw new BizException("活动修改失败");
    } finally {
        activityModificationLock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) BizException(com.actionworks.flashsale.app.exception.BizException) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 12 with DistributedLock

use of com.actionworks.flashsale.lock.DistributedLock in project flash-sale by ThoughtsBeta.

the class DefaultActivityAppService method onlineFlashActivity.

@Override
public AppResult onlineFlashActivity(Long userId, Long activityId) {
    logger.info("activityOnline|上线活动|{},{}", userId, activityId);
    if (userId == null || activityId == null) {
        throw new BizException(INVALID_PARAMS);
    }
    AuthResult authResult = authorizationService.auth(userId, FLASH_ACTIVITY_CREATE);
    if (!authResult.isSuccess()) {
        throw new AuthException(UNAUTHORIZED_ACCESS);
    }
    DistributedLock activityModificationLock = lockFactoryService.getDistributedLock(getActivityModificationLockKey(activityId));
    try {
        boolean isLockSuccess = activityModificationLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
        if (!isLockSuccess) {
            throw new BizException(FREQUENTLY_ERROR);
        }
        flashActivityDomainService.onlineActivity(userId, activityId);
        logger.info("activityOnline|活动已上线");
        return AppResult.buildSuccess();
    } catch (Exception e) {
        logger.error("activityModification|活动修改失败|{},{}", userId, activityId, e);
        throw new BizException("活动修改失败");
    } finally {
        activityModificationLock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) BizException(com.actionworks.flashsale.app.exception.BizException) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 13 with DistributedLock

use of com.actionworks.flashsale.lock.DistributedLock in project flash-sale by ThoughtsBeta.

the class DefaultBucketsArrangementService method arrangeStockBuckets.

@Transactional
@Override
public void arrangeStockBuckets(Long itemId, Integer stocksAmount, Integer bucketsQuantity, Integer assignmentMode) {
    logger.info("arrangeBuckets|准备库存分桶|{},{},{}", itemId, stocksAmount, bucketsQuantity);
    if (itemId == null || stocksAmount == null || stocksAmount < 0 || bucketsQuantity == null || bucketsQuantity <= 0) {
        throw new StockBucketException("参数错误");
    }
    DistributedLock lock = lockFactoryService.getDistributedLock(ITEM_STOCK_BUCKETS_SUSPEND_KEY + itemId);
    try {
        boolean isLockSuccess = lock.tryLock(5, 5, TimeUnit.SECONDS);
        if (!isLockSuccess) {
            logger.info("arrangeBuckets|库存分桶时获取锁失败|{}", itemId);
            return;
        }
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        try {
            boolean success = bucketsDomainService.suspendBuckets(itemId);
            if (!success) {
                logger.info("arrangeBuckets|关闭库存分桶失败|{}", itemId);
                throw new StockBucketException("关闭库存分桶失败");
            }
            dataSourceTransactionManager.commit(transactionStatus);
        } catch (Exception e) {
            logger.info("arrangeBuckets|关闭分桶失败回滚中|{}", itemId, e);
            dataSourceTransactionManager.rollback(transactionStatus);
        }
        List<Bucket> buckets = bucketsDomainService.getBucketsByItem(itemId);
        if (buckets.size() == 0) {
            initStockBuckets(itemId, stocksAmount, bucketsQuantity);
            return;
        }
        if (ArrangementMode.isTotalAmountMode(assignmentMode)) {
            arrangeStockBucketsBasedTotalMode(itemId, stocksAmount, bucketsQuantity, buckets);
        }
        if (ArrangementMode.isIncrementalAmountMode(assignmentMode)) {
            rearrangeStockBucketsBasedIncrementalMode(itemId, stocksAmount, bucketsQuantity, buckets);
        }
    } catch (Exception e) {
        logger.error("arrangeBuckets|库存分桶错误|", e);
        throw new StockBucketException("库存分桶错误");
    } finally {
        lock.unlock();
        boolean success = bucketsDomainService.resumeBuckets(itemId);
        if (!success) {
            logger.error("arrangeBuckets|打开库存分桶失败|");
        }
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) Bucket(com.actionworks.flashsale.domain.model.Bucket) TransactionStatus(org.springframework.transaction.TransactionStatus) StockBucketException(com.actionworks.flashsale.app.exception.StockBucketException) StockBucketException(com.actionworks.flashsale.app.exception.StockBucketException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with DistributedLock

use of com.actionworks.flashsale.lock.DistributedLock in project flash-sale by ThoughtsBeta.

the class DefaultFlashItemAppService method publishFlashItem.

@Override
public AppResult publishFlashItem(Long userId, Long activityId, FlashItemPublishCommand itemPublishCommand) {
    logger.info("itemPublish|发布秒杀品|{},{},{}", userId, activityId, JSON.toJSON(itemPublishCommand));
    if (userId == null || activityId == null || itemPublishCommand == null || !itemPublishCommand.validate()) {
        throw new BizException(INVALID_PARAMS);
    }
    AuthResult authResult = authorizationService.auth(userId, FLASH_ITEM_CREATE);
    if (!authResult.isSuccess()) {
        throw new AuthException(UNAUTHORIZED_ACCESS);
    }
    DistributedLock itemCreateLock = lockFactoryService.getDistributedLock(getItemCreateLockKey(userId));
    try {
        boolean isLockSuccess = itemCreateLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
        if (!isLockSuccess) {
            throw new BizException(FREQUENTLY_ERROR);
        }
        FlashActivity flashActivity = flashActivityDomainService.getFlashActivity(activityId);
        if (flashActivity == null) {
            throw new BizException(ACTIVITY_NOT_FOUND);
        }
        FlashItem flashItem = toDomain(itemPublishCommand);
        flashItem.setActivityId(activityId);
        flashItem.setStockWarmUp(0);
        flashItemDomainService.publishFlashItem(flashItem);
        logger.info("itemPublish|秒杀品已发布");
        return AppResult.buildSuccess();
    } catch (Exception e) {
        logger.error("itemPublish|秒杀品发布失败|{}", userId, e);
        throw new BizException("秒杀品发布失败");
    } finally {
        itemCreateLock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) BizException(com.actionworks.flashsale.app.exception.BizException) FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 15 with DistributedLock

use of com.actionworks.flashsale.lock.DistributedLock in project flash-sale by ThoughtsBeta.

the class FlashActivitiesCacheService method tryToUpdateActivitiesCacheByLock.

public FlashActivitiesCache tryToUpdateActivitiesCacheByLock(Integer pageNumber) {
    logger.info("activitiesCache|更新远程缓存|{}", pageNumber);
    DistributedLock lock = distributedLockFactoryService.getDistributedLock(UPDATE_ACTIVITIES_CACHE_LOCK_KEY);
    try {
        boolean isLockSuccess = lock.tryLock(1, 5, TimeUnit.SECONDS);
        if (!isLockSuccess) {
            return new FlashActivitiesCache().tryLater();
        }
        PagesQueryCondition pagesQueryCondition = new PagesQueryCondition();
        PageResult<FlashActivity> flashActivityPageResult = flashActivityDomainService.getFlashActivities(pagesQueryCondition);
        FlashActivitiesCache flashActivitiesCache;
        if (flashActivityPageResult == null) {
            flashActivitiesCache = new FlashActivitiesCache().notExist();
        } else {
            flashActivitiesCache = new FlashActivitiesCache().setTotal(flashActivityPageResult.getTotal()).setFlashActivities(flashActivityPageResult.getData()).setVersion(System.currentTimeMillis());
        }
        distributedCacheService.put(buildActivityCacheKey(pageNumber), JSON.toJSONString(flashActivitiesCache), FIVE_MINUTES);
        logger.info("activitiesCache|远程缓存已更新|{}", pageNumber);
        return flashActivitiesCache;
    } catch (InterruptedException e) {
        logger.error("activitiesCache|远程缓存更新失败", e);
        return new FlashActivitiesCache().tryLater();
    } finally {
        lock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) FlashActivitiesCache(com.actionworks.flashsale.app.service.activity.cache.model.FlashActivitiesCache) PagesQueryCondition(com.actionworks.flashsale.domain.model.PagesQueryCondition)

Aggregations

DistributedLock (com.actionworks.flashsale.lock.DistributedLock)17 BizException (com.actionworks.flashsale.app.exception.BizException)9 AuthResult (com.actionworks.flashsale.app.auth.model.AuthResult)7 AuthException (com.actionworks.flashsale.controller.exception.AuthException)7 FlashActivity (com.actionworks.flashsale.domain.model.entity.FlashActivity)4 FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)4 Bucket (com.actionworks.flashsale.domain.model.Bucket)2 PagesQueryCondition (com.actionworks.flashsale.domain.model.PagesQueryCondition)2 Transactional (org.springframework.transaction.annotation.Transactional)2 AppException (com.actionworks.flashsale.app.exception.AppException)1 StockBucketException (com.actionworks.flashsale.app.exception.StockBucketException)1 PlaceOrderResult (com.actionworks.flashsale.app.model.result.PlaceOrderResult)1 FlashActivitiesCache (com.actionworks.flashsale.app.service.activity.cache.model.FlashActivitiesCache)1 FlashActivityCache (com.actionworks.flashsale.app.service.activity.cache.model.FlashActivityCache)1 FlashItemCache (com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache)1 FlashItemsCache (com.actionworks.flashsale.app.service.item.cache.model.FlashItemsCache)1 ItemStockCache (com.actionworks.flashsale.app.service.stock.model.ItemStockCache)1 TimeUnit (java.util.concurrent.TimeUnit)1 RLock (org.redisson.api.RLock)1 DefaultRedisScript (org.springframework.data.redis.core.script.DefaultRedisScript)1