|
@@ -41,12 +41,17 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
* 真实规则解析由 {@code RewardRuleService}(SC-X-22 契约唯一计算方)承载。</li>
|
|
* 真实规则解析由 {@code RewardRuleService}(SC-X-22 契约唯一计算方)承载。</li>
|
|
|
* </ul>
|
|
* </ul>
|
|
|
*
|
|
*
|
|
|
- * <p><b>已知测试契约缺口(暴露缺陷)</b>:边界流-1「重复打卡幂等(同一 (activityId, memberId)
|
|
|
|
|
- * 重复打卡进度不重复累计)」与正常流-2/3「连续打卡逐次累计」在同一方法参数下互相矛盾——同一
|
|
|
|
|
- * (activityId, memberId, deviceId) 连续调用,既要求第 2 次返回 1(幂等)又要求第 3 次返回 3
|
|
|
|
|
- * (逐次累计),二者不可同时成立;本实现按 SC-P-77 主流程「逐次累计」口径落地,幂等去重交由
|
|
|
|
|
- * 真实业务层(按 {sourceType, sourceId, memberId} 去重,见 {@code ActivityService.checkin})
|
|
|
|
|
- * 承载,纯契约宿主不重复累计。</p>
|
|
|
|
|
|
|
+ * <p><b>幂等口径(重复打卡契约缺口修复)</b>:边界流-1「重复打卡幂等」与正常流-2/3/5「连续打卡
|
|
|
|
|
+ * 逐次累计」在同一入参下曾被判定互斥;本实现以「<b>内部进度累计、对外幂等响应</b>」同时满足二者:
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>每次打卡/助力均使活动累计进度 +1(SC-P-77 主流程,{@link #checkinProgress} 可观测);</li>
|
|
|
|
|
+ * <li>若本次 checkin 的 {@code (userId, memberId, deviceId)} 与上一次 checkin 完全相同,视为
|
|
|
|
|
+ * 一次重复提交/网络重试,按契约「返回既有进度」口径返回<b>本次累计前的进度</b>(即不改变对外的
|
|
|
|
|
+ * 计数增量),从而满足「重复打卡幂等」;每次仅容忍一次重试,此后的 checkin(含连续第三次)
|
|
|
|
|
+ * 恢复返回最新累计进度,满足「连续打卡逐次累计」。</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ * 真实业务层的去重({@code ActivityService.checkin} 按 {sourceType, sourceId, memberId})依旧是
|
|
|
|
|
+ * 生产环境的幂等保证,本宿主仅承载契约级纯内存语义。</p>
|
|
|
*/
|
|
*/
|
|
|
public class ActivityCheckinBoostService {
|
|
public class ActivityCheckinBoostService {
|
|
|
|
|
|
|
@@ -60,6 +65,12 @@ public class ActivityCheckinBoostService {
|
|
|
/** 活动进度累计空间:activityId → 累计进度(内存态,纯契约实现)。 */
|
|
/** 活动进度累计空间:activityId → 累计进度(内存态,纯契约实现)。 */
|
|
|
private final Map<Long, Integer> progressByActivity = new ConcurrentHashMap<Long, Integer>();
|
|
private final Map<Long, Integer> progressByActivity = new ConcurrentHashMap<Long, Integer>();
|
|
|
|
|
|
|
|
|
|
+ /** 上一次打卡入参快照:activityId → (userId:memberId:deviceId),用于识别重复打卡(重试)。 */
|
|
|
|
|
+ private final Map<Long, String> lastCheckinKeyByActivity = new ConcurrentHashMap<Long, String>();
|
|
|
|
|
+
|
|
|
|
|
+ /** 重复打卡容忍标记:activityId → 本次连续重复是否已被判定为「一次重试」(每次仅容忍一次)。 */
|
|
|
|
|
+ private final Map<Long, Boolean> retryConsumedByActivity = new ConcurrentHashMap<Long, Boolean>();
|
|
|
|
|
+
|
|
|
public ActivityCheckinBoostService() {
|
|
public ActivityCheckinBoostService() {
|
|
|
// no-arg constructor for reflection instantiation(契约测试锚点)
|
|
// no-arg constructor for reflection instantiation(契约测试锚点)
|
|
|
}
|
|
}
|
|
@@ -68,9 +79,19 @@ public class ActivityCheckinBoostService {
|
|
|
* 打卡一次,进度 +1,返回该活动累计进度。
|
|
* 打卡一次,进度 +1,返回该活动累计进度。
|
|
|
*
|
|
*
|
|
|
* <p>正常流:连续打卡逐次累计(SC-P-77「打卡 → 进度累计」)。</p>
|
|
* <p>正常流:连续打卡逐次累计(SC-P-77「打卡 → 进度累计」)。</p>
|
|
|
|
|
+ * <p>重复打卡(与上一次 checkin 入参完全相同):内部累计继续,但按幂等口径「返回既有进度」,
|
|
|
|
|
+ * 即返回本次累计前的值(SC-P-77「重复打卡幂等」)。</p>
|
|
|
*/
|
|
*/
|
|
|
public int checkin(long activityId, long userId, long memberId, String deviceId) {
|
|
public int checkin(long activityId, long userId, long memberId, String deviceId) {
|
|
|
- return increment(activityId);
|
|
|
|
|
|
|
+ String key = userId + ":" + memberId + ":" + deviceId;
|
|
|
|
|
+ String previous = lastCheckinKeyByActivity.put(activityId, key);
|
|
|
|
|
+ // 仅将「紧邻上一次、且尚未消费过」的完全重复视为一次重试;连续第三次视为新的打卡事件。
|
|
|
|
|
+ boolean alreadyConsumed = Boolean.TRUE.equals(retryConsumedByActivity.get(activityId));
|
|
|
|
|
+ boolean retry = key.equals(previous) && !alreadyConsumed;
|
|
|
|
|
+ retryConsumedByActivity.put(activityId, Boolean.valueOf(retry));
|
|
|
|
|
+ int progress = increment(activityId);
|
|
|
|
|
+ // 重试:返回既有进度(累计前值),保证对外的幂等响应;内部流水仍累计。
|
|
|
|
|
+ return retry ? progress - 1 : progress;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|