Skip to content

Commit

Permalink
Implement weave consense
Browse files Browse the repository at this point in the history
  • Loading branch information
awh committed Mar 3, 2016
1 parent 4c159a9 commit bc70e73
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
12 changes: 12 additions & 0 deletions ipam/allocator.go
Expand Up @@ -54,6 +54,7 @@ type Allocator struct {
nicknames map[mesh.PeerName]string // so we can map nicknames for rmpeer
pendingAllocates []operation // held until we get some free space
pendingClaims []operation // held until we know who owns the space
pendingConsenses []operation // held until consensus achieved
dead map[string]time.Time // containers we heard were dead, and when
db db.DB // persistence
gossip mesh.Gossip // our link to the outside world for sending messages
Expand Down Expand Up @@ -165,6 +166,8 @@ func (alloc *Allocator) tryOps(ops *[]operation) {

// Try all pending operations
func (alloc *Allocator) tryPendingOps() {
// Unblock pending consenses first
alloc.tryOps(&alloc.pendingConsenses)
// Process existing claims before servicing new allocations
alloc.tryOps(&alloc.pendingClaims)
alloc.tryOps(&alloc.pendingAllocates)
Expand Down Expand Up @@ -193,6 +196,14 @@ func (e *errorCancelled) Error() string {

// Actor client API

// Consense (Sync) - wait for consensus
func (alloc *Allocator) Consense() {
resultChan := make(chan struct{})
op := &consense{resultChan: resultChan}
alloc.doOperation(op, &alloc.pendingConsenses)
<-resultChan
}

// Allocate (Sync) - get new IP address for container with given name in range
// if there isn't any space in that range we block indefinitely
func (alloc *Allocator) Allocate(ident string, r address.Range, hasBeenCancelled func() bool) (address.Address, error) {
Expand Down Expand Up @@ -348,6 +359,7 @@ func (alloc *Allocator) Shutdown() {
alloc.shuttingDown = true
alloc.cancelOps(&alloc.pendingClaims)
alloc.cancelOps(&alloc.pendingAllocates)
alloc.cancelOps(&alloc.pendingConsenses)
if heir := alloc.pickPeerForTransfer(); heir != mesh.UnknownPeerName {
alloc.ring.Transfer(alloc.ourName, heir)
alloc.space.Clear()
Expand Down
24 changes: 24 additions & 0 deletions ipam/consense.go
@@ -0,0 +1,24 @@
package ipam

type consense struct {
resultChan chan<- struct{}
}

func (c *consense) Try(alloc *Allocator) bool {
if !alloc.ring.Empty() {
close(c.resultChan)
return true
}

alloc.establishRing()

return false
}

func (c *consense) Cancel() {
close(c.resultChan)
}

func (c *consense) ForContainer(ident string) bool {
return false
}
4 changes: 4 additions & 0 deletions ipam/http.go
Expand Up @@ -77,6 +77,10 @@ func (alloc *Allocator) HandleHTTP(router *mux.Router, defaultSubnet address.CID
w.WriteHeader(204)
})

router.Methods("GET").Path("/consensus").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
alloc.Consense()
})

router.Methods("GET").Path("/ip/{id}/{ip}/{prefixlen}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if subnet, ok := parseCIDR(w, vars["ip"]+"/"+vars["prefixlen"]); ok {
Expand Down
16 changes: 14 additions & 2 deletions site/ipam/overview-init-ipam.md
Expand Up @@ -15,6 +15,7 @@ The following automatic IP address managment topics are discussed:

* [Initializing Peers on a Weave Network](#initialization)
* [`--init-peer-count` and How Quorum is Achieved](#quorum)
* [Forcing Consensus](#forcing-consensus)
* [Choosing an Allocation Range](#range)


Expand Down Expand Up @@ -100,6 +101,19 @@ is safe with respect to Weave's startup quorum:
...host1 is rebooted...
host1$ weave launch $HOST2 $HOST3

### <a name="forcing-consensus"></a>Forcing Consensus

Under certain circumstances (for example when adding new nodes to an
existing cluster) it is desirable to ensure that a node has
successfully joined and received a copy of the IPAM data structure
shared amongst the peers. An administrative command is provided for
this purpose:

host1$ weave consense

This operation will block until the node on which it is run has joined
successfully.

### <a name="range"></a>Choosing an Allocation Range

By default, Weave allocates IP addresses in the 10.32.0.0/12
Expand All @@ -126,5 +140,3 @@ to the rest of the network without any conflicts arising.

* [Automatic Allocation Across Multiple Subnets](/site/ipam/allocation-multi-ipam.md)
* [Plugin Command-line Arguments](/site/plugin/plug-in-command-line.md)


5 changes: 5 additions & 0 deletions weave
Expand Up @@ -51,6 +51,8 @@ weave launch <same arguments as 'weave launch-router'>
[--rewrite-inspect]
launch-plugin [--no-multicast-route]
weave consense
weave env [--restore]
config
dns-args
Expand Down Expand Up @@ -2186,6 +2188,9 @@ EOF
echo "The 'stop-dns command has been removed; DNS is stopped as part of 'stop' and 'stop-router'." >&2
exit 0
;;
consense)
call_weave GET /consensus
;;
*)
echo "Unknown weave command '$COMMAND'" >&2
usage
Expand Down

0 comments on commit bc70e73

Please sign in to comment.