feat(app): dual pointer for app channel

This commit is contained in:
Timi
2026-11-17 23:37:31 +08:00
parent 82110e36b7
commit 09a8547ded
5 changed files with 82 additions and 77 deletions
@@ -7,7 +7,7 @@ import lombok.EqualsAndHashCode;
/// ///
/// 应用发布通道 /// 应用发布通道
/// ///
/// 通道只持有一个指针,回滚就是改指向,发布记录本身不可变 /// 通道分别持有热更包和原生包指针
/// ///
/// @author Codex /// @author Codex
/// @since 2026-11-06 /// @since 2026-11-06
@@ -24,7 +24,13 @@ public class AppChannel extends UUIDEntity {
/// 通道名称 /// 通道名称
protected String name; protected String name;
/// 当前指向的发布 ID,为空表示该通道无发布 /// 当前热更包发布 ID
protected String bundleReleaseId;
/// 当前原生包发布 ID
protected String nativeReleaseId;
/// 过渡期旧指针
protected String releaseId; protected String releaseId;
/// 最后操作人用户 ID /// 最后操作人用户 ID
@@ -44,4 +44,11 @@ public interface AppChannelMapper extends BaseMapper<AppChannel, String> {
@Param("updatedBy") String updatedBy, @Param("updatedBy") String updatedBy,
@Param("updatedAt") Long updatedAt @Param("updatedAt") Long updatedAt
); );
/// 按类型切换指针并同步旧指针
@Update("UPDATE app_channel SET bundle_release_id = CASE WHEN #{nativeRelease} = false THEN #{releaseId} ELSE bundle_release_id END, native_release_id = CASE WHEN #{nativeRelease} = true THEN #{releaseId} ELSE native_release_id END, release_id = #{releaseId}, updated_by = #{updatedBy}, updated_at = #{updatedAt} WHERE id = #{id}")
int updatePointers(@Param("id") String id, @Param("releaseId") String releaseId, @Param("nativeRelease") boolean nativeRelease, @Param("updatedBy") String updatedBy, @Param("updatedAt") Long updatedAt);
/// 清除所有通道中指向撤回记录的指针
@Update("UPDATE app_channel SET bundle_release_id = CASE WHEN bundle_release_id = #{releaseId} THEN NULL ELSE bundle_release_id END, native_release_id = CASE WHEN native_release_id = #{releaseId} THEN NULL ELSE native_release_id END, release_id = CASE WHEN release_id = #{releaseId} THEN NULL ELSE release_id END, updated_by = #{updatedBy}, updated_at = #{updatedAt} WHERE product_id = #{productId} AND deleted_at IS NULL AND (bundle_release_id = #{releaseId} OR native_release_id = #{releaseId} OR release_id = #{releaseId})")
int clearRelease(@Param("productId") String productId, @Param("releaseId") String releaseId, @Param("updatedBy") String updatedBy, @Param("updatedAt") Long updatedAt);
} }
@@ -16,6 +16,7 @@ import com.imyeyu.java.TimiJava;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@@ -48,6 +49,12 @@ public class AppVersionService {
/// 通道指向原生包时,低于这个版本的设备改为下发热更包,先把它升到能自升级为止 /// 通道指向原生包时,低于这个版本的设备改为下发热更包,先把它升到能自升级为止
public static final String SHELL_AWARE_VERSION = "1.3.2"; public static final String SHELL_AWARE_VERSION = "1.3.2";
/// 首个按独立字段接收外壳更新的 Web 包版本
///
/// 从这版起响应把安装包放到 shellUrl,主字段 url 永远给热更包;
/// 更早的包只认「主字段 + shell 标记」,所以两套语义要并存到 1.5.0 铺开为止
public static final String SHELL_JSON_AWARE_VERSION = "1.5.0";
/// 更新日志最多返回的版本条数 /// 更新日志最多返回的版本条数
public static final int MAX_NOTES = 20; public static final int MAX_NOTES = 20;
@@ -72,28 +79,26 @@ public class AppVersionService {
if (product == null) { if (product == null) {
return AppUpdateCheckResponse.none(currentVersion); return AppUpdateCheckResponse.none(currentVersion);
} }
AppRelease release = resolveChannelRelease(product, req);
if (release == null) {
return AppUpdateCheckResponse.none(currentVersion);
}
int currentCode = parseInt(req.getVersionCode(), 0); int currentCode = parseInt(req.getVersionCode(), 0);
if (release.getKind() == AppRelease.Kind.NATIVE) { AppRelease bundlePointer = resolvePointer(product, req, AppRelease.Kind.BUNDLE);
// 低于外壳升级能力的包拿不动安装包,先补热更,否则它会把 APK 当 zip 解 AppRelease nativePointer = resolvePointer(product, req, AppRelease.Kind.NATIVE);
AppRelease bundle = bundlePointer == null ? null : resolveBundle(bundlePointer, product, currentCode, currentVersion, req.getDeviceId());
AppRelease nativeTarget = nativePointer == null ? null : resolveNative(nativePointer, currentCode);
boolean shellAware = 0 <= compareVersion(currentVersion, SHELL_AWARE_VERSION); boolean shellAware = 0 <= compareVersion(currentVersion, SHELL_AWARE_VERSION);
AppRelease nativeTarget = shellAware ? resolveNative(release, currentCode) : null; boolean jsonAware = 0 <= compareVersion(currentVersion, SHELL_JSON_AWARE_VERSION);
if (nativeTarget != null) { // 认识 shell 标记但还不认识独立字段的包,主字段继续给安装包
if (nativeTarget != null && shellAware && !jsonAware) {
return buildResponse(nativeTarget, Boolean.TRUE.equals(nativeTarget.getMandatory()), true); return buildResponse(nativeTarget, Boolean.TRUE.equals(nativeTarget.getMandatory()), true);
} }
// 装不了安装包,或壳已经是最新:热更不能因为通道被原生包占着就断流 AppUpdateCheckResponse response = bundle == null ? AppUpdateCheckResponse.none(currentVersion) : buildResponse(bundle, false, false);
AppRelease bundle = resolveLatestBundle(product, currentCode, currentVersion, req.getDeviceId()); if (nativeTarget != null && jsonAware) {
return bundle == null AppUpdateCheckResponse shell = buildResponse(nativeTarget, false, true);
? AppUpdateCheckResponse.none(currentVersion) response.setShellUrl(shell.getUrl());
: buildResponse(bundle, false, false); response.setShellVersion(shell.getVersion());
response.setShellChecksum(shell.getChecksum());
response.setShellMandatory(Boolean.TRUE.equals(nativeTarget.getMandatory()));
} }
AppRelease target = resolveBundle(release, product, currentCode, currentVersion, req.getDeviceId()); return response;
return target == null
? AppUpdateCheckResponse.none(currentVersion)
: buildResponse(target, false, false);
} }
/// 查询更新日志 /// 查询更新日志
@@ -113,9 +118,9 @@ public class AppVersionService {
if (product == null) { if (product == null) {
return new AppUpdateNotesResponse(null, Collections.emptyList()); return new AppUpdateNotesResponse(null, Collections.emptyList());
} }
AppRelease target = resolveChannelRelease(product, req); AppRelease target = resolvePointer(product, req, AppRelease.Kind.BUNDLE);
// 安装包不属于更新日志:通道指向原生包时,改用最新发布的热更包做上界 // 指针为空(刚建通道还没发过热更)时退回最近一次发布,日志不该跟着空
if (target == null || target.getKind() == AppRelease.Kind.NATIVE) { if (target == null) {
target = latestPublishedBundle(product); target = latestPublishedBundle(product);
} }
if (target == null) { if (target == null) {
@@ -172,17 +177,22 @@ public class AppVersionService {
/// @param product 产品 /// @param product 产品
/// @param req 插件上报信息 /// @param req 插件上报信息
/// @return 发布记录,通道为空或指向的记录不可用时为 null /// @return 发布记录,通道为空或指向的记录不可用时为 null
private AppRelease resolveChannelRelease(AppProduct product, AppUpdateCheckRequest req) { private AppRelease resolvePointer(AppProduct product, AppUpdateCheckRequest req, AppRelease.Kind kind) {
String channelCode = TimiJava.isEmpty(req.getChannel()) ? DEFAULT_CHANNEL : req.getChannel(); String channelCode = TimiJava.isEmpty(req.getChannel()) ? DEFAULT_CHANNEL : req.getChannel();
AppChannel channel = channelMapper.selectByProductAndCode(product.getId(), channelCode); AppChannel channel = channelMapper.selectByProductAndCode(product.getId(), channelCode);
if (channel == null || TimiJava.isEmpty(channel.getReleaseId())) { if (channel == null) {
return null; return null;
} }
AppRelease release = releaseMapper.select(channel.getReleaseId()); String id = kind == AppRelease.Kind.BUNDLE ? channel.getBundleReleaseId() : channel.getNativeReleaseId();
if (release == null || release.getStatus() != AppRelease.Status.PUBLISHED) { // 兼容旧代码在回滚期间仅更新旧指针的情况
if (TimiJava.isEmpty(id)) {
id = channel.getReleaseId();
}
if (TimiJava.isEmpty(id)) {
return null; return null;
} }
return release; AppRelease release = releaseMapper.select(id);
return release != null && release.getKind() == kind && release.getStatus() == AppRelease.Status.PUBLISHED ? release : null;
} }
/// 原生包是否需要更新 /// 原生包是否需要更新
@@ -233,45 +243,6 @@ public class AppVersionService {
return release; return release;
} }
/// 查询设备此刻可用的最新热更包
///
/// 通道指向原生包时用它兜底:通道是单指针,原生包一上线热更就停发,
/// 而低于 SHELL_AWARE_VERSION 的设备又拿不动安装包,只能靠这里把它先升上去。
/// 只认 PUBLISHED 且版本高于当前包的记录,不会降级,最坏情况是多升一次。
///
/// @param product 产品
/// @param currentCode 当前原生 versionCode
/// @param currentVersion 当前 Web 包版本
/// @param deviceId 设备标识
/// @return 可用热更包,没有则返回 null
private AppRelease resolveLatestBundle(
AppProduct product,
int currentCode,
String currentVersion,
String deviceId
) {
AppRelease latest = null;
for (AppRelease item : releaseMapper.selectPublished(product.getId(), AppRelease.Kind.BUNDLE)) {
if (item.getMinVersionCode() != null && currentCode < item.getMinVersionCode()) {
continue;
}
if (item.getMaxVersionCode() != null && item.getMaxVersionCode() < currentCode) {
continue;
}
// 同级或更低不下发,避免把设备降回去
if (0 >= compareVersion(item.getVersionName(), currentVersion)) {
continue;
}
if (!hitGray(item.getGrayPermille(), deviceId, product.getAppKey())) {
continue;
}
if (latest == null || 0 < compareVersion(item.getVersionName(), latest.getVersionName())) {
latest = item;
}
}
return latest;
}
/// 取最近一次发布的热更包 /// 取最近一次发布的热更包
/// ///
/// 通道指向原生包时,更新日志没有「通道版本」可用,改用这里兜底。 /// 通道指向原生包时,更新日志没有「通道版本」可用,改用这里兜底。
@@ -320,7 +291,7 @@ public class AppVersionService {
.path("/attach/download/") .path("/attach/download/")
.path(release.getAttachmentId()) .path(release.getAttachmentId())
.toUriString(); .toUriString();
return new AppUpdateCheckResponse(release.getVersionName(), url, release.getSha256(), mandatory, shell, release.getNote()); return new AppUpdateCheckResponse(release.getVersionName(), url, release.getSha256(), mandatory, shell, release.getNote(), null, null, null, null);
} }
/// 比较版本号 /// 比较版本号
@@ -388,6 +359,7 @@ public class AppVersionService {
/// @param grayPermille 灰度比例,为空时保持原值 /// @param grayPermille 灰度比例,为空时保持原值
/// @param operatorId 操作人用户 ID /// @param operatorId 操作人用户 ID
/// @param note 变更说明 /// @param note 变更说明
@Transactional
public void publish(String releaseId, String channelCode, Integer grayPermille, String operatorId, String note) { public void publish(String releaseId, String channelCode, Integer grayPermille, String operatorId, String note) {
AppRelease release = releaseMapper.select(releaseId); AppRelease release = releaseMapper.select(releaseId);
if (release == null) { if (release == null) {
@@ -408,7 +380,7 @@ public class AppVersionService {
release.setNote(TimiJava.isEmpty(note) ? release.getNote() : note); release.setNote(TimiJava.isEmpty(note) ? release.getNote() : note);
release.setUpdatedAt(now); release.setUpdatedAt(now);
releaseMapper.updateSelective(release); releaseMapper.updateSelective(release);
channelMapper.updateReleaseId(channel.getId(), releaseId, operatorId, now); channelMapper.updatePointers(channel.getId(), releaseId, release.getKind() == AppRelease.Kind.NATIVE, operatorId, now);
log("APP_RELEASE_PUBLISH", releaseId); log("APP_RELEASE_PUBLISH", releaseId);
} }
@@ -420,6 +392,7 @@ public class AppVersionService {
/// @param channelCode 通道代码 /// @param channelCode 通道代码
/// @param targetReleaseId 目标发布 ID /// @param targetReleaseId 目标发布 ID
/// @param operatorId 操作人用户 ID /// @param operatorId 操作人用户 ID
@Transactional
public void rollback(String productId, String channelCode, String targetReleaseId, String operatorId) { public void rollback(String productId, String channelCode, String targetReleaseId, String operatorId) {
AppChannel channel = channelMapper.selectByProductAndCode(productId, channelCode); AppChannel channel = channelMapper.selectByProductAndCode(productId, channelCode);
if (channel == null) { if (channel == null) {
@@ -434,7 +407,7 @@ public class AppVersionService {
target.setStatus(AppRelease.Status.PUBLISHED); target.setStatus(AppRelease.Status.PUBLISHED);
target.setUpdatedAt(now); target.setUpdatedAt(now);
releaseMapper.updateSelective(target); releaseMapper.updateSelective(target);
channelMapper.updateReleaseId(channel.getId(), targetReleaseId, operatorId, now); channelMapper.updatePointers(channel.getId(), targetReleaseId, target.getKind() == AppRelease.Kind.NATIVE, operatorId, now);
log("APP_RELEASE_ROLLBACK", targetReleaseId); log("APP_RELEASE_ROLLBACK", targetReleaseId);
} }
@@ -442,6 +415,7 @@ public class AppVersionService {
/// ///
/// @param releaseId 发布 ID /// @param releaseId 发布 ID
/// @param operatorId 操作人用户 ID /// @param operatorId 操作人用户 ID
@Transactional
public void revoke(String releaseId, String operatorId) { public void revoke(String releaseId, String operatorId) {
AppRelease release = releaseMapper.select(releaseId); AppRelease release = releaseMapper.select(releaseId);
if (release == null) { if (release == null) {
@@ -451,10 +425,7 @@ public class AppVersionService {
release.setStatus(AppRelease.Status.REVOKED); release.setStatus(AppRelease.Status.REVOKED);
release.setUpdatedAt(now); release.setUpdatedAt(now);
releaseMapper.updateSelective(release); releaseMapper.updateSelective(release);
AppChannel channel = channelMapper.selectByProductAndCode(release.getProductId(), DEFAULT_CHANNEL); channelMapper.clearRelease(release.getProductId(), releaseId, operatorId, now);
if (channel != null && releaseId.equals(channel.getReleaseId())) {
channelMapper.updateReleaseId(channel.getId(), null, operatorId, now);
}
log("APP_RELEASE_REVOKE", releaseId); log("APP_RELEASE_REVOKE", releaseId);
} }
@@ -39,11 +39,23 @@ public class AppUpdateCheckResponse {
/// 字段名不能叫 message:Capgo 插件把 message 当作错误信息,收到会直接判为检查失败 /// 字段名不能叫 message:Capgo 插件把 message 当作错误信息,收到会直接判为检查失败
protected String note; protected String note;
/// 外壳下载地址
protected String shellUrl;
/// 外壳版本
protected String shellVersion;
/// 外壳 SHA-256
protected String shellChecksum;
/// true 为强制更新
protected Boolean shellMandatory;
/// 构建无更新响应,version 与当前一致时插件不会下载 /// 构建无更新响应,version 与当前一致时插件不会下载
/// ///
/// @param currentVersion 当前运行的 Web 包版本 /// @param currentVersion 当前运行的 Web 包版本
/// @return 无更新响应 /// @return 无更新响应
public static AppUpdateCheckResponse none(String currentVersion) { public static AppUpdateCheckResponse none(String currentVersion) {
return new AppUpdateCheckResponse(currentVersion, "", null, false, null, null); return new AppUpdateCheckResponse(currentVersion, "", null, false, null, null, null, null, null, null);
} }
} }
@@ -0,0 +1,9 @@
ALTER TABLE `app_channel`
ADD COLUMN `bundle_release_id` VARCHAR(36) NULL COMMENT '当前热更包发布 ID' AFTER `name`,
ADD COLUMN `native_release_id` VARCHAR(36) NULL COMMENT '当前原生包发布 ID' AFTER `bundle_release_id`;
UPDATE `app_channel` c JOIN `app_release` r ON r.`id` = c.`release_id`
SET c.`native_release_id` = IF(r.`kind` = 'NATIVE', c.`release_id`, NULL),
c.`bundle_release_id` = IF(r.`kind` = 'BUNDLE', c.`release_id`,
(SELECT b.`id` FROM `app_release` b WHERE b.`product_id` = c.`product_id` AND b.`kind` = 'BUNDLE'
AND b.`status` = 'PUBLISHED' AND b.`deleted_at` IS NULL ORDER BY b.`published_at` DESC LIMIT 1));