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.

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