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.

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