|
@@ -6,7 +6,6 @@ import (
|
|
|
|
|
|
"github.com/docker/libnetwork/bitseq"
|
|
"github.com/docker/libnetwork/bitseq"
|
|
"github.com/docker/libnetwork/datastore"
|
|
"github.com/docker/libnetwork/datastore"
|
|
- "github.com/docker/libnetwork/types"
|
|
|
|
)
|
|
)
|
|
|
|
|
|
// Idm manages the reservation/release of numerical ids from a contiguos set
|
|
// Idm manages the reservation/release of numerical ids from a contiguos set
|
|
@@ -25,7 +24,7 @@ func New(ds datastore.DataStore, id string, start, end uint32) (*Idm, error) {
|
|
return nil, fmt.Errorf("Invalid set range: [%d, %d]", start, end)
|
|
return nil, fmt.Errorf("Invalid set range: [%d, %d]", start, end)
|
|
}
|
|
}
|
|
|
|
|
|
- h, err := bitseq.NewHandle("idm", ds, id, uint32(1+end-start))
|
|
|
|
|
|
+ h, err := bitseq.NewHandle("idm", ds, id, 1+end-start)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to initialize bit sequence handler: %s", err.Error())
|
|
return nil, fmt.Errorf("failed to initialize bit sequence handler: %s", err.Error())
|
|
}
|
|
}
|
|
@@ -38,28 +37,8 @@ func (i *Idm) GetID() (uint32, error) {
|
|
if i.handle == nil {
|
|
if i.handle == nil {
|
|
return 0, fmt.Errorf("ID set is not initialized")
|
|
return 0, fmt.Errorf("ID set is not initialized")
|
|
}
|
|
}
|
|
-
|
|
|
|
- for {
|
|
|
|
- bytePos, bitPos, err := i.handle.GetFirstAvailable()
|
|
|
|
- if err != nil {
|
|
|
|
- return 0, fmt.Errorf("no available ids")
|
|
|
|
- }
|
|
|
|
- id := i.start + uint32(bitPos+bytePos*8)
|
|
|
|
-
|
|
|
|
- // for sets which length is non multiple of 32 this check is needed
|
|
|
|
- if i.end < id {
|
|
|
|
- return 0, fmt.Errorf("no available ids")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if err := i.handle.PushReservation(bytePos, bitPos, false); err != nil {
|
|
|
|
- if _, ok := err.(types.RetryError); !ok {
|
|
|
|
- return 0, fmt.Errorf("internal failure while reserving the id: %s", err.Error())
|
|
|
|
- }
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return id, nil
|
|
|
|
- }
|
|
|
|
|
|
+ ordinal, err := i.handle.SetAny()
|
|
|
|
+ return i.start + ordinal, err
|
|
}
|
|
}
|
|
|
|
|
|
// GetSpecificID tries to reserve the specified id
|
|
// GetSpecificID tries to reserve the specified id
|
|
@@ -72,23 +51,10 @@ func (i *Idm) GetSpecificID(id uint32) error {
|
|
return fmt.Errorf("Requested id does not belong to the set")
|
|
return fmt.Errorf("Requested id does not belong to the set")
|
|
}
|
|
}
|
|
|
|
|
|
- for {
|
|
|
|
- bytePos, bitPos, err := i.handle.CheckIfAvailable(id - i.start)
|
|
|
|
- if err != nil {
|
|
|
|
- return fmt.Errorf("requested id is not available")
|
|
|
|
- }
|
|
|
|
- if err := i.handle.PushReservation(bytePos, bitPos, false); err != nil {
|
|
|
|
- if _, ok := err.(types.RetryError); !ok {
|
|
|
|
- return fmt.Errorf("internal failure while reserving the id: %s", err.Error())
|
|
|
|
- }
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- return nil
|
|
|
|
- }
|
|
|
|
|
|
+ return i.handle.Set(id - i.start)
|
|
}
|
|
}
|
|
|
|
|
|
// Release releases the specified id
|
|
// Release releases the specified id
|
|
func (i *Idm) Release(id uint32) {
|
|
func (i *Idm) Release(id uint32) {
|
|
- ordinal := id - i.start
|
|
|
|
- i.handle.PushReservation(ordinal/8, ordinal%8, true)
|
|
|
|
|
|
+ i.handle.Unset(id - i.start)
|
|
}
|
|
}
|