octopus算力接口调整
This commit is contained in:
parent
f55f041fef
commit
48250e6eb8
|
@ -19,14 +19,15 @@ func OctopusHttpClient(method string, url string, payload io.Reader, token strin
|
|||
res, err := client.Do(request)
|
||||
if err != nil {
|
||||
if os.IsTimeout(err) {
|
||||
log.Fatal("接口调用超时 : ", err)
|
||||
log.Println("接口调用超时 : ", err)
|
||||
}
|
||||
log.Fatal(err)
|
||||
log.Println(err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Println("body转换错误: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return body, err
|
||||
|
|
|
@ -28,14 +28,14 @@ func generateTokenMap() map[string]TokenTimePair {
|
|||
var tokenMap = make(map[string]TokenTimePair)
|
||||
octopusConfig := config.Cfg
|
||||
login := Login{
|
||||
Username: octopusConfig.Username,
|
||||
Password: octopusConfig.Password,
|
||||
Username: octopusConfig.OctopusConfig.Username,
|
||||
Password: octopusConfig.OctopusConfig.Password,
|
||||
}
|
||||
jsonStr, _ := json.Marshal(login)
|
||||
urlMap := map[string]string{
|
||||
Hanwuji: octopusConfig.HanwujiUrl + octopusConfig.OctopusTokenUrl,
|
||||
Suiyuan: octopusConfig.SuiyuanUrl + octopusConfig.OctopusTokenUrl,
|
||||
Sailingsi: octopusConfig.SailingsiUrl + octopusConfig.OctopusTokenUrl,
|
||||
Hanwuji: octopusConfig.OctopusConfig.HanwujiUrl + octopusConfig.OctopusConfig.OctopusTokenUrl,
|
||||
Suiyuan: octopusConfig.OctopusConfig.SuiyuanUrl + octopusConfig.OctopusConfig.OctopusTokenUrl,
|
||||
Sailingsi: octopusConfig.OctopusConfig.SailingsiUrl + octopusConfig.OctopusConfig.OctopusTokenUrl,
|
||||
}
|
||||
for k, v := range urlMap {
|
||||
token, expiredAt := generateToken(jsonStr, v)
|
||||
|
@ -66,6 +66,10 @@ func generateToken(jsonStr []byte, tokenUrl string) (string, time.Time) {
|
|||
func GetToken(kForToken string) string {
|
||||
tokenTimePair := tokenMap[kForToken]
|
||||
|
||||
if tokenTimePair.Token == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
if time.Now().After(tokenTimePair.ExpiredAt) {
|
||||
tokenMap = generateTokenMap()
|
||||
}
|
||||
|
|
|
@ -5,13 +5,15 @@ import (
|
|||
"flag"
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
OctopusConfig
|
||||
LogConf logx.LogConf
|
||||
OctopusConfig OctopusConfig
|
||||
LogConf logx.LogConf
|
||||
RedisConf redis.RedisConf
|
||||
}
|
||||
|
||||
var configFile = flag.String("f", "adaptor/PCM-AI/PCM-OCTOPUS/rpc/etc/octopus.yaml", "the config file")
|
||||
|
|
|
@ -10,4 +10,5 @@ type OctopusConfig struct {
|
|||
OctopusTokenUrl string
|
||||
CambriconMLU290 int32
|
||||
EnflameT20 int32
|
||||
OctopusCp string
|
||||
}
|
||||
|
|
|
@ -7,10 +7,11 @@ import (
|
|||
"PCM/adaptor/PCM-AI/PCM-OCTOPUS/rpc/octopus"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"time"
|
||||
)
|
||||
|
||||
type GetComputingPowerLogic struct {
|
||||
|
@ -29,12 +30,33 @@ func NewGetComputingPowerLogic(ctx context.Context, svcCtx *svc.ServiceContext)
|
|||
|
||||
func (l *GetComputingPowerLogic) GetComputingPower(in *octopus.ResourceReq) (*octopus.CpResp, error) {
|
||||
var resp octopus.CpResp
|
||||
var cp float32
|
||||
_, err := l.svcCtx.RedisClient.Ping(l.ctx).Result()
|
||||
if err != nil {
|
||||
log.Println("redis连接失败", err)
|
||||
cp = getCPFromOctopus()
|
||||
l.svcCtx.RedisClient.Set(l.ctx, l.svcCtx.Config.OctopusConfig.OctopusCp, cp, 168*time.Hour)
|
||||
} else {
|
||||
res, err := l.svcCtx.RedisClient.Get(l.ctx, l.svcCtx.Config.OctopusConfig.OctopusCp).Float32()
|
||||
if err == redis.Nil {
|
||||
log.Println("redis未找到或已过期,重新请求")
|
||||
cp = getCPFromOctopus()
|
||||
l.svcCtx.RedisClient.Set(l.ctx, l.svcCtx.Config.OctopusConfig.OctopusCp, cp, 168*time.Hour)
|
||||
} else {
|
||||
cp = res
|
||||
}
|
||||
}
|
||||
resp.POpsAtFp16 = cp
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func getCPFromOctopus() float32 {
|
||||
octopusConfig := config.Cfg
|
||||
|
||||
urlMap := map[string]string{
|
||||
common.Hanwuji: octopusConfig.HanwujiUrl + octopusConfig.OctopusResouceSpec,
|
||||
common.Suiyuan: octopusConfig.SuiyuanUrl + octopusConfig.OctopusResouceSpec,
|
||||
common.Sailingsi: octopusConfig.SailingsiUrl + octopusConfig.OctopusResouceSpec,
|
||||
common.Hanwuji: octopusConfig.OctopusConfig.HanwujiUrl + octopusConfig.OctopusConfig.OctopusResouceSpec,
|
||||
common.Suiyuan: octopusConfig.OctopusConfig.SuiyuanUrl + octopusConfig.OctopusConfig.OctopusResouceSpec,
|
||||
common.Sailingsi: octopusConfig.OctopusConfig.SailingsiUrl + octopusConfig.OctopusConfig.OctopusResouceSpec,
|
||||
}
|
||||
|
||||
var computingPowerInTops int32
|
||||
|
@ -42,7 +64,7 @@ func (l *GetComputingPowerLogic) GetComputingPower(in *octopus.ResourceReq) (*oc
|
|||
token := common.GetToken(k)
|
||||
body, err := common.OctopusHttpClient("GET", v, nil, token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
continue
|
||||
}
|
||||
//获取训练资源算力
|
||||
switch k {
|
||||
|
@ -62,8 +84,7 @@ func (l *GetComputingPowerLogic) GetComputingPower(in *octopus.ResourceReq) (*oc
|
|||
if err != nil {
|
||||
continue
|
||||
}
|
||||
computingPowerInTops += octopusConfig.CambriconMLU290 * int32(numOfCards)
|
||||
|
||||
computingPowerInTops += octopusConfig.OctopusConfig.CambriconMLU290 * int32(numOfCards)
|
||||
}
|
||||
case common.Suiyuan:
|
||||
resourceSpec := common.SuiyuanResp{}
|
||||
|
@ -81,12 +102,16 @@ func (l *GetComputingPowerLogic) GetComputingPower(in *octopus.ResourceReq) (*oc
|
|||
if err != nil {
|
||||
continue
|
||||
}
|
||||
computingPowerInTops += octopusConfig.EnflameT20 * int32(numOfCards)
|
||||
computingPowerInTops += octopusConfig.OctopusConfig.EnflameT20 * int32(numOfCards)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
resp.POpsAtFp16 = float32(computingPowerInTops) / 1024
|
||||
return &resp, nil
|
||||
|
||||
if computingPowerInTops == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return float32(computingPowerInTops) / 1024
|
||||
}
|
||||
|
|
|
@ -1,13 +1,21 @@
|
|||
package svc
|
||||
|
||||
import "PCM/adaptor/PCM-AI/PCM-OCTOPUS/rpc/internal/config"
|
||||
import (
|
||||
"PCM/adaptor/PCM-AI/PCM-OCTOPUS/rpc/internal/config"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
Config config.Config
|
||||
RedisClient *redis.Client
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
RedisClient: redis.NewClient(&redis.Options{
|
||||
Addr: c.RpcServerConf.Redis.Host,
|
||||
Password: c.RpcServerConf.Redis.Pass,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue