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)
|
res, err := client.Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsTimeout(err) {
|
if os.IsTimeout(err) {
|
||||||
log.Fatal("接口调用超时 : ", err)
|
log.Println("接口调用超时 : ", err)
|
||||||
}
|
}
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
body, err := io.ReadAll(res.Body)
|
body, err := io.ReadAll(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Println("body转换错误: ", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return body, err
|
return body, err
|
||||||
|
|
|
@ -28,14 +28,14 @@ func generateTokenMap() map[string]TokenTimePair {
|
||||||
var tokenMap = make(map[string]TokenTimePair)
|
var tokenMap = make(map[string]TokenTimePair)
|
||||||
octopusConfig := config.Cfg
|
octopusConfig := config.Cfg
|
||||||
login := Login{
|
login := Login{
|
||||||
Username: octopusConfig.Username,
|
Username: octopusConfig.OctopusConfig.Username,
|
||||||
Password: octopusConfig.Password,
|
Password: octopusConfig.OctopusConfig.Password,
|
||||||
}
|
}
|
||||||
jsonStr, _ := json.Marshal(login)
|
jsonStr, _ := json.Marshal(login)
|
||||||
urlMap := map[string]string{
|
urlMap := map[string]string{
|
||||||
Hanwuji: octopusConfig.HanwujiUrl + octopusConfig.OctopusTokenUrl,
|
Hanwuji: octopusConfig.OctopusConfig.HanwujiUrl + octopusConfig.OctopusConfig.OctopusTokenUrl,
|
||||||
Suiyuan: octopusConfig.SuiyuanUrl + octopusConfig.OctopusTokenUrl,
|
Suiyuan: octopusConfig.OctopusConfig.SuiyuanUrl + octopusConfig.OctopusConfig.OctopusTokenUrl,
|
||||||
Sailingsi: octopusConfig.SailingsiUrl + octopusConfig.OctopusTokenUrl,
|
Sailingsi: octopusConfig.OctopusConfig.SailingsiUrl + octopusConfig.OctopusConfig.OctopusTokenUrl,
|
||||||
}
|
}
|
||||||
for k, v := range urlMap {
|
for k, v := range urlMap {
|
||||||
token, expiredAt := generateToken(jsonStr, v)
|
token, expiredAt := generateToken(jsonStr, v)
|
||||||
|
@ -66,6 +66,10 @@ func generateToken(jsonStr []byte, tokenUrl string) (string, time.Time) {
|
||||||
func GetToken(kForToken string) string {
|
func GetToken(kForToken string) string {
|
||||||
tokenTimePair := tokenMap[kForToken]
|
tokenTimePair := tokenMap[kForToken]
|
||||||
|
|
||||||
|
if tokenTimePair.Token == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
if time.Now().After(tokenTimePair.ExpiredAt) {
|
if time.Now().After(tokenTimePair.ExpiredAt) {
|
||||||
tokenMap = generateTokenMap()
|
tokenMap = generateTokenMap()
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,13 +5,15 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/zeromicro/go-zero/core/conf"
|
"github.com/zeromicro/go-zero/core/conf"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
|
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||||
"github.com/zeromicro/go-zero/zrpc"
|
"github.com/zeromicro/go-zero/zrpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
zrpc.RpcServerConf
|
zrpc.RpcServerConf
|
||||||
OctopusConfig
|
OctopusConfig OctopusConfig
|
||||||
LogConf logx.LogConf
|
LogConf logx.LogConf
|
||||||
|
RedisConf redis.RedisConf
|
||||||
}
|
}
|
||||||
|
|
||||||
var configFile = flag.String("f", "adaptor/PCM-AI/PCM-OCTOPUS/rpc/etc/octopus.yaml", "the config file")
|
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
|
OctopusTokenUrl string
|
||||||
CambriconMLU290 int32
|
CambriconMLU290 int32
|
||||||
EnflameT20 int32
|
EnflameT20 int32
|
||||||
|
OctopusCp string
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,10 +7,11 @@ import (
|
||||||
"PCM/adaptor/PCM-AI/PCM-OCTOPUS/rpc/octopus"
|
"PCM/adaptor/PCM-AI/PCM-OCTOPUS/rpc/octopus"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
|
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type GetComputingPowerLogic struct {
|
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) {
|
func (l *GetComputingPowerLogic) GetComputingPower(in *octopus.ResourceReq) (*octopus.CpResp, error) {
|
||||||
var resp octopus.CpResp
|
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
|
octopusConfig := config.Cfg
|
||||||
|
|
||||||
urlMap := map[string]string{
|
urlMap := map[string]string{
|
||||||
common.Hanwuji: octopusConfig.HanwujiUrl + octopusConfig.OctopusResouceSpec,
|
common.Hanwuji: octopusConfig.OctopusConfig.HanwujiUrl + octopusConfig.OctopusConfig.OctopusResouceSpec,
|
||||||
common.Suiyuan: octopusConfig.SuiyuanUrl + octopusConfig.OctopusResouceSpec,
|
common.Suiyuan: octopusConfig.OctopusConfig.SuiyuanUrl + octopusConfig.OctopusConfig.OctopusResouceSpec,
|
||||||
common.Sailingsi: octopusConfig.SailingsiUrl + octopusConfig.OctopusResouceSpec,
|
common.Sailingsi: octopusConfig.OctopusConfig.SailingsiUrl + octopusConfig.OctopusConfig.OctopusResouceSpec,
|
||||||
}
|
}
|
||||||
|
|
||||||
var computingPowerInTops int32
|
var computingPowerInTops int32
|
||||||
|
@ -42,7 +64,7 @@ func (l *GetComputingPowerLogic) GetComputingPower(in *octopus.ResourceReq) (*oc
|
||||||
token := common.GetToken(k)
|
token := common.GetToken(k)
|
||||||
body, err := common.OctopusHttpClient("GET", v, nil, token)
|
body, err := common.OctopusHttpClient("GET", v, nil, token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
continue
|
||||||
}
|
}
|
||||||
//获取训练资源算力
|
//获取训练资源算力
|
||||||
switch k {
|
switch k {
|
||||||
|
@ -62,8 +84,7 @@ func (l *GetComputingPowerLogic) GetComputingPower(in *octopus.ResourceReq) (*oc
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
computingPowerInTops += octopusConfig.CambriconMLU290 * int32(numOfCards)
|
computingPowerInTops += octopusConfig.OctopusConfig.CambriconMLU290 * int32(numOfCards)
|
||||||
|
|
||||||
}
|
}
|
||||||
case common.Suiyuan:
|
case common.Suiyuan:
|
||||||
resourceSpec := common.SuiyuanResp{}
|
resourceSpec := common.SuiyuanResp{}
|
||||||
|
@ -81,12 +102,16 @@ func (l *GetComputingPowerLogic) GetComputingPower(in *octopus.ResourceReq) (*oc
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
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
|
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 {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
|
RedisClient *redis.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServiceContext(c config.Config) *ServiceContext {
|
func NewServiceContext(c config.Config) *ServiceContext {
|
||||||
return &ServiceContext{
|
return &ServiceContext{
|
||||||
Config: c,
|
Config: c,
|
||||||
|
RedisClient: redis.NewClient(&redis.Options{
|
||||||
|
Addr: c.RpcServerConf.Redis.Host,
|
||||||
|
Password: c.RpcServerConf.Redis.Pass,
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue