Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/jujuPublic

Commita41d716

Browse files
committed
feat(cmr): rewrite RemoveRelationWithRemoteConsumer to handle consumers
We recently duplicated RemoveRelationWithRemoteOfferer to establishboiler plate for RemoveRelationWtihRemoteConsumerRewrite these functions to handle the specifics ofRelationsWithRemoteConsumers
1 parenteac52a0 commita41d716

File tree

9 files changed

+92
-123
lines changed

9 files changed

+92
-123
lines changed

‎domain/removal/service/relationwithremoteconsumer.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (s *Service) RemoveRelationWithRemoteConsumer(
6363

6464
res,err:=s.modelState.EnsureRelationWithRemoteConsumerNotAliveCascade(ctx,relUUID.String())
6565
iferr!=nil {
66-
return"",errors.Errorf("remote relation %q: %w",relUUID,err)
66+
return"",errors.Errorf("settingremote relation %q to dying: %w",relUUID,err)
6767
}
6868

6969
varjUUID removal.UUID
@@ -111,7 +111,7 @@ func (s *Service) relationWithRemoteConsumerScheduleRemoval(
111111
iferr:=s.modelState.RelationWithRemoteConsumerScheduleRemoval(
112112
ctx,jobUUID.String(),relUUID.String(),force,s.clock.Now().UTC().Add(wait),
113113
);err!=nil {
114-
return"",errors.Errorf("remote relation %q: %w",relUUID,err)
114+
return"",errors.Errorf("schedulingremote relation %q for removal: %w",relUUID,err)
115115
}
116116

117117
s.logger.Infof(ctx,"scheduled removal job %q for remote relation %q",jobUUID,relUUID)

‎domain/removal/service/relationwithremoteofferer.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (s *Service) RemoveRelationWithRemoteOfferer(
6363

6464
res,err:=s.modelState.EnsureRelationWithRemoteOffererNotAliveCascade(ctx,relUUID.String())
6565
iferr!=nil {
66-
return"",errors.Errorf("remote relation %q: %w",relUUID,err)
66+
return"",errors.Errorf("settingremote relation %q to dying: %w",relUUID,err)
6767
}
6868

6969
varjUUID removal.UUID
@@ -91,6 +91,7 @@ func (s *Service) RemoveRelationWithRemoteOfferer(
9191

9292
// Depart the synthetic units here ourselves, since synthetic units don't
9393
// have their own uniter.
94+
// TODO: This should ideally be handled in the same transaction as the cascade
9495
for_,r:=rangeres.SyntheticRelationUnitUUIDs {
9596
iferr:=s.modelState.LeaveScope(ctx,r);err!=nil {
9697
return"",errors.Errorf("leaving scope for synthetic relation unit %q: %w",r,err)
@@ -111,7 +112,7 @@ func (s *Service) relationWithRemoteOffererScheduleRemoval(
111112
iferr:=s.modelState.RelationWithRemoteOffererScheduleRemoval(
112113
ctx,jobUUID.String(),relUUID.String(),force,s.clock.Now().UTC().Add(wait),
113114
);err!=nil {
114-
return"",errors.Errorf("remote relation %q: %w",relUUID,err)
115+
return"",errors.Errorf("schedulingremote relation %q for removal: %w",relUUID,err)
115116
}
116117

117118
s.logger.Infof(ctx,"scheduled removal job %q for remote relation %q",jobUUID,relUUID)

‎domain/removal/state/model/offer.go‎

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,24 @@ WHERE uuid IN ($uuids[:])
103103
returnerrors.Errorf("preparing delete relations query: %w",err)
104104
}
105105

106+
// offer connection uuid is set to match it's the synthetic application uuid
106107
getSynthAppsStmt,err:=st.Prepare(`
107-
SELECT (arc.consumer_application_uuid, oc.uuid) AS (&consumerApplicationUUID.*)
108-
FROM application_remote_consumer AS arc
109-
JOIN offer_connection AS oc ON arc.offer_connection_uuid = oc.uuid
110-
WHERE oc.offer_uuid = $entityUUID.uuid
111-
`,entityUUID{},consumerApplicationUUID{})
108+
SELECT &entityUUID.*
109+
FROM offer_connection
110+
WHERE offer_uuid = $entityUUID.uuid
111+
`,entityUUID{})
112112
iferr!=nil {
113113
returnerrors.Errorf("preparing synthetic application query: %w",err)
114114
}
115115

116+
deleteRemoteApplicationConsumerStmt,err:=st.Prepare(`
117+
DELETE FROM application_remote_consumer
118+
WHERE offer_connection_uuid = $entityUUID.uuid
119+
`,entityUUID{})
120+
iferr!=nil {
121+
returnerrors.Errorf("preparing delete remote application consumer query: %w",err)
122+
}
123+
116124
deleteOfferConnectionStmt,err:=st.Prepare(`
117125
DELETE FROM offer_connection
118126
WHERE offer_uuid = $entityUUID.uuid
@@ -159,7 +167,7 @@ WHERE uuid = $entityUUID.uuid
159167
}
160168
relUUIDs:=uuids(transform.Slice(synthRelationUUIDs,func(eentityUUID)string {returne.UUID }))
161169

162-
varsynthAppUUIDs []consumerApplicationUUID
170+
varsynthAppUUIDs []entityUUID
163171
err=tx.Query(ctx,getSynthAppsStmt,offerUUID).GetAll(&synthAppUUIDs)
164172
iferr!=nil&&!errors.Is(err,sqlair.ErrNoRows) {
165173
returnerrors.Errorf("getting synthetic application UUIDs: %w",err)
@@ -169,9 +177,13 @@ WHERE uuid = $entityUUID.uuid
169177
returnerrors.Errorf("deleting relation endpoints: %w",err)
170178
}
171179

172-
for_,uuids:=rangesynthAppUUIDs {
173-
iferr:=st.deleteRemoteApplicationConsumer(ctx,tx,uuids);err!=nil {
174-
returnerrors.Errorf("deleting remote application %q consumer: %w",uuids.ConsumerApplicationUUID,err)
180+
for_,synthAppUUID:=rangesynthAppUUIDs {
181+
iferr:=tx.Query(ctx,deleteRemoteApplicationConsumerStmt,synthAppUUID).Run();err!=nil {
182+
returnerrors.Errorf("deleting synthetic application remote consumer: %w",err)
183+
}
184+
185+
iferr:=st.deleteSynthApplication(ctx,tx,synthAppUUID);err!=nil {
186+
returnerrors.Errorf("deleting synthetic application: %w",err)
175187
}
176188
}
177189

@@ -198,23 +210,3 @@ WHERE uuid = $entityUUID.uuid
198210

199211
returnnil
200212
}
201-
202-
func (st*State)deleteRemoteApplicationConsumer(ctx context.Context,tx*sqlair.TX,uuidsconsumerApplicationUUID)error {
203-
deleteRemoteApplicationConsumerStmt,err:=st.Prepare(`
204-
DELETE FROM application_remote_consumer
205-
WHERE consumer_application_uuid = $entityUUID.uuid
206-
`,entityUUID{})
207-
iferr!=nil {
208-
returnerrors.Errorf("preparing delete remote application consumer query: %w",err)
209-
}
210-
211-
iferr:=tx.Query(ctx,deleteRemoteApplicationConsumerStmt,entityUUID{UUID:uuids.ConsumerApplicationUUID}).Run();err!=nil {
212-
returnerrors.Errorf("deleting synthetic application remote consumer: %w",err)
213-
}
214-
215-
iferr:=st.deleteSynthApplication(ctx,tx,entityUUID{UUID:uuids.SynthApplicationUUID});err!=nil {
216-
returnerrors.Errorf("deleting synthetic application: %w",err)
217-
}
218-
219-
returnnil
220-
}

‎domain/removal/state/model/offer_test.go‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ func (s *offerSuite) TestDeleteOfferForceWithRelations(c *tc.C) {
8181
offerUUID:=s.createOffer(c,"foo")
8282
s.createRemoteApplicationConsumer(c,"bar",offerUUID)
8383

84-
s.DumpTable(c,"relation","application_remote_consumer","application")
85-
8684
st:=NewState(s.TxnRunnerFactory(),loggertesting.WrapCheckLog(c))
8785

8886
err:=st.DeleteOffer(c.Context(),offerUUID.String(),true)

‎domain/removal/state/model/relationwithremoteconsumer.go‎

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ AND cs.name = 'cmr'`, remoteRelationUUID)
5656

5757
// EnsureRelationWithRemoteConsumerNotAliveCascade ensures that the relation identified
5858
// by the input UUID is not alive, and sets the synthetic units in scope
59-
// of this relation to dead.
59+
// of this relation to dead. We do this because synthetic units do not have a
60+
// uniter, so we need to handle their life ourselves.
6061
func (st*State)EnsureRelationWithRemoteConsumerNotAliveCascade(ctx context.Context,rUUIDstring) (internal.CascadedRelationWithRemoteConsumerLives,error) {
6162
db,err:=st.DB(ctx)
6263
iferr!=nil {
@@ -198,36 +199,19 @@ AND cs.name = 'cmr'`, remoteRelationUUID)
198199
returnerrors.Errorf("preparing remote relation application UUID query: %w",err)
199200
}
200201

201-
countSyntheticAppRelationsStmt,err:=st.Prepare(`
202-
SELECT COUNT(*) AS &count.count
203-
FROM relation_endpoint AS re
204-
JOIN application_endpoint AS ae ON re.endpoint_uuid = ae.uuid
205-
WHERE ae.application_uuid = $entityUUID.uuid
206-
`,count{},entityUUID{})
207-
iferr!=nil {
208-
returnerrors.Errorf("preparing remote relation application relation count query: %w",err)
209-
}
210-
211-
deleteRelationNetworkEgressStmt,err:=st.Prepare(`
212-
DELETE FROM relation_network_egress
202+
deleteRelationNetworkIngressStmt,err:=st.Prepare(`
203+
DELETE FROM relation_network_ingress
213204
WHERE relation_uuid = $entityUUID.uuid`,entityUUID{})
214205
iferr!=nil {
215206
returnerrors.Errorf("preparing relation network egress deletion: %w",err)
216207
}
217208

218-
deleteRemoteOffererRelationMacaroonStmt,err:=st.Prepare(`
219-
DELETE FROM application_remote_offerer_relation_macaroon
220-
WHERE relation_uuid = $entityUUID.uuid`,entityUUID{})
221-
iferr!=nil {
222-
returnerrors.Errorf("preparing remote relation macaroon deletion: %w",err)
223-
}
224-
225-
deleteSyntheticUnitsStmt,err:=st.Prepare(`
226-
DELETE FROM unit
227-
WHERE application_uuid = $entityUUID.uuid
209+
deleteOfferConnectionStmt,err:=st.Prepare(`
210+
DELETE FROM offer_connection
211+
WHERE remote_relation_uuid = $entityUUID.uuid
228212
`,entityUUID{})
229213
iferr!=nil {
230-
returnerrors.Errorf("preparingremote relation unit deletion: %w",err)
214+
returnerrors.Errorf("preparingoffer connection deletion: %w",err)
231215
}
232216

233217
returnerrors.Capture(db.Txn(ctx,func(ctx context.Context,tx*sqlair.TX)error {
@@ -237,32 +221,36 @@ WHERE application_uuid = $entityUUID.uuid
237221
returnerrors.Errorf("getting application UUID: %w",err)
238222
}
239223

240-
err=tx.Query(ctx,deleteRelationNetworkEgressStmt,remoteRelationUUID).Run()
224+
err=tx.Query(ctx,deleteRelationNetworkIngressStmt,remoteRelationUUID).Run()
241225
iferr!=nil {
242226
returnerrors.Errorf("running relation network egress deletion: %w",err)
243227
}
244228

245-
err=tx.Query(ctx,deleteRemoteOffererRelationMacaroonStmt,remoteRelationUUID).Run()
229+
deleteRemoteApplicationConsumerStmt,err:=st.Prepare(`
230+
DELETE FROM application_remote_consumer
231+
WHERE offer_connection_uuid = $entityUUID.uuid
232+
`,synthAppUUID)
246233
iferr!=nil {
247-
returnerrors.Errorf("runningremoterelation macaroon deletion: %w",err)
234+
returnerrors.Errorf("preparing deleteremoteapplication consumer query: %w",err)
248235
}
249236

250-
err:=st.deleteRelation(ctx,tx,remoteRelationUUID)
237+
err=tx.Query(ctx,deleteRemoteApplicationConsumerStmt,synthAppUUID).Run()
251238
iferr!=nil {
252-
returnerrors.Capture(err)
239+
returnerrors.Errorf("deleting synthetic application remote consumer: %w",err)
240+
}
241+
242+
err=tx.Query(ctx,deleteOfferConnectionStmt,remoteRelationUUID).Run()
243+
iferr!=nil {
244+
returnerrors.Errorf("running offer connection deletion: %w",err)
253245
}
254246

255-
varrelationCountcount
256-
err=tx.Query(ctx,countSyntheticAppRelationsStmt,synthAppUUID).Get(&relationCount)
247+
err=st.deleteRelation(ctx,tx,remoteRelationUUID)
257248
iferr!=nil {
258-
returnerrors.Errorf("getting relation count: %w",err)
249+
returnerrors.Capture(err)
259250
}
260251

261-
ifrelationCount.Count==0 {
262-
err=tx.Query(ctx,deleteSyntheticUnitsStmt,synthAppUUID).Run()
263-
iferr!=nil {
264-
returnerrors.Errorf("running unit deletion: %w",err)
265-
}
252+
iferr:=st.deleteSynthApplication(ctx,tx,synthAppUUID);err!=nil {
253+
returnerrors.Errorf("deleting synthetic application: %w",err)
266254
}
267255

268256
returnnil

‎domain/removal/state/model/relationwithremoteconsumer_test.go‎

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/juju/tc"
1313

14-
applicationservice"github.com/juju/juju/domain/application/service"
1514
"github.com/juju/juju/domain/life"
1615
removalerrors"github.com/juju/juju/domain/removal/errors"
1716
loggertesting"github.com/juju/juju/internal/logger/testing"
@@ -26,7 +25,7 @@ func TestRelationWithRemoteConsumerSuite(t *testing.T) {
2625
}
2726

2827
func (s*relationWithRemoteConsumer)TestRelationWithRemoteConsumerExists(c*tc.C) {
29-
relUUID,_:=s.createRelationWithRemoteOfferer(c)
28+
relUUID,_:=s.createRelationWithRemoteConsumer(c)
3029

3130
st:=NewState(s.TxnRunnerFactory(),loggertesting.WrapCheckLog(c))
3231

@@ -55,7 +54,7 @@ func (s *relationWithRemoteConsumer) TestRelationWithRemoteConsumerExistsFalseFo
5554
}
5655

5756
func (s*relationWithRemoteConsumer)TestEnsureRelationWithRemoteConsumerNotAliveCascadeNormalSuccess(c*tc.C) {
58-
relUUID,synthAppUUID:=s.createRelationWithRemoteOfferer(c)
57+
relUUID,synthAppUUID:=s.createRelationWithRemoteConsumer(c)
5958

6059
st:=NewState(s.TxnRunnerFactory(),loggertesting.WrapCheckLog(c))
6160

@@ -112,7 +111,7 @@ func (s *relationWithRemoteConsumer) TestEnsureRelationWithRemoteConsumerNotAliv
112111
}
113112

114113
func (s*relationWithRemoteConsumer)TestRelationWithRemoteConsumerScheduleRemovalNormalSuccess(c*tc.C) {
115-
relUUID,_:=s.createRelationWithRemoteOfferer(c)
114+
relUUID,_:=s.createRelationWithRemoteConsumer(c)
116115

117116
st:=NewState(s.TxnRunnerFactory(),loggertesting.WrapCheckLog(c))
118117

@@ -174,7 +173,7 @@ where r.uuid = ?`, "removal-uuid",
174173
}
175174

176175
func (s*relationWithRemoteConsumer)TestDeleteRelationWithRemoteConsumerUnitsUnitsStillInScope(c*tc.C) {
177-
relUUID,_:=s.createRelationWithRemoteOfferer(c)
176+
relUUID,_:=s.createRelationWithRemoteConsumer(c)
178177

179178
s.advanceRelationLife(c,relUUID,life.Dying)
180179

@@ -186,7 +185,7 @@ func (s *relationWithRemoteConsumer) TestDeleteRelationWithRemoteConsumerUnitsUn
186185

187186
func (s*relationWithRemoteConsumer)TestDeleteRelationWithRemoteConsumerUnits(c*tc.C) {
188187
// Arrange
189-
relUUID,synthAppUUID:=s.createRelationWithRemoteOfferer(c)
188+
relUUID,synthAppUUID:=s.createRelationWithRemoteConsumer(c)
190189

191190
s.advanceRelationLife(c,relUUID,life.Dying)
192191

@@ -201,46 +200,10 @@ func (s *relationWithRemoteConsumer) TestDeleteRelationWithRemoteConsumerUnits(c
201200
// Assert
202201
c.Assert(err,tc.ErrorIsNil)
203202

204-
// The synth app shouldNOTbe deleted.
203+
// The synth app should be deleted.
205204
row:=s.DB().QueryRow("SELECT COUNT(*) FROM application WHERE uuid = ?",synthAppUUID.String())
206205
varcountint
207206
err=row.Scan(&count)
208207
c.Assert(err,tc.ErrorIsNil)
209-
c.Check(count,tc.Equals,1)
210-
211-
// But the synth units should be cleaned up.
212-
row=s.DB().QueryRow("SELECT COUNT(*) FROM unit WHERE application_uuid = ?",synthAppUUID.String())
213-
err=row.Scan(&count)
214-
c.Assert(err,tc.ErrorIsNil)
215208
c.Check(count,tc.Equals,0)
216209
}
217-
218-
func (s*relationWithRemoteConsumer)TestDeleteRelationWithRemoteConsumerWhenRemoteAppHasMultipleRelations(c*tc.C) {
219-
synthAppUUID,_:=s.createRemoteApplicationOfferer(c,"foo")
220-
s.createIAASApplication(c,s.setupApplicationService(c),"app1",
221-
applicationservice.AddIAASUnitArg{},
222-
)
223-
s.createIAASApplication(c,s.setupApplicationService(c),"app2",
224-
applicationservice.AddIAASUnitArg{},
225-
)
226-
relUUID:=s.createRemoteRelationBetween(c,"foo","app1")
227-
s.createRemoteRelationBetween(c,"foo","app2")
228-
229-
st:=NewState(s.TxnRunnerFactory(),loggertesting.WrapCheckLog(c))
230-
231-
err:=st.DeleteRelationWithRemoteConsumer(c.Context(),relUUID.String())
232-
c.Assert(err,tc.ErrorIsNil)
233-
234-
// The synth app should NOT be deleted.
235-
row:=s.DB().QueryRow("SELECT COUNT(*) FROM application WHERE uuid = ?",synthAppUUID.String())
236-
varcountint
237-
err=row.Scan(&count)
238-
c.Assert(err,tc.ErrorIsNil)
239-
c.Check(count,tc.Equals,1)
240-
241-
// And the synth units should also NOT be deleted.
242-
row=s.DB().QueryRow("SELECT COUNT(*) FROM unit WHERE application_uuid = ?",synthAppUUID.String())
243-
err=row.Scan(&count)
244-
c.Assert(err,tc.ErrorIsNil)
245-
c.Check(count,tc.Equals,3)
246-
}

‎domain/removal/state/model/state_test.go‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,8 @@ func (s *baseSuite) createRemoteApplicationConsumer(
462462
appUUID:=tc.Must(c,coreapplication.NewUUID)
463463
relationUUID:=tc.Must(c,relation.NewUUID)
464464
err:=cmrState.AddConsumedRelation(c.Context(),name, crossmodelrelation.AddRemoteApplicationConsumerArgs{
465-
SynthApplicationUUID:remoteAppUUID.String(),
466-
ConsumerApplicationUUID:appUUID.String(),
465+
SynthApplicationUUID:appUUID.String(),
466+
ConsumerApplicationUUID:remoteAppUUID.String(),
467467
ConsumerApplicationEndpoint:"foo",
468468
CharmUUID:tc.Must(c,uuid.NewUUID).String(),
469469
Charm:ch,
@@ -546,6 +546,38 @@ func (s *baseSuite) createRelationWithRemoteOfferer(c *tc.C) (relation.UUID, cor
546546
returnrelUUID,synthAppUUID
547547
}
548548

549+
func (s*baseSuite)createRelationWithRemoteConsumer(c*tc.C) (relation.UUID, coreapplication.UUID) {
550+
svc:=s.setupApplicationService(c)
551+
s.createIAASApplication(c,svc,"bar")
552+
offerUUID:=s.createOfferForApplication(c,"bar","some-offer")
553+
synthAppUUID,_:=s.createRemoteApplicationConsumer(c,"foo",offerUUID)
554+
555+
relSvc:=s.setupRelationService(c)
556+
relUUID,err:=relSvc.GetRelationUUIDForRemoval(c.Context(), domainrelation.GetRelationUUIDForRemovalArgs{
557+
Endpoints: []string{"foo:foo","bar:bar"},
558+
})
559+
c.Assert(err,tc.ErrorIsNil)
560+
561+
cmrState:=crossmodelrelationstate.NewState(
562+
s.TxnRunnerFactory(),coremodel.UUID(s.ModelUUID()),testclock.NewClock(s.now),loggertesting.WrapCheckLog(c),
563+
)
564+
565+
err=cmrState.EnsureUnitsExist(c.Context(),synthAppUUID.String(), []string{"foo/0","foo/1","foo/2"})
566+
c.Assert(err,tc.ErrorIsNil)
567+
568+
err=relSvc.SetRelationRemoteApplicationAndUnitSettings(c.Context(),synthAppUUID,relUUID,
569+
map[string]string{"do":"da"},
570+
map[unit.Name]map[string]string{
571+
unit.Name("foo/0"): {"do":"da"},
572+
unit.Name("foo/1"): {"do":"da"},
573+
unit.Name("foo/2"): {"do":"da"},
574+
},
575+
)
576+
c.Assert(err,tc.ErrorIsNil)
577+
578+
returnrelUUID,synthAppUUID
579+
}
580+
549581
func (s*baseSuite)createRemoteRelationBetween(c*tc.C,synthAppName,appNamestring) relation.UUID {
550582
relSvc:=s.setupRelationService(c)
551583

‎domain/removal/state/model/types.go‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ type linkLayerDevice struct {
105105
UUIDstring`db:"uuid"`
106106
}
107107

108-
typeconsumerApplicationUUIDstruct {
109-
ConsumerApplicationUUIDstring`db:"consumer_application_uuid"`
110-
SynthApplicationUUIDstring`db:"uuid"`
111-
}
112-
113108
// storageAttachmentDetachInfo represents the information needed to make a
114109
// decision if a given storage attachment can be detached from a unit safely.
115110
// This information assumes the case where the unit will continue to run after

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp