@@ -296,53 +296,55 @@ public void addRemotePeer(ConsensusGroupId groupId, Peer peer) throws ConsensusE
296296 IoTConsensusServerImpl impl =
297297 Optional .ofNullable (stateMachineMap .get (groupId ))
298298 .orElseThrow (() -> new ConsensusGroupNotExistException (groupId ));
299- if (impl .getConfiguration ().contains (peer )) {
300- throw new PeerAlreadyInConsensusGroupException (groupId , peer );
301- }
302- try {
303- // step 1: inactive new Peer to prepare for following steps
304- logger .info ("[IoTConsensus] inactivate new peer: {}" , peer );
305- impl .inactivePeer (peer , false );
299+ synchronized (impl ) {
300+ if (impl .getConfiguration ().contains (peer )) {
301+ throw new PeerAlreadyInConsensusGroupException (groupId , peer );
302+ }
303+ try {
304+ // step 1: inactive new Peer to prepare for following steps
305+ logger .info ("[IoTConsensus] inactivate new peer: {}" , peer );
306+ impl .inactivePeer (peer , false );
306307
307- // step 2: notify all the other Peers to build the sync connection to newPeer
308- logger .info ("[IoTConsensus] notify current peers to build sync log..." );
309- impl .notifyPeersToBuildSyncLogChannel (peer );
308+ // step 2: notify all the other Peers to build the sync connection to newPeer
309+ logger .info ("[IoTConsensus] notify current peers to build sync log..." );
310+ impl .notifyPeersToBuildSyncLogChannel (peer );
310311
311- // step 3: take snapshot
312- logger .info ("[IoTConsensus] start to take snapshot..." );
312+ // step 3: take snapshot
313+ logger .info ("[IoTConsensus] start to take snapshot..." );
313314
314- impl .takeSnapshot ();
315+ impl .takeSnapshot ();
315316
316- // step 4: transit snapshot
317- logger .info ("[IoTConsensus] start to transmit snapshot..." );
318- impl .transmitSnapshot (peer );
317+ // step 4: transit snapshot
318+ logger .info ("[IoTConsensus] start to transmit snapshot..." );
319+ impl .transmitSnapshot (peer );
319320
320- // step 5: let the new peer load snapshot
321- logger .info ("[IoTConsensus] trigger new peer to load snapshot..." );
322- impl .triggerSnapshotLoad (peer );
323- KillPoint .setKillPoint (DataNodeKillPoints .COORDINATOR_ADD_PEER_TRANSITION );
321+ // step 5: let the new peer load snapshot
322+ logger .info ("[IoTConsensus] trigger new peer to load snapshot..." );
323+ impl .triggerSnapshotLoad (peer );
324+ KillPoint .setKillPoint (DataNodeKillPoints .COORDINATOR_ADD_PEER_TRANSITION );
324325
325- // step 6: active new Peer
326- logger .info ("[IoTConsensus] activate new peer..." );
327- impl .activePeer (peer );
326+ // step 6: active new Peer
327+ logger .info ("[IoTConsensus] activate new peer..." );
328+ impl .activePeer (peer );
329+
330+ // step 7: notify remote peer to clean up transferred snapshot
331+ logger .info ("[IoTConsensus] clean up remote snapshot..." );
332+ try {
333+ impl .cleanupRemoteSnapshot (peer );
334+ } catch (ConsensusGroupModifyPeerException e ) {
335+ logger .warn ("[IoTConsensus] failed to cleanup remote snapshot" , e );
336+ }
337+ KillPoint .setKillPoint (DataNodeKillPoints .COORDINATOR_ADD_PEER_DONE );
328338
329- // step 7: notify remote peer to clean up transferred snapshot
330- logger .info ("[IoTConsensus] clean up remote snapshot..." );
331- try {
332- impl .cleanupRemoteSnapshot (peer );
333339 } catch (ConsensusGroupModifyPeerException e ) {
334- logger .warn ("[IoTConsensus] failed to cleanup remote snapshot" , e );
340+ logger .info ("[IoTConsensus] add remote peer failed, automatic cleanup side effects..." );
341+ // try to clean up the sync log channel
342+ impl .notifyPeersToRemoveSyncLogChannel (peer );
343+ throw new ConsensusException (e );
344+ } finally {
345+ logger .info ("[IoTConsensus] clean up local snapshot..." );
346+ impl .cleanupLocalSnapshot ();
335347 }
336- KillPoint .setKillPoint (DataNodeKillPoints .COORDINATOR_ADD_PEER_DONE );
337-
338- } catch (ConsensusGroupModifyPeerException e ) {
339- logger .info ("[IoTConsensus] add remote peer failed, automatic cleanup side effects..." );
340- // try to clean up the sync log channel
341- impl .notifyPeersToRemoveSyncLogChannel (peer );
342- throw new ConsensusException (e );
343- } finally {
344- logger .info ("[IoTConsensus] clean up local snapshot..." );
345- impl .cleanupLocalSnapshot ();
346348 }
347349 }
348350
@@ -352,29 +354,32 @@ public void removeRemotePeer(ConsensusGroupId groupId, Peer peer) throws Consens
352354 Optional .ofNullable (stateMachineMap .get (groupId ))
353355 .orElseThrow (() -> new ConsensusGroupNotExistException (groupId ));
354356
355- if (!impl .getConfiguration ().contains (peer )) {
356- throw new PeerNotInConsensusGroupException (groupId , peer .toString ());
357- }
357+ synchronized (impl ) {
358+ if (!impl .getConfiguration ().contains (peer )) {
359+ throw new PeerNotInConsensusGroupException (groupId , peer .toString ());
360+ }
358361
359- KillPoint .setKillPoint (IoTConsensusRemovePeerCoordinatorKillPoints .INIT );
362+ KillPoint .setKillPoint (IoTConsensusRemovePeerCoordinatorKillPoints .INIT );
360363
361- // let other peers remove the sync channel with target peer
362- impl .notifyPeersToRemoveSyncLogChannel (peer );
363- KillPoint .setKillPoint (
364- IoTConsensusRemovePeerCoordinatorKillPoints .AFTER_NOTIFY_PEERS_TO_REMOVE_REPLICATE_CHANNEL );
364+ // let other peers remove the sync channel with target peer
365+ impl .notifyPeersToRemoveSyncLogChannel (peer );
366+ KillPoint .setKillPoint (
367+ IoTConsensusRemovePeerCoordinatorKillPoints
368+ .AFTER_NOTIFY_PEERS_TO_REMOVE_REPLICATE_CHANNEL );
365369
366- try {
367- // let target peer reject new write
368- impl .inactivePeer (peer , true );
369- KillPoint .setKillPoint (IoTConsensusRemovePeerCoordinatorKillPoints .AFTER_INACTIVE_PEER );
370- // wait its SyncLog to complete
371- impl .waitTargetPeerUntilSyncLogCompleted (peer );
372- // wait its region related resource to release
373- impl .waitReleaseAllRegionRelatedResource (peer );
374- } catch (ConsensusGroupModifyPeerException e ) {
375- throw new ConsensusException (e .getMessage ());
370+ try {
371+ // let target peer reject new write
372+ impl .inactivePeer (peer , true );
373+ KillPoint .setKillPoint (IoTConsensusRemovePeerCoordinatorKillPoints .AFTER_INACTIVE_PEER );
374+ // wait its SyncLog to complete
375+ impl .waitTargetPeerUntilSyncLogCompleted (peer );
376+ // wait its region related resource to release
377+ impl .waitReleaseAllRegionRelatedResource (peer );
378+ } catch (ConsensusGroupModifyPeerException e ) {
379+ throw new ConsensusException (e .getMessage ());
380+ }
381+ KillPoint .setKillPoint (IoTConsensusRemovePeerCoordinatorKillPoints .FINISH );
376382 }
377- KillPoint .setKillPoint (IoTConsensusRemovePeerCoordinatorKillPoints .FINISH );
378383 }
379384
380385 @ Override
@@ -485,6 +490,7 @@ public void resetPeerList(ConsensusGroupId groupId, List<Peer> correctPeers)
485490 IoTConsensusServerImpl impl =
486491 Optional .ofNullable (stateMachineMap .get (groupId ))
487492 .orElseThrow (() -> new ConsensusGroupNotExistException (groupId ));
493+
488494 Peer localPeer = new Peer (groupId , thisNodeId , thisNode );
489495 if (!correctPeers .contains (localPeer )) {
490496 logger .info (
@@ -493,25 +499,28 @@ public void resetPeerList(ConsensusGroupId groupId, List<Peer> correctPeers)
493499 deleteLocalPeer (groupId );
494500 return ;
495501 }
496- ImmutableList <Peer > currentMembers = ImmutableList .copyOf (impl .getConfiguration ());
497- String previousPeerListStr = currentMembers .toString ();
498- for (Peer peer : currentMembers ) {
499- if (!correctPeers .contains (peer )) {
500- if (!impl .removeSyncLogChannel (peer )) {
501- logger .error (
502- "[RESET PEER LIST] Failed to remove peer {}'s sync log channel from group {}" ,
503- peer ,
504- groupId );
502+
503+ synchronized (impl ) {
504+ ImmutableList <Peer > currentMembers = ImmutableList .copyOf (impl .getConfiguration ());
505+ String previousPeerListStr = currentMembers .toString ();
506+ for (Peer peer : currentMembers ) {
507+ if (!correctPeers .contains (peer )) {
508+ if (!impl .removeSyncLogChannel (peer )) {
509+ logger .error (
510+ "[RESET PEER LIST] Failed to remove peer {}'s sync log channel from group {}" ,
511+ peer ,
512+ groupId );
513+ }
505514 }
506515 }
507- }
508- logger . info (
509- "[RESET PEER LIST] Local peer list has been reset: {} -> {}" ,
510- previousPeerListStr ,
511- impl . getConfiguration ());
512- for ( Peer peer : correctPeers ) {
513- if (! impl . getConfiguration (). contains ( peer )) {
514- logger . warn ( "[RESET PEER LIST] \" Correct peer \" {} is not in local peer list" , peer );
516+ logger . info (
517+ "[RESET PEER LIST] Local peer list has been reset: {} -> {}" ,
518+ previousPeerListStr ,
519+ impl . getConfiguration ());
520+ for ( Peer peer : correctPeers ) {
521+ if (! impl . getConfiguration (). contains ( peer ) ) {
522+ logger . warn ( "[RESET PEER LIST] \" Correct peer\" {} is not in local peer list" , peer );
523+ }
515524 }
516525 }
517526 }
0 commit comments