You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.3 KiB

  1. package consensus
  2. import (
  3. "bufio"
  4. "os"
  5. "time"
  6. . "github.com/tendermint/go-common"
  7. "github.com/tendermint/go-wire"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. //--------------------------------------------------------
  11. // types and functions for savings consensus messages
  12. type ConsensusLogMessage struct {
  13. Time time.Time `json:"time"`
  14. Msg ConsensusLogMessageInterface `json:"msg"`
  15. }
  16. type ConsensusLogMessageInterface interface{}
  17. var _ = wire.RegisterInterface(
  18. struct{ ConsensusLogMessageInterface }{},
  19. wire.ConcreteType{types.EventDataRoundState{}, 0x01},
  20. wire.ConcreteType{msgInfo{}, 0x02},
  21. wire.ConcreteType{timeoutInfo{}, 0x03},
  22. )
  23. //--------------------------------------------------------
  24. // Simple write-ahead logger
  25. // Write ahead logger writes msgs to disk before they are processed.
  26. // Can be used for crash-recovery and deterministic replay
  27. // TODO: currently the wal is overwritten during replay catchup
  28. // give it a mode so it's either reading or appending - must read to end to start appending again
  29. type WAL struct {
  30. fp *os.File
  31. exists bool // if the file already existed (restarted process)
  32. done chan struct{}
  33. light bool // ignore block parts
  34. }
  35. func NewWAL(file string, light bool) (*WAL, error) {
  36. var walExists bool
  37. if _, err := os.Stat(file); err == nil {
  38. walExists = true
  39. }
  40. fp, err := os.OpenFile(file, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return &WAL{
  45. fp: fp,
  46. exists: walExists,
  47. done: make(chan struct{}),
  48. light: light,
  49. }, nil
  50. }
  51. func (wal *WAL) Exists() bool {
  52. if wal == nil {
  53. log.Warn("consensus msg log is nil")
  54. return false
  55. }
  56. return wal.exists
  57. }
  58. // called in newStep and for each pass in receiveRoutine
  59. func (wal *WAL) Save(clm ConsensusLogMessageInterface) {
  60. if wal != nil {
  61. if wal.light {
  62. // in light mode we only write new steps, timeouts, and our own votes (no proposals, block parts)
  63. if mi, ok := clm.(msgInfo); ok {
  64. _ = mi
  65. if mi.PeerKey != "" {
  66. return
  67. }
  68. }
  69. }
  70. var n int
  71. var err error
  72. wire.WriteJSON(ConsensusLogMessage{time.Now(), clm}, wal.fp, &n, &err)
  73. wire.WriteTo([]byte("\n"), wal.fp, &n, &err) // one message per line
  74. if err != nil {
  75. PanicQ(Fmt("Error writing msg to consensus wal. Error: %v \n\nMessage: %v", err, clm))
  76. }
  77. }
  78. }
  79. // Must not be called concurrently with a write.
  80. func (wal *WAL) Close() {
  81. if wal != nil {
  82. wal.fp.Close()
  83. }
  84. wal.done <- struct{}{}
  85. }
  86. func (wal *WAL) Wait() {
  87. <-wal.done
  88. }
  89. func (wal *WAL) SeekFromEnd(found func([]byte) bool) (nLines int, err error) {
  90. var current int64
  91. // start at the end
  92. current, err = wal.fp.Seek(0, 2)
  93. if err != nil {
  94. return
  95. }
  96. // backup until we find the the right line
  97. // current is how far we are from the beginning
  98. for {
  99. current -= 1
  100. if current < 0 {
  101. wal.fp.Seek(0, 0) // back to beginning
  102. return
  103. }
  104. // backup one and read a new byte
  105. if _, err = wal.fp.Seek(current, 0); err != nil {
  106. return
  107. }
  108. b := make([]byte, 1)
  109. if _, err = wal.fp.Read(b); err != nil {
  110. return
  111. }
  112. if b[0] == '\n' || len(b) == 0 {
  113. nLines += 1
  114. // read a full line
  115. reader := bufio.NewReader(wal.fp)
  116. lineBytes, _ := reader.ReadBytes('\n')
  117. if len(lineBytes) == 0 {
  118. continue
  119. }
  120. if found(lineBytes) {
  121. wal.fp.Seek(0, 1) // (?)
  122. wal.fp.Seek(current, 0)
  123. return
  124. }
  125. }
  126. }
  127. }