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.

92 lines
2.3 KiB

8 years ago
8 years ago
8 years ago
  1. package consensus
  2. import (
  3. "time"
  4. auto "github.com/tendermint/go-autofile"
  5. . "github.com/tendermint/go-common"
  6. "github.com/tendermint/go-wire"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. //--------------------------------------------------------
  10. // types and functions for savings consensus messages
  11. type TimedWALMessage struct {
  12. Time time.Time `json:"time"`
  13. Msg WALMessage `json:"msg"`
  14. }
  15. type WALMessage interface{}
  16. var _ = wire.RegisterInterface(
  17. struct{ WALMessage }{},
  18. wire.ConcreteType{types.EventDataRoundState{}, 0x01},
  19. wire.ConcreteType{msgInfo{}, 0x02},
  20. wire.ConcreteType{timeoutInfo{}, 0x03},
  21. )
  22. //--------------------------------------------------------
  23. // Simple write-ahead logger
  24. // Write ahead logger writes msgs to disk before they are processed.
  25. // Can be used for crash-recovery and deterministic replay
  26. // TODO: currently the wal is overwritten during replay catchup
  27. // give it a mode so it's either reading or appending - must read to end to start appending again
  28. type WAL struct {
  29. BaseService
  30. group *auto.Group
  31. light bool // ignore block parts
  32. }
  33. func NewWAL(walDir string, light bool) (*WAL, error) {
  34. head, err := auto.OpenAutoFile(walDir + "/wal")
  35. if err != nil {
  36. return nil, err
  37. }
  38. group, err := auto.OpenGroup(head)
  39. if err != nil {
  40. return nil, err
  41. }
  42. wal := &WAL{
  43. group: group,
  44. light: light,
  45. }
  46. wal.BaseService = *NewBaseService(log, "WAL", wal)
  47. return wal, nil
  48. }
  49. func (wal *WAL) OnStop() {
  50. wal.BaseService.OnStop()
  51. wal.group.Head.Close()
  52. wal.group.Close()
  53. }
  54. // called in newStep and for each pass in receiveRoutine
  55. func (wal *WAL) Save(wmsg WALMessage) {
  56. if wal == nil {
  57. return
  58. }
  59. if wal.light {
  60. // in light mode we only write new steps, timeouts, and our own votes (no proposals, block parts)
  61. if mi, ok := wmsg.(msgInfo); ok {
  62. _ = mi
  63. if mi.PeerKey != "" {
  64. return
  65. }
  66. }
  67. }
  68. // Write #HEIGHT: XYZ if new height
  69. if edrs, ok := wmsg.(types.EventDataRoundState); ok {
  70. if edrs.Step == RoundStepNewHeight.String() {
  71. wal.group.WriteLine(Fmt("#HEIGHT: %v", edrs.Height))
  72. }
  73. }
  74. // Write the wal message
  75. var wmsgBytes = wire.JSONBytes(TimedWALMessage{time.Now(), wmsg})
  76. err := wal.group.WriteLine(string(wmsgBytes))
  77. if err != nil {
  78. PanicQ(Fmt("Error writing msg to consensus wal. Error: %v \n\nMessage: %v", err, wmsg))
  79. }
  80. }