[
https://issues.apache.org/jira/browse/BEAM-6081?focusedWorklogId=207899&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-207899
]
ASF GitHub Bot logged work on BEAM-6081:
----------------------------------------
Author: ASF GitHub Bot
Created on: 05/Mar/19 17:41
Start Date: 05/Mar/19 17:41
Worklog Time Spent: 10m
Work Description: pabloem commented on pull request #7985: [BEAM-6081]:
Create "Dataflow Reaper" infrastructure to periodically clean up stuck Dataflow
jobs
URL: https://github.com/apache/beam/pull/7985#discussion_r262606281
##########
File path: .test-infra/tools/stale_dataflow_jobs_cleaner.go
##########
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 main
+
+import (
+ "context"
+ "log"
+ "strings"
+ "time"
+
+ "golang.org/x/oauth2/google"
+ df "google.golang.org/api/dataflow/v1b3"
+)
+
+const (
+ longRunningPrefix = "long-running-"
+)
+
+// client contains methods for listing and cancelling jobs, extracted to allow
easier testing.
+type client interface {
+ CurrentTime() time.Time
+ ListJobs(projectId string) ([]*df.Job, error)
+ CancelJob(job *df.Job) error
+}
+
+// dataflowClient implements the client interface for Google Cloud Dataflow.
+type dataflowClient struct {
+ s *df.ProjectsJobsService
+}
+
+// newDataflowClient creates a new Dataflow ProjectsJobsService.
+func newDataflowClient() (*dataflowClient, error) {
+ ctx := context.Background()
+ cl, err := google.DefaultClient(ctx, df.CloudPlatformScope)
+ if err != nil {
+ return nil, err
+ }
+ service, err := df.New(cl)
+ if err != nil {
+ return nil, err
+ }
+ return &dataflowClient{s: df.NewProjectsJobsService(service)}, nil
+}
+
+// CurrentTime gets the time Now.
+func (c dataflowClient) CurrentTime() time.Time {
+ return time.Now()
+}
+
+// ListJobs lists the active Dataflow jobs for a project.
+func (c dataflowClient) ListJobs(projectId string) ([]*df.Job, error) {
+ resp, err :=
c.s.Aggregated(projectId).Filter("ACTIVE").Fields("jobs(id,name,projectId,createTime)").Do()
+ if err != nil {
+ return nil, err
+ }
+ return resp.Jobs, nil
+}
+
+// CancelJob requests the cancellation od a Dataflow job.
+func (c dataflowClient) CancelJob(job *df.Job) error {
+ jobDone := df.Job{
+ RequestedState: "JOB_STATE_DONE",
+ }
+ _, err := c.s.Update(job.ProjectId, job.Id, &jobDone).Do()
+ return err
+}
+
+// cleanDataflowJobs cancels stale Dataflow jobs, excluding the
longRunningPrefix prefixed jobs.
+func cleanDataflowJobs(c client, projectId string, hoursStale float64) error {
+ now := c.CurrentTime()
+ jobs, err := c.ListJobs(projectId)
+ if err != nil {
+ return err
+ }
+ for _, j := range jobs {
+ t, err := time.Parse(time.RFC3339, j.CreateTime)
+ if err != nil {
+ return err
+ }
+ hoursSinceCreate := now.Sub(t).Hours()
+ log.Printf("Job %v %v %v %v %.2f\n", j.ProjectId, j.Id, j.Name,
j.CreateTime, hoursSinceCreate)
+ if hoursSinceCreate > hoursStale && !strings.HasPrefix(j.Name,
longRunningPrefix) {
+ log.Printf("Attempting to cancel %v\n", j.Id)
+ c.CancelJob(j)
+ }
+ }
+ return nil
+}
+
+func main() {
+ client, err := newDataflowClient()
+ if err != nil {
+ log.Fatalf("Error creating dataflow client, %v", err)
+ }
+ err = cleanDataflowJobs(client, "apache-beam-testing", 12.0)
Review comment:
I'd be more aggressive to mark something as stale. Maybe even 3 or 4 hours?
WDYT?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 207899)
Time Spent: 40m (was: 0.5h)
> Create "Dataflow Reaper" infrastructure to periodically clean up stuck
> Dataflow jobs
> ------------------------------------------------------------------------------------
>
> Key: BEAM-6081
> URL: https://issues.apache.org/jira/browse/BEAM-6081
> Project: Beam
> Issue Type: New Feature
> Components: build-system, testing
> Reporter: Scott Wegner
> Assignee: Alan Myrvold
> Priority: Minor
> Time Spent: 40m
> Remaining Estimate: 0h
>
> Our Jenkins infrastructure continuously runs many Dataflow jobs as part of
> pre- and post-commit tests. These are scheduled against our shared
> {{apache-beam-testing}} project, which has some amount of GCP quota for these
> jobs.
> Some bugs can cause Dataflow jobs to get stuck and hang indefinitely. This
> causes many test jobs to stack up, which eats up our GCP quota and then
> causes all subsequent jobs to fail for quota issues. For an example, see
> [[BEAM-6080]|https://issues.apache.org/jira/browse/BEAM-6080].
> We should harden the Dataflow runner and test framework to prevent Dataflow
> jobs getting stuck indefinitely, but in reality: bugs happen.
> We should add some "reaper" process to periodically query for long-running
> jobs on our Dataflow project and cancel them. This would be fairly
> straight-forward using the [Dataflow REST
> API|https://cloud.google.com/dataflow/docs/reference/rest/], and scheduled on
> Jenkins.
> If we build such a mechanism, we should also document the imposed policy
> (i.e. the threshold for "long running jobs"), and perhaps some mechanism for
> opting out. For example, performance benchmarking jobs might be long-running
> by design.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)