2018-02-23 02:16:43 +03:00
|
|
|
package eph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2018-02-24 03:30:54 +03:00
|
|
|
// Leaser provides the functionality needed to persist and coordinate leases for partitions
|
2018-02-23 02:16:43 +03:00
|
|
|
Leaser interface {
|
|
|
|
StoreExists(ctx context.Context) (bool, error)
|
|
|
|
EnsureStore(ctx context.Context) error
|
|
|
|
DeleteStore(ctx context.Context) error
|
2018-03-08 01:35:27 +03:00
|
|
|
GetLeases(ctx context.Context) ([]Lease, error)
|
|
|
|
EnsureLease(ctx context.Context, lease Lease) (Lease, error)
|
|
|
|
DeleteLease(ctx context.Context, lease Lease) error
|
|
|
|
AcquireLease(ctx context.Context, lease Lease) (Lease, bool, error)
|
|
|
|
RenewLease(ctx context.Context, lease Lease) (Lease, bool, error)
|
|
|
|
ReleaseLease(ctx context.Context, lease Lease) (bool, error)
|
|
|
|
UpdateLease(ctx context.Context, lease Lease) (Lease, bool, error)
|
2018-02-23 02:16:43 +03:00
|
|
|
}
|
|
|
|
|
2018-02-24 03:30:54 +03:00
|
|
|
// Lease represents the information needed to coordinate partitions
|
2018-02-23 02:16:43 +03:00
|
|
|
Lease struct {
|
2018-03-08 01:35:27 +03:00
|
|
|
PartitionID string
|
2018-02-23 02:16:43 +03:00
|
|
|
epoch int64
|
2018-03-08 01:35:27 +03:00
|
|
|
Owner string
|
2018-02-23 02:16:43 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-02-24 03:30:54 +03:00
|
|
|
// NewLease constructs a lease given a partitionID
|
2018-02-23 02:16:43 +03:00
|
|
|
func NewLease(partitionID string) *Lease {
|
|
|
|
return &Lease{
|
2018-03-08 01:35:27 +03:00
|
|
|
PartitionID: partitionID,
|
2018-02-23 02:16:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-24 03:30:54 +03:00
|
|
|
// IncrementEpoch increase the time on the lease by one
|
2018-02-23 02:16:43 +03:00
|
|
|
func (l *Lease) IncrementEpoch() int64 {
|
|
|
|
return atomic.AddInt64(&l.epoch, 1)
|
|
|
|
}
|
|
|
|
|
2018-02-24 03:30:54 +03:00
|
|
|
// IsExpired indicates that the lease has expired and is no longer valid
|
2018-02-23 02:16:43 +03:00
|
|
|
func (l *Lease) IsExpired() bool {
|
|
|
|
return false
|
|
|
|
}
|
2018-03-08 01:35:27 +03:00
|
|
|
|
|
|
|
// IsNotOwnedOrExpired indicates that the lease has expired and does not owned by a processor
|
|
|
|
func (l *Lease) IsNotOwnedOrExpired() bool {
|
|
|
|
return l.IsExpired() || l.Owner == ""
|
|
|
|
}
|