-
Notifications
You must be signed in to change notification settings - Fork 97
[raft/memstore] Add aux memstore #1529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
the-glu
wants to merge
5
commits into
interuss:master
Choose a base branch
from
Orbitalize:memstore_aux
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
543783e
implement consensus logic part 2
MariemBaccari 48c3cac
use separate consensus instances
MariemBaccari a52ee35
implement generic raftstore
MariemBaccari 2bb37c0
[raft] Add base memstore
the-glu ca2e6e7
[raft/memstore] Add aux memstore
the-glu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,3 @@ | ||
| // Package aux_.store.memstore provides a full implementation of store.Store[aux_.repos.Repository] | ||
| // storing data in memory. It is meant to be used by raftstore. | ||
| package memstore |
This file contains hidden or 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,86 @@ | ||
| package memstore | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "time" | ||
|
|
||
| auxmodels "github.com/interuss/dss/pkg/aux_/models" | ||
| dsserr "github.com/interuss/dss/pkg/errors" | ||
| "github.com/interuss/stacktrace" | ||
| ) | ||
|
|
||
| func (r *repo) SaveOwnMetadata(_ context.Context, locality string, publicEndpoint string) error { | ||
| if locality == "" { | ||
| return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Locality not set") | ||
| } | ||
| if publicEndpoint == "" { | ||
| return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Public endpoint not set") | ||
| } | ||
|
|
||
| r.participants[locality] = &participant{ | ||
| publicEndpoint: publicEndpoint, | ||
| updatedAt: time.Now(), | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (r *repo) GetDSSMetadata(_ context.Context) ([]*auxmodels.DSSMetadata, error) { | ||
| metadata := make([]*auxmodels.DSSMetadata, 0, len(r.participants)) | ||
| for locality, p := range r.participants { | ||
| updatedAt := p.updatedAt | ||
| m := &auxmodels.DSSMetadata{ | ||
| Locality: locality, | ||
| PublicEndpoint: p.publicEndpoint, | ||
| UpdatedAt: &updatedAt, | ||
| } | ||
|
|
||
| // Find the latest heartbeat across all sources for this locality. | ||
| var latest auxmodels.Heartbeat | ||
| found := false | ||
| for key, hb := range r.heartbeats { | ||
| if key.locality != locality { | ||
| continue | ||
| } | ||
| if !found || hb.Timestamp.After(*latest.Timestamp) { | ||
| latest = hb | ||
| found = true | ||
| } | ||
| } | ||
|
|
||
| if found { | ||
| m.LatestTimestamp.Source = sql.NullString{String: latest.Source, Valid: true} | ||
| m.LatestTimestamp.Timestamp = latest.Timestamp | ||
| m.LatestTimestamp.NextHeartbeatExpectedBefore = latest.NextHeartbeatExpectedBefore | ||
| m.LatestTimestamp.Reporter = sql.NullString{String: latest.Reporter, Valid: true} | ||
| } | ||
|
|
||
| metadata = append(metadata, m) | ||
| } | ||
| return metadata, nil | ||
| } | ||
|
|
||
| func (r *repo) RecordHeartbeat(_ context.Context, heartbeat auxmodels.Heartbeat) error { | ||
| if heartbeat.Locality == "" { | ||
| return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Locality not set") | ||
| } | ||
| if heartbeat.Source == "" { | ||
| return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Source not set") | ||
| } | ||
|
|
||
| if heartbeat.Timestamp == nil { | ||
| now := time.Now() | ||
| heartbeat.Timestamp = &now | ||
| } | ||
|
|
||
| if heartbeat.NextHeartbeatExpectedBefore != nil && heartbeat.NextHeartbeatExpectedBefore.Before(*heartbeat.Timestamp) { | ||
| return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Cannot expect the timestamp of the next heartbeat before the timestamp of the new heartbeat") | ||
| } | ||
|
|
||
| r.heartbeats[heartbeatKey{locality: heartbeat.Locality, source: heartbeat.Source}] = heartbeat | ||
| return nil | ||
| } | ||
|
|
||
| func (r *repo) GetDSSAirspaceRepresentationID(_ context.Context) (string, error) { | ||
| return "", stacktrace.NewErrorWithCode(dsserr.NotImplemented, "GetDSSAirspaceRepresentationID not implementable for memstore") | ||
| } | ||
This file contains hidden or 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,125 @@ | ||
| package memstore | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| auxmodels "github.com/interuss/dss/pkg/aux_/models" | ||
| dsserr "github.com/interuss/dss/pkg/errors" | ||
| "github.com/interuss/stacktrace" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSaveOwnMetadataValidation(t *testing.T) { | ||
| ctx := context.Background() | ||
| r := newRepo() | ||
|
|
||
| require.Equal(t, dsserr.BadRequest, stacktrace.GetCode(r.SaveOwnMetadata(ctx, "", "https://example.com"))) | ||
| require.Equal(t, dsserr.BadRequest, stacktrace.GetCode(r.SaveOwnMetadata(ctx, "dss-1", ""))) | ||
| } | ||
|
|
||
| func TestSaveOwnMetadataRoundTrip(t *testing.T) { | ||
| ctx := context.Background() | ||
| r := newRepo() | ||
|
|
||
| require.NoError(t, r.SaveOwnMetadata(ctx, "dss-1", "https://example.com")) | ||
|
|
||
| md, err := r.GetDSSMetadata(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Len(t, md, 1) | ||
| require.Equal(t, "dss-1", md[0].Locality) | ||
| require.Equal(t, "https://example.com", md[0].PublicEndpoint) | ||
| require.NotNil(t, md[0].UpdatedAt) | ||
|
|
||
| // No heartbeat recorded yet. | ||
| require.False(t, md[0].LatestTimestamp.Source.Valid) | ||
| require.Nil(t, md[0].LatestTimestamp.Timestamp) | ||
| } | ||
|
|
||
| func TestSaveOwnMetadataUpsert(t *testing.T) { | ||
| ctx := context.Background() | ||
| r := newRepo() | ||
|
|
||
| require.NoError(t, r.SaveOwnMetadata(ctx, "dss-1", "https://old.example.com")) | ||
| require.NoError(t, r.SaveOwnMetadata(ctx, "dss-1", "https://new.example.com")) | ||
|
|
||
| md, err := r.GetDSSMetadata(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Len(t, md, 1) | ||
| require.Equal(t, "https://new.example.com", md[0].PublicEndpoint) | ||
| } | ||
|
|
||
| func TestRecordHeartbeatValidation(t *testing.T) { | ||
| ctx := context.Background() | ||
| r := newRepo() | ||
|
|
||
| require.Equal(t, dsserr.BadRequest, stacktrace.GetCode(r.RecordHeartbeat(ctx, auxmodels.Heartbeat{Source: "source1"}))) | ||
| require.Equal(t, dsserr.BadRequest, stacktrace.GetCode(r.RecordHeartbeat(ctx, auxmodels.Heartbeat{Locality: "dss-1"}))) | ||
|
|
||
| ts := time.Now() | ||
| before := ts.Add(-time.Minute) | ||
| err := r.RecordHeartbeat(ctx, auxmodels.Heartbeat{ | ||
| Locality: "dss-1", | ||
| Source: "source1", | ||
| Timestamp: &ts, | ||
| NextHeartbeatExpectedBefore: &before, | ||
| }) | ||
|
|
||
| require.Equal(t, dsserr.BadRequest, stacktrace.GetCode(err)) | ||
| } | ||
|
|
||
| func TestRecordHeartbeatDefaultsTimestamp(t *testing.T) { | ||
| ctx := context.Background() | ||
| r := newRepo() | ||
|
|
||
| require.NoError(t, r.SaveOwnMetadata(ctx, "dss-1", "https://example.com")) | ||
| require.NoError(t, r.RecordHeartbeat(ctx, auxmodels.Heartbeat{Locality: "dss-1", Source: "source1"})) | ||
|
|
||
| md, err := r.GetDSSMetadata(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Len(t, md, 1) | ||
| require.True(t, md[0].LatestTimestamp.Source.Valid) | ||
| require.NotNil(t, md[0].LatestTimestamp.Timestamp) | ||
| } | ||
|
|
||
| func TestGetDSSMetadataPicksLatestHeartbeat(t *testing.T) { | ||
| ctx := context.Background() | ||
| r := newRepo() | ||
|
|
||
| require.NoError(t, r.SaveOwnMetadata(ctx, "dss-1", "https://example.com")) | ||
|
|
||
| older := time.Now().Add(-time.Hour) | ||
| newer := time.Now() | ||
| require.NoError(t, r.RecordHeartbeat(ctx, auxmodels.Heartbeat{Locality: "dss-1", Source: "source1", Timestamp: &older, Reporter: "uss1"})) | ||
| require.NoError(t, r.RecordHeartbeat(ctx, auxmodels.Heartbeat{Locality: "dss-1", Source: "source2", Timestamp: &newer, Reporter: "uss2"})) | ||
|
|
||
| md, err := r.GetDSSMetadata(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Len(t, md, 1) | ||
| require.True(t, md[0].LatestTimestamp.Timestamp.Equal(newer)) | ||
| require.Equal(t, "source2", md[0].LatestTimestamp.Source.String) | ||
| require.Equal(t, "uss2", md[0].LatestTimestamp.Reporter.String) | ||
| } | ||
|
|
||
| func TestGetDSSMetadataUpdatesHeartbeatPerSource(t *testing.T) { | ||
| ctx := context.Background() | ||
| r := newRepo() | ||
|
|
||
| require.NoError(t, r.SaveOwnMetadata(ctx, "dss-1", "https://example.com")) | ||
|
|
||
| first := time.Now().Add(-time.Hour) | ||
| second := time.Now() | ||
| require.NoError(t, r.RecordHeartbeat(ctx, auxmodels.Heartbeat{Locality: "dss-1", Source: "source1", Timestamp: &first})) | ||
| require.NoError(t, r.RecordHeartbeat(ctx, auxmodels.Heartbeat{Locality: "dss-1", Source: "source1", Timestamp: &second})) | ||
|
|
||
| md, err := r.GetDSSMetadata(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Len(t, md, 1) | ||
| require.True(t, md[0].LatestTimestamp.Timestamp.Equal(second)) | ||
| } |
This file contains hidden or 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,42 @@ | ||
| package memstore | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| auxmodels "github.com/interuss/dss/pkg/aux_/models" | ||
| "github.com/interuss/dss/pkg/aux_/repos" | ||
| "github.com/interuss/dss/pkg/memstore" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| // repo is a full implementation of aux_.repos.Repository for memory-based storage. | ||
| type repo struct { | ||
| // participants holds pool participants metadata, keyed by locality. | ||
| participants map[string]*participant | ||
| // heartbeats holds the latest heartbeat per (locality, source). | ||
| heartbeats map[heartbeatKey]auxmodels.Heartbeat | ||
| } | ||
|
|
||
| type participant struct { | ||
| publicEndpoint string | ||
| updatedAt time.Time | ||
| } | ||
|
|
||
| type heartbeatKey struct { | ||
| locality string | ||
| source string | ||
| } | ||
|
|
||
| func newRepo() *repo { | ||
| return &repo{ | ||
| participants: map[string]*participant{}, | ||
| heartbeats: map[heartbeatKey]auxmodels.Heartbeat{}, | ||
| } | ||
| } | ||
|
|
||
| func Init(ctx context.Context, logger *zap.Logger) (*memstore.Store[repos.Repository], error) { | ||
| return memstore.Init(ctx, logger, "aux_", newRepo()) | ||
| } | ||
|
|
||
| func (r *repo) GetRepo() repos.Repository { return r } |
This file contains hidden or 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,29 @@ | ||
| package params | ||
|
|
||
| import ( | ||
| "flag" | ||
|
|
||
| raftparams "github.com/interuss/dss/pkg/raftstore/params" | ||
| "github.com/interuss/stacktrace" | ||
| ) | ||
|
|
||
| const peersFlag = "aux_raft_peers" | ||
|
|
||
| var peers string | ||
|
|
||
| func init() { | ||
| flag.StringVar(&peers, peersFlag, "", `Comma-separated "nodeID=peerURL" pairs for the aux store, e.g. "1=http://node1:9031,2=http://node2:9031,3=http://node3:9031"`) | ||
| } | ||
|
|
||
| func GetConnectParameters() (raftparams.ConnectParameters, error) { | ||
| if peers == "" { | ||
| return raftparams.ConnectParameters{}, stacktrace.NewError("--%s is required", peersFlag) | ||
| } | ||
|
|
||
| p, err := raftparams.GetConnectParameters("aux") | ||
| if err != nil { | ||
| return raftparams.ConnectParameters{}, err | ||
| } | ||
| p.Peers = peers | ||
| return p, nil | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.