40
40
import org .opensearch .cluster .node .DiscoveryNode ;
41
41
import org .opensearch .common .settings .Settings ;
42
42
import org .opensearch .common .util .io .IOUtils ;
43
+ import org .opensearch .gateway .remote .ClusterMetadataManifest ;
43
44
44
45
import java .io .Closeable ;
45
46
import java .io .IOException ;
@@ -104,6 +105,7 @@ public CoordinationState(
104
105
.getLastAcceptedConfiguration ();
105
106
this .publishVotes = new VoteCollection ();
106
107
this .isRemoteStateEnabled = isRemoteStoreClusterStateEnabled (settings );
108
+ // ToDo: revisit this check while making the setting dynamic
107
109
this .isRemotePublicationEnabled = isRemoteStateEnabled
108
110
&& REMOTE_PUBLICATION_SETTING .get (settings )
109
111
&& localNode .isRemoteStatePublicationEnabled ();
@@ -459,6 +461,9 @@ public PublishResponse handlePublishRequest(PublishRequest publishRequest) {
459
461
clusterState .term ()
460
462
);
461
463
persistedStateRegistry .getPersistedState (PersistedStateType .LOCAL ).setLastAcceptedState (clusterState );
464
+ if (shouldUpdateRemotePersistedState (publishRequest )) {
465
+ updateRemotePersistedStateOnPublishRequest (publishRequest );
466
+ }
462
467
assert getLastAcceptedState () == clusterState ;
463
468
464
469
return new PublishResponse (clusterState .term (), clusterState .version ());
@@ -571,6 +576,9 @@ public void handleCommit(ApplyCommitRequest applyCommit) {
571
576
);
572
577
573
578
persistedStateRegistry .getPersistedState (PersistedStateType .LOCAL ).markLastAcceptedStateAsCommitted ();
579
+ if (shouldCommitRemotePersistedState ()) {
580
+ persistedStateRegistry .getPersistedState (PersistedStateType .REMOTE ).markLastAcceptedStateAsCommitted ();
581
+ }
574
582
assert getLastCommittedConfiguration ().equals (getLastAcceptedConfiguration ());
575
583
}
576
584
@@ -616,6 +624,33 @@ public void close() throws IOException {
616
624
IOUtils .close (persistedStateRegistry );
617
625
}
618
626
627
+ private boolean shouldUpdateRemotePersistedState (PublishRequest publishRequest ) {
628
+ return persistedStateRegistry .getPersistedState (PersistedStateType .REMOTE ) != null
629
+ && publishRequest .getAcceptedState ().getNodes ().isLocalNodeElectedClusterManager () == false ;
630
+ }
631
+
632
+ private void updateRemotePersistedStateOnPublishRequest (PublishRequest publishRequest ) {
633
+ if (publishRequest instanceof RemoteStatePublishRequest ) {
634
+ persistedStateRegistry .getPersistedState (PersistedStateType .REMOTE ).setLastAcceptedState (publishRequest .getAcceptedState ());
635
+ persistedStateRegistry .getPersistedState (PersistedStateType .REMOTE )
636
+ .setLastAcceptedManifest (((RemoteStatePublishRequest ) publishRequest ).getAcceptedManifest ());
637
+ } else {
638
+ // We will end up here if PublishRequest was sent not using Remote Store even with remote persisted state on this node
639
+ persistedStateRegistry .getPersistedState (PersistedStateType .REMOTE ).setLastAcceptedState (null );
640
+ persistedStateRegistry .getPersistedState (PersistedStateType .REMOTE ).setLastAcceptedManifest (null );
641
+ }
642
+ }
643
+
644
+ private boolean shouldCommitRemotePersistedState () {
645
+ return persistedStateRegistry .getPersistedState (PersistedStateType .REMOTE ) != null
646
+ && persistedStateRegistry .getPersistedState (PersistedStateType .LOCAL )
647
+ .getLastAcceptedState ()
648
+ .getNodes ()
649
+ .isLocalNodeElectedClusterManager () == false
650
+ && persistedStateRegistry .getPersistedState (PersistedStateType .REMOTE ).getLastAcceptedState () != null
651
+ && persistedStateRegistry .getPersistedState (PersistedStateType .REMOTE ).getLastAcceptedManifest () != null ;
652
+ }
653
+
619
654
/**
620
655
* Pluggable persistence layer for {@link CoordinationState}.
621
656
*
@@ -653,6 +688,22 @@ public interface PersistedState extends Closeable {
653
688
*/
654
689
PersistedStateStats getStats ();
655
690
691
+ /**
692
+ * Returns the last accepted {@link ClusterMetadataManifest}.
693
+ *
694
+ * @return The last accepted {@link ClusterMetadataManifest}, or null if no manifest
695
+ * has been accepted yet.
696
+ */
697
+ default ClusterMetadataManifest getLastAcceptedManifest () {
698
+ // return null by default, this method needs to be overridden wherever required
699
+ return null ;
700
+ }
701
+
702
+ /**
703
+ * Sets the last accepted {@link ClusterMetadataManifest}.
704
+ */
705
+ default void setLastAcceptedManifest (ClusterMetadataManifest manifest ) {}
706
+
656
707
/**
657
708
* Marks the last accepted cluster state as committed.
658
709
* After a successful call to this method, {@link #getLastAcceptedState()} should return the last cluster state that was set,
@@ -661,14 +712,7 @@ public interface PersistedState extends Closeable {
661
712
*/
662
713
default void markLastAcceptedStateAsCommitted () {
663
714
final ClusterState lastAcceptedState = getLastAcceptedState ();
664
- Metadata .Builder metadataBuilder = null ;
665
- if (lastAcceptedState .getLastAcceptedConfiguration ().equals (lastAcceptedState .getLastCommittedConfiguration ()) == false ) {
666
- final CoordinationMetadata coordinationMetadata = CoordinationMetadata .builder (lastAcceptedState .coordinationMetadata ())
667
- .lastCommittedConfiguration (lastAcceptedState .getLastAcceptedConfiguration ())
668
- .build ();
669
- metadataBuilder = Metadata .builder (lastAcceptedState .metadata ());
670
- metadataBuilder .coordinationMetadata (coordinationMetadata );
671
- }
715
+ Metadata .Builder metadataBuilder = commitVotingConfiguration (lastAcceptedState );
672
716
// if we receive a commit from a Zen1 cluster-manager that has not recovered its state yet,
673
717
// the cluster uuid might not been known yet.
674
718
assert lastAcceptedState .metadata ().clusterUUID ().equals (Metadata .UNKNOWN_CLUSTER_UUID ) == false
@@ -693,6 +737,18 @@ default void markLastAcceptedStateAsCommitted() {
693
737
}
694
738
}
695
739
740
+ default Metadata .Builder commitVotingConfiguration (ClusterState lastAcceptedState ) {
741
+ Metadata .Builder metadataBuilder = null ;
742
+ if (lastAcceptedState .getLastAcceptedConfiguration ().equals (lastAcceptedState .getLastCommittedConfiguration ()) == false ) {
743
+ final CoordinationMetadata coordinationMetadata = CoordinationMetadata .builder (lastAcceptedState .coordinationMetadata ())
744
+ .lastCommittedConfiguration (lastAcceptedState .getLastAcceptedConfiguration ())
745
+ .build ();
746
+ metadataBuilder = Metadata .builder (lastAcceptedState .metadata ());
747
+ metadataBuilder .coordinationMetadata (coordinationMetadata );
748
+ }
749
+ return metadataBuilder ;
750
+ }
751
+
696
752
default void close () throws IOException {}
697
753
}
698
754
0 commit comments