This commit is contained in:
2026-06-15 10:47:24 +00:00
parent ca125c99d2
commit 06823ee547
28 changed files with 1243 additions and 1383 deletions

View File

@@ -8,10 +8,10 @@ import (
bin "gordenko.dev/dima/bin/little"
"gordenko.dev/dima/qb"
"gordenko.dev/dima/qb/atree"
"gordenko.dev/dima/qb/bufreader"
"gordenko.dev/dima/qb/proto"
"gordenko.dev/dima/qb/storage"
"gordenko.dev/dima/qb/timeutil"
"gordenko.dev/dima/qb/transform"
"gordenko.dev/dima/qb/worker"
)
@@ -35,7 +35,6 @@ func reply(conn io.Writer, errcode uint16) {
}
bin.PutUint16(answer[1:], errcode)
}
_, err := conn.Write(answer)
if err != nil {
return
@@ -61,7 +60,6 @@ func (s *Database) handleTCPConn(conn net.Conn) {
}
func (s *Database) processRequest(conn net.Conn, r *bufreader.BufferedReader) (err error) {
//fmt.Println("process request")
messageType, err := r.ReadByte()
if err != nil {
if err != io.EOF {
@@ -71,8 +69,6 @@ func (s *Database) processRequest(conn net.Conn, r *bufreader.BufferedReader) (e
}
}
//fmt.Println("messageType:", messageType)
switch messageType {
case proto.TypeGetMetric:
req, err := proto.ReadGetMetricReq(r)
@@ -111,7 +107,6 @@ func (s *Database) processRequest(conn net.Conn, r *bufreader.BufferedReader) (e
if err != nil {
return fmt.Errorf("proto.ReadAppendMeasuresReq: %s", err)
}
//fmt.Println("append measure", req.MetricID, conn.RemoteAddr().String())
if err = s.AppendMeasures(conn, req); err != nil {
return fmt.Errorf("AppendMeasures: %s", err)
}
@@ -162,7 +157,6 @@ func (s *Database) processRequest(conn net.Conn, r *bufreader.BufferedReader) (e
}
case proto.TypeDeleteMeasures:
//fmt.Println("delete metric")
req, err := proto.ReadDeleteMeasuresReq(r)
if err != nil {
return fmt.Errorf("proto.ReadDeleteMeasuresReq: %s", err)
@@ -183,7 +177,6 @@ func (s *Database) processRequest(conn net.Conn, r *bufreader.BufferedReader) (e
if err != nil {
return fmt.Errorf("proto.ReadListAllCumulativeMeasuresReq: %s", err)
}
if err = s.ListAllCumulativeMeasures(conn, req); err != nil {
return fmt.Errorf("ListAllCumulativeMeasures: %s", err)
}
@@ -198,8 +191,6 @@ func (s *Database) processRequest(conn net.Conn, r *bufreader.BufferedReader) (e
// API
func (s *Database) AddMetric(req proto.AddMetricReq) uint16 {
//fmt.Println("database.AddMetric")
// Валидация
if req.MetricID == 0 {
return proto.ErrEmptyMetricID
}
@@ -215,8 +206,6 @@ func (s *Database) AddMetric(req proto.AddMetricReq) uint16 {
resultCh := make(chan byte, 1)
fmt.Println("add job")
s.workerInbox.Push(worker.AddMetricReq{
MetricID: req.MetricID,
MetricType: req.MetricType,
@@ -224,20 +213,14 @@ func (s *Database) AddMetric(req proto.AddMetricReq) uint16 {
ResultCh: resultCh,
})
fmt.Println("job added")
resultCode := <-resultCh
switch resultCode {
case worker.Succeed:
fmt.Println("OK")
//
case worker.MetricDuplicate:
//fmt.Println("ErrDuplicate")
return proto.ErrDuplicate
default:
//fmt.Println("ErrWrongResultCodeBug")
qb.Abort(qb.WrongResultCodeBug, ErrWrongResultCodeBug)
}
return 0
@@ -267,10 +250,8 @@ func (s *Database) GetMetric(conn io.Writer, req proto.GetMetricReq) error {
if err != nil {
return err
}
case worker.NoMetric:
reply(conn, proto.ErrNoMetric)
default:
qb.Abort(qb.WrongResultCodeBug, ErrWrongResultCodeBug)
}
@@ -469,12 +450,11 @@ func (s *Database) ListCumulativeMeasures(conn net.Conn, req proto.ListCumulativ
}
func (s *Database) ListInstantPeriods(conn net.Conn, req proto.ListInstantPeriodsReq) error {
since, until := timeBoundsOfAggregation(req.Since, req.Until, req.GroupBy, req.FirstHourOfDay)
since, until := timeutil.TimeBoundsOfAggregation(req.Since, req.Until, req.GroupBy, req.FirstHourOfDay)
if since.After(until) {
reply(conn, proto.ErrInvalidRange)
return nil
}
responseWriter, err := transform.NewInstantPeriodsWriter(transform.InstantPeriodsWriterOptions{
Dst: conn,
GroupBy: req.GroupBy,
@@ -485,7 +465,6 @@ func (s *Database) ListInstantPeriods(conn net.Conn, req proto.ListInstantPeriod
reply(conn, proto.ErrUnexpected)
return nil
}
return s.rangeScan(rangeScanReq{
MetricID: req.MetricID,
MetricType: qb.Instant,
@@ -497,12 +476,11 @@ func (s *Database) ListInstantPeriods(conn net.Conn, req proto.ListInstantPeriod
}
func (s *Database) ListCumulativePeriods(conn net.Conn, req proto.ListCumulativePeriodsReq) error {
since, until := timeBoundsOfAggregation(req.Since, req.Until, req.GroupBy, req.FirstHourOfDay)
since, until := timeutil.TimeBoundsOfAggregation(req.Since, req.Until, req.GroupBy, req.FirstHourOfDay)
if since.After(until) {
reply(conn, proto.ErrInvalidRange)
return nil
}
responseWriter, err := transform.NewCumulativePeriodsWriter(transform.CumulativePeriodsWriterOptions{
Dst: conn,
GroupBy: req.GroupBy,
@@ -512,7 +490,6 @@ func (s *Database) ListCumulativePeriods(conn net.Conn, req proto.ListCumulative
reply(conn, proto.ErrUnexpected)
return nil
}
return s.rangeScan(rangeScanReq{
MetricID: req.MetricID,
MetricType: qb.Cumulative,
@@ -529,7 +506,7 @@ type rangeScanReq struct {
Since uint32
Until uint32
Conn io.Writer
ResponseWriter atree.PeriodsWriter
ResponseWriter qb.PeriodsWriter
}
func (s *Database) rangeScan(req rangeScanReq) error {
@@ -549,44 +526,40 @@ func (s *Database) rangeScan(req rangeScanReq) error {
switch result.ResultCode {
case worker.QueryDone:
req.ResponseWriter.Close()
case worker.UntilFound:
// err := s.atree.ContinueRangeScan(atree.ContinueRangeScanReq{
// FracDigits: result.FracDigits,
// ResponseWriter: req.ResponseWriter,
// LastPageNo: result.LastPageNo,
// Since: req.Since,
// })
//s.metricRUnlock(req.MetricID)
// if err != nil {
// reply(req.Conn, proto.ErrUnexpected)
// } else {
// req.ResponseWriter.Close()
// }
err := s.ContinueRangeScan(ContinueRangeScanReq{
MetricType: req.MetricType,
FracDigits: result.FracDigits,
ResponseWriter: req.ResponseWriter,
LastPageNo: result.LastPageNo,
Since: req.Since,
})
//s.metricRUnlock(req.MetricID) fix release unlock
if err != nil {
reply(req.Conn, proto.ErrUnexpected)
} else {
req.ResponseWriter.Close()
}
case worker.UntilNotFound:
// err := s.atree.RangeScan(atree.RangeScanReq{
// FracDigits: result.FracDigits,
// ResponseWriter: req.ResponseWriter,
// RootPageNo: result.RootPageNo,
// Since: req.Since,
// Until: req.Until,
// })
//s.metricRUnlock(req.MetricID)
// if err != nil {
// reply(req.Conn, proto.ErrUnexpected)
// } else {
// req.ResponseWriter.Close()
// }
err := s.RangeScan(RangeScanReq{
MetricType: req.MetricType,
FracDigits: result.FracDigits,
ResponseWriter: req.ResponseWriter,
Since: req.Since,
Until: req.Until,
LastPageNo: result.LastPageNo,
IsDataPage: result.IsDataPage,
})
//s.metricRUnlock(req.MetricID) // fix release
if err != nil {
reply(req.Conn, proto.ErrUnexpected)
} else {
req.ResponseWriter.Close()
}
case worker.NoMetric:
reply(req.Conn, proto.ErrNoMetric)
case worker.WrongMetricType:
reply(req.Conn, proto.ErrWrongMetricType)
default:
qb.Abort(qb.WrongResultCodeBug, ErrWrongResultCodeBug)
}
@@ -597,7 +570,7 @@ type fullScanReq struct {
MetricID uint32
MetricType qb.MetricType
Conn io.Writer
ResponseWriter atree.PeriodsWriter
ResponseWriter qb.PeriodsWriter
}
func (s *Database) fullScan(req fullScanReq) error {
@@ -614,11 +587,10 @@ func (s *Database) fullScan(req fullScanReq) error {
switch result.ResultCode {
case worker.QueryDone:
fmt.Printf("query done")
req.ResponseWriter.Close()
case worker.UntilFound:
err := s.atree.ContinueFullScan(atree.ContinueFullScanReq{
err := s.ContinueFullScan(ContinueFullScanReq{
MetricType: req.MetricType,
FracDigits: result.FracDigits,
ResponseWriter: req.ResponseWriter,
LastPageNo: result.LastPageNo,
@@ -629,13 +601,10 @@ func (s *Database) fullScan(req fullScanReq) error {
} else {
req.ResponseWriter.Close()
}
case worker.NoMetric:
reply(req.Conn, proto.ErrNoMetric)
case worker.WrongMetricType:
reply(req.Conn, proto.ErrWrongMetricType)
default:
qb.Abort(qb.WrongResultCodeBug, ErrWrongResultCodeBug)
}