5050import org .apache .iotdb .commons .auth .entity .PrivilegeType ;
5151import org .apache .iotdb .commons .client .request .AsyncRequestContext ;
5252import org .apache .iotdb .commons .cluster .NodeStatus ;
53+ import org .apache .iotdb .commons .concurrent .Await ;
54+ import org .apache .iotdb .commons .concurrent .AwaitTimeoutException ;
5355import org .apache .iotdb .commons .concurrent .IoTThreadFactory ;
5456import org .apache .iotdb .commons .concurrent .ThreadName ;
5557import org .apache .iotdb .commons .concurrent .threadpool .WrappedThreadPoolExecutor ;
193195import org .apache .iotdb .db .schemaengine .template .TemplateInternalRPCUpdateType ;
194196import org .apache .iotdb .db .schemaengine .template .TemplateInternalRPCUtil ;
195197import org .apache .iotdb .db .service .DataNode ;
198+ import org .apache .iotdb .db .service .DataNode .DataNodeContext ;
196199import org .apache .iotdb .db .service .RegionMigrateService ;
197200import org .apache .iotdb .db .service .externalservice .ExternalServiceManagementService ;
198201import org .apache .iotdb .db .service .metrics .FileMetrics ;
@@ -416,6 +419,8 @@ public class DataNodeInternalRPCServiceImpl implements IDataNodeRPCService.Iface
416419
417420 private final CommonConfig commonConfig = CommonDescriptor .getInstance ().getConfig ();
418421
422+ private final DataNodeContext dataNodeContext ;
423+
419424 private final ExecutorService schemaExecutor =
420425 new WrappedThreadPoolExecutor (
421426 0 ,
@@ -430,10 +435,33 @@ public class DataNodeInternalRPCServiceImpl implements IDataNodeRPCService.Iface
430435
431436 private static final String SYSTEM = "system" ;
432437
433- public DataNodeInternalRPCServiceImpl () {
438+ public DataNodeInternalRPCServiceImpl (DataNodeContext dataNodeContext ) {
434439 super ();
435440 partitionFetcher = ClusterPartitionFetcher .getInstance ();
436441 schemaFetcher = ClusterSchemaFetcher .getInstance ();
442+ this .dataNodeContext = dataNodeContext ;
443+ }
444+
445+ private long consensusWaitTimeoutSeconds = 30 ;
446+
447+ private TSStatus waitForConsensusStarted () {
448+ if (dataNodeContext .isAllConsensusStarted ()) {
449+ return null ;
450+ }
451+ try {
452+ Await .await ()
453+ .atMost (consensusWaitTimeoutSeconds , TimeUnit .SECONDS )
454+ .pollInterval (100 , TimeUnit .MILLISECONDS )
455+ .until (dataNodeContext ::isAllConsensusStarted );
456+ return null ;
457+ } catch (AwaitTimeoutException e ) {
458+ LOGGER .warn (
459+ "Consensus has not been started after {} seconds, rejecting region request" ,
460+ consensusWaitTimeoutSeconds );
461+ return RpcUtils .getStatus (
462+ TSStatusCode .CONSENSUS_NOT_INITIALIZED ,
463+ "Consensus has not been started after " + consensusWaitTimeoutSeconds + " seconds" );
464+ }
437465 }
438466
439467 @ Override
@@ -624,11 +652,19 @@ private TLoadResp createTLoadResp(final TSStatus resultStatus) {
624652
625653 @ Override
626654 public TSStatus createSchemaRegion (final TCreateSchemaRegionReq req ) {
655+ TSStatus consensusStatus = waitForConsensusStarted ();
656+ if (consensusStatus != null ) {
657+ return consensusStatus ;
658+ }
627659 return regionManager .createSchemaRegion (req .getRegionReplicaSet (), req .getStorageGroup ());
628660 }
629661
630662 @ Override
631663 public TSStatus createDataRegion (TCreateDataRegionReq req ) {
664+ TSStatus consensusStatus = waitForConsensusStarted ();
665+ if (consensusStatus != null ) {
666+ return consensusStatus ;
667+ }
632668 return regionManager .createDataRegion (req .getRegionReplicaSet (), req .getStorageGroup ());
633669 }
634670
@@ -2616,6 +2652,10 @@ public TSStatus updateTemplate(final TUpdateTemplateReq req) {
26162652
26172653 @ Override
26182654 public TSStatus deleteRegion (TConsensusGroupId tconsensusGroupId ) {
2655+ TSStatus consensusStatus = waitForConsensusStarted ();
2656+ if (consensusStatus != null ) {
2657+ return consensusStatus ;
2658+ }
26192659 ConsensusGroupId consensusGroupId =
26202660 ConsensusGroupId .Factory .createFromTConsensusGroupId (tconsensusGroupId );
26212661 if (consensusGroupId instanceof DataRegionId ) {
@@ -2644,6 +2684,12 @@ public TRegionLeaderChangeResp changeRegionLeader(TRegionLeaderChangeReq req) {
26442684 LOGGER .info ("[ChangeRegionLeader] {}" , req );
26452685 TRegionLeaderChangeResp resp = new TRegionLeaderChangeResp ();
26462686
2687+ TSStatus consensusStatus = waitForConsensusStarted ();
2688+ if (consensusStatus != null ) {
2689+ resp .setStatus (consensusStatus );
2690+ return resp ;
2691+ }
2692+
26472693 TSStatus successStatus = new TSStatus (TSStatusCode .SUCCESS_STATUS .getStatusCode ());
26482694 TConsensusGroupId tgId = req .getRegionId ();
26492695 ConsensusGroupId regionId = ConsensusGroupId .Factory .createFromTConsensusGroupId (tgId );
@@ -2713,6 +2759,10 @@ private boolean isLeader(ConsensusGroupId regionId) {
27132759
27142760 @ Override
27152761 public TSStatus createNewRegionPeer (TCreatePeerReq req ) {
2762+ TSStatus consensusStatus = waitForConsensusStarted ();
2763+ if (consensusStatus != null ) {
2764+ return consensusStatus ;
2765+ }
27162766 ConsensusGroupId regionId =
27172767 ConsensusGroupId .Factory .createFromTConsensusGroupId (req .getRegionId ());
27182768 List <Peer > peers =
@@ -2733,6 +2783,10 @@ public TSStatus createNewRegionPeer(TCreatePeerReq req) {
27332783
27342784 @ Override
27352785 public TSStatus addRegionPeer (TMaintainPeerReq req ) {
2786+ TSStatus consensusStatus = waitForConsensusStarted ();
2787+ if (consensusStatus != null ) {
2788+ return consensusStatus ;
2789+ }
27362790 TConsensusGroupId regionId = req .getRegionId ();
27372791 String selectedDataNodeIP = req .getDestNode ().getInternalEndPoint ().getIp ();
27382792 boolean submitSucceed = RegionMigrateService .getInstance ().submitAddRegionPeerTask (req );
@@ -2751,6 +2805,10 @@ public TSStatus addRegionPeer(TMaintainPeerReq req) {
27512805
27522806 @ Override
27532807 public TSStatus removeRegionPeer (TMaintainPeerReq req ) {
2808+ TSStatus consensusStatus = waitForConsensusStarted ();
2809+ if (consensusStatus != null ) {
2810+ return consensusStatus ;
2811+ }
27542812 TConsensusGroupId regionId = req .getRegionId ();
27552813 String selectedDataNodeIP = req .getDestNode ().getInternalEndPoint ().getIp ();
27562814 boolean submitSucceed = RegionMigrateService .getInstance ().submitRemoveRegionPeerTask (req );
@@ -2769,6 +2827,10 @@ public TSStatus removeRegionPeer(TMaintainPeerReq req) {
27692827
27702828 @ Override
27712829 public TSStatus deleteOldRegionPeer (TMaintainPeerReq req ) {
2830+ TSStatus consensusStatus = waitForConsensusStarted ();
2831+ if (consensusStatus != null ) {
2832+ return consensusStatus ;
2833+ }
27722834 TConsensusGroupId regionId = req .getRegionId ();
27732835 String selectedDataNodeIP = req .getDestNode ().getInternalEndPoint ().getIp ();
27742836 boolean submitSucceed = RegionMigrateService .getInstance ().submitDeleteOldRegionPeerTask (req );
@@ -2788,6 +2850,10 @@ public TSStatus deleteOldRegionPeer(TMaintainPeerReq req) {
27882850 // TODO: return which DataNode fail
27892851 @ Override
27902852 public TSStatus resetPeerList (TResetPeerListReq req ) throws TException {
2853+ TSStatus consensusStatus = waitForConsensusStarted ();
2854+ if (consensusStatus != null ) {
2855+ return consensusStatus ;
2856+ }
27912857 return RegionMigrateService .getInstance ().resetPeerList (req );
27922858 }
27932859
@@ -2798,6 +2864,10 @@ public TRegionMigrateResult getRegionMaintainResult(long taskId) throws TExcepti
27982864
27992865 @ Override
28002866 public TSStatus notifyRegionMigration (TNotifyRegionMigrationReq req ) throws TException {
2867+ TSStatus consensusStatus = waitForConsensusStarted ();
2868+ if (consensusStatus != null ) {
2869+ return consensusStatus ;
2870+ }
28012871 RegionMigrateService .getInstance ().notifyRegionMigration (req );
28022872 return new TSStatus (TSStatusCode .SUCCESS_STATUS .getStatusCode ());
28032873 }
@@ -3439,4 +3509,8 @@ private List<ByteBuffer> serializeDatabaseScopedTableList(
34393509
34403510 return result ;
34413511 }
3512+
3513+ public void setConsensusWaitTimeoutSeconds (long consensusWaitTimeoutSeconds ) {
3514+ this .consensusWaitTimeoutSeconds = consensusWaitTimeoutSeconds ;
3515+ }
34423516}
0 commit comments