Skip to content

Commit

Permalink
Optimize coordinator and fix logger setting (#99)
Browse files Browse the repository at this point in the history
* fix: parse the flags before setting global logger
* refactor(coordinator): speed up reconciling when status change
  • Loading branch information
powerfooI authored Nov 15, 2023
1 parent 74116a9 commit a69509c
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 13 deletions.
14 changes: 11 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ func main() {
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.IntVar(&logVerbosity, "log-verbosity", 0, "Log verbosity level, 0 is info, 1 is debug, 2 is trace")
flag.Parse()

opts := zap.Options{
Development: logVerbosity > 0,
Level: zapcore.Level(-logVerbosity),
}
// opts.BindFlags(flag.CommandLine)
flag.Parse()

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

Expand Down Expand Up @@ -215,7 +215,15 @@ func main() {
rcd := telemetry.NewRecorder(context.Background(), mgr.GetEventRecorderFor("ob-operator"))
rcd.GenerateTelemetryRecord(nil, telemetry.ObjectTypeOperator, "Start", "", "start ob-operator", nil)

setupLog.Info("starting manager")
setupLog.WithValues(
"namespace", namespace,
"manager-namespace", managerNamespace,
"metrics-bind-address", metricsAddr,
"health-probe-bind-address", probeAddr,
"leader-elect", enableLeaderElection,
"log-verbosity", logVerbosity,
).Info("starting manager")

if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: oceanbasedev/ob-operator
newTag: 2.1.0-alpha.2
newName: oceanbase/ob-operator
newTag: 2.1.0
3 changes: 0 additions & 3 deletions make/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/manager cmd/main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./cmd/main.go

# If you wish built the manager image targeting other platforms you can use the --platform flag.
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
Expand Down
6 changes: 5 additions & 1 deletion make/debug.mk
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ run-delve: generate fmt vet manifests ## Run with Delve for development purposes
DISABLE_WEBHOOKS=true dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec ./bin/manager --continue -- -log-verbosity=${LOG_LEVEL}

install-delve: ## Install delve, a debugger for the Go programming language. More info: https://github.com/go-delve/delve
go install github.com/go-delve/delve/cmd/dlv@master
go install github.com/go-delve/delve/cmd/dlv@master

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./cmd/main.go --log-verbosity=${LOG_LEVEL}
14 changes: 10 additions & 4 deletions pkg/controller/observer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -103,16 +104,21 @@ func (r *OBServerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
}
}
coordinator := resource.NewCoordinator(observerManager, &logger)
_, err = coordinator.Coordinate()
return ctrl.Result{
RequeueAfter: 5 * time.Second,
}, err
result, err := coordinator.Coordinate()
if err != nil {
return result, err
}
if result.RequeueAfter > 5*time.Second {
result.RequeueAfter = 5 * time.Second
}
return result, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *OBServerReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.OBServer{}).
Owns(&corev1.Pod{}).
WithEventFilter(preds).
Complete(r)
}
6 changes: 6 additions & 0 deletions pkg/resource/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (c *Coordinator) Coordinate() (ctrl.Result, error) {
}
var f *task.TaskFlow
var err error
beforeStatus := c.Manager.GetStatus()
if c.Manager.IsNewResource() {
c.Manager.InitStatus()
} else {
Expand Down Expand Up @@ -96,6 +97,11 @@ func (c *Coordinator) Coordinate() (ctrl.Result, error) {
if err != nil {
c.Logger.Error(err, "Failed to update status")
}
// When status changes(e.g. from running to other status), set a shorter `requeue after` to speed up processing.
if c.Manager.GetStatus() != beforeStatus {
result.RequeueAfter = ExecutionRequeueDuration
}
c.Logger.V(obconst.LogLevelTrace).Info("Requeue after", "duration", result.RequeueAfter)
return result, err
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/resource/obcluster_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (m *OBClusterManager) IsNewResource() bool {
return m.OBCluster.Status.Status == ""
}

func (m *OBClusterManager) GetStatus() string {
return m.OBCluster.Status.Status
}

func (m *OBClusterManager) InitStatus() {
m.Logger.Info("newly created cluster, init status")
m.Recorder.Event(m.OBCluster, "Init", "", "newly created cluster, init status")
Expand Down
4 changes: 4 additions & 0 deletions pkg/resource/obparameter_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (m *OBParameterManager) IsNewResource() bool {
return m.OBParameter.Status.Status == ""
}

func (m *OBParameterManager) GetStatus() string {
return m.OBParameter.Status.Status
}

func (m *OBParameterManager) InitStatus() {
m.Logger.Info("newly created obparameter, init status")
status := v1alpha1.OBParameterStatus{
Expand Down
4 changes: 4 additions & 0 deletions pkg/resource/observer_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func (m *OBServerManager) IsNewResource() bool {
return m.OBServer.Status.Status == ""
}

func (m *OBServerManager) GetStatus() string {
return m.OBServer.Status.Status
}

func (m *OBServerManager) InitStatus() {
m.Logger.Info("newly created server, init status")
status := v1alpha1.OBServerStatus{
Expand Down
4 changes: 4 additions & 0 deletions pkg/resource/obtenant_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (m *OBTenantManager) IsDeleting() bool {
return !m.OBTenant.ObjectMeta.DeletionTimestamp.IsZero()
}

func (m *OBTenantManager) GetStatus() string {
return m.OBTenant.Status.Status
}

func (m *OBTenantManager) InitStatus() {
m.OBTenant.Status = v1alpha1.OBTenantStatus{
Pools: make([]v1alpha1.ResourcePoolStatus, 0, len(m.OBTenant.Spec.Pools)),
Expand Down
4 changes: 4 additions & 0 deletions pkg/resource/obtenantbackup_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (m *OBTenantBackupManager) IsNewResource() bool {
return m.Resource.Status.Status == ""
}

func (m *OBTenantBackupManager) GetStatus() string {
return string(m.Resource.Status.Status)
}

func (m *OBTenantBackupManager) IsDeleting() bool {
return m.Resource.GetDeletionTimestamp() != nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/resource/obtenantbackuppolicy_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (m *ObTenantBackupPolicyManager) IsDeleting() bool {
return !m.BackupPolicy.ObjectMeta.DeletionTimestamp.IsZero()
}

func (m *ObTenantBackupPolicyManager) GetStatus() string {
return string(m.BackupPolicy.Status.Status)
}

func (m *ObTenantBackupPolicyManager) CheckAndUpdateFinalizers() error {
policy := m.BackupPolicy
finalizerName := "obtenantbackuppolicy.finalizers.oceanbase.com"
Expand Down
4 changes: 4 additions & 0 deletions pkg/resource/obtenantoperation_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func (m *ObTenantOperationManager) IsNewResource() bool {
return m.Resource.Status.Status == ""
}

func (m *ObTenantOperationManager) GetStatus() string {
return string(m.Resource.Status.Status)
}

func (m *ObTenantOperationManager) IsDeleting() bool {
return m.Resource.GetDeletionTimestamp() != nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/resource/obtenantrestore_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (m ObTenantRestoreManager) IsNewResource() bool {
return m.Resource.Status.Status == ""
}

func (m *ObTenantRestoreManager) GetStatus() string {
return string(m.Resource.Status.Status)
}

func (m ObTenantRestoreManager) IsDeleting() bool {
return m.Resource.GetDeletionTimestamp() != nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/resource/obzone_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (m *OBZoneManager) IsNewResource() bool {
return m.OBZone.Status.Status == ""
}

func (m *OBZoneManager) GetStatus() string {
return m.OBZone.Status.Status
}

func (m *OBZoneManager) InitStatus() {
m.Logger.Info("newly created zone, init status")
status := v1alpha1.OBZoneStatus{
Expand Down
1 change: 1 addition & 0 deletions pkg/resource/resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ResourceManager interface {
HandleFailure()
FinishTask()
UpdateStatus() error
GetStatus() string
GetTaskFunc(string) (func() error, error)
GetTaskFlow() (*task.TaskFlow, error)
PrintErrEvent(error)
Expand Down
4 changes: 4 additions & 0 deletions pkg/resource/template_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (m *ObResourceManager[T]) IsNewResource() bool {
return false
}

func (m *ObResourceManager[T]) GetStatus() string {
return ""
}

func (m *ObResourceManager[T]) IsDeleting() bool {
return false
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/task/obcluster_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func BootstrapOBCluster() *TaskFlow {
Name: flowname.BootstrapOBCluster,
Tasks: []string{taskname.CreateOBZone, taskname.WaitOBZoneBootstrapReady, taskname.Bootstrap},
TargetStatus: clusterstatus.Bootstrapped,
OnFailure: strategy.FailureRule{
NextTryStatus: "new",
},
},
}
}
Expand Down

0 comments on commit a69509c

Please sign in to comment.