v2_raft.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Copyright 2014 CoreOS Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcd
  14. import (
  15. "fmt"
  16. "time"
  17. "github.com/coreos/etcd/raft"
  18. )
  19. type v2Proposal struct {
  20. data []byte
  21. ret chan interface{}
  22. }
  23. type wait struct {
  24. index int64
  25. term int64
  26. }
  27. type v2Raft struct {
  28. *raft.Node
  29. result map[wait]chan interface{}
  30. term int64
  31. }
  32. func (r *v2Raft) Propose(p v2Proposal) {
  33. if !r.Node.IsLeader() {
  34. p.ret <- fmt.Errorf("not leader")
  35. return
  36. }
  37. r.Node.Propose(p.data)
  38. r.result[wait{r.Index(), r.Term()}] = p.ret
  39. return
  40. }
  41. func (r *v2Raft) Sync() {
  42. if !r.Node.IsLeader() {
  43. return
  44. }
  45. t := time.Now()
  46. sync := &Cmd{Type: stsync, Time: mustMarshalTime(&t)}
  47. data, err := sync.Marshal()
  48. if err != nil {
  49. panic(err)
  50. }
  51. r.Node.Propose(data)
  52. }