dbindex: Fix heartbeat checks

This commit is contained in:
Łukasz Magiera 2024-01-05 16:49:56 +01:00
parent 0244c7df50
commit 725460bc45

View File

@ -187,7 +187,7 @@ retryAttachStorage:
var urls sql.NullString
var storageId sql.NullString
err = tx.QueryRow(
"Select storage_id, urls FROM storage_path WHERE storage_id = $1", string(si.ID)).Scan(&storageId, &urls)
"SELECT storage_id, urls FROM storage_path WHERE storage_id = $1", string(si.ID)).Scan(&storageId, &urls)
if err != nil && !strings.Contains(err.Error(), "no rows in result set") {
return false, xerrors.Errorf("storage attach select fails: %v", err)
}
@ -202,7 +202,7 @@ retryAttachStorage:
currUrls = union(currUrls, si.URLs)
_, err = tx.Exec(
"UPDATE storage_path set urls=$1, weight=$2, max_storage=$3, can_seal=$4, can_store=$5, groups=$6, allow_to=$7, allow_types=$8, deny_types=$9 WHERE storage_id=$10",
"UPDATE storage_path set urls=$1, weight=$2, max_storage=$3, can_seal=$4, can_store=$5, groups=$6, allow_to=$7, allow_types=$8, deny_types=$9, last_heartbeat=NOW() WHERE storage_id=$10",
strings.Join(currUrls, ","),
si.Weight,
si.MaxStorage,
@ -223,7 +223,7 @@ retryAttachStorage:
// Insert storage id
_, err = tx.Exec(
"INSERT INTO storage_path "+
"Values($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)",
"Values($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, NOW())",
si.ID,
strings.Join(si.URLs, ","),
si.Weight,
@ -238,8 +238,7 @@ retryAttachStorage:
st.Available,
st.FSAvailable,
st.Reserved,
st.Used,
time.Now())
st.Used)
if err != nil {
return false, xerrors.Errorf("StorageAttach insert fails: %v", err)
}
@ -331,13 +330,12 @@ func (dbi *DBIndex) StorageReportHealth(ctx context.Context, id storiface.ID, re
}
_, err = dbi.harmonyDB.Exec(ctx,
"UPDATE storage_path set capacity=$1, available=$2, fs_available=$3, reserved=$4, used=$5, last_heartbeat=$6",
"UPDATE storage_path set capacity=$1, available=$2, fs_available=$3, reserved=$4, used=$5, last_heartbeat=NOW()",
report.Stat.Capacity,
report.Stat.Available,
report.Stat.FSAvailable,
report.Stat.Reserved,
report.Stat.Used,
time.Now().UTC())
report.Stat.Used)
if err != nil {
return xerrors.Errorf("updating storage health in DB fails with err: %v", err)
}
@ -574,9 +572,9 @@ func (dbi *DBIndex) StorageFindSector(ctx context.Context, s abi.SectorID, ft st
FROM storage_path
WHERE can_seal=true
and available >= $1
and NOW()-last_heartbeat < $2
and NOW()-($2 * INTERVAL '1 second') < last_heartbeat
and heartbeat_err is null`,
spaceReq, SkippedHeartbeatThresh)
spaceReq, SkippedHeartbeatThresh.Seconds())
if err != nil {
return nil, xerrors.Errorf("Selecting allowfetch storage paths from DB fails err: %v", err)
}
@ -713,12 +711,12 @@ func (dbi *DBIndex) StorageBestAlloc(ctx context.Context, allocate storiface.Sec
deny_types
FROM storage_path
WHERE available >= $1
and NOW()-last_heartbeat < $2
and NOW()-($2 * INTERVAL '1 second') < last_heartbeat
and heartbeat_err is null
and ($3 and can_seal = TRUE or $4 and can_store = TRUE)
and (($3 and can_seal = TRUE) or ($4 and can_store = TRUE))
order by (available::numeric * weight) desc`,
spaceReq,
SkippedHeartbeatThresh,
SkippedHeartbeatThresh.Seconds(),
pathType == storiface.PathSealing,
pathType == storiface.PathStorage,
)