2023-01-20 18:51:15 +00:00
|
|
|
// Package bitseq provides a structure and utilities for representing a long
|
|
|
|
// bitmask which is persisted in a datastore. It is backed by [bitmap.Bitmap]
|
|
|
|
// which operates directly on the encoded representation, without uncompressing.
|
2015-06-12 19:03:33 +00:00
|
|
|
package bitseq
|
|
|
|
|
|
|
|
import (
|
2015-10-05 21:53:25 +00:00
|
|
|
"encoding/json"
|
2015-06-12 19:03:33 +00:00
|
|
|
"fmt"
|
2015-06-14 17:21:59 +00:00
|
|
|
"sync"
|
2015-06-12 22:06:42 +00:00
|
|
|
|
2023-01-20 18:51:15 +00:00
|
|
|
"github.com/docker/docker/libnetwork/bitmap"
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2015-06-12 19:03:33 +00:00
|
|
|
)
|
|
|
|
|
2015-06-24 19:10:54 +00:00
|
|
|
var (
|
2015-10-21 00:05:01 +00:00
|
|
|
// ErrNoBitAvailable is returned when no more bits are available to set
|
2023-01-20 18:51:15 +00:00
|
|
|
ErrNoBitAvailable = bitmap.ErrNoBitAvailable
|
2015-10-21 00:05:01 +00:00
|
|
|
// ErrBitAllocated is returned when the specific bit requested is already set
|
2023-01-20 18:51:15 +00:00
|
|
|
ErrBitAllocated = bitmap.ErrBitAllocated
|
2015-06-24 19:10:54 +00:00
|
|
|
)
|
|
|
|
|
2018-09-07 03:43:42 +00:00
|
|
|
// Handle contains the sequence representing the bitmask and its identifier
|
2015-06-13 20:35:43 +00:00
|
|
|
type Handle struct {
|
2023-01-20 18:51:15 +00:00
|
|
|
app string
|
|
|
|
id string
|
|
|
|
dbIndex uint64
|
|
|
|
dbExists bool
|
|
|
|
store datastore.DataStore
|
|
|
|
bm *bitmap.Bitmap
|
2023-01-20 18:54:26 +00:00
|
|
|
mu sync.Mutex
|
2015-06-13 20:35:43 +00:00
|
|
|
}
|
|
|
|
|
2015-06-14 17:21:59 +00:00
|
|
|
// NewHandle returns a thread-safe instance of the bitmask handler
|
2015-10-09 03:04:13 +00:00
|
|
|
func NewHandle(app string, ds datastore.DataStore, id string, numElements uint64) (*Handle, error) {
|
2015-06-15 18:43:02 +00:00
|
|
|
h := &Handle{
|
2023-01-20 18:51:15 +00:00
|
|
|
bm: bitmap.New(numElements),
|
|
|
|
app: app,
|
|
|
|
id: id,
|
|
|
|
store: ds,
|
2015-06-13 20:35:43 +00:00
|
|
|
}
|
2015-06-16 01:28:00 +00:00
|
|
|
|
|
|
|
if h.store == nil {
|
2015-06-16 21:46:51 +00:00
|
|
|
return h, nil
|
2015-06-16 01:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the initial status from the ds if present.
|
2015-07-04 23:48:09 +00:00
|
|
|
if err := h.store.GetObject(datastore.Key(h.Key()...), h); err != nil && err != datastore.ErrKeyNotFound {
|
2015-06-18 22:13:38 +00:00
|
|
|
return nil, err
|
2015-06-16 01:28:00 +00:00
|
|
|
}
|
2015-06-18 19:06:11 +00:00
|
|
|
|
2015-10-04 03:23:26 +00: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 23:48:09 +00:00
|
|
|
return h, nil
|
2015-06-13 20:35:43 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 19:10:54 +00:00
|
|
|
func (h *Handle) getCopy() *Handle {
|
|
|
|
return &Handle{
|
2023-01-20 18:51:15 +00:00
|
|
|
bm: bitmap.Copy(h.bm),
|
|
|
|
app: h.app,
|
|
|
|
id: h.id,
|
|
|
|
dbIndex: h.dbIndex,
|
|
|
|
dbExists: h.dbExists,
|
|
|
|
store: h.store,
|
2015-06-24 19:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 22:16:21 +00:00
|
|
|
// SetAnyInRange atomically sets the first unset bit in the specified range in the sequence and returns the corresponding ordinal
|
2017-06-01 02:41:21 +00:00
|
|
|
func (h *Handle) SetAnyInRange(start, end uint64, serial bool) (uint64, error) {
|
2023-01-20 18:51:15 +00:00
|
|
|
return h.apply(func(b *bitmap.Bitmap) (uint64, error) { return b.SetAnyInRange(start, end, serial) })
|
2015-09-17 22:16:21 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 19:10:54 +00:00
|
|
|
// SetAny atomically sets the first unset bit in the sequence and returns the corresponding ordinal
|
2017-06-01 02:41:21 +00:00
|
|
|
func (h *Handle) SetAny(serial bool) (uint64, error) {
|
2023-01-20 18:51:15 +00:00
|
|
|
return h.apply(func(b *bitmap.Bitmap) (uint64, error) { return b.SetAny(serial) })
|
2015-06-24 19:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set atomically sets the corresponding bit in the sequence
|
2015-10-09 03:04:13 +00:00
|
|
|
func (h *Handle) Set(ordinal uint64) error {
|
2023-01-20 18:51:15 +00:00
|
|
|
_, err := h.apply(func(b *bitmap.Bitmap) (uint64, error) { return 0, b.Set(ordinal) })
|
2015-06-24 19:10:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unset atomically unsets the corresponding bit in the sequence
|
2015-10-09 03:04:13 +00:00
|
|
|
func (h *Handle) Unset(ordinal uint64) error {
|
2023-01-20 18:51:15 +00:00
|
|
|
_, err := h.apply(func(b *bitmap.Bitmap) (uint64, error) { return 0, b.Unset(ordinal) })
|
2015-06-24 19:10:54 +00: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-09 03:04:13 +00:00
|
|
|
func (h *Handle) IsSet(ordinal uint64) bool {
|
2023-01-20 18:54:26 +00:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
2023-01-20 18:51:15 +00:00
|
|
|
return h.bm.IsSet(ordinal)
|
2016-01-11 21:03:52 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 19:10:54 +00:00
|
|
|
// set/reset the bit
|
2023-01-20 18:51:15 +00:00
|
|
|
func (h *Handle) apply(op func(*bitmap.Bitmap) (uint64, error)) (uint64, error) {
|
2015-06-24 19:10:54 +00:00
|
|
|
for {
|
2015-11-30 21:18:52 +00:00
|
|
|
var store datastore.DataStore
|
2023-01-20 18:54:26 +00:00
|
|
|
h.mu.Lock()
|
2015-11-30 21:18:52 +00:00
|
|
|
store = h.store
|
|
|
|
if store != nil {
|
2023-01-20 18:54:26 +00:00
|
|
|
h.mu.Unlock() // The lock is acquired in the GetObject
|
2015-11-30 21:18:52 +00:00
|
|
|
if err := store.GetObject(datastore.Key(h.Key()...), h); err != nil && err != datastore.ErrKeyNotFound {
|
2023-01-20 18:51:15 +00:00
|
|
|
return 0, err
|
2015-10-05 11:24:44 +00:00
|
|
|
}
|
2023-01-20 18:54:26 +00:00
|
|
|
h.mu.Lock() // Acquire the lock back
|
2015-10-05 11:24:44 +00:00
|
|
|
}
|
2015-06-24 19:10:54 +00:00
|
|
|
|
2015-09-05 01:15:54 +00:00
|
|
|
// Create a private copy of h and work on it
|
2015-06-24 19:10:54 +00:00
|
|
|
nh := h.getCopy()
|
|
|
|
|
2023-01-20 18:51:15 +00:00
|
|
|
ret, err := op(nh.bm)
|
|
|
|
if err != nil {
|
2023-01-20 18:54:26 +00:00
|
|
|
h.mu.Unlock()
|
2023-01-20 18:51:15 +00:00
|
|
|
return ret, err
|
2015-06-24 19:10:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 18:34:40 +00:00
|
|
|
if h.store != nil {
|
2023-01-20 18:54:26 +00:00
|
|
|
h.mu.Unlock()
|
2018-03-08 18:34:40 +00:00
|
|
|
// 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-06-24 19:10:54 +00:00
|
|
|
}
|
2023-01-20 18:54:26 +00:00
|
|
|
h.mu.Lock()
|
2015-06-24 19:10:54 +00:00
|
|
|
}
|
|
|
|
|
2017-05-22 02:25:52 +00:00
|
|
|
// Previous atomic push was successful. Save private copy to local copy
|
2023-01-20 18:51:15 +00:00
|
|
|
h.bm = nh.bm
|
2015-06-24 19:10:54 +00:00
|
|
|
h.dbExists = nh.dbExists
|
|
|
|
h.dbIndex = nh.dbIndex
|
2023-01-20 18:54:26 +00:00
|
|
|
h.mu.Unlock()
|
2015-06-24 19:10:54 +00:00
|
|
|
return ret, nil
|
2015-06-16 01:28:00 +00:00
|
|
|
}
|
2015-06-24 19:10:54 +00:00
|
|
|
}
|
2015-06-16 01:28:00 +00:00
|
|
|
|
2015-06-16 21:46:51 +00:00
|
|
|
// Destroy removes from the datastore the data belonging to this handle
|
2015-09-22 20:20:55 +00:00
|
|
|
func (h *Handle) Destroy() error {
|
2015-10-03 23:11:50 +00: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 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bits returns the length of the bit sequence
|
2015-10-09 03:04:13 +00:00
|
|
|
func (h *Handle) Bits() uint64 {
|
2023-01-20 18:54:26 +00:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
2023-01-20 18:51:15 +00:00
|
|
|
return h.bm.Bits()
|
2015-06-16 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unselected returns the number of bits which are not selected
|
2015-10-09 03:04:13 +00:00
|
|
|
func (h *Handle) Unselected() uint64 {
|
2023-01-20 18:54:26 +00:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
2023-01-20 18:51:15 +00:00
|
|
|
return h.bm.Unselected()
|
2015-06-16 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 22:02:08 +00:00
|
|
|
func (h *Handle) String() string {
|
2023-01-20 18:54:26 +00:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
2023-01-20 18:51:15 +00:00
|
|
|
return fmt.Sprintf("App: %s, ID: %s, DBIndex: 0x%x, %s",
|
|
|
|
h.app, h.id, h.dbIndex, h.bm)
|
2015-06-24 22:02:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 18:55:41 +00:00
|
|
|
type jsonMessage struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Sequence *bitmap.Bitmap `json:"sequence"`
|
|
|
|
}
|
|
|
|
|
2023-01-20 18:51:15 +00:00
|
|
|
// MarshalJSON encodes h into a JSON message.
|
2015-10-05 21:53:25 +00:00
|
|
|
func (h *Handle) MarshalJSON() ([]byte, error) {
|
2023-01-20 18:55:41 +00:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
m := jsonMessage{ID: h.id, Sequence: h.bm}
|
2015-10-05 21:53:25 +00:00
|
|
|
return json.Marshal(m)
|
|
|
|
}
|
|
|
|
|
2023-01-20 18:51:15 +00:00
|
|
|
// UnmarshalJSON decodes a JSON message into h.
|
2015-10-05 21:53:25 +00:00
|
|
|
func (h *Handle) UnmarshalJSON(data []byte) error {
|
2023-01-20 18:55:41 +00:00
|
|
|
var m jsonMessage
|
|
|
|
if err := json.Unmarshal(data, &m); err != nil {
|
2015-10-05 21:53:25 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-06-12 19:03:33 +00:00
|
|
|
|
2023-01-20 18:54:26 +00:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
2023-01-20 18:55:41 +00:00
|
|
|
h.id, h.bm = m.ID, m.Sequence
|
2023-01-20 18:51:15 +00:00
|
|
|
return nil
|
2015-06-24 19:10:54 +00:00
|
|
|
}
|