Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit 438be31

Browse files
Refacto compute plan count (#80)
* Refacto compute plan count * Update chaincode/testtuple.go upon review Co-Authored-By: Aurélien Gasser <aurelien.gasser@owkin.com> * Correct errors (#78) * Specify bad request error * Use Internal when possible * Clean leftover in utils * Fix after reviews * BadRequest for DAG * Use directly the expected asset type Co-authored-by: Aurélien Gasser <aurelien.gasser@gmail.com>
1 parent 4c6e30c commit 438be31

File tree

5 files changed

+42
-71
lines changed

5 files changed

+42
-71
lines changed

chaincode/compute_plan.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ func cancelComputePlan(db *LedgerDB, args []string) (resp outputComputePlan, err
291291
func (cp *ComputePlan) Create(db *LedgerDB) (string, error) {
292292
ID := GetRandomHash()
293293
cp.AssetType = ComputePlanType
294-
cp.TupleCount = 1
295294
err := db.Add(ID, cp)
296295
if err != nil {
297296
return "", err
@@ -345,13 +344,32 @@ func (cp *ComputePlan) CheckNewTupleStatus(tupleStatus string) bool {
345344
cp.Status = tupleStatus
346345
return true
347346
}
347+
case "":
348+
cp.Status = tupleStatus
349+
return true
348350
}
349351
return false
350352
}
351353

354+
// AddTuple add the tuple key to the compute plan and update it accordingly
355+
func (cp *ComputePlan) AddTuple(tupleType AssetType, key, status string) {
356+
switch tupleType {
357+
case TraintupleType:
358+
cp.TraintupleKeys = append(cp.TraintupleKeys, key)
359+
case CompositeTraintupleType:
360+
cp.CompositeTraintupleKeys = append(cp.CompositeTraintupleKeys, key)
361+
case AggregatetupleType:
362+
cp.AggregatetupleKeys = append(cp.AggregatetupleKeys, key)
363+
case TesttupleType:
364+
cp.TesttupleKeys = append(cp.TesttupleKeys, key)
365+
}
366+
cp.TupleCount++
367+
cp.CheckNewTupleStatus(status)
368+
}
369+
352370
// UpdateComputePlan retreive the compute plan if the ID is not empty,
353371
// check if the updated status change anything and save it if it's the case
354-
func UpdateComputePlan(db *LedgerDB, ComputePlanID, tupleStatus string) error {
372+
func UpdateComputePlan(db *LedgerDB, ComputePlanID, tupleStatus, tupleKey string) error {
355373
if ComputePlanID == "" {
356374
return nil
357375
}

chaincode/testtuple.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,7 @@ func (testtuple *Testtuple) AddToComputePlan(db *LedgerDB, testtupleKey string)
189189
if err != nil {
190190
return err
191191
}
192-
computePlan.TesttupleKeys = append(computePlan.TesttupleKeys, testtupleKey)
193-
computePlan.TupleCount++
194-
computePlan.CheckNewTupleStatus(testtuple.Status)
192+
computePlan.AddTuple(TesttupleType, testtupleKey, testtuple.Status)
195193
err = computePlan.Save(db, testtuple.ComputePlanID)
196194
if err != nil {
197195
return err
@@ -299,10 +297,6 @@ func logStartTest(db *LedgerDB, args []string) (o outputTesttuple, err error) {
299297
return
300298
}
301299
err = o.Fill(db, inp.Key, testtuple)
302-
if err != nil {
303-
return
304-
}
305-
err = UpdateComputePlan(db, testtuple.ComputePlanID, testtuple.Status)
306300
return
307301
}
308302

@@ -329,10 +323,6 @@ func logSuccessTest(db *LedgerDB, args []string) (o outputTesttuple, err error)
329323
if err = testtuple.commitStatusUpdate(db, inp.Key, status); err != nil {
330324
return
331325
}
332-
err = UpdateComputePlan(db, testtuple.ComputePlanID, testtuple.Status)
333-
if err != nil {
334-
return
335-
}
336326
err = o.Fill(db, inp.Key, testtuple)
337327
return
338328
}
@@ -360,10 +350,6 @@ func logFailTest(db *LedgerDB, args []string) (o outputTesttuple, err error) {
360350
if err = testtuple.commitStatusUpdate(db, inp.Key, status); err != nil {
361351
return
362352
}
363-
err = UpdateComputePlan(db, testtuple.ComputePlanID, testtuple.Status)
364-
if err != nil {
365-
return
366-
}
367353
err = o.Fill(db, inp.Key, testtuple)
368354
return
369355
}
@@ -469,6 +455,9 @@ func (testtuple *Testtuple) commitStatusUpdate(db *LedgerDB, testtupleKey string
469455
if err := db.UpdateIndex(indexName, oldAttributes, newAttributes); err != nil {
470456
return err
471457
}
458+
if err := UpdateComputePlan(db, testtuple.ComputePlanID, newStatus, testtupleKey); err != nil {
459+
return err
460+
}
472461
logger.Infof("testtuple %s status updated: %s (from=%s)", testtupleKey, newStatus, oldStatus)
473462
return nil
474463
}

chaincode/traintuple.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ func (traintuple *Traintuple) AddToComputePlan(db *LedgerDB, inp inputTraintuple
135135
err = errors.BadRequest("invalid inputs, a new ComputePlan should have a rank 0")
136136
return err
137137
}
138-
computePlan := ComputePlan{Status: traintuple.Status, TraintupleKeys: []string{traintupleKey}}
138+
computePlan := ComputePlan{}
139+
computePlan.AddTuple(TraintupleType, traintupleKey, traintuple.Status)
139140
traintuple.ComputePlanID, err = computePlan.Create(db)
140141
if err != nil {
141142
return err
@@ -147,9 +148,7 @@ func (traintuple *Traintuple) AddToComputePlan(db *LedgerDB, inp inputTraintuple
147148
if err != nil {
148149
return err
149150
}
150-
computePlan.TraintupleKeys = append(computePlan.TraintupleKeys, traintupleKey)
151-
computePlan.TupleCount++
152-
computePlan.CheckNewTupleStatus(traintuple.Status)
151+
computePlan.AddTuple(TraintupleType, traintupleKey, traintuple.Status)
153152
err = computePlan.Save(db, traintuple.ComputePlanID)
154153
if err != nil {
155154
return err
@@ -284,10 +283,6 @@ func logStartTrain(db *LedgerDB, args []string) (o outputTraintuple, err error)
284283
return
285284
}
286285
err = o.Fill(db, traintuple, inp.Key)
287-
if err != nil {
288-
return
289-
}
290-
err = UpdateComputePlan(db, traintuple.ComputePlanID, traintuple.Status)
291286
return
292287
}
293288

@@ -332,15 +327,6 @@ func logSuccessTrain(db *LedgerDB, args []string) (o outputTraintuple, err error
332327
}
333328

334329
err = o.Fill(db, traintuple, inp.Key)
335-
if err != nil {
336-
return
337-
}
338-
339-
err = UpdateComputePlan(db, traintuple.ComputePlanID, traintuple.Status)
340-
if err != nil {
341-
return
342-
}
343-
344330
return
345331
}
346332

@@ -372,10 +358,6 @@ func logFailTrain(db *LedgerDB, args []string) (o outputTraintuple, err error) {
372358
return
373359
}
374360

375-
err = UpdateComputePlan(db, traintuple.ComputePlanID, traintuple.Status)
376-
if err != nil {
377-
return
378-
}
379361
// Do not propagate failure if we are in a compute plan
380362
if traintuple.ComputePlanID != "" {
381363
return
@@ -632,6 +614,9 @@ func (traintuple *Traintuple) commitStatusUpdate(db *LedgerDB, traintupleKey str
632614
if err := db.UpdateIndex(indexName, oldAttributes, newAttributes); err != nil {
633615
return err
634616
}
617+
if err := UpdateComputePlan(db, traintuple.ComputePlanID, newStatus, traintupleKey); err != nil {
618+
return err
619+
}
635620
logger.Infof("traintuple %s status updated: %s (from=%s)", traintupleKey, newStatus, oldStatus)
636621
return nil
637622
}

chaincode/traintuple_composite.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ func (traintuple *CompositeTraintuple) AddToComputePlan(db *LedgerDB, inp inputC
168168
err = errors.BadRequest("invalid inputs, a new ComputePlan should have a rank 0")
169169
return err
170170
}
171-
computePlan := ComputePlan{Status: traintuple.Status, CompositeTraintupleKeys: []string{traintupleKey}}
171+
computePlan := ComputePlan{}
172+
computePlan.AddTuple(CompositeTraintupleType, traintupleKey, traintuple.Status)
172173
traintuple.ComputePlanID, err = computePlan.Create(db)
173174
if err != nil {
174175
return err
@@ -180,9 +181,7 @@ func (traintuple *CompositeTraintuple) AddToComputePlan(db *LedgerDB, inp inputC
180181
if err != nil {
181182
return err
182183
}
183-
computePlan.CompositeTraintupleKeys = append(computePlan.CompositeTraintupleKeys, traintupleKey)
184-
computePlan.TupleCount++
185-
computePlan.CheckNewTupleStatus(traintuple.Status)
184+
computePlan.AddTuple(CompositeTraintupleType, traintupleKey, traintuple.Status)
186185
err = computePlan.Save(db, traintuple.ComputePlanID)
187186
if err != nil {
188187
return err
@@ -320,10 +319,6 @@ func logStartCompositeTrain(db *LedgerDB, args []string) (o outputCompositeTrain
320319
return
321320
}
322321
err = o.Fill(db, compositeTraintuple, inp.Key)
323-
if err != nil {
324-
return
325-
}
326-
err = UpdateComputePlan(db, compositeTraintuple.ComputePlanID, compositeTraintuple.Status)
327322
return
328323
}
329324

@@ -370,11 +365,6 @@ func logSuccessCompositeTrain(db *LedgerDB, args []string) (o outputCompositeTra
370365
}
371366

372367
err = o.Fill(db, compositeTraintuple, inp.Key)
373-
if err != nil {
374-
return
375-
}
376-
377-
err = UpdateComputePlan(db, compositeTraintuple.ComputePlanID, compositeTraintuple.Status)
378368
return
379369
}
380370

@@ -406,10 +396,6 @@ func logFailCompositeTrain(db *LedgerDB, args []string) (o outputCompositeTraint
406396
if err != nil {
407397
return
408398
}
409-
err = UpdateComputePlan(db, compositeTraintuple.ComputePlanID, compositeTraintuple.Status)
410-
if err != nil {
411-
return
412-
}
413399
// Do not propagate failure if we are in a compute plan
414400
if compositeTraintuple.ComputePlanID != "" {
415401
return
@@ -575,6 +561,9 @@ func (traintuple *CompositeTraintuple) commitStatusUpdate(db *LedgerDB, traintup
575561
if err := db.UpdateIndex(indexName, oldAttributes, newAttributes); err != nil {
576562
return err
577563
}
564+
if err := UpdateComputePlan(db, traintuple.ComputePlanID, newStatus, traintupleKey); err != nil {
565+
return err
566+
}
578567
logger.Infof("traintuple %s status updated: %s (from=%s)", traintupleKey, newStatus, oldStatus)
579568
return nil
580569
}

chaincode/tuple_aggregate.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ func (tuple *Aggregatetuple) AddToComputePlan(db *LedgerDB, inp inputAggregatetu
138138
err = errors.BadRequest("invalid inputs, a new ComputePlan should have a rank 0")
139139
return err
140140
}
141-
computePlan := ComputePlan{Status: tuple.Status, AggregatetupleKeys: []string{traintupleKey}}
141+
computePlan := ComputePlan{}
142+
computePlan.AddTuple(AggregatetupleType, traintupleKey, tuple.Status)
142143
tuple.ComputePlanID, err = computePlan.Create(db)
143144
if err != nil {
144145
return err
@@ -151,9 +152,7 @@ func (tuple *Aggregatetuple) AddToComputePlan(db *LedgerDB, inp inputAggregatetu
151152
if err != nil {
152153
return err
153154
}
154-
computePlan.AggregatetupleKeys = append(computePlan.AggregatetupleKeys, traintupleKey)
155-
computePlan.TupleCount++
156-
computePlan.CheckNewTupleStatus(tuple.Status)
155+
computePlan.AddTuple(AggregatetupleType, traintupleKey, tuple.Status)
157156
err = computePlan.Save(db, tuple.ComputePlanID)
158157
if err != nil {
159158
return err
@@ -289,10 +288,6 @@ func logStartAggregate(db *LedgerDB, args []string) (o outputAggregatetuple, err
289288
return
290289
}
291290
err = o.Fill(db, aggregatetuple, inp.Key)
292-
if err != nil {
293-
return
294-
}
295-
err = UpdateComputePlan(db, aggregatetuple.ComputePlanID, aggregatetuple.Status)
296291
return
297292
}
298293

@@ -321,10 +316,6 @@ func logFailAggregate(db *LedgerDB, args []string) (o outputAggregatetuple, err
321316
}
322317

323318
o.Fill(db, aggregatetuple, inp.Key)
324-
err = UpdateComputePlan(db, aggregatetuple.ComputePlanID, aggregatetuple.Status)
325-
if err != nil {
326-
return
327-
}
328319
// Do not propagate failure if we are in a compute plan
329320
if aggregatetuple.ComputePlanID != "" {
330321
return
@@ -368,10 +359,6 @@ func logSuccessAggregate(db *LedgerDB, args []string) (o outputAggregatetuple, e
368359
return
369360
}
370361

371-
err = UpdateComputePlan(db, aggregatetuple.ComputePlanID, aggregatetuple.Status)
372-
if err != nil {
373-
return
374-
}
375362
err = UpdateTraintupleChildren(db, aggregatetupleKey, aggregatetuple.Status, []string{})
376363
if err != nil {
377364
return
@@ -527,6 +514,9 @@ func (tuple *Aggregatetuple) commitStatusUpdate(db *LedgerDB, aggregatetupleKey
527514
if err := db.UpdateIndex(indexName, oldAttributes, newAttributes); err != nil {
528515
return err
529516
}
517+
if err := UpdateComputePlan(db, tuple.ComputePlanID, newStatus, aggregatetupleKey); err != nil {
518+
return err
519+
}
530520
logger.Infof("aggregatetuple %s status updated: %s (from=%s)", aggregatetupleKey, newStatus, oldStatus)
531521
return nil
532522
}

0 commit comments

Comments
 (0)