@@ -48,7 +48,9 @@ public void upload(RotatingSaltProvider.SaltSnapshot data) throws Exception {
4848 }
4949
5050 public void upload (RotatingSaltProvider .SaltSnapshot data , int iterations ) throws Exception {
51- this .buildAndUploadMetadata (this .getSnapshots (data , iterations ));
51+ List <RotatingSaltProvider .SaltSnapshot > snapshots = this .getSnapshots (data , iterations );
52+ this .buildAndUploadMetadata (snapshots );
53+ this .deleteOtherSnapshots (snapshots );
5254 refreshProvider ();
5355 }
5456
@@ -103,36 +105,18 @@ private List<RotatingSaltProvider.SaltSnapshot> getSnapshots(RotatingSaltProvide
103105 return filteredSnapshots ;
104106 }
105107
106- private List <RotatingSaltProvider .SaltSnapshot > getSnapshots (RotatingSaltProvider .SaltSnapshot data ){
107- if (provider .getSnapshots () == null ) {
108- throw new IllegalStateException ("Snapshots cannot be null" );
109- }
110- final Instant now = Instant .now ();
111- List <RotatingSaltProvider .SaltSnapshot > currentSnapshots = provider .getSnapshots ();
112- List <RotatingSaltProvider .SaltSnapshot > snapshots = null ;
113- snapshots = Stream .concat (currentSnapshots .stream (), Stream .of (data ))
114- .sorted (Comparator .comparing (RotatingSaltProvider .SaltSnapshot ::getEffective ))
115- .toList ();
116- RotatingSaltProvider .SaltSnapshot newestEffectiveSnapshot = snapshots .stream ()
117- .filter (snapshot -> snapshot .isEffective (now ))
118- .reduce ((a , b ) -> b ).orElse (null );
119-
120- List <RotatingSaltProvider .SaltSnapshot > filteredSnapshots = new ArrayList <>();
108+ private List <RotatingSaltProvider .SaltSnapshot > getSnapshots (RotatingSaltProvider .SaltSnapshot data ) {
109+ return getSnapshots (data , 0 );
110+ }
121111
122- for (RotatingSaltProvider .SaltSnapshot snapshot : snapshots ) {
123- if (!now .isBefore (snapshot .getExpires ())) {
124- LOGGER .info ("Skipping expired snapshot, effective=" + snapshot .getEffective () + ", expires=" + snapshot .getExpires ());
125- continue ;
126- }
127- if (newestEffectiveSnapshot != null && snapshot != newestEffectiveSnapshot ) {
128- LOGGER .info ("Skipping effective snapshot, effective=" + snapshot .getEffective () + ", expires=" + snapshot .getExpires ()
129- + " in favour of newer snapshot, effective=" + newestEffectiveSnapshot .getEffective () + ", expires=" + newestEffectiveSnapshot .getExpires ());
130- continue ;
131- }
132- filteredSnapshots .add (snapshot );
133- newestEffectiveSnapshot = null ;
112+ private void deleteOtherSnapshots (List <RotatingSaltProvider .SaltSnapshot > filteredSnapshots ) {
113+ List <RotatingSaltProvider .SaltSnapshot > currentSnapshots = provider .getSnapshots ();
114+ List <RotatingSaltProvider .SaltSnapshot > outdatedSnapshots = currentSnapshots .stream ().filter (snapshot -> !filteredSnapshots .contains (snapshot )).toList ();
115+ try {
116+ this .cloudStorage .delete (outdatedSnapshots .stream ().map (this ::getSaltSnapshotLocation ).toList ());
117+ } catch (Exception e ) {
118+ LOGGER .error ("Error deleting snapshots" , e );
134119 }
135- return filteredSnapshots ;
136120 }
137121
138122 protected JsonObject getMetadata () throws Exception {
0 commit comments