Skip to content

Commit

Permalink
Fix wrong error-handling logic in db interface (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougangliu authored and k8s-ci-robot committed May 16, 2019
1 parent f626d4b commit 3cbb565
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions pkg/db/v1alpha2/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ func (d *dbConn) GetExperiment(experimentName string) (*v1alpha2.Experiment, err
if start_time != "" {
start_timeMysql, err := time.Parse(mysqlTimeFmt, start_time)
if err != nil {
log.Printf("Error parsing Trial start time %s to mysqlFormat: %v", start_time, err)
return nil, fmt.Errorf("Error parsing Trial start time %s to mysqlFormat: %v", start_time, err)
}
experiment.ExperimentStatus.StartTime = start_timeMysql.UTC().Format(time.RFC3339Nano)
}
if completion_time != "" {
completion_timeMysql, err := time.Parse(mysqlTimeFmt, completion_time)
if err != nil {
log.Printf("Error parsing Trial completion time %s to mysqlFormat: %v", completion_time, err)
return nil, fmt.Errorf("Error parsing Trial completion time %s to mysqlFormat: %v", completion_time, err)
}
experiment.ExperimentStatus.CompletionTime = completion_timeMysql.UTC().Format(time.RFC3339Nano)
}
Expand Down Expand Up @@ -312,24 +312,21 @@ func (d *dbConn) GetExperimentList() ([]*v1alpha2.ExperimentSummary, error) {
&completion_time,
)
if err != nil {
log.Printf("Fail to get Experiment from DB. %v", err)
continue
return nil, fmt.Errorf("Fail to get Experiment from DB. %v", err)
}
if start_time != "" {
start_timeMysql, err := time.Parse(mysqlTimeFmt, start_time)
if err != nil {
log.Printf("Error parsing Trial start time %s to mysqlFormat: %v", start_time, err)
} else {
experiment_sum.Status.StartTime = start_timeMysql.UTC().Format(time.RFC3339Nano)
return nil, fmt.Errorf("Error parsing Trial start time %s to mysqlFormat: %v", start_time, err)
}
experiment_sum.Status.StartTime = start_timeMysql.UTC().Format(time.RFC3339Nano)
}
if completion_time != "" {
completion_timeMysql, err := time.Parse(mysqlTimeFmt, completion_time)
if err != nil {
log.Printf("Error parsing Trial completion time %s to mysqlFormat: %v", completion_time, err)
} else {
experiment_sum.Status.CompletionTime = completion_timeMysql.UTC().Format(time.RFC3339Nano)
return nil, fmt.Errorf("Error parsing Trial completion time %s to mysqlFormat: %v", completion_time, err)
}
experiment_sum.Status.CompletionTime = completion_timeMysql.UTC().Format(time.RFC3339Nano)
}
result = append(result, &experiment_sum)
}
Expand All @@ -343,14 +340,14 @@ func (d *dbConn) UpdateExperimentStatus(experimentName string, newStatus *v1alph
if newStatus.StartTime != "" {
s_time, err := time.Parse(time.RFC3339Nano, newStatus.StartTime)
if err != nil {
log.Printf("Error parsing start time %s: %v", newStatus.StartTime, err)
return fmt.Errorf("Error parsing start time %s: %v", newStatus.StartTime, err)
}
start_time = s_time.UTC().Format(mysqlTimeFmt)
}
if newStatus.CompletionTime != "" {
c_time, err := time.Parse(time.RFC3339Nano, newStatus.CompletionTime)
if err != nil {
log.Printf("Error parsing completion time %s: %v", newStatus.CompletionTime, err)
return fmt.Errorf("Error parsing completion time %s: %v", newStatus.CompletionTime, err)
}
completion_time = c_time.UTC().Format(mysqlTimeFmt)
}
Expand Down Expand Up @@ -418,7 +415,7 @@ func (d *dbConn) GetAlgorithmExtraSettings(experimentName string) ([]*v1alpha2.A
&as.Value,
)
if err != nil {
log.Printf("Failed to scan ExtraSetting %v", err)
return nil, fmt.Errorf("Failed to scan ExtraSetting %v", err)
}
result = append(result, as)
}
Expand All @@ -436,34 +433,34 @@ func (d *dbConn) RegisterTrial(trial *v1alpha2.Trial) error {
if trial.Spec.Objective != nil {
objSpec, err = (&jsonpb.Marshaler{}).MarshalToString(trial.Spec.Objective)
if err != nil {
log.Fatalf("Error marshaling Objective: %v", err)
return fmt.Errorf("Error marshaling Objective: %v", err)
}
}
if trial.Spec.ParameterAssignments != nil {
paramAssignment, err = (&jsonpb.Marshaler{}).MarshalToString(trial.Spec.ParameterAssignments)
if err != nil {
log.Fatalf("Error marshaling Parameters: %v", err)
return fmt.Errorf("Error marshaling Parameters: %v", err)
}
}
if trial.Status.Observation != nil {
observation, err = (&jsonpb.Marshaler{}).MarshalToString(trial.Status.Observation)
if err != nil {
log.Fatalf("Error marshaling Objective: %v", err)
return fmt.Errorf("Error marshaling Objective: %v", err)
}
}
}
if trial.Status != nil {
if trial.Status.StartTime != "" {
s_time, err := time.Parse(time.RFC3339Nano, trial.Status.StartTime)
if err != nil {
log.Printf("Error parsing start time %s: %v", trial.Status.StartTime, err)
return fmt.Errorf("Error parsing start time %s: %v", trial.Status.StartTime, err)
}
start_time = s_time.UTC().Format(mysqlTimeFmt)
}
if trial.Status.CompletionTime != "" {
c_time, err := time.Parse(time.RFC3339Nano, trial.Status.CompletionTime)
if err != nil {
log.Printf("Error parsing completion time %s: %v", trial.Status.CompletionTime, err)
return fmt.Errorf("Error parsing completion time %s: %v", trial.Status.CompletionTime, err)
}
completion_time = c_time.UTC().Format(mysqlTimeFmt)
}
Expand Down Expand Up @@ -538,7 +535,7 @@ func (d *dbConn) GetTrialList(experimentName string, filter string) ([]*v1alpha2
&completion_time,
)
if err != nil {
log.Printf("Failed to scan trial %v", err)
return nil, fmt.Errorf("Failed to scan trial %v", err)
}
if objSpec != "" {
trial.Spec.Objective = new(v1alpha2.ObjectiveSpec)
Expand All @@ -564,14 +561,14 @@ func (d *dbConn) GetTrialList(experimentName string, filter string) ([]*v1alpha2
if start_time != "" {
start_timeMysql, err := time.Parse(mysqlTimeFmt, start_time)
if err != nil {
log.Printf("Error parsing Trial start time %s to mysqlFormat: %v", start_time, err)
return nil, fmt.Errorf("Error parsing Trial start time %s to mysqlFormat: %v", start_time, err)
}
trial.Status.StartTime = start_timeMysql.UTC().Format(time.RFC3339Nano)
}
if completion_time != "" {
completion_timeMysql, err := time.Parse(mysqlTimeFmt, completion_time)
if err != nil {
log.Printf("Error parsing Trial completion time %s to mysqlFormat: %v", completion_time, err)
return nil, fmt.Errorf("Error parsing Trial completion time %s to mysqlFormat: %v", completion_time, err)
}
trial.Status.CompletionTime = completion_timeMysql.UTC().Format(time.RFC3339Nano)
}
Expand Down Expand Up @@ -629,14 +626,14 @@ func (d *dbConn) GetTrial(trialName string) (*v1alpha2.Trial, error) {
if start_time != "" {
start_timeMysql, err := time.Parse(mysqlTimeFmt, start_time)
if err != nil {
log.Printf("Error parsing Trial start time %s to mysqlFormat: %v", start_time, err)
return nil, fmt.Errorf("Error parsing Trial start time %s to mysqlFormat: %v", start_time, err)
}
trial.Status.StartTime = start_timeMysql.UTC().Format(time.RFC3339Nano)
}
if completion_time != "" {
completion_timeMysql, err := time.Parse(mysqlTimeFmt, completion_time)
if err != nil {
log.Printf("Error parsing Trial completion time %s to mysqlFormat: %v", completion_time, err)
return nil, fmt.Errorf("Error parsing Trial completion time %s to mysqlFormat: %v", completion_time, err)
}
trial.Status.CompletionTime = completion_timeMysql.UTC().Format(time.RFC3339Nano)
}
Expand All @@ -651,21 +648,21 @@ func (d *dbConn) UpdateTrialStatus(trialName string, newStatus *v1alpha2.TrialSt
if newStatus.Observation != nil {
observation, err = (&jsonpb.Marshaler{}).MarshalToString(newStatus.Observation)
if err != nil {
log.Fatalf("Error marshaling Objective: %v", err)
return fmt.Errorf("Error marshaling Objective: %v", err)
}
}

if newStatus.StartTime != "" {
start_time, err := time.Parse(time.RFC3339Nano, newStatus.StartTime)
if err != nil {
log.Printf("Error parsing start time %s: %v", newStatus.StartTime, err)
return fmt.Errorf("Error parsing start time %s: %v", newStatus.StartTime, err)
}
formattedStartTime = start_time.UTC().Format(mysqlTimeFmt)
}
if newStatus.CompletionTime != "" {
completion_time, err := time.Parse(time.RFC3339Nano, newStatus.CompletionTime)
if err != nil {
log.Printf("Error parsing completion time %s: %v", newStatus.CompletionTime, err)
return fmt.Errorf("Error parsing completion time %s: %v", newStatus.CompletionTime, err)
}
formattedCompletionTime = completion_time.UTC().Format(mysqlTimeFmt)
}
Expand Down Expand Up @@ -695,7 +692,7 @@ func (d *dbConn) RegisterObservationLog(trialName string, observationLog *v1alph
}
t, err := time.Parse(time.RFC3339Nano, mlog.TimeStamp)
if err != nil {
log.Printf("Error parsing start time %s: %v", mlog.TimeStamp, err)
return fmt.Errorf("Error parsing start time %s: %v", mlog.TimeStamp, err)
}
sqlTimeStr := t.UTC().Format(mysqlTimeFmt)
_, err = d.db.Exec(
Expand All @@ -722,7 +719,7 @@ func (d *dbConn) GetObservationLog(trialName string, startTime string, endTime s
if startTime != "" {
s_time, err := time.Parse(time.RFC3339Nano, startTime)
if err != nil {
log.Printf("Error parsing start time %s: %v", startTime, err)
return nil, fmt.Errorf("Error parsing start time %s: %v", startTime, err)
}
formattedStartTime := s_time.UTC().Format(mysqlTimeFmt)
qstr += " AND time >= ?"
Expand All @@ -731,7 +728,7 @@ func (d *dbConn) GetObservationLog(trialName string, startTime string, endTime s
if endTime != "" {
e_time, err := time.Parse(time.RFC3339Nano, endTime)
if err != nil {
log.Printf("Error parsing completion time %s: %v", endTime, err)
return nil, fmt.Errorf("Error parsing completion time %s: %v", endTime, err)
}
formattedEndTime := e_time.UTC().Format(mysqlTimeFmt)
qstr += " AND time <= ?"
Expand Down

0 comments on commit 3cbb565

Please sign in to comment.