-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
274 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: {{ include "models.autoscalerStateConfigMapName" . }} |
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,65 @@ | ||
package modelautoscaler | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func newTotalModelState() totalModelState { | ||
return totalModelState{ | ||
Models: make(map[string]modelState), | ||
LastCalculationTime: time.Now(), | ||
} | ||
} | ||
|
||
type totalModelState struct { | ||
Models map[string]modelState `json:"models"` | ||
LastCalculationTime time.Time `json:"lastCalculationTime"` | ||
} | ||
|
||
type modelState struct { | ||
AverageActiveRequests float64 `json:"averageActiveRequests"` | ||
} | ||
|
||
func (a *Autoscaler) loadLastTotalModelState(ctx context.Context) (totalModelState, error) { | ||
cm := &corev1.ConfigMap{} | ||
if err := a.k8sClient.Get(ctx, a.stateConfigMapRef, cm); err != nil { | ||
return totalModelState{}, fmt.Errorf("get ConfigMap %q: %w", a.stateConfigMapRef, err) | ||
} | ||
const key = "models" | ||
jsonState, ok := cm.Data[key] | ||
if !ok { | ||
log.Printf("Autoscaler state ConfigMap %q has no key %q, state not loaded", key, a.stateConfigMapRef) | ||
return totalModelState{}, nil | ||
} | ||
tms := totalModelState{} | ||
if err := json.Unmarshal([]byte(jsonState), &tms); err != nil { | ||
return totalModelState{}, fmt.Errorf("unmarshalling state: %w", err) | ||
} | ||
return tms, nil | ||
} | ||
|
||
func (a *Autoscaler) saveTotalModelState(ctx context.Context, state totalModelState) error { | ||
jsonState, err := json.Marshal(state) | ||
if err != nil { | ||
return fmt.Errorf("marshalling state: %w", err) | ||
} | ||
patch := fmt.Sprintf(`{"data":{"models":%q}}`, string(jsonState)) | ||
if err := a.k8sClient.Patch(ctx, &corev1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: a.stateConfigMapRef.Namespace, | ||
Name: a.stateConfigMapRef.Name, | ||
}, | ||
}, client.RawPatch(types.StrategicMergePatchType, []byte(patch))); err != nil { | ||
return fmt.Errorf("patching ConfigMap %q: %w", a.stateConfigMapRef, err) | ||
} | ||
return nil | ||
} |
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,71 @@ | ||
package integration | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/substratusai/kubeai/internal/config" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/utils/ptr" | ||
) | ||
|
||
// TestAutoscalerState tests the autoscaler's state management. | ||
func TestAutoscalerState(t *testing.T) { | ||
m := modelForTest(t) | ||
m.Spec.MaxReplicas = ptr.To[int32](100) | ||
m.Spec.TargetRequests = ptr.To[int32](1) | ||
m.Spec.ScaleDownDelaySeconds = ptr.To[int64](2) | ||
|
||
ms := newTestMetricsServer(t, m.Name) | ||
|
||
sysCfg := baseSysCfg(t) | ||
sysCfg.ModelAutoscaling.TimeWindow = config.Duration{Duration: 1 * time.Second} | ||
sysCfg.ModelAutoscaling.Interval = config.Duration{Duration: time.Second / 4} | ||
sysCfg.FixedSelfMetricAddrs = []string{ | ||
ms.addr(), | ||
} | ||
t.Logf("FixedSelfMetricAddrs: %v", sysCfg.FixedSelfMetricAddrs) | ||
initTest(t, sysCfg) | ||
|
||
ms.activeRequests.Store(2) | ||
|
||
// Create the Model object in the Kubernetes cluster. | ||
require.NoError(t, testK8sClient.Create(testCtx, m)) | ||
|
||
requireModelReplicas(t, m, 2, "Replicas should be autoscaled", 15*time.Second) | ||
|
||
// Assert that state was saved. | ||
cm := &corev1.ConfigMap{} | ||
require.EventuallyWithT(t, func(t *assert.CollectT) { | ||
err := testK8sClient.Get(testCtx, types.NamespacedName{Name: sysCfg.ModelAutoscaling.StateConfigMapName, Namespace: testNS}, cm) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
if !assert.NotNil(t, cm.Data) { | ||
return | ||
} | ||
data, ok := cm.Data["models"] | ||
if !assert.True(t, ok) { | ||
return | ||
} | ||
type modelState struct { | ||
AverageActiveRequests float64 `json:"averageActiveRequests"` | ||
} | ||
var state struct { | ||
Models map[string]modelState `json:"models"` | ||
} | ||
if !assert.NoError(t, json.Unmarshal([]byte(data), &state)) { | ||
return | ||
} | ||
if !assert.Len(t, state.Models, 1) { | ||
return | ||
} | ||
assert.Equal(t, state.Models[m.Name], modelState{ | ||
AverageActiveRequests: 2.0, | ||
}) | ||
}, 15*time.Second, 1*time.Second) | ||
} |
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
Oops, something went wrong.