2015-10-12 05:28:26 +00:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/datastore"
|
2015-10-12 05:28:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type endpointCnt struct {
|
2023-07-21 22:38:57 +00:00
|
|
|
n *Network
|
2015-10-12 05:28:26 +00:00
|
|
|
Count uint64
|
|
|
|
dbIndex uint64
|
|
|
|
dbExists bool
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
const epCntKeyPrefix = "endpoint_count"
|
|
|
|
|
|
|
|
func (ec *endpointCnt) Key() []string {
|
|
|
|
ec.Lock()
|
|
|
|
defer ec.Unlock()
|
|
|
|
|
|
|
|
return []string{epCntKeyPrefix, ec.n.id}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *endpointCnt) KeyPrefix() []string {
|
|
|
|
ec.Lock()
|
|
|
|
defer ec.Unlock()
|
|
|
|
|
|
|
|
return []string{epCntKeyPrefix, ec.n.id}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *endpointCnt) Value() []byte {
|
|
|
|
ec.Lock()
|
|
|
|
defer ec.Unlock()
|
|
|
|
|
|
|
|
b, err := json.Marshal(ec)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *endpointCnt) SetValue(value []byte) error {
|
|
|
|
ec.Lock()
|
|
|
|
defer ec.Unlock()
|
|
|
|
|
|
|
|
return json.Unmarshal(value, &ec)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *endpointCnt) Index() uint64 {
|
|
|
|
ec.Lock()
|
|
|
|
defer ec.Unlock()
|
|
|
|
return ec.dbIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *endpointCnt) SetIndex(index uint64) {
|
|
|
|
ec.Lock()
|
|
|
|
ec.dbIndex = index
|
|
|
|
ec.dbExists = true
|
|
|
|
ec.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *endpointCnt) Exists() bool {
|
|
|
|
ec.Lock()
|
|
|
|
defer ec.Unlock()
|
|
|
|
return ec.dbExists
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *endpointCnt) Skip() bool {
|
|
|
|
ec.Lock()
|
|
|
|
defer ec.Unlock()
|
|
|
|
return !ec.n.persist
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *endpointCnt) New() datastore.KVObject {
|
|
|
|
ec.Lock()
|
|
|
|
defer ec.Unlock()
|
|
|
|
|
|
|
|
return &endpointCnt{
|
|
|
|
n: ec.n,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *endpointCnt) CopyTo(o datastore.KVObject) error {
|
|
|
|
ec.Lock()
|
|
|
|
defer ec.Unlock()
|
|
|
|
|
|
|
|
dstEc := o.(*endpointCnt)
|
|
|
|
dstEc.n = ec.n
|
|
|
|
dstEc.Count = ec.Count
|
|
|
|
dstEc.dbExists = ec.dbExists
|
|
|
|
dstEc.dbIndex = ec.dbIndex
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *endpointCnt) EndpointCnt() uint64 {
|
|
|
|
ec.Lock()
|
|
|
|
defer ec.Unlock()
|
|
|
|
|
|
|
|
return ec.Count
|
|
|
|
}
|
|
|
|
|
2015-10-23 03:18:25 +00:00
|
|
|
func (ec *endpointCnt) updateStore() error {
|
2023-01-14 00:04:43 +00:00
|
|
|
store := ec.n.getController().getStore()
|
2015-10-23 03:18:25 +00:00
|
|
|
if store == nil {
|
2023-10-19 16:10:44 +00:00
|
|
|
return fmt.Errorf("store not found on endpoint count update")
|
2015-10-23 03:18:25 +00:00
|
|
|
}
|
2016-07-15 02:51:06 +00:00
|
|
|
// make a copy of count and n to avoid being overwritten by store.GetObject
|
|
|
|
count := ec.EndpointCnt()
|
|
|
|
n := ec.n
|
2015-10-27 17:14:54 +00:00
|
|
|
for {
|
|
|
|
if err := ec.n.getController().updateToStore(ec); err == nil || err != datastore.ErrKeyModified {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := store.GetObject(datastore.Key(ec.Key()...), ec); err != nil {
|
|
|
|
return fmt.Errorf("could not update the kvobject to latest on endpoint count update: %v", err)
|
2015-10-23 03:18:25 +00:00
|
|
|
}
|
2016-07-15 02:51:06 +00:00
|
|
|
ec.Lock()
|
|
|
|
ec.Count = count
|
|
|
|
ec.n = n
|
|
|
|
ec.Unlock()
|
2015-10-23 03:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 04:07:42 +00:00
|
|
|
func (ec *endpointCnt) setCnt(cnt uint64) error {
|
|
|
|
ec.Lock()
|
|
|
|
ec.Count = cnt
|
|
|
|
ec.Unlock()
|
|
|
|
return ec.updateStore()
|
|
|
|
}
|
|
|
|
|
2015-10-12 05:28:26 +00:00
|
|
|
func (ec *endpointCnt) atomicIncDecEpCnt(inc bool) error {
|
2023-01-14 00:04:43 +00:00
|
|
|
store := ec.n.getController().getStore()
|
2017-10-26 19:57:35 +00:00
|
|
|
if store == nil {
|
2023-10-19 16:10:44 +00:00
|
|
|
return fmt.Errorf("store not found on endpoint count atomic inc/dec")
|
2017-10-26 19:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmp := &endpointCnt{n: ec.n}
|
|
|
|
if err := store.GetObject(datastore.Key(ec.Key()...), tmp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-12 05:28:26 +00:00
|
|
|
retry:
|
|
|
|
ec.Lock()
|
|
|
|
if inc {
|
|
|
|
ec.Count++
|
|
|
|
} else {
|
2016-07-15 02:52:44 +00:00
|
|
|
if ec.Count > 0 {
|
|
|
|
ec.Count--
|
|
|
|
}
|
2015-10-12 05:28:26 +00:00
|
|
|
}
|
|
|
|
ec.Unlock()
|
|
|
|
|
|
|
|
if err := ec.n.getController().updateToStore(ec); err != nil {
|
|
|
|
if err == datastore.ErrKeyModified {
|
|
|
|
if err := store.GetObject(datastore.Key(ec.Key()...), ec); err != nil {
|
|
|
|
return fmt.Errorf("could not update the kvobject to latest when trying to atomic add endpoint count: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *endpointCnt) IncEndpointCnt() error {
|
|
|
|
return ec.atomicIncDecEpCnt(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *endpointCnt) DecEndpointCnt() error {
|
|
|
|
return ec.atomicIncDecEpCnt(false)
|
|
|
|
}
|