Skip to content

Commit

Permalink
Merge pull request #10 from golemcloud/fixes
Browse files Browse the repository at this point in the history
transaction fixes and cleanups
  • Loading branch information
noise64 authored Aug 22, 2024
2 parents 75de0d8 + 1a1ece5 commit 0e0b7bd
Show file tree
Hide file tree
Showing 12 changed files with 364 additions and 62 deletions.
8 changes: 4 additions & 4 deletions golemhost/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (

type ComponentID uuid.UUID

func newComponentID(componentID binding.GolemApi0_2_0_HostComponentId) ComponentID {
return ComponentID(newUUID(componentID.Uuid))
func NewComponentID(componentID binding.GolemApi0_2_0_HostComponentId) ComponentID {
return ComponentID(NewUUID(componentID.Uuid))
}

func (componentID ComponentID) toBinding() binding.GolemApi0_2_0_HostComponentId {
func (componentID ComponentID) ToBinding() binding.GolemApi0_2_0_HostComponentId {
return binding.GolemApi0_2_0_HostComponentId{
Uuid: uuidToBinding(uuid.UUID(componentID)),
Uuid: UUIDToBinding(uuid.UUID(componentID)),
}
}
177 changes: 177 additions & 0 deletions golemhost/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package golemhost

import (
"fmt"
"time"

"github.com/golemcloud/golem-go/binding"
)

type FilterComparator int

const (
FilterComparatorEqual FilterComparator = iota
FilterComparatorNotEqual
FilterComparatorGreaterEqual
FilterComparatorGreater
FilterComparatorLessEqual
FilterComparatorLess
)

func (filterComparator FilterComparator) ToBinding() binding.GolemApi0_2_0_HostFilterComparator {
switch filterComparator {
case FilterComparatorEqual:
return binding.GolemApi0_2_0_HostFilterComparatorEqual()
case FilterComparatorNotEqual:
return binding.GolemApi0_2_0_HostFilterComparatorNotEqual()
case FilterComparatorGreaterEqual:
return binding.GolemApi0_2_0_HostFilterComparatorGreaterEqual()
case FilterComparatorGreater:
return binding.GolemApi0_2_0_HostFilterComparatorGreater()
case FilterComparatorLessEqual:
return binding.GolemApi0_2_0_HostFilterComparatorLessEqual()
case FilterComparatorLess:
return binding.GolemApi0_2_0_HostFilterComparatorLess()
default:
panic(fmt.Sprintf("ToBinding: unhandled filterComparator: %d", filterComparator))
}
}

type StringFilterComparator int

const (
StringFilterComparatorEqual StringFilterComparator = iota
StringFilterComparatorNotEqual
StringFilterComparatorLike
StringFilterComparatorNotLike
)

func (stringFilterComparator StringFilterComparator) ToBinding() binding.GolemApi0_2_0_HostStringFilterComparator {
switch stringFilterComparator {
case StringFilterComparatorEqual:
return binding.GolemApi0_2_0_HostStringFilterComparatorEqual()
case StringFilterComparatorNotEqual:
return binding.GolemApi0_2_0_HostStringFilterComparatorNotEqual()
case StringFilterComparatorLike:
return binding.GolemApi0_2_0_HostStringFilterComparatorLike()
case StringFilterComparatorNotLike:
return binding.GolemApi0_2_0_HostStringFilterComparatorNotLike()
default:
panic(fmt.Sprintf("ToBinding: unhandled stringFilterComparator: %d", stringFilterComparator))
}
}

type WorkerAnyFilter struct {
Filters []WorkerAllFilter
}

func (f WorkerAnyFilter) ToBinding() binding.GolemApi0_2_0_HostWorkerAnyFilter {
filter := binding.GolemApi0_2_0_HostWorkerAnyFilter{
Filters: make([]binding.GolemApi0_2_0_HostWorkerAllFilter, len(f.Filters)),
}
for i := range f.Filters {
filter.Filters[i] = f.Filters[i].ToBinding()
}
return filter
}

type WorkerAllFilter struct {
Filters []WorkerFilter
}

func (f WorkerAllFilter) ToBinding() binding.GolemApi0_2_0_HostWorkerAllFilter {
filter := binding.GolemApi0_2_0_HostWorkerAllFilter{}
for i := range f.Filters {
filter.Filters = append(filter.Filters, f.Filters[i].ToBinding()...)
}
return filter
}

type WorkerEnvFilter struct {
Name string
Value string
}

type WorkerFilter struct {
Name *string
NameComparator StringFilterComparator

Status *WorkerStatus
StatusComparator FilterComparator

Version *uint64
VersionComparator FilterComparator

CreatedAt *time.Time
CreatedAtComparator FilterComparator

Env *WorkerEnvFilter
EnvComparator StringFilterComparator
}

func (f WorkerFilter) ToBinding() []binding.GolemApi0_2_0_HostWorkerPropertyFilter {
var filter []binding.GolemApi0_2_0_HostWorkerPropertyFilter

if f.Name != nil {
filter = append(
filter,
binding.GolemApi0_2_0_HostWorkerPropertyFilterName(
binding.GolemApi0_2_0_HostWorkerNameFilter{
Comparator: f.NameComparator.ToBinding(),
Value: *f.Name,
},
),
)
}

if f.Status != nil {
filter = append(
filter,
binding.GolemApi0_2_0_HostWorkerPropertyFilterStatus(
binding.GolemApi0_2_0_HostWorkerStatusFilter{
Comparator: f.StatusComparator.ToBinding(),
Value: f.Status.ToBinding(),
},
),
)
}

if f.Version != nil {
filter = append(
filter,
binding.GolemApi0_2_0_HostWorkerPropertyFilterVersion(
binding.GolemApi0_2_0_HostWorkerVersionFilter{
Comparator: f.VersionComparator.ToBinding(),
Value: *f.Version,
},
),
)
}

if f.CreatedAt != nil {
filter = append(
filter,
binding.GolemApi0_2_0_HostWorkerPropertyFilterCreatedAt(
binding.GolemApi0_2_0_HostWorkerCreatedAtFilter{
Comparator: f.CreatedAtComparator.ToBinding(),
Value: uint64(f.CreatedAt.UnixNano()),
},
),
)
}

if f.Env != nil {
filter = append(
filter,
binding.GolemApi0_2_0_HostWorkerPropertyFilterEnv(
binding.GolemApi0_2_0_HostWorkerEnvFilter{
Comparator: f.EnvComparator.ToBinding(),
Name: f.Env.Name,
Value: f.Env.Value,
},
),
)
}

return filter
}
12 changes: 6 additions & 6 deletions golemhost/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
PersistenceLevelSmart
)

func newPersistenceLevel(level binding.GolemApi0_2_0_HostPersistenceLevel) PersistenceLevel {
func NewPersistenceLevel(level binding.GolemApi0_2_0_HostPersistenceLevel) PersistenceLevel {
switch level.Kind() {
case binding.GolemApi0_2_0_HostPersistenceLevelKindPersistRemoteSideEffects:
return PersistenceLevelPersistRemoteSideEffects
Expand All @@ -23,11 +23,11 @@ func newPersistenceLevel(level binding.GolemApi0_2_0_HostPersistenceLevel) Persi
case binding.GolemApi0_2_0_HostPersistenceLevelKindSmart:
return PersistenceLevelSmart
default:
panic(fmt.Sprintf("newPersistenceLevel: unhandled persistence level: %d", level))
panic(fmt.Sprintf("NewPersistenceLevel: unhandled persistence level: %d", level))
}
}

func (level PersistenceLevel) toBinding() binding.GolemApi0_2_0_HostPersistenceLevel {
func (level PersistenceLevel) ToBinding() binding.GolemApi0_2_0_HostPersistenceLevel {
switch level {
case PersistenceLevelPersistNothing:
return binding.GolemApi0_2_0_HostPersistenceLevelPersistNothing()
Expand All @@ -36,16 +36,16 @@ func (level PersistenceLevel) toBinding() binding.GolemApi0_2_0_HostPersistenceL
case PersistenceLevelSmart:
return binding.GolemApi0_2_0_HostPersistenceLevelSmart()
default:
panic(fmt.Sprintf("toBinding: unhandled persistence level: %d", level))
panic(fmt.Sprintf("ToBinding: unhandled persistence level: %d", level))
}
}

func SetPersistenceLevel(level PersistenceLevel) {
binding.GolemApi0_2_0_HostSetOplogPersistenceLevel(level.toBinding())
binding.GolemApi0_2_0_HostSetOplogPersistenceLevel(level.ToBinding())
}

func GetPersistenceLevel() PersistenceLevel {
return newPersistenceLevel(binding.GolemApi0_2_0_HostGetOplogPersistenceLevel())
return NewPersistenceLevel(binding.GolemApi0_2_0_HostGetOplogPersistenceLevel())
}

func WithPersistenceLevel[T any](level PersistenceLevel, f func() (T, error)) (T, error) {
Expand Down
12 changes: 6 additions & 6 deletions golemhost/promise.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@ type PromiseID struct {
func NewPromise() PromiseID {
promise := binding.GolemApi0_2_0_HostCreatePromise()
return PromiseID{
WorkerID: newWorkerID(promise.WorkerId),
WorkerID: NewWorkerID(promise.WorkerId),
OplogIdx: OpLogIndex(promise.OplogIdx),
}
}

func (promiseID PromiseID) toBinding() binding.GolemApi0_2_0_HostPromiseId {
func (promiseID PromiseID) ToBinding() binding.GolemApi0_2_0_HostPromiseId {
return binding.GolemApi0_2_0_HostPromiseId{
WorkerId: promiseID.WorkerID.toBinding(),
WorkerId: promiseID.WorkerID.ToBinding(),
OplogIdx: binding.GolemApi0_2_0_HostOplogIndex(promiseID.OplogIdx),
}
}

func DeletePromise(promiseID PromiseID) {
binding.GolemApi0_2_0_HostDeletePromise(promiseID.toBinding())
binding.GolemApi0_2_0_HostDeletePromise(promiseID.ToBinding())
}

func AwaitPromise(promiseID PromiseID) []byte {
return binding.GolemApi0_2_0_HostAwaitPromise(promiseID.toBinding())
return binding.GolemApi0_2_0_HostAwaitPromise(promiseID.ToBinding())
}

func AwaitPromiseJSON(promiseID PromiseID, v any) error {
return json.Unmarshal(AwaitPromise(promiseID), v)
}

func CompletePromise(promiseID PromiseID, payload []byte) bool {
return binding.GolemApi0_2_0_HostCompletePromise(promiseID.toBinding(), payload)
return binding.GolemApi0_2_0_HostCompletePromise(promiseID.ToBinding(), payload)
}

func CompletePromiseJSON(promiseID PromiseID, v any) (bool, error) {
Expand Down
8 changes: 4 additions & 4 deletions golemhost/retrypolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type RetryPolicy struct {
Multiplier float64
}

func newRetryPolicy(policy binding.GolemApi0_2_0_HostRetryPolicy) RetryPolicy {
func NewRetryPolicy(policy binding.GolemApi0_2_0_HostRetryPolicy) RetryPolicy {
return RetryPolicy{
MaxAttempts: policy.MaxAttempts,
MinDelay: time.Duration(policy.MinDelay) * time.Nanosecond,
Expand All @@ -22,7 +22,7 @@ func newRetryPolicy(policy binding.GolemApi0_2_0_HostRetryPolicy) RetryPolicy {
}
}

func (policy RetryPolicy) toBinding() binding.GolemApi0_2_0_HostRetryPolicy {
func (policy RetryPolicy) ToBinding() binding.GolemApi0_2_0_HostRetryPolicy {
return binding.GolemApi0_2_0_HostRetryPolicy{
MaxAttempts: policy.MaxAttempts,
MinDelay: binding.GolemApi0_2_0_HostDuration(policy.MinDelay.Nanoseconds()),
Expand All @@ -32,11 +32,11 @@ func (policy RetryPolicy) toBinding() binding.GolemApi0_2_0_HostRetryPolicy {
}

func GetRetryPolicy() RetryPolicy {
return newRetryPolicy(binding.GolemApi0_2_0_HostGetRetryPolicy())
return NewRetryPolicy(binding.GolemApi0_2_0_HostGetRetryPolicy())
}

func SetRetryPolicy(policy RetryPolicy) {
binding.GolemApi0_2_0_HostSetRetryPolicy(policy.toBinding())
binding.GolemApi0_2_0_HostSetRetryPolicy(policy.ToBinding())
}

func WithRetryPolicy[T any](policy RetryPolicy, f func() (T, error)) (T, error) {
Expand Down
32 changes: 16 additions & 16 deletions golemhost/transaction/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,55 @@ package transaction
import "fmt"

type FailedAndRolledBackPartiallyError struct {
StepIndex uint
StepError error
ExecuteIndex uint
ExecuteError error
CompensationIndex uint
CompensationError error
}

func (e *FailedAndRolledBackPartiallyError) Error() string {
return fmt.Sprintf(
"fallible transaction failed and rolled back partially, step (%d) error: %s, compensation (%d) error: %s",
e.StepIndex,
e.StepError.Error(),
"fallible transaction failed and rolled back partially, execute (%d) error: %s, compensation (%d) error: %s",
e.ExecuteIndex,
e.ExecuteError.Error(),
e.CompensationIndex,
e.CompensationError.Error(),
)
}

func (e *FailedAndRolledBackPartiallyError) Unwrap() []error {
return []error{e.StepError, e.CompensationError}
return []error{e.ExecuteError, e.CompensationError}
}

type FailedAndRolledBackCompletelyError struct {
StepIndex uint
StepError error
ExecuteIndex uint
ExecuteError error
}

func (e *FailedAndRolledBackCompletelyError) Error() string {
return fmt.Sprintf(
"fallible transaction failed and rolled back completely, step (%d) error: %s",
e.StepIndex,
e.StepError.Error(),
"fallible transaction failed and rolled back completely, execute (%d) error: %s",
e.ExecuteIndex,
e.ExecuteError.Error(),
)
}

func (e *FailedAndRolledBackCompletelyError) Unwrap() error {
return e.StepError
return e.ExecuteError
}

type CannotExecuteStepInFailedTransactionError struct {
type CannotExecuteInFailedTransactionError struct {
OriginalError error
}

func (e *CannotExecuteStepInFailedTransactionError) Error() string {
func (e *CannotExecuteInFailedTransactionError) Error() string {
return fmt.Sprintf(
"cannot execute step in failed transaction, original error: %s",
"cannot execute in failed transaction, original error: %s",
e.OriginalError.Error(),
)
}

func (e *CannotExecuteStepInFailedTransactionError) Unwrap() error {
func (e *CannotExecuteInFailedTransactionError) Unwrap() error {
return e.OriginalError
}

Expand Down
Loading

0 comments on commit 0e0b7bd

Please sign in to comment.