Skip to content

Commit

Permalink
fix sql query naming
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Dec 15, 2024
1 parent 9b012b7 commit 99e383f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 40 deletions.
7 changes: 1 addition & 6 deletions db/queries/nodes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ SELECT id, fingerprint, last_heartbeat_at, created_at
FROM nodes
WHERE fingerprint = ?;

-- name: UpdateNodeHeartbeatByFingerprint :exec
UPDATE nodes
SET last_heartbeat_at = unixepoch()
WHERE fingerprint = ?;

-- name: UpdateNodeHeartbeatAndClaimedAtByFingerprint :exec
-- name: PingNodeByFingerprint :exec
UPDATE nodes
SET last_heartbeat_at = unixepoch()
WHERE fingerprint = ?;
Expand Down
17 changes: 3 additions & 14 deletions internal/db/nodes.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 9 additions & 12 deletions internal/db/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ func (s *Store) InsertNode(ctx context.Context, fingerprint string) (*Node, erro
return &node, nil
}

func (s *Store) UpdateNodeHeartbeat(ctx context.Context, fingerprint string) error {
return s.queries.UpdateNodeHeartbeatByFingerprint(ctx, fingerprint)
}

func (s *Store) DeleteNodeByFingerprint(ctx context.Context, fingerprint string) error {
return s.queries.DeleteNodeByFingerprint(ctx, fingerprint)
}
Expand All @@ -140,6 +136,10 @@ func (s *Store) GetNodeByFingerprint(ctx context.Context, fingerprint string) (*
return &node, nil
}

func (s *Store) PingNodeByFingerprint(ctx context.Context, fingerprint string) error {
return s.queries.PingNodeByFingerprint(ctx, fingerprint)
}

func (s *Store) InsertAuditLog(ctx context.Context, eventTypeId EventTypeId, entityTypeId EntityTypeId, entityID string) error {
params := InsertAuditLogParams{
EventTypeID: int64(eventTypeId),
Expand Down Expand Up @@ -185,13 +185,10 @@ func (s *Store) GetLicenseByNodeID(ctx context.Context, nodeID *int64) (*License
return &license, nil
}

func (s *Store) UpdateNodeHeartbeatAndClaimedAtByFingerprint(ctx context.Context, fingerprint string) error {
return s.queries.UpdateNodeHeartbeatAndClaimedAtByFingerprint(ctx, fingerprint)
}

func (s *Store) ReleaseLicensesFromInactiveNodes(ctx context.Context, ttl time.Duration) ([]License, error) {
ttlDuration := fmt.Sprintf("-%d seconds", int(ttl.Seconds()))
licenses, err := s.queries.ReleaseLicensesFromInactiveNodes(ctx, ttlDuration)
t := fmt.Sprintf("-%d seconds", int(ttl.Seconds()))

licenses, err := s.queries.ReleaseLicensesFromInactiveNodes(ctx, t)
if err != nil {
slog.Error("failed to release licenses from inactive nodes", "error", err)

Expand All @@ -202,9 +199,9 @@ func (s *Store) ReleaseLicensesFromInactiveNodes(ctx context.Context, ttl time.D
}

func (s *Store) DeleteInactiveNodes(ctx context.Context, ttl time.Duration) error {
ttlDuration := fmt.Sprintf("-%d seconds", int(ttl.Seconds()))
t := fmt.Sprintf("-%d seconds", int(ttl.Seconds()))

if err := s.queries.DeleteInactiveNodes(ctx, ttlDuration); err != nil {
if err := s.queries.DeleteInactiveNodes(ctx, t); err != nil {
slog.Error("failed to delete inactive nodes", "error", err)
return err
}
Expand Down
17 changes: 9 additions & 8 deletions internal/licenses/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,23 +192,22 @@ func (m *manager) ClaimLicense(ctx context.Context, fingerprint string) (*Licens
if err != nil {
return nil, fmt.Errorf("failed to begin transaction: %w", err)
}
storeWithTx := m.store.WithTx(tx)
txs := m.store.WithTx(tx)
defer tx.Rollback()

node, err := m.fetchOrCreateNode(ctx, *storeWithTx, fingerprint)
node, err := m.fetchOrCreateNode(ctx, *txs, fingerprint)
if err != nil {
return nil, fmt.Errorf("failed to fetch or create node: %w", err)
}

claimedLicense, err := storeWithTx.GetLicenseByNodeID(ctx, &node.ID)

claimedLicense, err := txs.GetLicenseByNodeID(ctx, &node.ID)
if err == nil {
if !m.config.ExtendOnHeartbeat { // if heartbeat is disabled, we can't extend the claimed license
slog.Warn("failed to claim license due to conflict due to heartbeat disabled", "nodeID", node.ID, "Fingerprint", node.Fingerprint)
return &LicenseOperationResult{Status: OperationStatusConflict}, nil
}

if err := storeWithTx.UpdateNodeHeartbeat(ctx, fingerprint); err != nil {
if err := txs.PingNodeByFingerprint(ctx, fingerprint); err != nil {
return nil, fmt.Errorf("failed to update node heartbeat: %w", err)
}

Expand All @@ -230,17 +229,19 @@ func (m *manager) ClaimLicense(ctx context.Context, fingerprint string) (*Licens
}

// claim a new license based on the strategy
newLicense, err := m.selectLicenseClaimStrategy(ctx, *storeWithTx, &node.ID)
newLicense, err := m.selectLicenseClaimStrategy(ctx, *txs, &node.ID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
slog.Warn("no licenses available for claim", "Fingerprint", node.Fingerprint)

return &LicenseOperationResult{Status: OperationStatusNoLicensesAvailable}, nil
}

return nil, fmt.Errorf("failed to claim license: %w", err)
}

// Update node claim timestamp
if err := storeWithTx.UpdateNodeHeartbeatAndClaimedAtByFingerprint(ctx, fingerprint); err != nil {
// ping node heartbeat
if err := txs.PingNodeByFingerprint(ctx, fingerprint); err != nil {
return nil, fmt.Errorf("failed to update node claim: %w", err)
}

Expand Down

0 comments on commit 99e383f

Please sign in to comment.