2015-06-12 15:03:33 -04:00
|
|
|
// Package bitseq provides a structure and utilities for representing long bitmask
|
2016-11-28 13:44:45 -05:00
|
|
|
// as sequence of run-length encoded blocks. It operates directly on the encoded
|
2015-06-12 15:03:33 -04:00
|
|
|
// representation, it does not decode/encode.
|
|
|
|
package bitseq
|
|
|
|
|
|
|
|
import (
|
2015-06-18 22:14:03 -04:00
|
|
|
"encoding/binary"
|
2015-10-05 17:53:25 -04:00
|
|
|
"encoding/json"
|
2016-11-21 20:29:53 -05:00
|
|
|
"errors"
|
2015-06-12 15:03:33 -04:00
|
|
|
"fmt"
|
2015-06-14 13:21:59 -04:00
|
|
|
"sync"
|
2015-06-12 18:06:42 -04:00
|
|
|
|
2015-06-15 14:43:02 -04:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-06-24 15:10:54 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-06-12 15:03:33 -04:00
|
|
|
)
|
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
// block sequence constants
|
2015-06-12 15:03:33 -04:00
|
|
|
// If needed we can think of making these configurable
|
|
|
|
const (
|
2015-06-24 13:04:34 -04:00
|
|
|
blockLen = uint32(32)
|
2015-10-08 23:04:13 -04:00
|
|
|
blockBytes = uint64(blockLen / 8)
|
2015-06-24 13:04:34 -04:00
|
|
|
blockMAX = uint32(1<<blockLen - 1)
|
|
|
|
blockFirstBit = uint32(1) << (blockLen - 1)
|
2015-10-08 23:04:13 -04:00
|
|
|
invalidPos = uint64(0xFFFFFFFFFFFFFFFF)
|
2015-06-12 15:03:33 -04:00
|
|
|
)
|
|
|
|
|
2015-06-24 15:10:54 -04:00
|
|
|
var (
|
2015-10-20 20:05:01 -04:00
|
|
|
// ErrNoBitAvailable is returned when no more bits are available to set
|
2016-11-21 20:29:53 -05:00
|
|
|
ErrNoBitAvailable = errors.New("no bit available")
|
2015-10-20 20:05:01 -04:00
|
|
|
// ErrBitAllocated is returned when the specific bit requested is already set
|
2016-11-21 20:29:53 -05:00
|
|
|
ErrBitAllocated = errors.New("requested bit is already allocated")
|
2015-06-24 15:10:54 -04:00
|
|
|
)
|
|
|
|
|
2015-06-13 16:35:43 -04:00
|
|
|
// Handle contains the sequece representing the bitmask and its identifier
|
|
|
|
type Handle struct {
|
2015-10-08 23:04:13 -04:00
|
|
|
bits uint64
|
|
|
|
unselected uint64
|
2015-06-24 11:52:53 -04:00
|
|
|
head *sequence
|
2015-06-16 17:46:51 -04:00
|
|
|
app string
|
|
|
|
id string
|
|
|
|
dbIndex uint64
|
2015-06-18 18:13:38 -04:00
|
|
|
dbExists bool
|
2017-05-31 16:33:51 -04:00
|
|
|
curr uint64
|
2015-06-16 17:46:51 -04:00
|
|
|
store datastore.DataStore
|
2015-06-14 13:21:59 -04:00
|
|
|
sync.Mutex
|
2015-06-13 16:35:43 -04:00
|
|
|
}
|
|
|
|
|
2015-06-14 13:21:59 -04:00
|
|
|
// NewHandle returns a thread-safe instance of the bitmask handler
|
2015-10-08 23:04:13 -04:00
|
|
|
func NewHandle(app string, ds datastore.DataStore, id string, numElements uint64) (*Handle, error) {
|
2015-06-15 14:43:02 -04:00
|
|
|
h := &Handle{
|
2015-06-16 17:46:51 -04:00
|
|
|
app: app,
|
|
|
|
id: id,
|
|
|
|
store: ds,
|
|
|
|
bits: numElements,
|
|
|
|
unselected: numElements,
|
2015-06-24 11:52:53 -04:00
|
|
|
head: &sequence{
|
|
|
|
block: 0x0,
|
|
|
|
count: getNumBlocks(numElements),
|
2015-06-13 16:35:43 -04:00
|
|
|
},
|
|
|
|
}
|
2015-06-15 21:28:00 -04:00
|
|
|
|
|
|
|
if h.store == nil {
|
2015-06-16 17:46:51 -04:00
|
|
|
return h, nil
|
2015-06-15 21:28:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the initial status from the ds if present.
|
2015-07-04 19:48:09 -04:00
|
|
|
if err := h.store.GetObject(datastore.Key(h.Key()...), h); err != nil && err != datastore.ErrKeyNotFound {
|
2015-06-18 18:13:38 -04:00
|
|
|
return nil, err
|
2015-06-15 21:28:00 -04:00
|
|
|
}
|
2015-06-18 15:06:11 -04:00
|
|
|
|
2015-10-03 23:23:26 -04:00
|
|
|
// If the handle is not in store, write it.
|
|
|
|
if !h.Exists() {
|
|
|
|
if err := h.writeToStore(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to write bitsequence to store: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-04 19:48:09 -04:00
|
|
|
return h, nil
|
2015-06-13 16:35:43 -04:00
|
|
|
}
|
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
// sequence represents a recurring sequence of 32 bits long bitmasks
|
|
|
|
type sequence struct {
|
|
|
|
block uint32 // block is a symbol representing 4 byte long allocation bitmask
|
2015-10-08 23:04:13 -04:00
|
|
|
count uint64 // number of consecutive blocks (symbols)
|
2015-06-24 11:52:53 -04:00
|
|
|
next *sequence // next sequence
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string representation of the block sequence starting from this block
|
2015-06-24 11:52:53 -04:00
|
|
|
func (s *sequence) toString() string {
|
2015-06-12 15:03:33 -04:00
|
|
|
var nextBlock string
|
2015-06-24 11:52:53 -04:00
|
|
|
if s.next == nil {
|
2015-06-12 15:03:33 -04:00
|
|
|
nextBlock = "end"
|
|
|
|
} else {
|
2015-06-24 11:52:53 -04:00
|
|
|
nextBlock = s.next.toString()
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
2015-06-24 11:52:53 -04:00
|
|
|
return fmt.Sprintf("(0x%x, %d)->%s", s.block, s.count, nextBlock)
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAvailableBit returns the position of the first unset bit in the bitmask represented by this sequence
|
2015-10-08 23:04:13 -04:00
|
|
|
func (s *sequence) getAvailableBit(from uint64) (uint64, uint64, error) {
|
2015-06-24 11:52:53 -04:00
|
|
|
if s.block == blockMAX || s.count == 0 {
|
2015-10-20 20:05:01 -04:00
|
|
|
return invalidPos, invalidPos, ErrNoBitAvailable
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
2015-09-17 18:16:21 -04:00
|
|
|
bits := from
|
|
|
|
bitSel := blockFirstBit >> from
|
2015-06-24 11:52:53 -04:00
|
|
|
for bitSel > 0 && s.block&bitSel != 0 {
|
2015-06-12 15:03:33 -04:00
|
|
|
bitSel >>= 1
|
|
|
|
bits++
|
|
|
|
}
|
2015-06-24 13:04:34 -04:00
|
|
|
return bits / 8, bits % 8, nil
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
|
2015-06-15 21:28:00 -04:00
|
|
|
// GetCopy returns a copy of the linked list rooted at this node
|
2015-06-24 11:52:53 -04:00
|
|
|
func (s *sequence) getCopy() *sequence {
|
|
|
|
n := &sequence{block: s.block, count: s.count}
|
2015-06-15 21:28:00 -04:00
|
|
|
pn := n
|
2015-06-24 11:52:53 -04:00
|
|
|
ps := s.next
|
2015-06-15 21:28:00 -04:00
|
|
|
for ps != nil {
|
2015-06-24 11:52:53 -04:00
|
|
|
pn.next = &sequence{block: ps.block, count: ps.count}
|
|
|
|
pn = pn.next
|
|
|
|
ps = ps.next
|
2015-06-15 21:28:00 -04:00
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2015-06-12 15:03:33 -04:00
|
|
|
// Equal checks if this sequence is equal to the passed one
|
2015-06-24 11:52:53 -04:00
|
|
|
func (s *sequence) equal(o *sequence) bool {
|
2015-06-12 15:03:33 -04:00
|
|
|
this := s
|
|
|
|
other := o
|
|
|
|
for this != nil {
|
|
|
|
if other == nil {
|
|
|
|
return false
|
|
|
|
}
|
2015-06-24 11:52:53 -04:00
|
|
|
if this.block != other.block || this.count != other.count {
|
2015-06-12 15:03:33 -04:00
|
|
|
return false
|
|
|
|
}
|
2015-06-24 11:52:53 -04:00
|
|
|
this = this.next
|
|
|
|
other = other.next
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
// Check if other is longer than this
|
|
|
|
if other != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-06-12 18:06:42 -04:00
|
|
|
// ToByteArray converts the sequence into a byte array
|
2015-06-24 11:52:53 -04:00
|
|
|
func (s *sequence) toByteArray() ([]byte, error) {
|
2015-06-12 18:06:42 -04:00
|
|
|
var bb []byte
|
|
|
|
|
|
|
|
p := s
|
|
|
|
for p != nil {
|
2015-10-08 23:04:13 -04:00
|
|
|
b := make([]byte, 12)
|
2015-06-24 11:52:53 -04:00
|
|
|
binary.BigEndian.PutUint32(b[0:], p.block)
|
2015-10-08 23:04:13 -04:00
|
|
|
binary.BigEndian.PutUint64(b[4:], p.count)
|
2015-06-18 22:14:03 -04:00
|
|
|
bb = append(bb, b...)
|
2015-06-24 11:52:53 -04:00
|
|
|
p = p.next
|
2015-06-12 18:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return bb, nil
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:10:54 -04:00
|
|
|
// fromByteArray construct the sequence from the byte array
|
2015-06-24 11:52:53 -04:00
|
|
|
func (s *sequence) fromByteArray(data []byte) error {
|
2015-06-12 18:06:42 -04:00
|
|
|
l := len(data)
|
2015-10-08 23:04:13 -04:00
|
|
|
if l%12 != 0 {
|
2016-02-23 16:44:38 -05:00
|
|
|
return fmt.Errorf("cannot deserialize byte sequence of length %d (%v)", l, data)
|
2015-06-12 18:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p := s
|
|
|
|
i := 0
|
|
|
|
for {
|
2015-06-24 11:52:53 -04:00
|
|
|
p.block = binary.BigEndian.Uint32(data[i : i+4])
|
2015-10-08 23:04:13 -04:00
|
|
|
p.count = binary.BigEndian.Uint64(data[i+4 : i+12])
|
|
|
|
i += 12
|
2015-06-12 18:06:42 -04:00
|
|
|
if i == l {
|
|
|
|
break
|
|
|
|
}
|
2015-06-24 11:52:53 -04:00
|
|
|
p.next = &sequence{}
|
|
|
|
p = p.next
|
2015-06-12 18:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:10:54 -04:00
|
|
|
func (h *Handle) getCopy() *Handle {
|
|
|
|
return &Handle{
|
|
|
|
bits: h.bits,
|
|
|
|
unselected: h.unselected,
|
|
|
|
head: h.head.getCopy(),
|
|
|
|
app: h.app,
|
|
|
|
id: h.id,
|
|
|
|
dbIndex: h.dbIndex,
|
|
|
|
dbExists: h.dbExists,
|
|
|
|
store: h.store,
|
2017-05-31 16:33:51 -04:00
|
|
|
curr: h.curr,
|
2015-06-24 15:10:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 18:16:21 -04:00
|
|
|
// SetAnyInRange atomically sets the first unset bit in the specified range in the sequence and returns the corresponding ordinal
|
2015-10-08 23:04:13 -04:00
|
|
|
func (h *Handle) SetAnyInRange(start, end uint64) (uint64, error) {
|
2016-09-13 14:52:12 -04:00
|
|
|
if end < start || end >= h.bits {
|
2015-09-17 18:16:21 -04:00
|
|
|
return invalidPos, fmt.Errorf("invalid bit range [%d, %d]", start, end)
|
|
|
|
}
|
|
|
|
if h.Unselected() == 0 {
|
2015-10-20 20:05:01 -04:00
|
|
|
return invalidPos, ErrNoBitAvailable
|
2015-09-17 18:16:21 -04:00
|
|
|
}
|
|
|
|
return h.set(0, start, end, true, false)
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:10:54 -04:00
|
|
|
// SetAny atomically sets the first unset bit in the sequence and returns the corresponding ordinal
|
2015-10-08 23:04:13 -04:00
|
|
|
func (h *Handle) SetAny() (uint64, error) {
|
2015-06-24 15:10:54 -04:00
|
|
|
if h.Unselected() == 0 {
|
2015-10-20 20:05:01 -04:00
|
|
|
return invalidPos, ErrNoBitAvailable
|
2015-06-24 15:10:54 -04:00
|
|
|
}
|
2015-09-17 18:16:21 -04:00
|
|
|
return h.set(0, 0, h.bits-1, true, false)
|
2015-06-24 15:10:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set atomically sets the corresponding bit in the sequence
|
2015-10-08 23:04:13 -04:00
|
|
|
func (h *Handle) Set(ordinal uint64) error {
|
2015-06-24 15:10:54 -04:00
|
|
|
if err := h.validateOrdinal(ordinal); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-17 18:16:21 -04:00
|
|
|
_, err := h.set(ordinal, 0, 0, false, false)
|
2015-06-24 15:10:54 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unset atomically unsets the corresponding bit in the sequence
|
2015-10-08 23:04:13 -04:00
|
|
|
func (h *Handle) Unset(ordinal uint64) error {
|
2015-06-24 15:10:54 -04:00
|
|
|
if err := h.validateOrdinal(ordinal); err != nil {
|
|
|
|
return err
|
2015-06-18 18:13:38 -04:00
|
|
|
}
|
2015-09-17 18:16:21 -04:00
|
|
|
_, err := h.set(ordinal, 0, 0, false, true)
|
2015-06-24 15:10:54 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSet atomically checks if the ordinal bit is set. In case ordinal
|
|
|
|
// is outside of the bit sequence limits, false is returned.
|
2015-10-08 23:04:13 -04:00
|
|
|
func (h *Handle) IsSet(ordinal uint64) bool {
|
2015-06-24 15:10:54 -04:00
|
|
|
if err := h.validateOrdinal(ordinal); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
h.Lock()
|
|
|
|
_, _, err := checkIfAvailable(h.head, ordinal)
|
2015-06-15 14:43:02 -04:00
|
|
|
h.Unlock()
|
2015-06-24 15:10:54 -04:00
|
|
|
return err != nil
|
|
|
|
}
|
2015-06-15 21:28:00 -04:00
|
|
|
|
2016-01-11 16:03:52 -05:00
|
|
|
func (h *Handle) runConsistencyCheck() bool {
|
|
|
|
corrupted := false
|
|
|
|
for p, c := h.head, h.head.next; c != nil; c = c.next {
|
|
|
|
if c.count == 0 {
|
|
|
|
corrupted = true
|
|
|
|
p.next = c.next
|
|
|
|
continue // keep same p
|
|
|
|
}
|
|
|
|
p = c
|
|
|
|
}
|
|
|
|
return corrupted
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckConsistency checks if the bit sequence is in an inconsistent state and attempts to fix it.
|
|
|
|
// It looks for a corruption signature that may happen in docker 1.9.0 and 1.9.1.
|
|
|
|
func (h *Handle) CheckConsistency() error {
|
|
|
|
for {
|
|
|
|
h.Lock()
|
|
|
|
store := h.store
|
|
|
|
h.Unlock()
|
|
|
|
|
|
|
|
if store != nil {
|
|
|
|
if err := store.GetObject(datastore.Key(h.Key()...), h); err != nil && err != datastore.ErrKeyNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
nh := h.getCopy()
|
|
|
|
h.Unlock()
|
|
|
|
|
|
|
|
if !nh.runConsistencyCheck() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nh.writeToStore(); err != nil {
|
|
|
|
if _, ok := err.(types.RetryError); !ok {
|
|
|
|
return fmt.Errorf("internal failure while fixing inconsistent bitsequence: %v", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Infof("Fixed inconsistent bit sequence in datastore:\n%s\n%s", h, nh)
|
2016-01-11 16:03:52 -05:00
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
h.head = nh.head
|
|
|
|
h.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:10:54 -04:00
|
|
|
// set/reset the bit
|
2015-10-08 23:04:13 -04:00
|
|
|
func (h *Handle) set(ordinal, start, end uint64, any bool, release bool) (uint64, error) {
|
2015-06-24 15:10:54 -04:00
|
|
|
var (
|
2015-10-08 23:04:13 -04:00
|
|
|
bitPos uint64
|
|
|
|
bytePos uint64
|
|
|
|
ret uint64
|
2015-06-24 15:10:54 -04:00
|
|
|
err error
|
|
|
|
)
|
2015-06-15 21:28:00 -04:00
|
|
|
|
2015-06-24 15:10:54 -04:00
|
|
|
for {
|
2015-11-30 16:18:52 -05:00
|
|
|
var store datastore.DataStore
|
|
|
|
h.Lock()
|
|
|
|
store = h.store
|
|
|
|
h.Unlock()
|
|
|
|
if store != nil {
|
|
|
|
if err := store.GetObject(datastore.Key(h.Key()...), h); err != nil && err != datastore.ErrKeyNotFound {
|
2015-10-05 07:24:44 -04:00
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 21:28:00 -04:00
|
|
|
h.Lock()
|
2015-06-24 15:10:54 -04:00
|
|
|
// Get position if available
|
2015-06-16 17:46:51 -04:00
|
|
|
if release {
|
2015-06-24 15:10:54 -04:00
|
|
|
bytePos, bitPos = ordinalToPos(ordinal)
|
2015-06-16 17:46:51 -04:00
|
|
|
} else {
|
2015-06-24 15:10:54 -04:00
|
|
|
if any {
|
2017-05-31 16:33:51 -04:00
|
|
|
bytePos, bitPos, err = getAvailableFromCurrent(h.head, start, h.curr, end)
|
2015-06-24 15:10:54 -04:00
|
|
|
ret = posToOrdinal(bytePos, bitPos)
|
2017-05-31 16:33:51 -04:00
|
|
|
if err == nil {
|
|
|
|
h.curr = ret + 1
|
2015-09-17 18:16:21 -04:00
|
|
|
}
|
2015-06-24 15:10:54 -04:00
|
|
|
} else {
|
|
|
|
bytePos, bitPos, err = checkIfAvailable(h.head, ordinal)
|
|
|
|
ret = ordinal
|
|
|
|
}
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
2015-06-24 15:10:54 -04:00
|
|
|
if err != nil {
|
|
|
|
h.Unlock()
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2015-09-04 21:15:54 -04:00
|
|
|
// Create a private copy of h and work on it
|
2015-06-24 15:10:54 -04:00
|
|
|
nh := h.getCopy()
|
2015-06-15 21:28:00 -04:00
|
|
|
h.Unlock()
|
2015-06-24 15:10:54 -04:00
|
|
|
|
|
|
|
nh.head = pushReservation(bytePos, bitPos, nh.head, release)
|
|
|
|
if release {
|
|
|
|
nh.unselected++
|
|
|
|
} else {
|
|
|
|
nh.unselected--
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to write private copy to store
|
|
|
|
if err := nh.writeToStore(); err != nil {
|
|
|
|
if _, ok := err.(types.RetryError); !ok {
|
|
|
|
return ret, fmt.Errorf("internal failure while setting the bit: %v", err)
|
|
|
|
}
|
|
|
|
// Retry
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-04 21:15:54 -04:00
|
|
|
// Previous atomic push was succesfull. Save private copy to local copy
|
2015-06-24 15:10:54 -04:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
h.unselected = nh.unselected
|
|
|
|
h.head = nh.head
|
|
|
|
h.dbExists = nh.dbExists
|
|
|
|
h.dbIndex = nh.dbIndex
|
|
|
|
return ret, nil
|
2015-06-15 21:28:00 -04:00
|
|
|
}
|
2015-06-24 15:10:54 -04:00
|
|
|
}
|
2015-06-15 21:28:00 -04:00
|
|
|
|
2015-06-24 15:10:54 -04:00
|
|
|
// checks is needed because to cover the case where the number of bits is not a multiple of blockLen
|
2015-10-08 23:04:13 -04:00
|
|
|
func (h *Handle) validateOrdinal(ordinal uint64) error {
|
2016-03-29 14:56:39 -04:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
2015-09-17 18:16:21 -04:00
|
|
|
if ordinal >= h.bits {
|
2016-11-21 20:29:53 -05:00
|
|
|
return errors.New("bit does not belong to the sequence")
|
2015-06-24 15:10:54 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:46:51 -04:00
|
|
|
// Destroy removes from the datastore the data belonging to this handle
|
2015-09-22 16:20:55 -04:00
|
|
|
func (h *Handle) Destroy() error {
|
2015-10-03 19:11:50 -04:00
|
|
|
for {
|
|
|
|
if err := h.deleteFromStore(); err != nil {
|
|
|
|
if _, ok := err.(types.RetryError); !ok {
|
|
|
|
return fmt.Errorf("internal failure while destroying the sequence: %v", err)
|
|
|
|
}
|
|
|
|
// Fetch latest
|
|
|
|
if err := h.store.GetObject(datastore.Key(h.Key()...), h); err != nil {
|
|
|
|
if err == datastore.ErrKeyNotFound { // already removed
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("failed to fetch from store when destroying the sequence: %v", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToByteArray converts this handle's data into a byte array
|
|
|
|
func (h *Handle) ToByteArray() ([]byte, error) {
|
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
2015-10-08 23:04:13 -04:00
|
|
|
ba := make([]byte, 16)
|
|
|
|
binary.BigEndian.PutUint64(ba[0:], h.bits)
|
|
|
|
binary.BigEndian.PutUint64(ba[8:], h.unselected)
|
2015-06-24 11:52:53 -04:00
|
|
|
bm, err := h.head.toByteArray()
|
2015-06-16 17:46:51 -04:00
|
|
|
if err != nil {
|
2015-06-18 15:06:11 -04:00
|
|
|
return nil, fmt.Errorf("failed to serialize head: %s", err.Error())
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
ba = append(ba, bm...)
|
|
|
|
|
|
|
|
return ba, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromByteArray reads his handle's data from a byte array
|
|
|
|
func (h *Handle) FromByteArray(ba []byte) error {
|
2015-06-18 15:06:11 -04:00
|
|
|
if ba == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
return errors.New("nil byte array")
|
2015-06-18 15:06:11 -04:00
|
|
|
}
|
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
nh := &sequence{}
|
2015-10-08 23:04:13 -04:00
|
|
|
err := nh.fromByteArray(ba[16:])
|
2015-06-16 17:46:51 -04:00
|
|
|
if err != nil {
|
2015-06-18 15:06:11 -04:00
|
|
|
return fmt.Errorf("failed to deserialize head: %s", err.Error())
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
h.head = nh
|
2015-10-08 23:04:13 -04:00
|
|
|
h.bits = binary.BigEndian.Uint64(ba[0:8])
|
|
|
|
h.unselected = binary.BigEndian.Uint64(ba[8:16])
|
2015-06-16 17:46:51 -04:00
|
|
|
h.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bits returns the length of the bit sequence
|
2015-10-08 23:04:13 -04:00
|
|
|
func (h *Handle) Bits() uint64 {
|
2015-06-16 17:46:51 -04:00
|
|
|
return h.bits
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unselected returns the number of bits which are not selected
|
2015-10-08 23:04:13 -04:00
|
|
|
func (h *Handle) Unselected() uint64 {
|
2015-06-16 17:46:51 -04:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
return h.unselected
|
|
|
|
}
|
|
|
|
|
2015-06-24 18:02:08 -04:00
|
|
|
func (h *Handle) String() string {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
return fmt.Sprintf("App: %s, ID: %s, DBIndex: 0x%x, bits: %d, unselected: %d, sequence: %s",
|
|
|
|
h.app, h.id, h.dbIndex, h.bits, h.unselected, h.head.toString())
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
// MarshalJSON encodes Handle into json message
|
|
|
|
func (h *Handle) MarshalJSON() ([]byte, error) {
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"id": h.id,
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := h.ToByteArray()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m["sequence"] = b
|
|
|
|
return json.Marshal(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes json message into Handle
|
|
|
|
func (h *Handle) UnmarshalJSON(data []byte) error {
|
|
|
|
var (
|
|
|
|
m map[string]interface{}
|
|
|
|
b []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if err = json.Unmarshal(data, &m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
h.id = m["id"].(string)
|
|
|
|
bi, _ := json.Marshal(m["sequence"])
|
|
|
|
if err := json.Unmarshal(bi, &b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return h.FromByteArray(b)
|
|
|
|
}
|
|
|
|
|
2015-09-17 18:16:21 -04:00
|
|
|
// getFirstAvailable looks for the first unset bit in passed mask starting from start
|
2015-10-08 23:04:13 -04:00
|
|
|
func getFirstAvailable(head *sequence, start uint64) (uint64, uint64, error) {
|
2015-09-17 18:16:21 -04:00
|
|
|
// Find sequence which contains the start bit
|
|
|
|
byteStart, bitStart := ordinalToPos(start)
|
|
|
|
current, _, _, inBlockBytePos := findSequence(head, byteStart)
|
|
|
|
|
|
|
|
// Derive the this sequence offsets
|
|
|
|
byteOffset := byteStart - inBlockBytePos
|
|
|
|
bitOffset := inBlockBytePos*8 + bitStart
|
2017-06-05 01:21:41 -04:00
|
|
|
var firstOffset uint64
|
|
|
|
if current == head {
|
|
|
|
firstOffset = byteOffset
|
|
|
|
}
|
2015-06-12 15:03:33 -04:00
|
|
|
for current != nil {
|
2015-06-24 11:52:53 -04:00
|
|
|
if current.block != blockMAX {
|
2015-09-17 18:16:21 -04:00
|
|
|
bytePos, bitPos, err := current.getAvailableBit(bitOffset)
|
|
|
|
return byteOffset + bytePos, bitPos, err
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
2015-09-17 18:16:21 -04:00
|
|
|
// Moving to next block: Reset bit offset.
|
|
|
|
bitOffset = 0
|
2017-06-05 01:21:41 -04:00
|
|
|
byteOffset += (current.count * blockBytes) - firstOffset
|
|
|
|
firstOffset = 0
|
2015-06-24 11:52:53 -04:00
|
|
|
current = current.next
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
2015-10-20 20:05:01 -04:00
|
|
|
return invalidPos, invalidPos, ErrNoBitAvailable
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
|
2017-05-31 16:33:51 -04:00
|
|
|
//getAvailableFromCurrent will look for available ordinal from the current ordinal.
|
|
|
|
// If none found then it will loop back to the start to check of the available bit.
|
|
|
|
//This can be further optimized to check from start till curr in case of a rollover
|
|
|
|
func getAvailableFromCurrent(head *sequence, start, curr, end uint64) (uint64, uint64, error) {
|
|
|
|
var bytePos, bitPos uint64
|
|
|
|
if curr != 0 && curr > start {
|
|
|
|
bytePos, bitPos, _ = getFirstAvailable(head, curr)
|
|
|
|
ret := posToOrdinal(bytePos, bitPos)
|
|
|
|
if end < ret {
|
|
|
|
goto begin
|
|
|
|
}
|
|
|
|
return bytePos, bitPos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
begin:
|
|
|
|
bytePos, bitPos, _ = getFirstAvailable(head, start)
|
|
|
|
ret := posToOrdinal(bytePos, bitPos)
|
|
|
|
if end < ret {
|
|
|
|
return invalidPos, invalidPos, ErrNoBitAvailable
|
|
|
|
}
|
|
|
|
return bytePos, bitPos, nil
|
|
|
|
}
|
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
// checkIfAvailable checks if the bit correspondent to the specified ordinal is unset
|
|
|
|
// If the ordinal is beyond the sequence limits, a negative response is returned
|
2015-10-08 23:04:13 -04:00
|
|
|
func checkIfAvailable(head *sequence, ordinal uint64) (uint64, uint64, error) {
|
2015-09-17 18:16:21 -04:00
|
|
|
bytePos, bitPos := ordinalToPos(ordinal)
|
2015-06-12 15:03:33 -04:00
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
// Find the sequence containing this byte
|
2015-06-12 15:03:33 -04:00
|
|
|
current, _, _, inBlockBytePos := findSequence(head, bytePos)
|
|
|
|
if current != nil {
|
|
|
|
// Check whether the bit corresponding to the ordinal address is unset
|
2015-06-24 13:04:34 -04:00
|
|
|
bitSel := blockFirstBit >> (inBlockBytePos*8 + bitPos)
|
2015-06-24 11:52:53 -04:00
|
|
|
if current.block&bitSel == 0 {
|
2015-06-13 19:04:06 -04:00
|
|
|
return bytePos, bitPos, nil
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 20:05:01 -04:00
|
|
|
return invalidPos, invalidPos, ErrBitAllocated
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Given the byte position and the sequences list head, return the pointer to the
|
|
|
|
// sequence containing the byte (current), the pointer to the previous sequence,
|
|
|
|
// the number of blocks preceding the block containing the byte inside the current sequence.
|
2015-06-24 13:04:34 -04:00
|
|
|
// If bytePos is outside of the list, function will return (nil, nil, 0, invalidPos)
|
2015-10-08 23:04:13 -04:00
|
|
|
func findSequence(head *sequence, bytePos uint64) (*sequence, *sequence, uint64, uint64) {
|
2015-06-24 11:52:53 -04:00
|
|
|
// Find the sequence containing this byte
|
2015-06-12 15:03:33 -04:00
|
|
|
previous := head
|
|
|
|
current := head
|
|
|
|
n := bytePos
|
2015-06-24 13:04:34 -04:00
|
|
|
for current.next != nil && n >= (current.count*blockBytes) { // Nil check for less than 32 addresses masks
|
|
|
|
n -= (current.count * blockBytes)
|
2015-06-12 15:03:33 -04:00
|
|
|
previous = current
|
2015-06-24 11:52:53 -04:00
|
|
|
current = current.next
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If byte is outside of the list, let caller know
|
2015-06-24 13:04:34 -04:00
|
|
|
if n >= (current.count * blockBytes) {
|
|
|
|
return nil, nil, 0, invalidPos
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the byte position inside the block and the number of blocks
|
|
|
|
// preceding the block containing the byte inside this sequence
|
2015-06-24 13:04:34 -04:00
|
|
|
precBlocks := n / blockBytes
|
2015-06-12 15:03:33 -04:00
|
|
|
inBlockBytePos := bytePos % blockBytes
|
|
|
|
|
|
|
|
return current, previous, precBlocks, inBlockBytePos
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushReservation pushes the bit reservation inside the bitmask.
|
|
|
|
// Given byte and bit positions, identify the sequence (current) which holds the block containing the affected bit.
|
|
|
|
// Create a new block with the modified bit according to the operation (allocate/release).
|
2015-06-24 11:52:53 -04:00
|
|
|
// Create a new sequence containing the new block and insert it in the proper position.
|
2015-06-12 15:03:33 -04:00
|
|
|
// Remove current sequence if empty.
|
2015-06-24 11:52:53 -04:00
|
|
|
// Check if new sequence can be merged with neighbour (previous/next) sequences.
|
2015-06-12 15:03:33 -04:00
|
|
|
//
|
|
|
|
//
|
2015-06-24 11:52:53 -04:00
|
|
|
// Identify "current" sequence containing block:
|
|
|
|
// [prev seq] [current seq] [next seq]
|
2015-06-12 15:03:33 -04:00
|
|
|
//
|
|
|
|
// Based on block position, resulting list of sequences can be any of three forms:
|
|
|
|
//
|
2015-06-24 11:52:53 -04:00
|
|
|
// block position Resulting list of sequences
|
|
|
|
// A) block is first in current: [prev seq] [new] [modified current seq] [next seq]
|
|
|
|
// B) block is last in current: [prev seq] [modified current seq] [new] [next seq]
|
|
|
|
// C) block is in the middle of current: [prev seq] [curr pre] [new] [curr post] [next seq]
|
2015-10-08 23:04:13 -04:00
|
|
|
func pushReservation(bytePos, bitPos uint64, head *sequence, release bool) *sequence {
|
2015-06-12 15:03:33 -04:00
|
|
|
// Store list's head
|
|
|
|
newHead := head
|
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
// Find the sequence containing this byte
|
2015-06-12 15:03:33 -04:00
|
|
|
current, previous, precBlocks, inBlockBytePos := findSequence(head, bytePos)
|
|
|
|
if current == nil {
|
|
|
|
return newHead
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct updated block
|
2015-06-24 15:10:54 -04:00
|
|
|
bitSel := blockFirstBit >> (inBlockBytePos*8 + bitPos)
|
2015-06-24 11:52:53 -04:00
|
|
|
newBlock := current.block
|
2015-06-12 15:03:33 -04:00
|
|
|
if release {
|
|
|
|
newBlock &^= bitSel
|
|
|
|
} else {
|
|
|
|
newBlock |= bitSel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Quit if it was a redundant request
|
2015-06-24 11:52:53 -04:00
|
|
|
if current.block == newBlock {
|
2015-06-12 15:03:33 -04:00
|
|
|
return newHead
|
|
|
|
}
|
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
// Current sequence inevitably looses one block, upadate count
|
|
|
|
current.count--
|
2015-06-12 15:03:33 -04:00
|
|
|
|
|
|
|
// Create new sequence
|
2015-06-24 11:52:53 -04:00
|
|
|
newSequence := &sequence{block: newBlock, count: 1}
|
2015-06-12 15:03:33 -04:00
|
|
|
|
|
|
|
// Insert the new sequence in the list based on block position
|
|
|
|
if precBlocks == 0 { // First in sequence (A)
|
2015-06-24 11:52:53 -04:00
|
|
|
newSequence.next = current
|
2015-06-12 15:03:33 -04:00
|
|
|
if current == head {
|
|
|
|
newHead = newSequence
|
|
|
|
previous = newHead
|
|
|
|
} else {
|
2015-06-24 11:52:53 -04:00
|
|
|
previous.next = newSequence
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
removeCurrentIfEmpty(&newHead, newSequence, current)
|
|
|
|
mergeSequences(previous)
|
2015-11-24 00:34:16 -05:00
|
|
|
} else if precBlocks == current.count { // Last in sequence (B)
|
2015-06-24 11:52:53 -04:00
|
|
|
newSequence.next = current.next
|
|
|
|
current.next = newSequence
|
2015-06-12 15:03:33 -04:00
|
|
|
mergeSequences(current)
|
|
|
|
} else { // In between the sequence (C)
|
2015-06-24 11:52:53 -04:00
|
|
|
currPre := &sequence{block: current.block, count: precBlocks, next: newSequence}
|
2015-06-12 15:03:33 -04:00
|
|
|
currPost := current
|
2015-06-24 11:52:53 -04:00
|
|
|
currPost.count -= precBlocks
|
|
|
|
newSequence.next = currPost
|
2015-06-12 15:03:33 -04:00
|
|
|
if currPost == head {
|
|
|
|
newHead = currPre
|
|
|
|
} else {
|
2015-06-24 11:52:53 -04:00
|
|
|
previous.next = currPre
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
// No merging or empty current possible here
|
|
|
|
}
|
|
|
|
|
|
|
|
return newHead
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes the current sequence from the list if empty, adjusting the head pointer if needed
|
2015-06-24 11:52:53 -04:00
|
|
|
func removeCurrentIfEmpty(head **sequence, previous, current *sequence) {
|
|
|
|
if current.count == 0 {
|
2015-06-12 15:03:33 -04:00
|
|
|
if current == *head {
|
2015-06-24 11:52:53 -04:00
|
|
|
*head = current.next
|
2015-06-12 15:03:33 -04:00
|
|
|
} else {
|
2015-06-24 11:52:53 -04:00
|
|
|
previous.next = current.next
|
|
|
|
current = current.next
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
// Given a pointer to a sequence, it checks if it can be merged with any following sequences
|
2015-06-12 15:03:33 -04:00
|
|
|
// It stops when no more merging is possible.
|
|
|
|
// TODO: Optimization: only attempt merge from start to end sequence, no need to scan till the end of the list
|
2015-06-24 11:52:53 -04:00
|
|
|
func mergeSequences(seq *sequence) {
|
2015-06-12 15:03:33 -04:00
|
|
|
if seq != nil {
|
|
|
|
// Merge all what possible from seq
|
2015-06-24 11:52:53 -04:00
|
|
|
for seq.next != nil && seq.block == seq.next.block {
|
|
|
|
seq.count += seq.next.count
|
|
|
|
seq.next = seq.next.next
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
2015-06-24 11:52:53 -04:00
|
|
|
// Move to next
|
|
|
|
mergeSequences(seq.next)
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:04:13 -04:00
|
|
|
func getNumBlocks(numBits uint64) uint64 {
|
|
|
|
numBlocks := numBits / uint64(blockLen)
|
|
|
|
if numBits%uint64(blockLen) != 0 {
|
2015-06-12 15:03:33 -04:00
|
|
|
numBlocks++
|
|
|
|
}
|
|
|
|
return numBlocks
|
|
|
|
}
|
2015-06-24 15:10:54 -04:00
|
|
|
|
2015-10-08 23:04:13 -04:00
|
|
|
func ordinalToPos(ordinal uint64) (uint64, uint64) {
|
2015-06-24 15:10:54 -04:00
|
|
|
return ordinal / 8, ordinal % 8
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:04:13 -04:00
|
|
|
func posToOrdinal(bytePos, bitPos uint64) uint64 {
|
2015-06-24 15:10:54 -04:00
|
|
|
return bytePos*8 + bitPos
|
|
|
|
}
|