-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cmd/opampsupervisor]: Persist the instance ID between restarts (#32618)
**Description:** <Describe what has changed.> * Adds the ability to persist the instance ID on disk and load it on startup. **Link to tracking Issue:** Closes #21073 **Testing:** * Add unit tests for persistence functionality * Add e2e tests for persistence (testing that initial id is persisted, and that server specified ID is persisted) **Documentation:** N/A
- Loading branch information
1 parent
4b9a237
commit 125ff49
Showing
12 changed files
with
432 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: opampsupervisor | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Allows the supervisor to persist its instance ID between restarts. | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [21073] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package supervisor | ||
|
||
import ( | ||
"crypto/rand" | ||
"errors" | ||
"os" | ||
"time" | ||
|
||
"github.com/oklog/ulid/v2" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// persistentState represents persistent state for the supervisor | ||
type persistentState struct { | ||
InstanceID ulid.ULID `yaml:"instance_id"` | ||
|
||
// Path to the config file that the state should be saved to. | ||
// This is not marshaled. | ||
configPath string `yaml:"-"` | ||
} | ||
|
||
func (p *persistentState) SetInstanceID(id ulid.ULID) error { | ||
p.InstanceID = id | ||
return p.writeState() | ||
} | ||
|
||
func (p *persistentState) writeState() error { | ||
by, err := yaml.Marshal(p) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return os.WriteFile(p.configPath, by, 0600) | ||
} | ||
|
||
// loadOrCreatePersistentState attempts to load the persistent state from disk. If it doesn't | ||
// exist, a new persistent state file is created. | ||
func loadOrCreatePersistentState(file string) (*persistentState, error) { | ||
state, err := loadPersistentState(file) | ||
switch { | ||
case errors.Is(err, os.ErrNotExist): | ||
return createNewPersistentState(file) | ||
case err != nil: | ||
return nil, err | ||
default: | ||
return state, nil | ||
} | ||
} | ||
|
||
func loadPersistentState(file string) (*persistentState, error) { | ||
var state *persistentState | ||
|
||
by, err := os.ReadFile(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := yaml.Unmarshal(by, &state); err != nil { | ||
return nil, err | ||
} | ||
|
||
state.configPath = file | ||
|
||
return state, nil | ||
} | ||
|
||
func createNewPersistentState(file string) (*persistentState, error) { | ||
id, err := generateNewULID() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
p := &persistentState{ | ||
InstanceID: id, | ||
configPath: file, | ||
} | ||
|
||
return p, p.writeState() | ||
} | ||
|
||
func generateNewULID() (ulid.ULID, error) { | ||
entropy := ulid.Monotonic(rand.Reader, 0) | ||
id, err := ulid.New(ulid.Timestamp(time.Now()), entropy) | ||
if err != nil { | ||
return ulid.ULID{}, err | ||
} | ||
|
||
return id, nil | ||
} |
Oops, something went wrong.