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.

137 lines
3.2 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. // called in newStep and for each pass in receiveRoutine
  52. func (wal *WAL) Save(clm ConsensusLogMessageInterface) {
  53. if wal != nil {
  54. if wal.light {
  55. // in light mode we only write new steps, timeouts, and our own votes (no proposals, block parts)
  56. if mi, ok := clm.(msgInfo); ok {
  57. _ = mi
  58. if mi.PeerKey != "" {
  59. return
  60. }
  61. }
  62. }
  63. var n int
  64. var err error
  65. wire.WriteJSON(ConsensusLogMessage{time.Now(), clm}, wal.fp, &n, &err)
  66. wire.WriteTo([]byte("\n"), wal.fp, &n, &err) // one message per line
  67. if err != nil {
  68. PanicQ(Fmt("Error writing msg to consensus wal. Error: %v \n\nMessage: %v", err, clm))
  69. }
  70. }
  71. }
  72. // Must not be called concurrently with a write.
  73. func (wal *WAL) Close() {
  74. if wal != nil {
  75. wal.fp.Close()
  76. }
  77. wal.done <- struct{}{}
  78. }
  79. func (wal *WAL) Wait() {
  80. <-wal.done
  81. }
  82. func (wal *WAL) SeekFromEnd(found func([]byte) bool) (nLines int, err error) {
  83. var current int64
  84. // start at the end
  85. current, err = wal.fp.Seek(0, 2)
  86. if err != nil {
  87. return
  88. }
  89. // backup until we find the the right line
  90. // current is how far we are from the beginning
  91. for {
  92. current -= 1
  93. if current < 0 {
  94. wal.fp.Seek(0, 0) // back to beginning
  95. return
  96. }
  97. // backup one and read a new byte
  98. if _, err = wal.fp.Seek(current, 0); err != nil {
  99. return
  100. }
  101. b := make([]byte, 1)
  102. if _, err = wal.fp.Read(b); err != nil {
  103. return
  104. }
  105. if b[0] == '\n' || len(b) == 0 {
  106. nLines += 1
  107. // read a full line
  108. reader := bufio.NewReader(wal.fp)
  109. lineBytes, _ := reader.ReadBytes('\n')
  110. if len(lineBytes) == 0 {
  111. continue
  112. }
  113. if found(lineBytes) {
  114. wal.fp.Seek(0, 1) // (?)
  115. wal.fp.Seek(current, 0)
  116. return
  117. }
  118. }
  119. }
  120. }