Skip to content

Commit

Permalink
Merge pull request #466 from Abirdcfly/vs
Browse files Browse the repository at this point in the history
fix: vectorstore retry too quickly
  • Loading branch information
bjwswang authored Dec 28, 2023
2 parents c8a8a3a + 6d2db81 commit 4bc9243
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
38 changes: 38 additions & 0 deletions controllers/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2023 KubeAGI.
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 controllers

import (
"reflect"

"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

// FinalizersChangedPredicate implements an update predicate function on Finalizers change.
type FinalizersChangedPredicate struct {
predicate.Funcs
}

// Update implements default UpdateEvent filter for validating generation change.
func (FinalizersChangedPredicate) Update(e event.UpdateEvent) bool {
if e.ObjectOld == nil || e.ObjectNew == nil {
return false
}

return !reflect.DeepEqual(e.ObjectOld.GetFinalizers(), e.ObjectNew.GetFinalizers())
}
21 changes: 15 additions & 6 deletions controllers/vectorstore_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import (
"github.com/go-logr/logr"
"github.com/tmc/langchaingo/vectorstores/chroma"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

arcadiav1alpha1 "github.com/kubeagi/arcadia/api/base/v1alpha1"
Expand Down Expand Up @@ -103,7 +104,7 @@ func (r *VectorStoreReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

if err := r.CheckVectorStore(ctx, log, vs); err != nil {
return reconcile.Result{RequeueAfter: waitMedium}, err
return reconcile.Result{RequeueAfter: waitMedium}, nil
}

return ctrl.Result{RequeueAfter: waitLonger}, nil
Expand All @@ -112,29 +113,37 @@ func (r *VectorStoreReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// SetupWithManager sets up the controller with the Manager.
func (r *VectorStoreReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&arcadiav1alpha1.VectorStore{}).
For(&arcadiav1alpha1.VectorStore{},
builder.WithPredicates(
predicate.Or(
FinalizersChangedPredicate{},
predicate.GenerationChangedPredicate{},
predicate.LabelChangedPredicate{}))).
Complete(r)
}

func (r *VectorStoreReconciler) CheckVectorStore(ctx context.Context, log logr.Logger, vs *arcadiav1alpha1.VectorStore) (err error) {
log.V(5).Info("check vectorstore")
switch vs.Spec.Type() {
case arcadiav1alpha1.VectorStoreTypeChroma:
_, err := chroma.New(
_, err = chroma.New(
chroma.WithOpenAiAPIKey("fake_key_just_for_chroma_heartbeat"),
chroma.WithChromaURL(vs.Spec.Endpoint.URL),
chroma.WithDistanceFunction(vs.Spec.Chroma.DistanceFunction),
)
if err != nil {
klog.Errorln("failed to connect to vectorstore", err)
log.Error(err, "failed to connect to vectorstore")
r.setCondition(vs, vs.ErrorCondition(err.Error()))
} else {
r.setCondition(vs, vs.ReadyCondition())
}
default:
r.setCondition(vs, vs.ErrorCondition("unsupported vectorstore type"))
}
return r.patchStatus(ctx, vs)
if err := r.patchStatus(ctx, vs); err != nil {
return err
}
return err
}
func (r *VectorStoreReconciler) setCondition(vs *arcadiav1alpha1.VectorStore, condition ...arcadiav1alpha1.Condition) *arcadiav1alpha1.VectorStore {
vs.Status.SetConditions(condition...)
Expand Down

0 comments on commit 4bc9243

Please sign in to comment.