Move leases to meta.Data

pull/5994/head
Jason Wilder 2016-03-02 12:38:50 -07:00 committed by Ben Johnson
parent c972c717b6
commit 31a964e890
3 changed files with 51 additions and 46 deletions

View File

@ -64,8 +64,11 @@ type Handler struct {
Ping(checkAllMetaServers bool) error
}
QueryAuthorizer *meta.QueryAuthorizer
QueryExecutor influxql.QueryExecutor
QueryAuthorizer interface {
AuthorizeQuery(u *meta.UserInfo, query *influxql.Query, database string) error
}
QueryExecutor influxql.QueryExecutor
PointsWriter interface {
WritePoints(p *cluster.WritePointsRequest) error

View File

@ -1,8 +1,10 @@
package meta
import (
"errors"
"fmt"
"sort"
"sync"
"time"
"github.com/gogo/protobuf/proto"
@ -1524,6 +1526,50 @@ func (ui *UserInfo) unmarshal(pb *internal.UserInfo) {
}
}
type Lease struct {
Name string `json:"name"`
Expiration time.Time `json:"expiration"`
Owner uint64 `json:"owner"`
}
type Leases struct {
mu sync.Mutex
m map[string]*Lease
d time.Duration
}
func NewLeases(d time.Duration) *Leases {
return &Leases{
m: make(map[string]*Lease),
d: d,
}
}
func (leases *Leases) Acquire(name string, nodeID uint64) (*Lease, error) {
leases.mu.Lock()
defer leases.mu.Unlock()
l, ok := leases.m[name]
if ok {
if time.Now().After(l.Expiration) || l.Owner == nodeID {
l.Expiration = time.Now().Add(leases.d)
l.Owner = nodeID
return l, nil
}
return l, errors.New("another node has the lease")
}
l = &Lease{
Name: name,
Expiration: time.Now().Add(leases.d),
Owner: nodeID,
}
leases.m[name] = l
return l, nil
}
// MarshalTime converts t to nanoseconds since epoch. A zero time returns 0.
func MarshalTime(t time.Time) int64 {
if t.IsZero() {

View File

@ -479,47 +479,3 @@ func (h *handler) httpError(err error, w http.ResponseWriter, status int) {
}
http.Error(w, "", status)
}
type Lease struct {
Name string `json:"name"`
Expiration time.Time `json:"expiration"`
Owner uint64 `json:"owner"`
}
type Leases struct {
mu sync.Mutex
m map[string]*Lease
d time.Duration
}
func NewLeases(d time.Duration) *Leases {
return &Leases{
m: make(map[string]*Lease),
d: d,
}
}
func (leases *Leases) Acquire(name string, nodeID uint64) (*Lease, error) {
leases.mu.Lock()
defer leases.mu.Unlock()
l, ok := leases.m[name]
if ok {
if time.Now().After(l.Expiration) || l.Owner == nodeID {
l.Expiration = time.Now().Add(leases.d)
l.Owner = nodeID
return l, nil
}
return l, errors.New("another node has the lease")
}
l = &Lease{
Name: name,
Expiration: time.Now().Add(leases.d),
Owner: nodeID,
}
leases.m[name] = l
return l, nil
}