perf(douyu): 降低商品能力同步写放大

This commit is contained in:
yml
2026-11-23 06:00:19 +08:00
parent 8e01c3c69e
commit 1e67111715
4 changed files with 157 additions and 65 deletions
+4 -2
View File
@@ -55,8 +55,10 @@ MYSQL_CPUS=2
# MySQL 性能观测;同机部署资源紧张时可先使用 256M。
MYSQL_INNODB_BUFFER_POOL_SIZE=512M
MYSQL_INNODB_REDO_LOG_CAPACITY=512M
# binlog 保留 3 天;当前无副本,避免 1GiB 级 binlog 长期占满磁盘。
MYSQL_BINLOG_EXPIRE_LOGS_SECONDS=259200
# binlog 保留 12 小时;当前无副本,避免 1GiB 级 binlog 长期占满磁盘。
MYSQL_BINLOG_EXPIRE_LOGS_SECONDS=43200
# 只记录实际变化的列,避免时间戳更新时重复写入整行大 JSON。
MYSQL_BINLOG_ROW_IMAGE=MINIMAL
MYSQL_SLOW_QUERY_LOG=1
MYSQL_LONG_QUERY_TIME=1
+4 -2
View File
@@ -198,8 +198,10 @@ services:
- --innodb-buffer-pool-size=${MYSQL_INNODB_BUFFER_POOL_SIZE:-512M}
# 100MiB redo 在高写入时出现过 checkpointer 落后告警,默认提升到 512MiB。
- --innodb-redo-log-capacity=${MYSQL_INNODB_REDO_LOG_CAPACITY:-512M}
# 当前库没有副本;保留 3 天足以覆盖短期误操作,避免 binlog 持续挤占磁盘。
- --binlog-expire-logs-seconds=${MYSQL_BINLOG_EXPIRE_LOGS_SECONDS:-259200}
# 当前库没有副本;12 小时足以覆盖短期误操作,又不会在高峰期挤满磁盘。
- --binlog-expire-logs-seconds=${MYSQL_BINLOG_EXPIRE_LOGS_SECONDS:-43200}
# 行日志只记录实际变化的列,避免更新时间戳时重复写入整行大 JSON。
- --binlog-row-image=${MYSQL_BINLOG_ROW_IMAGE:-MINIMAL}
# 默认记录 1 秒以上查询,便于验证查询优化;稳定后可设 MYSQL_SLOW_QUERY_LOG=0。
- --slow-query-log=${MYSQL_SLOW_QUERY_LOG:-1}
- --long-query-time=${MYSQL_LONG_QUERY_TIME:-1}
+42
View File
@@ -217,6 +217,48 @@ def test_sync_account_goods_states_writes_capabilities_and_exchange_stats(db):
)
def test_sync_same_fresh_snapshot_does_not_rewrite_state(db):
account = _account(db, "cap-noop")
items = [{"commodityId": "A", "name": "商品 A", "status": "0"}]
first_changed = service.sync_account_goods_states(
db,
account,
items,
observed_at=NOW,
)
db.commit()
assert first_changed == 1
state = db.query(AccountCommodityState).filter_by(account_id=account.id).one()
assert state.observed_at == NOW
repeated_changed = service.sync_account_goods_states(
db,
account,
items,
observed_at=NOW + timedelta(minutes=1),
)
db.commit()
assert repeated_changed == 0
db.refresh(state)
assert state.observed_at == NOW
refreshed_at = NOW + timedelta(hours=7)
expired_changed = service.sync_account_goods_states(
db,
account,
items,
observed_at=refreshed_at,
)
db.commit()
assert expired_changed == 1
db.refresh(state)
assert state.observed_at == refreshed_at
def test_next_sync_marks_missing_goods_not_offered(db):
account = _account(db, "cap-remove")
service.sync_account_goods_states(
@@ -45,6 +45,31 @@ DEFAULT_NO_STOCK_CAPABILITY_TTL_SECONDS = 15 * 60
DEFAULT_UNKNOWN_CAPABILITY_TTL_SECONDS = 5 * 60
def _set_if_changed(instance: Any, field: str, value: Any) -> bool:
"""只在值真正变化时赋值,避免无意义 UPDATE 写满 binlog。"""
if getattr(instance, field) == value:
return False
setattr(instance, field, value)
return True
def _commodity_state_is_fresh(
state: AccountCommodityState,
moment: datetime,
) -> bool:
return bool(state.next_check_at is not None and state.next_check_at > moment)
def _apply_commodity_state_values(
state: AccountCommodityState,
values: dict[str, Any],
) -> bool:
changed = False
for field, value in values.items():
changed = _set_if_changed(state, field, value) or changed
return changed
def _env_positive_int(name: str, default: int) -> int:
try:
value = int(os.getenv(name, str(default)))
@@ -507,34 +532,42 @@ def sync_account_goods_states(
commodity_id,
(None, 0),
)
state.commodity_name = _commodity_name(item)[:256]
state.account_eligibility = assessment.account_eligibility
state.offer_state = assessment.offer_state
state.stock_state = assessment.stock_state
state.stock_count = assessment.stock_count
state.store_status = assessment.store_status
state.open_status = assessment.open_status
state.availability = assessment.availability
state.blocking_reason = assessment.blocking_reason[:255]
state.raw_status = assessment.raw_status[:16]
state.raw_json = dict(item)
state.time_limited = window.time_limited
state.available_from = window.start
state.available_until = window.end
state.schedule_json = window.schedule or None
state.observed_at = moment
state.valid_until = (
valid_until = (
min(ttl_valid_until, window.next_boundary)
if window.next_boundary is not None
else ttl_valid_until
)
state.next_check_at = state.valid_until
state.last_exchange_at = last_exchange_at
state.exchange_count_365d = int(exchange_count)
state.source = source[:24]
state.error_code = ""
state.updated_at = moment
changed += 1
fresh_before = _commodity_state_is_fresh(state, moment)
data_changed = _apply_commodity_state_values(
state,
{
"commodity_name": _commodity_name(item)[:256],
"account_eligibility": assessment.account_eligibility,
"offer_state": assessment.offer_state,
"stock_state": assessment.stock_state,
"stock_count": assessment.stock_count,
"store_status": assessment.store_status,
"open_status": assessment.open_status,
"availability": assessment.availability,
"blocking_reason": assessment.blocking_reason[:255],
"raw_status": assessment.raw_status[:16],
"raw_json": dict(item),
"time_limited": window.time_limited,
"available_from": window.start,
"available_until": window.end,
"schedule_json": window.schedule or None,
"last_exchange_at": last_exchange_at,
"exchange_count_365d": int(exchange_count),
"source": source[:24],
"error_code": "",
},
)
if data_changed or not fresh_before:
state.observed_at = moment
state.valid_until = valid_until
state.next_check_at = valid_until
state.updated_at = moment
changed += 1
if replace_missing:
for commodity_id, state in by_commodity.items():
@@ -545,27 +578,33 @@ def sync_account_goods_states(
and state.blocking_reason == "官方商品列表本次未返回该商品"
):
continue
state.availability = AVAILABILITY_NOT_OFFERED
state.account_eligibility = ACCOUNT_ELIGIBILITY_UNKNOWN
state.offer_state = OFFER_STATE_NOT_OFFERED
state.stock_state = STOCK_STATE_UNKNOWN
state.stock_count = None
state.store_status = None
state.open_status = None
state.blocking_reason = "官方商品列表本次未返回该商品"
state.raw_json = None
state.time_limited = False
state.available_from = None
state.available_until = None
state.schedule_json = None
state.observed_at = moment
state.valid_until = None
state.next_check_at = moment + timedelta(
seconds=capability_ttl_seconds(AVAILABILITY_NOT_OFFERED)
data_changed = _apply_commodity_state_values(
state,
{
"availability": AVAILABILITY_NOT_OFFERED,
"account_eligibility": ACCOUNT_ELIGIBILITY_UNKNOWN,
"offer_state": OFFER_STATE_NOT_OFFERED,
"stock_state": STOCK_STATE_UNKNOWN,
"stock_count": None,
"store_status": None,
"open_status": None,
"blocking_reason": "官方商品列表本次未返回该商品",
"raw_json": None,
"time_limited": False,
"available_from": None,
"available_until": None,
"schedule_json": None,
"source": source[:24],
},
)
state.source = source[:24]
state.updated_at = moment
changed += 1
if data_changed:
state.observed_at = moment
state.valid_until = None
state.next_check_at = moment + timedelta(
seconds=capability_ttl_seconds(AVAILABILITY_NOT_OFFERED)
)
state.updated_at = moment
changed += 1
db.flush()
return changed
@@ -643,23 +682,30 @@ def record_account_commodity_state(
now=moment,
)
last_exchange_at, exchange_count = exchange_stats.get(commodity_id, (None, 0))
state.account_eligibility = account_eligibility
state.offer_state = offer_state
state.stock_state = stock_state
state.stock_count = stock_count
state.store_status = store_status
state.open_status = open_status
state.availability = availability
state.blocking_reason = blocking_reason[:255]
state.raw_status = raw_status[:16]
state.observed_at = moment
state.valid_until = moment + timedelta(seconds=ttl)
state.next_check_at = state.valid_until
state.last_exchange_at = last_exchange_at
state.exchange_count_365d = int(exchange_count)
state.source = source[:24]
state.error_code = error_code[:64]
state.updated_at = moment
fresh_before = _commodity_state_is_fresh(state, moment)
data_changed = _apply_commodity_state_values(
state,
{
"account_eligibility": account_eligibility,
"offer_state": offer_state,
"stock_state": stock_state,
"stock_count": stock_count,
"store_status": store_status,
"open_status": open_status,
"availability": availability,
"blocking_reason": blocking_reason[:255],
"raw_status": raw_status[:16],
"last_exchange_at": last_exchange_at,
"exchange_count_365d": int(exchange_count),
"source": source[:24],
"error_code": error_code[:64],
},
)
if data_changed or not fresh_before:
state.observed_at = moment
state.valid_until = moment + timedelta(seconds=ttl)
state.next_check_at = state.valid_until
state.updated_at = moment
db.flush()
return state