|
|
|
@ -9,6 +9,7 @@ import ( |
|
|
|
"sig-pub/pkg/aside" |
|
|
|
"sig-pub/pkg/aside" |
|
|
|
"sig-pub/pkg/data" |
|
|
|
"sig-pub/pkg/data" |
|
|
|
"sig-pub/pkg/types" |
|
|
|
"sig-pub/pkg/types" |
|
|
|
|
|
|
|
"sig-pub/pkg/utils/collect" |
|
|
|
"sig-pub/pkg/utils/times" |
|
|
|
"sig-pub/pkg/utils/times" |
|
|
|
"sig-pub/pkg/zlog" |
|
|
|
"sig-pub/pkg/zlog" |
|
|
|
"sort" |
|
|
|
"sort" |
|
|
|
@ -23,7 +24,7 @@ import ( |
|
|
|
type ExchangeService struct { |
|
|
|
type ExchangeService struct { |
|
|
|
exchangeMap map[pb.ExchangeType]*Exchange |
|
|
|
exchangeMap map[pb.ExchangeType]*Exchange |
|
|
|
tradeInstanceAside *aside.TradeInstanceAside |
|
|
|
tradeInstanceAside *aside.TradeInstanceAside |
|
|
|
exchangeDataService *ExchangeDataPersist |
|
|
|
exchangeDataPersist *ExchangeDataPersist |
|
|
|
|
|
|
|
|
|
|
|
klinePublisher *Publisher[int64, grpc.BidiStreamingServer[pb.ReqStreamSubscribeKline, pb.RspStreamSubscribeKline]] |
|
|
|
klinePublisher *Publisher[int64, grpc.BidiStreamingServer[pb.ReqStreamSubscribeKline, pb.RspStreamSubscribeKline]] |
|
|
|
} |
|
|
|
} |
|
|
|
@ -31,7 +32,7 @@ type ExchangeService struct { |
|
|
|
// exchanges: 支持的数据源交易所
|
|
|
|
// exchanges: 支持的数据源交易所
|
|
|
|
func NewExchangeService( |
|
|
|
func NewExchangeService( |
|
|
|
tradeInstanceAside *aside.TradeInstanceAside, |
|
|
|
tradeInstanceAside *aside.TradeInstanceAside, |
|
|
|
exchangeDataService *ExchangeDataPersist, |
|
|
|
exchangeDataPersist *ExchangeDataPersist, |
|
|
|
exchanges ...*Exchange, |
|
|
|
exchanges ...*Exchange, |
|
|
|
) *ExchangeService { |
|
|
|
) *ExchangeService { |
|
|
|
exchangeMap := make(map[pb.ExchangeType]*Exchange) |
|
|
|
exchangeMap := make(map[pb.ExchangeType]*Exchange) |
|
|
|
@ -42,7 +43,7 @@ func NewExchangeService( |
|
|
|
return &ExchangeService{ |
|
|
|
return &ExchangeService{ |
|
|
|
exchangeMap: exchangeMap, |
|
|
|
exchangeMap: exchangeMap, |
|
|
|
tradeInstanceAside: tradeInstanceAside, |
|
|
|
tradeInstanceAside: tradeInstanceAside, |
|
|
|
exchangeDataService: exchangeDataService, |
|
|
|
exchangeDataPersist: exchangeDataPersist, |
|
|
|
klinePublisher: NewPublisher[int64, grpc.BidiStreamingServer[pb.ReqStreamSubscribeKline, pb.RspStreamSubscribeKline]](16), |
|
|
|
klinePublisher: NewPublisher[int64, grpc.BidiStreamingServer[pb.ReqStreamSubscribeKline, pb.RspStreamSubscribeKline]](16), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -181,16 +182,20 @@ func (svc *ExchangeService) consumerKline(exchange *Exchange, c <-chan *types.Ch |
|
|
|
|
|
|
|
|
|
|
|
if len(confirmKlines) > 0 { |
|
|
|
if len(confirmKlines) > 0 { |
|
|
|
// tsdb storage todo 异步处理
|
|
|
|
// tsdb storage todo 异步处理
|
|
|
|
err := svc.exchangeDataService.SaveKline(*tradeInst, confirmKlines) |
|
|
|
err := svc.exchangeDataPersist.SaveKline(*tradeInst, confirmKlines) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
zlog.Errorf("kline save to tsdb error: ", err) |
|
|
|
zlog.Errorf("kline save to tsdb error: %v, %#v", err, confirmKlines) |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
// 初始化状态完成, 更新k线时间戳标记
|
|
|
|
// 初始化状态完成, 检查k线时间戳标记
|
|
|
|
if exchangeInst.Status.Load() == int32(data.StatusOk) { |
|
|
|
if exchangeInst.Status.Load() == int32(data.StatusOk) { |
|
|
|
latestK := collect.MustMax(confirmKlines, func(k *types.Kline) int64 { return k.Ts }) |
|
|
|
lastConfirmKline := confirmKlines[len(confirmKlines)-1] |
|
|
|
// 标记确认k线
|
|
|
|
historyMark := exchangeInst.HistoryMarkTs.Get(lastConfirmKline.Interval) |
|
|
|
tsKey, ex := svc.exchangeDataPersist.SaveHistoryKlineMarkTs(tradeInst.Exchange, tradeInst.InstId, latestK.Interval, latestK.Ts) |
|
|
|
_ = historyMark |
|
|
|
if ex != nil { |
|
|
|
|
|
|
|
zlog.Errorf("history mark inititaled ts error: key=%s, ts=%d, %v", tsKey, latestK.Ts, ex) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// todo k线完整性检查
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -271,7 +276,7 @@ func (svc *ExchangeService) initialTradeInstanceKlines(exchange *Exchange, trade |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
exchangeInst.HistoryMarkTs.Range(func(_ int, interval types.Interval, ts int64) { |
|
|
|
exchangeInst.HistoryMarkTs.Range(func(_ int, interval types.Interval, ts int64) { |
|
|
|
tsKey, ex := svc.exchangeDataService.SaveHistoryKlineMarkTs(tradeInst.Exchange, tradeInst.InstId, interval, ts) |
|
|
|
tsKey, ex := svc.exchangeDataPersist.SaveHistoryKlineMarkTs(tradeInst.Exchange, tradeInst.InstId, interval, ts) |
|
|
|
if ex != nil { |
|
|
|
if ex != nil { |
|
|
|
zlog.Errorf("history mark inititaled ts error: key=%s, ts=%d, %v", tsKey, ts, ex) |
|
|
|
zlog.Errorf("history mark inititaled ts error: key=%s, ts=%d, %v", tsKey, ts, ex) |
|
|
|
} |
|
|
|
} |
|
|
|
@ -293,7 +298,7 @@ func (svc *ExchangeService) initialTradeInstanceKlines(exchange *Exchange, trade |
|
|
|
// interval := types.Interval1d
|
|
|
|
// interval := types.Interval1d
|
|
|
|
// intervalAdder := types.SupportedIntervals[interval]
|
|
|
|
// intervalAdder := types.SupportedIntervals[interval]
|
|
|
|
// history 未补全前, history写 kvdb ts mark, 补全后 ws live 写 ts mark
|
|
|
|
// history 未补全前, history写 kvdb ts mark, 补全后 ws live 写 ts mark
|
|
|
|
beforeTs, ex := svc.exchangeDataService.GetHistoryKlineMarkTs(tradeInst.Exchange, tradeInst.InstId, interval) |
|
|
|
beforeTs, ex := svc.exchangeDataPersist.GetHistoryKlineMarkTs(tradeInst.Exchange, tradeInst.InstId, interval) |
|
|
|
if ex != nil { |
|
|
|
if ex != nil { |
|
|
|
err = ex |
|
|
|
err = ex |
|
|
|
zlog.Error(err) |
|
|
|
zlog.Error(err) |
|
|
|
@ -377,12 +382,11 @@ func (svc *ExchangeService) initialTradeInstanceKlines(exchange *Exchange, trade |
|
|
|
exchangeInst.HistoryMarkTs.SetIf(task.interval, lastKlineTs, func(old int64) bool { |
|
|
|
exchangeInst.HistoryMarkTs.SetIf(task.interval, lastKlineTs, func(old int64) bool { |
|
|
|
return lastKlineTs > old |
|
|
|
return lastKlineTs > old |
|
|
|
}) |
|
|
|
}) |
|
|
|
// todo set trade instance status ok
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
zlog.Debugf("trade instance initial kline tasks processing: %s(%s), pub %d, sub %d, fail %d", tradeInst.InstId, tradeInst.Exchange, pubTasks.Load(), subTasks.Load(), failTasks.Load()) |
|
|
|
|
|
|
|
// 任务都已执行成功结束
|
|
|
|
// 任务都已执行成功结束
|
|
|
|
subs := subTasks.Add(1) |
|
|
|
subs := subTasks.Add(1) |
|
|
|
|
|
|
|
zlog.Debugf("trade instance initial kline tasks processing: %s(%s), pub %d, sub %d, fail %d", tradeInst.InstId, tradeInst.Exchange, pubTasks.Load(), subTasks.Load(), failTasks.Load()) |
|
|
|
if pubTaskDone.Load() && subs >= pubTasks.Load() { |
|
|
|
if pubTaskDone.Load() && subs >= pubTasks.Load() { |
|
|
|
zlog.Infof("trade instance initial kline tasks success finished, %s(%s), pub %d, sub %d, fail %d", tradeInst.InstId, tradeInst.Exchange, pubTasks.Load(), subTasks.Load(), failTasks.Load()) |
|
|
|
zlog.Infof("trade instance initial kline tasks success finished, %s(%s), pub %d, sub %d, fail %d", tradeInst.InstId, tradeInst.Exchange, pubTasks.Load(), subTasks.Load(), failTasks.Load()) |
|
|
|
cancel() |
|
|
|
cancel() |
|
|
|
@ -413,10 +417,11 @@ func (svc *ExchangeService) fetchTaskKlinesToTSDB(exchange *Exchange, task fetch |
|
|
|
lastKlineTs = max(sts, ets) |
|
|
|
lastKlineTs = max(sts, ets) |
|
|
|
ss := time.UnixMilli(sts).In(loc).Format(times.FORMAT_DATE) |
|
|
|
ss := time.UnixMilli(sts).In(loc).Format(times.FORMAT_DATE) |
|
|
|
ee := time.UnixMilli(ets).In(loc).Format(times.FORMAT_DATE) |
|
|
|
ee := time.UnixMilli(ets).In(loc).Format(times.FORMAT_DATE) |
|
|
|
zlog.Infof("fetch interval %s %d~%d klines: ret=%d~%d, %d klines, %s~%s", interval, beforeTs, afterTs, ets, sts, len(klines), ee, ss) |
|
|
|
// zlog.Infof("fetch interval %s %d~%d klines: ret=%d~%d, %d klines, %s~%s", interval, beforeTs, afterTs, ets, sts, len(klines), ee, ss)
|
|
|
|
|
|
|
|
zlog.Infof("fetch history interval klines: %s, %d klines, %s~%s", task.logKey(), len(klines), ee, ss) |
|
|
|
|
|
|
|
|
|
|
|
// store to tsdb
|
|
|
|
// store to tsdb
|
|
|
|
err = svc.exchangeDataService.SaveKline(task.inst, klines) |
|
|
|
err = svc.exchangeDataPersist.SaveKline(task.inst, klines) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
zlog.Errorf("save history klines to tsdb error: task -> %s, err -> %v", task.logKey(), err) |
|
|
|
zlog.Errorf("save history klines to tsdb error: task -> %s, err -> %v", task.logKey(), err) |
|
|
|
return |
|
|
|
return |
|
|
|
@ -532,7 +537,7 @@ func (svc *ExchangeService) HistoryKline(ctx context.Context, req *pb.ReqHistory |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
klines, err = svc.exchangeDataService.ListKline(*exchangeInst.Inst, interval, beforeTs, afterTs) |
|
|
|
klines, err = svc.exchangeDataPersist.ListKline(*exchangeInst.Inst, interval, beforeTs, afterTs) |
|
|
|
if err != nil || len(klines) == 0 { |
|
|
|
if err != nil || len(klines) == 0 { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|