-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: lorry support oceanbase and crobjobs (#6796)
- Loading branch information
Showing
37 changed files
with
937 additions
and
605 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Copyright (C) 2022-2024 ApeCloud Co., Ltd | ||
This file is part of KubeBlocks project | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package cronjobs | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/apecloud/kubeblocks/pkg/lorry/operations" | ||
) | ||
|
||
type Job struct { | ||
Name string | ||
Ticker *time.Ticker | ||
Operation operations.Operation | ||
TimeoutSeconds int | ||
PeriodSeconds int | ||
SuccessThreshold int | ||
FailureThreshold int | ||
} | ||
|
||
func NewJob(name string, settings map[string]string) (*Job, error) { | ||
operations := operations.Operations() | ||
ops, ok := operations[name] | ||
if !ok { | ||
logger.Info("Operation not found", "name", name) | ||
return nil, nil | ||
} | ||
job := &Job{ | ||
Name: name, | ||
Operation: ops, | ||
TimeoutSeconds: 60, | ||
PeriodSeconds: 60, | ||
SuccessThreshold: 1, | ||
FailureThreshold: 3, | ||
} | ||
|
||
if v, ok := settings["timeoutSeconds"]; ok { | ||
timeoutSeconds, err := strconv.Atoi(v) | ||
if err != nil { | ||
logger.Info("Failed to parse timeoutSeconds", "error", err.Error(), "job", name, "value", v) | ||
return nil, err | ||
} | ||
job.TimeoutSeconds = timeoutSeconds | ||
} | ||
|
||
if settings["periodSeconds"] != "" { | ||
periodSeconds, err := strconv.Atoi(settings["periodSeconds"]) | ||
if err != nil { | ||
logger.Info("Failed to parse periodSeconds", "error", err.Error(), "job", name, "value", settings["periodSeconds"]) | ||
return nil, err | ||
} | ||
job.PeriodSeconds = periodSeconds | ||
} | ||
|
||
if settings["successThreshold"] != "" { | ||
successThreshold, err := strconv.Atoi(settings["successThreshold"]) | ||
if err != nil { | ||
logger.Info("Failed to parse successThreshold", "error", err.Error(), "job", name, "value", settings["successThreshold"]) | ||
return nil, err | ||
} | ||
job.SuccessThreshold = successThreshold | ||
} | ||
|
||
if settings["failureThreshold"] != "" { | ||
failureThreshold, err := strconv.Atoi(settings["failureThreshold"]) | ||
if err != nil { | ||
logger.Info("Failed to parse failureThreshold", "error", err.Error(), "job", name, "value", settings["failureThreshold"]) | ||
return nil, err | ||
} | ||
job.FailureThreshold = failureThreshold | ||
} | ||
// operation is initialized in httpserver/apis.go | ||
job.Operation.SetTimeout(time.Duration(job.TimeoutSeconds) * time.Second) | ||
return job, nil | ||
} | ||
|
||
func (job *Job) Start() { | ||
job.Ticker = time.NewTicker(time.Duration(job.PeriodSeconds) * time.Second) | ||
defer job.Ticker.Stop() | ||
for range job.Ticker.C { | ||
_, err := job.Operation.Do(context.Background(), nil) | ||
if err != nil { | ||
logger.Info("Failed to run job", "name", job.Name, "error", err.Error()) | ||
// Handle error, e.g., increase failure count, stop job if failureThreshold is reached, etc. | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright (C) 2022-2024 ApeCloud Co., Ltd | ||
This file is part of KubeBlocks project | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package cronjobs | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
||
"github.com/apecloud/kubeblocks/pkg/constant" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type Manager struct { | ||
Jobs map[string]*Job | ||
} | ||
|
||
var logger = ctrl.Log.WithName("cronjobs") | ||
|
||
func NewManager() (*Manager, error) { | ||
cronSettings := make(map[string]map[string]string) | ||
jsonStr := viper.GetString(constant.KBEnvCronJobs) | ||
if jsonStr == "" { | ||
logger.Info("env is not set", "env", constant.KBEnvCronJobs) | ||
return &Manager{}, nil | ||
} | ||
|
||
err := json.Unmarshal([]byte(jsonStr), &cronSettings) | ||
if err != nil { | ||
logger.Info("Failed to unmarshal env", "name", constant.KBEnvCronJobs, "value", jsonStr, "error", err.Error()) | ||
return nil, err | ||
} | ||
|
||
jobs := make(map[string]*Job) | ||
for name, setting := range cronSettings { | ||
job, err := NewJob(name, setting) | ||
if err != nil { | ||
logger.Info("Failed to create job", "error", err.Error(), "name", name, "setting", setting) | ||
} | ||
jobs[name] = job | ||
} | ||
return &Manager{ | ||
Jobs: jobs, | ||
}, nil | ||
} | ||
|
||
func (m *Manager) Start() { | ||
for _, job := range m.Jobs { | ||
go job.Start() | ||
} | ||
} |
Oops, something went wrong.