2018-02-23 02:16:43 +03:00
|
|
|
package eph
|
|
|
|
|
2018-03-22 01:12:54 +03:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE
|
|
|
|
|
2018-02-23 02:16:43 +03:00
|
|
|
import (
|
|
|
|
"context"
|
2018-04-20 22:21:15 +03:00
|
|
|
"encoding/json"
|
2018-03-17 01:06:25 +03:00
|
|
|
"io"
|
2018-02-23 02:16:43 +03:00
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2018-03-13 04:20:51 +03:00
|
|
|
// StoreProvisioner provides CRUD functionality for Lease and Checkpoint storage
|
|
|
|
StoreProvisioner interface {
|
2018-02-23 02:16:43 +03:00
|
|
|
StoreExists(ctx context.Context) (bool, error)
|
|
|
|
EnsureStore(ctx context.Context) error
|
|
|
|
DeleteStore(ctx context.Context) error
|
2018-03-13 04:20:51 +03:00
|
|
|
}
|
|
|
|
|
2018-03-17 01:06:25 +03:00
|
|
|
// EventProcessHostSetter provides the ability to set an EventHostProcessor on the implementor
|
|
|
|
EventProcessHostSetter interface {
|
|
|
|
SetEventHostProcessor(eph *EventProcessorHost)
|
|
|
|
}
|
|
|
|
|
2018-03-13 04:20:51 +03:00
|
|
|
// Leaser provides the functionality needed to persist and coordinate leases for partitions
|
|
|
|
Leaser interface {
|
2018-03-17 01:06:25 +03:00
|
|
|
io.Closer
|
2018-03-13 04:20:51 +03:00
|
|
|
StoreProvisioner
|
2018-03-17 01:06:25 +03:00
|
|
|
EventProcessHostSetter
|
2018-03-09 20:02:49 +03:00
|
|
|
GetLeases(ctx context.Context) ([]LeaseMarker, error)
|
|
|
|
EnsureLease(ctx context.Context, partitionID string) (LeaseMarker, error)
|
|
|
|
DeleteLease(ctx context.Context, partitionID string) error
|
|
|
|
AcquireLease(ctx context.Context, partitionID string) (LeaseMarker, bool, error)
|
|
|
|
RenewLease(ctx context.Context, partitionID string) (LeaseMarker, bool, error)
|
|
|
|
ReleaseLease(ctx context.Context, partitionID string) (bool, error)
|
|
|
|
UpdateLease(ctx context.Context, partitionID string) (LeaseMarker, 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-13 04:20:51 +03:00
|
|
|
PartitionID string `json:"partitionID"`
|
|
|
|
Epoch int64 `json:"epoch"`
|
|
|
|
Owner string `json:"owner"`
|
2018-03-09 20:02:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// LeaseMarker provides the functionality expected of a partition lease with an owner
|
|
|
|
LeaseMarker interface {
|
|
|
|
GetPartitionID() string
|
2018-03-13 04:20:51 +03:00
|
|
|
IsExpired(context.Context) bool
|
2018-03-09 20:02:49 +03:00
|
|
|
GetOwner() string
|
|
|
|
IncrementEpoch() int64
|
2018-03-15 18:22:59 +03:00
|
|
|
GetEpoch() int64
|
2018-04-20 22:21:15 +03:00
|
|
|
String() string
|
2018-02-23 02:16:43 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-03-09 20:02:49 +03:00
|
|
|
// GetPartitionID returns the partition which belongs to this lease
|
|
|
|
func (l *Lease) GetPartitionID() string {
|
|
|
|
return l.PartitionID
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOwner returns the owner of the lease
|
|
|
|
func (l *Lease) GetOwner() string {
|
|
|
|
return l.Owner
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-03-13 04:20:51 +03:00
|
|
|
return atomic.AddInt64(&l.Epoch, 1)
|
2018-03-08 01:35:27 +03:00
|
|
|
}
|
2018-03-15 18:22:59 +03:00
|
|
|
|
|
|
|
// GetEpoch returns the value of the epoch
|
|
|
|
func (l *Lease) GetEpoch() int64 {
|
|
|
|
return l.Epoch
|
|
|
|
}
|
2018-04-20 22:21:15 +03:00
|
|
|
|
|
|
|
func (l *Lease) String() string {
|
|
|
|
bytes, _ := json.Marshal(l)
|
|
|
|
return string(bytes)
|
|
|
|
}
|