Skip to content

Commit bcd5350

Browse files
committed
handle uptime report correctly
1 parent e0458ef commit bcd5350

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

node-registrar/client/node.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ func (c *RegistrarClient) registerNode(
254254
}{}
255255

256256
err = json.NewDecoder(resp.Body).Decode(&result)
257-
257+
if err != nil {
258+
return 0, errors.Wrap(err, "failed to decode response body")
259+
}
258260
c.nodeID = result.NodeID
259261
return result.NodeID, err
260262
}

node-registrar/client/node_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ func TestUpdateNode(t *testing.T) {
9999
request = updateNodeSendUptimeReport
100100

101101
report := UptimeReport{
102-
Uptime: 40 * time.Minute,
103-
Timestamp: time.Now(),
102+
Uptime: 40 * 60,
103+
Timestamp: time.Now().Unix(),
104104
}
105105
err = c.ReportUptime(report)
106106
require.NoError(err)

node-registrar/client/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ type Node struct {
3636
}
3737

3838
type UptimeReport struct {
39-
Uptime time.Duration `json:"uptime"`
40-
Timestamp time.Time `json:"timestamp"`
39+
Uptime uint64 `json:"uptime"` // in seconds
40+
Timestamp int64 `json:"timestamp"` // in seconds since epoch
4141
}
4242

4343
type ZosVersion struct {

node-registrar/pkg/db/farms.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package db
22

33
import (
4+
"strings"
5+
46
"github.com/pkg/errors"
57
"gorm.io/gorm"
68
)
@@ -41,9 +43,10 @@ func (db *Database) GetFarm(farmID uint64) (farm Farm, err error) {
4143

4244
func (db *Database) CreateFarm(farm Farm) (uint64, error) {
4345
if err := db.gormDB.Create(&farm).Error; err != nil {
44-
if errors.Is(err, gorm.ErrDuplicatedKey) {
46+
if strings.Contains(err.Error(), "duplicate key value") {
4547
return 0, ErrRecordAlreadyExists
4648
}
49+
return 0, err
4750
}
4851

4952
return farm.FarmID, nil

node-registrar/pkg/server/handlers.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ func (s *Server) updateNodeHandler(c *gin.Context) {
423423

424424
type UptimeReportRequest struct {
425425
Uptime uint64 `json:"uptime" binding:"required"`
426-
Timestamp uint64 `json:"timestamp" binding:"required"`
426+
Timestamp int64 `json:"timestamp" binding:"required"`
427427
}
428428

429429
// @Summary Report node uptime
@@ -473,7 +473,8 @@ func (s *Server) uptimeReportHandler(c *gin.Context) {
473473
// Ensuring the timestamp_hint is within an Acceptable Range
474474
err = validateTimestampHint(req.Timestamp)
475475
if err != nil {
476-
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid timestamp hint"})
476+
// include the error message
477+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
477478
return
478479
}
479480

@@ -807,8 +808,8 @@ func ensureOwner(c *gin.Context, twinID uint64) {
807808
}
808809

809810
// Helper function to validate timestamp hint
810-
func validateTimestampHint(timestampHint uint64) error {
811-
hintTime := time.Unix(int64(timestampHint), 0)
811+
func validateTimestampHint(timestampHint int64) error {
812+
hintTime := time.Unix(timestampHint, 0)
812813

813814
now := time.Now()
814815

@@ -819,7 +820,7 @@ func validateTimestampHint(timestampHint uint64) error {
819820

820821
// Check if the hint is within the acceptable range
821822
if hintTime.Before(earliestAllowed) || hintTime.After(latestAllowed) {
822-
return errors.New("InvalidTimestampHint")
823+
return fmt.Errorf("invalid timestamp hint: must be within ±%d seconds of the current time (%s)", UptimeReportTimestampHintDrift, now)
823824
}
824825

825826
return nil

0 commit comments

Comments
 (0)