99
1010package org .elasticsearch .gradle .internal .transport ;
1111
12+ import org .elasticsearch .gradle .internal .transport .TransportVersionResourcesService .IdAndDefinition ;
1213import org .gradle .api .DefaultTask ;
1314import org .gradle .api .file .ConfigurableFileCollection ;
1415import org .gradle .api .file .RegularFileProperty ;
3132import java .util .Collections ;
3233import java .util .HashSet ;
3334import java .util .List ;
35+ import java .util .Map ;
3436import java .util .Set ;
3537
3638/**
@@ -95,29 +97,39 @@ public abstract class GenerateTransportVersionDefinitionTask extends DefaultTask
9597 public void run () throws IOException {
9698 TransportVersionResourcesService resources = getResourceService ().get ();
9799 Set <String > referencedNames = TransportVersionReference .collectNames (getReferencesFiles ());
98- List <String > changedDefinitionNames = resources .getChangedReferableDefinitionNames ();
100+ Set <String > changedDefinitionNames = resources .getChangedReferableDefinitionNames ();
99101 String targetDefinitionName = getTargetDefinitionName (resources , referencedNames , changedDefinitionNames );
100102
101- getLogger ().lifecycle ("Generating transport version name: " + targetDefinitionName );
103+ // First check for any unused definitions. This later generation to not get confused by a definition that can't be used.
104+ removeUnusedNamedDefinitions (resources , referencedNames , changedDefinitionNames );
105+
106+ Map <Integer , List <IdAndDefinition >> idsByBase = resources .getIdsByBase ();
102107 if (targetDefinitionName .isEmpty ()) {
103- // TODO: resetting upper bounds needs to be done locally, otherwise it pulls in some (incomplete) changes from upstream main
104- // resetAllUpperBounds(resources);
108+ getLogger (). lifecycle ( "No transport version name detected, resetting upper bounds" );
109+ resetAllUpperBounds (resources , idsByBase );
105110 } else {
111+ getLogger ().lifecycle ("Generating transport version name: " + targetDefinitionName );
106112 List <TransportVersionUpperBound > upstreamUpperBounds = resources .getUpperBoundsFromUpstream ();
107113 Set <String > targetUpperBoundNames = getTargetUpperBoundNames (resources , upstreamUpperBounds , targetDefinitionName );
108114
109- List <TransportVersionId > ids = updateUpperBounds (resources , upstreamUpperBounds , targetUpperBoundNames , targetDefinitionName );
115+ List <TransportVersionId > ids = updateUpperBounds (
116+ resources ,
117+ upstreamUpperBounds ,
118+ targetUpperBoundNames ,
119+ idsByBase ,
120+ targetDefinitionName
121+ );
110122 // (Re)write the definition file.
111123 resources .writeDefinition (new TransportVersionDefinition (targetDefinitionName , ids , true ));
112124 }
113125
114- removeUnusedNamedDefinitions (resources , referencedNames , changedDefinitionNames );
115126 }
116127
117128 private List <TransportVersionId > updateUpperBounds (
118129 TransportVersionResourcesService resources ,
119130 List <TransportVersionUpperBound > existingUpperBounds ,
120131 Set <String > targetUpperBoundNames ,
132+ Map <Integer , List <IdAndDefinition >> idsByBase ,
121133 String definitionName
122134 ) throws IOException {
123135 String currentUpperBoundName = getCurrentUpperBoundName ().get ();
@@ -146,9 +158,9 @@ private List<TransportVersionId> updateUpperBounds(
146158 resources .writeUpperBound (newUpperBound , stageInGit );
147159 }
148160 ids .add (targetId );
149- } else {
161+ } else if ( resources . getChangedUpperBoundNames (). contains ( upperBoundName )) {
150162 // Default case: we're not targeting this branch so reset it
151- resources . writeUpperBound ( existingUpperBound , false );
163+ resetUpperBound ( resources , existingUpperBound , idsByBase , definitionName );
152164 }
153165 }
154166
@@ -159,7 +171,7 @@ private List<TransportVersionId> updateUpperBounds(
159171 private String getTargetDefinitionName (
160172 TransportVersionResourcesService resources ,
161173 Set <String > referencedNames ,
162- List <String > changedDefinitions
174+ Set <String > changedDefinitions
163175 ) {
164176 if (getDefinitionName ().isPresent ()) {
165177 // an explicit name was passed in, so use it
@@ -180,7 +192,7 @@ private String getTargetDefinitionName(
180192 if (changedDefinitions .isEmpty ()) {
181193 return "" ;
182194 } else {
183- String changedDefinitionName = changedDefinitions .getFirst ();
195+ String changedDefinitionName = changedDefinitions .iterator (). next ();
184196 if (referencedNames .contains (changedDefinitionName )) {
185197 return changedDefinitionName ;
186198 } else {
@@ -248,16 +260,38 @@ private Set<String> getUpperBoundNamesFromDefinition(
248260 return upperBoundNames ;
249261 }
250262
251- private void resetAllUpperBounds (TransportVersionResourcesService resources ) throws IOException {
252- for (TransportVersionUpperBound upperBound : resources .getUpperBoundsFromUpstream ()) {
253- resources .writeUpperBound (upperBound , false );
263+ private void resetAllUpperBounds (TransportVersionResourcesService resources , Map <Integer , List <IdAndDefinition >> idsByBase )
264+ throws IOException {
265+ for (String upperBoundName : resources .getChangedUpperBoundNames ()) {
266+ TransportVersionUpperBound upstreamUpperBound = resources .getUpperBoundFromUpstream (upperBoundName );
267+ resetUpperBound (resources , upstreamUpperBound , idsByBase , null );
268+ }
269+ }
270+
271+ private void resetUpperBound (
272+ TransportVersionResourcesService resources ,
273+ TransportVersionUpperBound upperBound ,
274+ Map <Integer , List <IdAndDefinition >> idsByBase ,
275+ String ignoreDefinitionName
276+ ) throws IOException {
277+ List <IdAndDefinition > idsForUpperBound = idsByBase .get (upperBound .definitionId ().base ());
278+ if (idsForUpperBound == null ) {
279+ throw new RuntimeException ("Could not find base id: " + upperBound .definitionId ().base ());
280+ }
281+ IdAndDefinition resetValue = idsForUpperBound .getLast ();
282+ if (resetValue .definition ().name ().equals (ignoreDefinitionName )) {
283+ // there must be another definition in this base since the ignored definition is new
284+ assert idsForUpperBound .size () >= 2 ;
285+ resetValue = idsForUpperBound .get (idsForUpperBound .size () - 2 );
254286 }
287+ var resetUpperBound = new TransportVersionUpperBound (upperBound .name (), resetValue .definition ().name (), resetValue .id ());
288+ resources .writeUpperBound (resetUpperBound , false );
255289 }
256290
257291 private void removeUnusedNamedDefinitions (
258292 TransportVersionResourcesService resources ,
259293 Set <String > referencedNames ,
260- List <String > changedDefinitions
294+ Set <String > changedDefinitions
261295 ) throws IOException {
262296 for (String definitionName : changedDefinitions ) {
263297 if (referencedNames .contains (definitionName ) == false ) {
0 commit comments