Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/envd/internal/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ func (a *API) PostInit(w http.ResponseWriter, r *http.Request) {
a.initLock.Lock()
defer a.initLock.Unlock()

// Update data only if the request is newer or if there's no timestamp at all
if initRequest.Timestamp == nil || a.lastSetTime.SetToGreater(initRequest.Timestamp.UnixNano()) {
// Update data only if the request is newer or if there's no timestamp at all.
// We check without mutating so that a failed SetData doesn't permanently
// raise lastSetTime — retries with the same pinned Timestamp must be able
// to re-run SetData until it succeeds.
shouldRun := initRequest.Timestamp == nil || initRequest.Timestamp.UnixNano() > a.lastSetTime.Load()
if shouldRun {
err = a.SetData(ctx, logger, initRequest)
if err != nil {
switch {
Expand All @@ -146,6 +150,9 @@ func (a *API) PostInit(w http.ResponseWriter, r *http.Request) {

return
}
if initRequest.Timestamp != nil {
a.lastSetTime.SetToGreater(initRequest.Timestamp.UnixNano())
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/envd/internal/utils/atomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ func (a *AtomicMax) SetToGreater(newValue int64) bool {

return true
}

// Load returns the current value.
func (a *AtomicMax) Load() int64 {
a.mu.Lock()
defer a.mu.Unlock()

return a.val
}
2 changes: 1 addition & 1 deletion packages/envd/pkg/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package pkg

const Version = "0.5.23"
const Version = "0.5.24"
16 changes: 9 additions & 7 deletions packages/orchestrator/pkg/sandbox/envd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func (s *Sandbox) doRequestWithInfiniteRetries(
) (*http.Response, int64, error) {
requestCount := int64(0)

// Pin Timestamp once so envd's lastSetTime.SetToGreater guard can skip
// re-delivered retries instead of re-running SetData against dead sockets.
now := time.Now()
Comment thread
ValentaTomas marked this conversation as resolved.
jsonBody := &envd.PostInitJSONBody{
LifecycleID: s.LifecycleID,
EnvVars: s.Config.Envd.Vars,
Expand All @@ -46,16 +49,15 @@ func (s *Sandbox) doRequestWithInfiniteRetries(
DefaultWorkdir: utils.DerefOrDefault(s.Config.Envd.DefaultWorkdir, ""),
VolumeMounts: s.convertMounts(s.Config.VolumeMounts),
CaBundle: s.CABundle,
Timestamp: now,
Comment thread
ValentaTomas marked this conversation as resolved.
}

for {
jsonBody.Timestamp = time.Now()

body, err := json.Marshal(jsonBody)
if err != nil {
return nil, requestCount, err
}
body, err := json.Marshal(jsonBody)
if err != nil {
return nil, requestCount, err
}

for {
requestCount++
reqCtx, cancel := context.WithTimeout(ctx, s.internalConfig.EnvdInitRequestTimeout)
request, err := http.NewRequestWithContext(reqCtx, method, address, bytes.NewReader(body))
Expand Down
Loading