forked from kubeflow/pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline_client.go
169 lines (150 loc) · 5.54 KB
/
pipeline_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"context"
"fmt"
"time"
api "github.com/kubeflow/pipelines/backend/api/go_client"
"github.com/kubeflow/pipelines/backend/src/common/util"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
addressTemp = "%s:%s"
)
type PipelineClientInterface interface {
ReportWorkflow(workflow *util.Workflow) error
ReportScheduledWorkflow(swf *util.ScheduledWorkflow) error
ReadArtifact(request *api.ReadArtifactRequest) (*api.ReadArtifactResponse, error)
ReportRunMetrics(request *api.ReportRunMetricsRequest) (*api.ReportRunMetricsResponse, error)
}
type PipelineClient struct {
initializeTimeout time.Duration
timeout time.Duration
basePath string
mlPipelineServiceName string
mlPipelineServicePort string
reportServiceClient api.ReportServiceClient
runServiceClient api.RunServiceClient
}
func NewPipelineClient(
initializeTimeout time.Duration,
timeout time.Duration,
basePath string,
mlPipelineServiceName string,
mlPipelineServiceHttpPort string,
mlPipelineServiceGRPCPort string) (*PipelineClient, error) {
httpAddress := fmt.Sprintf(addressTemp, mlPipelineServiceName, mlPipelineServiceHttpPort)
grpcAddress := fmt.Sprintf(addressTemp, mlPipelineServiceName, mlPipelineServiceGRPCPort)
err := util.WaitForAPIAvailable(initializeTimeout, basePath, httpAddress)
if err != nil {
return nil, errors.Wrapf(err,
"Failed to initialize pipeline client. Error: %s", err.Error())
}
connection, err := util.GetRpcConnection(grpcAddress)
if err != nil {
return nil, errors.Wrapf(err,
"Failed to get RPC connection. Error: %s", err.Error())
}
return &PipelineClient{
initializeTimeout: initializeTimeout,
timeout: timeout,
reportServiceClient: api.NewReportServiceClient(connection),
runServiceClient: api.NewRunServiceClient(connection),
}, nil
}
func (p *PipelineClient) ReportWorkflow(workflow *util.Workflow) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
_, err := p.reportServiceClient.ReportWorkflow(ctx, &api.ReportWorkflowRequest{
Workflow: workflow.ToStringForStore(),
})
if err != nil {
statusCode, _ := status.FromError(err)
if statusCode.Code() == codes.InvalidArgument {
// Do not retry if there is something wrong with the workflow
return util.NewCustomError(err, util.CUSTOM_CODE_PERMANENT,
"Error while reporting workflow resource (code: %v, message: %v): %v, %+v",
statusCode.Code(),
statusCode.Message(),
err.Error(),
workflow.Workflow)
} else {
// Retry otherwise
return util.NewCustomError(err, util.CUSTOM_CODE_TRANSIENT,
"Error while reporting workflow resource (code: %v, message: %v): %v, %+v",
statusCode.Code(),
statusCode.Message(),
err.Error(),
workflow.Workflow)
}
}
return nil
}
func (p *PipelineClient) ReportScheduledWorkflow(swf *util.ScheduledWorkflow) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
_, err := p.reportServiceClient.ReportScheduledWorkflow(ctx,
&api.ReportScheduledWorkflowRequest{
ScheduledWorkflow: swf.ToStringForStore(),
})
if err != nil {
statusCode, _ := status.FromError(err)
if statusCode.Code() == codes.InvalidArgument {
// Do not retry if there is something wrong with the workflow
return util.NewCustomError(err, util.CUSTOM_CODE_PERMANENT,
"Error while reporting workflow resource (code: %v, message: %v): %v, %+v",
statusCode.Code(),
statusCode.Message(),
err.Error(),
swf.ScheduledWorkflow)
} else {
// Retry otherwise
return util.NewCustomError(err, util.CUSTOM_CODE_TRANSIENT,
"Error while reporting workflow resource (code: %v, message: %v): %v, %+v",
statusCode.Code(),
statusCode.Message(),
err.Error(),
swf.ScheduledWorkflow)
}
}
return nil
}
// ReadArtifact reads artifact content from run service. If the artifact is not present, returns
// nil response.
func (p *PipelineClient) ReadArtifact(request *api.ReadArtifactRequest) (*api.ReadArtifactResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
response, err := p.runServiceClient.ReadArtifact(ctx, request)
if err != nil {
// TODO(hongyes): check NotFound error code before skip the error.
return nil, nil
}
return response, nil
}
// ReportRunMetrics reports run metrics to run service.
func (p *PipelineClient) ReportRunMetrics(request *api.ReportRunMetricsRequest) (*api.ReportRunMetricsResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
response, err := p.runServiceClient.ReportRunMetrics(ctx, request)
if err != nil {
// This call should always succeed unless the run doesn't exist or server is broken. In
// either cases, the job should retry at a later time.
return nil, util.NewCustomError(err, util.CUSTOM_CODE_TRANSIENT,
"Error while reporting metrics (%+v): %+v", request, err)
}
return response, nil
}