Skip to content

Commit

Permalink
feat: add orchestration (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored and flemzord committed May 12, 2023
1 parent 4ea669f commit 6aa5da8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
8 changes: 0 additions & 8 deletions pkg/core/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ type RunScript struct {
Metadata Metadata `json:"metadata"`
}

func (s *RunScript) WithDefaultValues() {
if s.Timestamp.IsZero() {
s.Timestamp = Now()
} else {
s.Timestamp = s.Timestamp.UTC().Round(DatePrecision)
}
}

type Script struct {
Plain string `json:"plain"`
Vars map[string]json.RawMessage `json:"vars" swaggertype:"object"`
Expand Down
4 changes: 3 additions & 1 deletion pkg/ledger/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (r *Runner) Execute(
return nil, nil, ErrNoScript
}

reserve, err := r.state.Reserve(ctx, state.ReserveRequest{
reserve, ts, err := r.state.Reserve(ctx, state.ReserveRequest{
Timestamp: script.Timestamp,
Reference: script.Reference,
})
Expand All @@ -65,6 +65,8 @@ func (r *Runner) Execute(
}
defer reserve.Clear(nil)

script.Timestamp = *ts

transaction, logHolder, err := r.execute(ctx, script, logComputer, dryRun)
if err != nil {
return nil, nil, err
Expand Down
13 changes: 9 additions & 4 deletions pkg/ledger/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,22 @@ func (s *State) checkConstraints(ctx context.Context, r ReserveRequest) error {
return nil
}

func (s *State) Reserve(ctx context.Context, r ReserveRequest) (*Reserve, error) {
func (s *State) Reserve(ctx context.Context, r ReserveRequest) (*Reserve, *core.Time, error) {
s.mu.Lock()
defer s.mu.Unlock()

if err := s.checkConstraints(ctx, r); err != nil {
return nil, err
return nil, nil, err
}

ts := r.Timestamp
if ts.IsZero() {
ts = core.Now()
}

ret := &inFlight{
reference: r.Reference,
timestamp: r.Timestamp,
timestamp: ts,
}
s.inFlights[ret] = struct{}{}
if r.Reference != "" {
Expand All @@ -101,7 +106,7 @@ func (s *State) Reserve(ctx context.Context, r ReserveRequest) (*Reserve, error)
return &Reserve{
inFlight: ret,
state: s,
}, nil
}, &ts, nil
}

func (s *State) GetMoreRecentTransactionDate() core.Time {
Expand Down
16 changes: 8 additions & 8 deletions pkg/ledger/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestState(t *testing.T) {
state := New(NoOpStore, false, core.Now().Add(-10*time.Second))
reserve, err := state.Reserve(context.Background(), ReserveRequest{
reserve, _, err := state.Reserve(context.Background(), ReserveRequest{
Timestamp: core.Now(),
})
require.NoError(t, err)
Expand All @@ -22,13 +22,13 @@ func TestState(t *testing.T) {
func TestStateInsertInPastWithNotAllowedPastTimestamp(t *testing.T) {
state := New(NoOpStore, false, core.Now().Add(-10*time.Second))
now := core.Now()
reserve1, err := state.Reserve(context.Background(), ReserveRequest{
reserve1, _, err := state.Reserve(context.Background(), ReserveRequest{
Timestamp: now,
})
require.NoError(t, err)
defer reserve1.Clear(nil)

_, err = state.Reserve(context.Background(), ReserveRequest{
_, _, err = state.Reserve(context.Background(), ReserveRequest{
Timestamp: now.Add(-time.Second),
})
require.Error(t, err)
Expand All @@ -38,13 +38,13 @@ func TestStateInsertInPastWithNotAllowedPastTimestamp(t *testing.T) {
func TestStateInsertInPastWithAllowPastTimestamps(t *testing.T) {
state := New(NoOpStore, true, core.Now().Add(-10*time.Second))
now := core.Now()
reserve1, err := state.Reserve(context.Background(), ReserveRequest{
reserve1, _, err := state.Reserve(context.Background(), ReserveRequest{
Timestamp: now,
})
require.NoError(t, err)
defer reserve1.Clear(nil)

reserve2, err := state.Reserve(context.Background(), ReserveRequest{
reserve2, _, err := state.Reserve(context.Background(), ReserveRequest{
Timestamp: now.Add(-time.Second),
})
require.NoError(t, err)
Expand All @@ -55,12 +55,12 @@ func TestStateWithError(t *testing.T) {
state := New(NoOpStore, false, core.Now().Add(-10*time.Second))
now := core.Now()

_, err := state.Reserve(context.Background(), ReserveRequest{
_, _, err := state.Reserve(context.Background(), ReserveRequest{
Timestamp: now,
})
require.NoError(t, err)

_, err = state.Reserve(context.Background(), ReserveRequest{
_, _, err = state.Reserve(context.Background(), ReserveRequest{
Timestamp: now.Add(-10 * time.Millisecond),
})
require.Error(t, err)
Expand All @@ -74,7 +74,7 @@ func BenchmarkState(b *testing.B) {
eg := errgroup.Group{}
for i := 0; i < b.N; i++ {
eg.Go(func() error {
reserve, err := state.Reserve(context.Background(), ReserveRequest{
reserve, _, err := state.Reserve(context.Background(), ReserveRequest{
Timestamp: now,
})
require.NoError(b, err)
Expand Down

0 comments on commit 6aa5da8

Please sign in to comment.