@@ -2,18 +2,21 @@ package mlflow
2
2
3
3
import (
4
4
"bytes"
5
+ "context"
5
6
"encoding/json"
6
7
"fmt"
7
8
"net/http"
8
9
10
+ "cloud.google.com/go/storage"
11
+
9
12
"github.com/gojek/mlp/api/pkg/artifact"
10
13
)
11
14
12
15
type Service interface {
13
16
searchRunsForExperiment (ExperimentID string ) (SearchRunsResponse , error )
14
17
searchRunData (RunID string ) (SearchRunResponse , error )
15
- DeleteExperiment (ExperimentID string , deleteArtifact bool ) error
16
- DeleteRun (RunID , artifactURL string , deleteArtifact bool ) error
18
+ DeleteExperiment (ctx context. Context , ExperimentID string , deleteArtifact bool ) error
19
+ DeleteRun (ctx context. Context , RunID , artifactURL string , deleteArtifact bool ) error
17
20
}
18
21
19
22
type mlflowService struct {
@@ -22,12 +25,24 @@ type mlflowService struct {
22
25
Config Config
23
26
}
24
27
25
- func NewMlflowService (httpClient * http.Client , config Config , artifactService artifact.Service ) Service {
28
+ func NewMlflowService (httpClient * http.Client , config Config ) (Service , error ) {
29
+ var artifactService artifact.Service
30
+ if config .ArtifactServiceType == "nop" {
31
+ artifactService = artifact .NewNopArtifactClient ()
32
+ } else if config .ArtifactServiceType == "gcs" {
33
+ api , err := storage .NewClient (context .Background ())
34
+ if err != nil {
35
+ return & mlflowService {}, fmt .Errorf ("failed initializing gcs for mlflow delete package" )
36
+ }
37
+ artifactService = artifact .NewGcsArtifactClient (api )
38
+ } else {
39
+ return & mlflowService {}, fmt .Errorf ("invalid artifact service type" )
40
+ }
26
41
return & mlflowService {
27
42
API : httpClient ,
28
43
Config : config ,
29
44
ArtifactService : artifactService ,
30
- }
45
+ }, nil
31
46
}
32
47
33
48
func (mfs * mlflowService ) httpCall (method string , url string , body []byte , response interface {}) error {
@@ -101,19 +116,19 @@ func (mfs *mlflowService) searchRunData(RunID string) (SearchRunResponse, error)
101
116
return runResponse , nil
102
117
}
103
118
104
- func (mfs * mlflowService ) DeleteExperiment (ExperimentID string , deleteArtifact bool ) error {
119
+ func (mfs * mlflowService ) DeleteExperiment (ctx context. Context , ExperimentID string , deleteArtifact bool ) error {
105
120
106
121
relatedRunData , err := mfs .searchRunsForExperiment (ExperimentID )
107
122
if err != nil {
108
123
return err
109
124
}
110
125
// Error handling for empty/no run for the experiment
111
126
if len (relatedRunData .RunsData ) == 0 {
112
- return fmt .Errorf ("There are no related run for experiment id %s" , ExperimentID )
127
+ return fmt .Errorf ("there are no related run for experiment id %s" , ExperimentID )
113
128
}
114
129
// Error Handling, when a RunID failed to delete return error
115
130
for _ , run := range relatedRunData .RunsData {
116
- err = mfs .DeleteRun (run .Info .RunID , run .Info .ArtifactURI , deleteArtifact )
131
+ err = mfs .DeleteRun (ctx , run .Info .RunID , run .Info .ArtifactURI , deleteArtifact )
117
132
if err != nil {
118
133
return fmt .Errorf ("deletion failed for run_id %s for experiment id %s: %s" , run .Info .RunID , ExperimentID , err )
119
134
}
@@ -122,7 +137,7 @@ func (mfs *mlflowService) DeleteExperiment(ExperimentID string, deleteArtifact b
122
137
return nil
123
138
}
124
139
125
- func (mfs * mlflowService ) DeleteRun (RunID , artifactURL string , deleteArtifact bool ) error {
140
+ func (mfs * mlflowService ) DeleteRun (ctx context. Context , RunID , artifactURL string , deleteArtifact bool ) error {
126
141
if artifactURL == "" {
127
142
runDetail , err := mfs .searchRunData (RunID )
128
143
if err != nil {
@@ -131,7 +146,7 @@ func (mfs *mlflowService) DeleteRun(RunID, artifactURL string, deleteArtifact bo
131
146
artifactURL = runDetail .RunData .Info .ArtifactURI
132
147
}
133
148
if deleteArtifact {
134
- err := mfs .ArtifactService .DeleteArtifact (artifactURL )
149
+ err := mfs .ArtifactService .DeleteArtifact (ctx , artifactURL )
135
150
if err != nil {
136
151
return err
137
152
}
0 commit comments