@@ -17,7 +17,10 @@ use crate::content_manager::consensus::entry_queue::{EntryApplyProgressQueue, En
17
17
use crate :: types:: PeerAddressById ;
18
18
use crate :: StorageError ;
19
19
20
- const STATE_FILE_NAME : & str = "raft_state" ;
20
+ // Deprecated, use `STATE_FILE_NAME` instead
21
+ const STATE_FILE_NAME_CBOR : & str = "raft_state" ;
22
+
23
+ const STATE_FILE_NAME : & str = "raft_state.json" ;
21
24
22
25
/// State of the Raft consensus, which should be saved between restarts.
23
26
/// State of the collections, aliases and transfers are stored as regular storage.
@@ -73,14 +76,23 @@ impl Persistent {
73
76
first_peer : bool ,
74
77
) -> Result < Self , StorageError > {
75
78
create_dir_all ( storage_path. as_ref ( ) ) ?;
76
- let path = storage_path. as_ref ( ) . join ( STATE_FILE_NAME ) ;
77
- let state = if path. exists ( ) {
78
- log:: info!( "Loading raft state from {}" , path. display( ) ) ;
79
- Self :: load ( path) ?
79
+ let path_legacy = storage_path. as_ref ( ) . join ( STATE_FILE_NAME_CBOR ) ;
80
+ let path_json = storage_path. as_ref ( ) . join ( STATE_FILE_NAME ) ;
81
+ let state = if path_json. exists ( ) {
82
+ log:: info!( "Loading raft state from {}" , path_json. display( ) ) ;
83
+ Self :: load_json ( path_json) ?
84
+ } else if path_legacy. exists ( ) {
85
+ log:: info!( "Loading raft state from {}" , path_legacy. display( ) ) ;
86
+ let mut state = Self :: load ( path_legacy) ?;
87
+ // migrate to json
88
+ state. path = path_json;
89
+ state. save ( ) ?;
90
+ state
80
91
} else {
81
- log:: info!( "Initializing new raft state at {}" , path . display( ) ) ;
82
- Self :: init ( path , first_peer) ?
92
+ log:: info!( "Initializing new raft state at {}" , path_json . display( ) ) ;
93
+ Self :: init ( path_json , first_peer) ?
83
94
} ;
95
+
84
96
log:: debug!( "State: {:?}" , state) ;
85
97
Ok ( state)
86
98
}
@@ -192,10 +204,17 @@ impl Persistent {
192
204
Ok ( state)
193
205
}
194
206
207
+ fn load_json ( path : PathBuf ) -> Result < Self , StorageError > {
208
+ let file = File :: open ( & path) ?;
209
+ let mut state: Self = serde_json:: from_reader ( & file) ?;
210
+ state. path = path;
211
+ Ok ( state)
212
+ }
213
+
195
214
pub fn save ( & self ) -> Result < ( ) , StorageError > {
196
215
let result = AtomicFile :: new ( & self . path , AllowOverwrite ) . write ( |file| {
197
216
let writer = BufWriter :: new ( file) ;
198
- serde_cbor :: to_writer ( writer, self )
217
+ serde_json :: to_writer ( writer, self )
199
218
} ) ;
200
219
log:: trace!( "Saved state: {:?}" , self ) ;
201
220
self . dirty . store ( result. is_err ( ) , Ordering :: Relaxed ) ;
0 commit comments