store.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. package libnetwork
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/docker/docker/libnetwork/datastore"
  6. "github.com/docker/libkv/store/boltdb"
  7. "github.com/sirupsen/logrus"
  8. )
  9. func registerKVStores() {
  10. boltdb.Register()
  11. }
  12. func (c *controller) initScopedStore(scope string, scfg *datastore.ScopeCfg) error {
  13. store, err := datastore.NewDataStore(scope, scfg)
  14. if err != nil {
  15. return err
  16. }
  17. c.Lock()
  18. c.stores = append(c.stores, store)
  19. c.Unlock()
  20. return nil
  21. }
  22. func (c *controller) initStores() error {
  23. registerKVStores()
  24. c.Lock()
  25. if c.cfg == nil {
  26. c.Unlock()
  27. return nil
  28. }
  29. scopeConfigs := c.cfg.Scopes
  30. c.stores = nil
  31. c.Unlock()
  32. for scope, scfg := range scopeConfigs {
  33. if err := c.initScopedStore(scope, scfg); err != nil {
  34. return err
  35. }
  36. }
  37. c.startWatch()
  38. return nil
  39. }
  40. func (c *controller) closeStores() {
  41. for _, store := range c.getStores() {
  42. store.Close()
  43. }
  44. }
  45. func (c *controller) getStore(scope string) datastore.DataStore {
  46. c.Lock()
  47. defer c.Unlock()
  48. for _, store := range c.stores {
  49. if store.Scope() == scope {
  50. return store
  51. }
  52. }
  53. return nil
  54. }
  55. func (c *controller) getStores() []datastore.DataStore {
  56. c.Lock()
  57. defer c.Unlock()
  58. return c.stores
  59. }
  60. func (c *controller) getNetworkFromStore(nid string) (*network, error) {
  61. for _, n := range c.getNetworksFromStore() {
  62. if n.id == nid {
  63. return n, nil
  64. }
  65. }
  66. return nil, ErrNoSuchNetwork(nid)
  67. }
  68. func (c *controller) getNetworksForScope(scope string) ([]*network, error) {
  69. var nl []*network
  70. store := c.getStore(scope)
  71. if store == nil {
  72. return nil, nil
  73. }
  74. kvol, err := store.List(datastore.Key(datastore.NetworkKeyPrefix),
  75. &network{ctrlr: c})
  76. if err != nil && err != datastore.ErrKeyNotFound {
  77. return nil, fmt.Errorf("failed to get networks for scope %s: %v",
  78. scope, err)
  79. }
  80. for _, kvo := range kvol {
  81. n := kvo.(*network)
  82. n.ctrlr = c
  83. ec := &endpointCnt{n: n}
  84. err = store.GetObject(datastore.Key(ec.Key()...), ec)
  85. if err != nil && !n.inDelete {
  86. logrus.Warnf("Could not find endpoint count key %s for network %s while listing: %v", datastore.Key(ec.Key()...), n.Name(), err)
  87. continue
  88. }
  89. n.epCnt = ec
  90. if n.scope == "" {
  91. n.scope = scope
  92. }
  93. nl = append(nl, n)
  94. }
  95. return nl, nil
  96. }
  97. func (c *controller) getNetworksFromStore() []*network {
  98. var nl []*network
  99. for _, store := range c.getStores() {
  100. kvol, err := store.List(datastore.Key(datastore.NetworkKeyPrefix), &network{ctrlr: c})
  101. // Continue searching in the next store if no keys found in this store
  102. if err != nil {
  103. if err != datastore.ErrKeyNotFound {
  104. logrus.Debugf("failed to get networks for scope %s: %v", store.Scope(), err)
  105. }
  106. continue
  107. }
  108. kvep, err := store.Map(datastore.Key(epCntKeyPrefix), &endpointCnt{})
  109. if err != nil && err != datastore.ErrKeyNotFound {
  110. logrus.Warnf("failed to get endpoint_count map for scope %s: %v", store.Scope(), err)
  111. }
  112. for _, kvo := range kvol {
  113. n := kvo.(*network)
  114. n.Lock()
  115. n.ctrlr = c
  116. ec := &endpointCnt{n: n}
  117. // Trim the leading & trailing "/" to make it consistent across all stores
  118. if val, ok := kvep[strings.Trim(datastore.Key(ec.Key()...), "/")]; ok {
  119. ec = val.(*endpointCnt)
  120. ec.n = n
  121. n.epCnt = ec
  122. }
  123. if n.scope == "" {
  124. n.scope = store.Scope()
  125. }
  126. n.Unlock()
  127. nl = append(nl, n)
  128. }
  129. }
  130. return nl
  131. }
  132. func (n *network) getEndpointFromStore(eid string) (*endpoint, error) {
  133. var errors []string
  134. for _, store := range n.ctrlr.getStores() {
  135. ep := &endpoint{id: eid, network: n}
  136. err := store.GetObject(datastore.Key(ep.Key()...), ep)
  137. // Continue searching in the next store if the key is not found in this store
  138. if err != nil {
  139. if err != datastore.ErrKeyNotFound {
  140. errors = append(errors, fmt.Sprintf("{%s:%v}, ", store.Scope(), err))
  141. logrus.Debugf("could not find endpoint %s in %s: %v", eid, store.Scope(), err)
  142. }
  143. continue
  144. }
  145. return ep, nil
  146. }
  147. return nil, fmt.Errorf("could not find endpoint %s: %v", eid, errors)
  148. }
  149. func (n *network) getEndpointsFromStore() ([]*endpoint, error) {
  150. var epl []*endpoint
  151. tmp := endpoint{network: n}
  152. for _, store := range n.getController().getStores() {
  153. kvol, err := store.List(datastore.Key(tmp.KeyPrefix()...), &endpoint{network: n})
  154. // Continue searching in the next store if no keys found in this store
  155. if err != nil {
  156. if err != datastore.ErrKeyNotFound {
  157. logrus.Debugf("failed to get endpoints for network %s scope %s: %v",
  158. n.Name(), store.Scope(), err)
  159. }
  160. continue
  161. }
  162. for _, kvo := range kvol {
  163. ep := kvo.(*endpoint)
  164. epl = append(epl, ep)
  165. }
  166. }
  167. return epl, nil
  168. }
  169. func (c *controller) updateToStore(kvObject datastore.KVObject) error {
  170. cs := c.getStore(kvObject.DataScope())
  171. if cs == nil {
  172. return ErrDataStoreNotInitialized(kvObject.DataScope())
  173. }
  174. if err := cs.PutObjectAtomic(kvObject); err != nil {
  175. if err == datastore.ErrKeyModified {
  176. return err
  177. }
  178. return fmt.Errorf("failed to update store for object type %T: %v", kvObject, err)
  179. }
  180. return nil
  181. }
  182. func (c *controller) deleteFromStore(kvObject datastore.KVObject) error {
  183. cs := c.getStore(kvObject.DataScope())
  184. if cs == nil {
  185. return ErrDataStoreNotInitialized(kvObject.DataScope())
  186. }
  187. retry:
  188. if err := cs.DeleteObjectAtomic(kvObject); err != nil {
  189. if err == datastore.ErrKeyModified {
  190. if err := cs.GetObject(datastore.Key(kvObject.Key()...), kvObject); err != nil {
  191. return fmt.Errorf("could not update the kvobject to latest when trying to delete: %v", err)
  192. }
  193. logrus.Warnf("Error (%v) deleting object %v, retrying....", err, kvObject.Key())
  194. goto retry
  195. }
  196. return err
  197. }
  198. return nil
  199. }
  200. type netWatch struct {
  201. localEps map[string]*endpoint
  202. remoteEps map[string]*endpoint
  203. stopCh chan struct{}
  204. }
  205. func (c *controller) getLocalEps(nw *netWatch) []*endpoint {
  206. c.Lock()
  207. defer c.Unlock()
  208. var epl []*endpoint
  209. for _, ep := range nw.localEps {
  210. epl = append(epl, ep)
  211. }
  212. return epl
  213. }
  214. func (c *controller) watchSvcRecord(ep *endpoint) {
  215. c.watchCh <- ep
  216. }
  217. func (c *controller) unWatchSvcRecord(ep *endpoint) {
  218. c.unWatchCh <- ep
  219. }
  220. func (c *controller) networkWatchLoop(nw *netWatch, ep *endpoint, ecCh <-chan datastore.KVObject) {
  221. for {
  222. select {
  223. case <-nw.stopCh:
  224. return
  225. case o := <-ecCh:
  226. ec := o.(*endpointCnt)
  227. epl, err := ec.n.getEndpointsFromStore()
  228. if err != nil {
  229. break
  230. }
  231. c.Lock()
  232. var addEp []*endpoint
  233. delEpMap := make(map[string]*endpoint)
  234. renameEpMap := make(map[string]bool)
  235. for k, v := range nw.remoteEps {
  236. delEpMap[k] = v
  237. }
  238. for _, lEp := range epl {
  239. if _, ok := nw.localEps[lEp.ID()]; ok {
  240. continue
  241. }
  242. if ep, ok := nw.remoteEps[lEp.ID()]; ok {
  243. // On a container rename EP ID will remain
  244. // the same but the name will change. service
  245. // records should reflect the change.
  246. // Keep old EP entry in the delEpMap and add
  247. // EP from the store (which has the new name)
  248. // into the new list
  249. if lEp.name == ep.name {
  250. delete(delEpMap, lEp.ID())
  251. continue
  252. }
  253. renameEpMap[lEp.ID()] = true
  254. }
  255. nw.remoteEps[lEp.ID()] = lEp
  256. addEp = append(addEp, lEp)
  257. }
  258. // EPs whose name are to be deleted from the svc records
  259. // should also be removed from nw's remote EP list, except
  260. // the ones that are getting renamed.
  261. for _, lEp := range delEpMap {
  262. if !renameEpMap[lEp.ID()] {
  263. delete(nw.remoteEps, lEp.ID())
  264. }
  265. }
  266. c.Unlock()
  267. for _, lEp := range delEpMap {
  268. ep.getNetwork().updateSvcRecord(lEp, c.getLocalEps(nw), false)
  269. }
  270. for _, lEp := range addEp {
  271. ep.getNetwork().updateSvcRecord(lEp, c.getLocalEps(nw), true)
  272. }
  273. }
  274. }
  275. }
  276. func (c *controller) processEndpointCreate(nmap map[string]*netWatch, ep *endpoint) {
  277. n := ep.getNetwork()
  278. if !c.isDistributedControl() && n.Scope() == datastore.SwarmScope && n.driverIsMultihost() {
  279. return
  280. }
  281. networkID := n.ID()
  282. endpointID := ep.ID()
  283. c.Lock()
  284. nw, ok := nmap[networkID]
  285. c.Unlock()
  286. if ok {
  287. // Update the svc db for the local endpoint join right away
  288. n.updateSvcRecord(ep, c.getLocalEps(nw), true)
  289. c.Lock()
  290. nw.localEps[endpointID] = ep
  291. // If we had learned that from the kv store remove it
  292. // from remote ep list now that we know that this is
  293. // indeed a local endpoint
  294. delete(nw.remoteEps, endpointID)
  295. c.Unlock()
  296. return
  297. }
  298. nw = &netWatch{
  299. localEps: make(map[string]*endpoint),
  300. remoteEps: make(map[string]*endpoint),
  301. }
  302. // Update the svc db for the local endpoint join right away
  303. // Do this before adding this ep to localEps so that we don't
  304. // try to update this ep's container's svc records
  305. n.updateSvcRecord(ep, c.getLocalEps(nw), true)
  306. c.Lock()
  307. nw.localEps[endpointID] = ep
  308. nmap[networkID] = nw
  309. nw.stopCh = make(chan struct{})
  310. c.Unlock()
  311. store := c.getStore(n.DataScope())
  312. if store == nil {
  313. return
  314. }
  315. if !store.Watchable() {
  316. return
  317. }
  318. ch, err := store.Watch(n.getEpCnt(), nw.stopCh)
  319. if err != nil {
  320. logrus.Warnf("Error creating watch for network: %v", err)
  321. return
  322. }
  323. go c.networkWatchLoop(nw, ep, ch)
  324. }
  325. func (c *controller) processEndpointDelete(nmap map[string]*netWatch, ep *endpoint) {
  326. n := ep.getNetwork()
  327. if !c.isDistributedControl() && n.Scope() == datastore.SwarmScope && n.driverIsMultihost() {
  328. return
  329. }
  330. networkID := n.ID()
  331. endpointID := ep.ID()
  332. c.Lock()
  333. nw, ok := nmap[networkID]
  334. if ok {
  335. delete(nw.localEps, endpointID)
  336. c.Unlock()
  337. // Update the svc db about local endpoint leave right away
  338. // Do this after we remove this ep from localEps so that we
  339. // don't try to remove this svc record from this ep's container.
  340. n.updateSvcRecord(ep, c.getLocalEps(nw), false)
  341. c.Lock()
  342. if len(nw.localEps) == 0 {
  343. close(nw.stopCh)
  344. // This is the last container going away for the network. Destroy
  345. // this network's svc db entry
  346. delete(c.svcRecords, networkID)
  347. delete(nmap, networkID)
  348. }
  349. }
  350. c.Unlock()
  351. }
  352. func (c *controller) watchLoop() {
  353. for {
  354. select {
  355. case ep := <-c.watchCh:
  356. c.processEndpointCreate(c.nmap, ep)
  357. case ep := <-c.unWatchCh:
  358. c.processEndpointDelete(c.nmap, ep)
  359. }
  360. }
  361. }
  362. func (c *controller) startWatch() {
  363. if c.watchCh != nil {
  364. return
  365. }
  366. c.watchCh = make(chan *endpoint)
  367. c.unWatchCh = make(chan *endpoint)
  368. c.nmap = make(map[string]*netWatch)
  369. go c.watchLoop()
  370. }
  371. func (c *controller) networkCleanup() {
  372. for _, n := range c.getNetworksFromStore() {
  373. if n.inDelete {
  374. logrus.Infof("Removing stale network %s (%s)", n.Name(), n.ID())
  375. if err := n.delete(true, true); err != nil {
  376. logrus.Debugf("Error while removing stale network: %v", err)
  377. }
  378. }
  379. }
  380. }
  381. var populateSpecial NetworkWalker = func(nw Network) bool {
  382. if n := nw.(*network); n.hasSpecialDriver() && !n.ConfigOnly() {
  383. if err := n.getController().addNetwork(n); err != nil {
  384. logrus.Warnf("Failed to populate network %q with driver %q", nw.Name(), nw.Type())
  385. }
  386. }
  387. return false
  388. }