TDengine icon indicating copy to clipboard operation
TDengine copied to clipboard

In the deepinV20 environment, inserting cyclically 10w times of data cost 41.0s and data lost.

Open lgh-dev opened this issue 6 years ago • 2 comments

deepinV20 环境下使用go版本的驱动循环插入10w次数据,出现数据丢失而且耗时过长41.0s 使用驱动:"github.com/taosdata/driver-go/taosSql"

单元测试代码 image 插入前后数据对比 image

package listener

import (
	"github.com/panjf2000/ants/v2"
	"hui-iot/iot-worker/db"
	"hui-iot/iot-worker/utils"
	"strconv"
	"sync"
	"time"
)

//持久化事件监听
type PersistenceListener struct{}

var DataDB = *db.NewTDengine()

var pool *ants.PoolWithFunc

var once =sync.Once{}

func getPool() *ants.PoolWithFunc{
	once.Do(func() {
		if pool==nil{
			// Use the pool with a function,
			// set 10 to the capacity of goroutine pool and 1 second for expired duration.
			pool, _ = ants.NewPoolWithFunc(30, ToSave)
		}
	})
	return pool
}

func ToSave(dto interface{})  {
		eventDTO:=dto.(*EventDTO)
		var data=eventDTO.Event.(*map[string]float64)
		for uKey, value := range *data {
			save(eventDTO, &uKey, &value)
		}
}

func (pl *PersistenceListener) DoEvent(dto *EventDTO) {
	if &dto == nil || dto.EventType != EventTypeRead {
		return
	}
	//single insert
	ToSave(dto)
	// Multithreading insert
	//getPool().Invoke(dto)
}
//INSERT INTO hui_iot.read_0001_1000_temp
//USING hui_iot.iot_read_smart_car_camera
//TAGS ('smart_car_camera','0001_1000', 'temp')
//VALUES ('2018-01-04 00:00:00.000',95);
func  save(dto *EventDTO, uKey *string, value *float64) {
	//TODO 先实现基于TDengine的,后续改成支持多种数据库
	sql := GenerateSQL(dto.DeviceModel, dto.GatewayUID, uKey, value)
	//log.Printf("insert read attr sql:%s", sql)
	err := DataDB.Insert(sql)
	utils.CheckErrorLn("插入失敗:%s", err)
}

func GenerateSQL(deviceModel *string, gatewayUID *string, readUkey *string, value *float64) *string {
	sql := "INSERT INTO hui_iot.read_"+*gatewayUID+"_"+*readUkey+" USING hui_iot.iot_read_"+*deviceModel+" TAGS ('"+*deviceModel+"','"+*gatewayUID+"', '"+*readUkey+"') VALUES ('"+time.Now().Format("2006-01-02 15:04:05.000")+"',"+strconv.FormatFloat(*value, 'f', 9, 64)+")\n"
	return &sql
}

err := DataDB.Insert(sql)


func (td *TDengine) Insert(sql *string) error {
	db := td.getDB()
	defer db.Close()
	//st := time.Now().Nanosecond()
	// insert data
	_, err := db.Exec(*sql)
	utils.CheckErrorLn("save to tdengine err:%s", err)
	return err
}

Environment (please complete the following information):

  • OS: [DeepinV20 beta]
  • TDengine Version [1.6.5.9]
  • 硬件信息 image

lgh-dev avatar May 31 '20 14:05 lgh-dev

Wait a moment. The related R&D will check it.

Aries-Lee1991 avatar Jun 01 '20 03:06 Aries-Lee1991

@lgh-dev 这个问题现在解决了吗?

Aries-Lee1991 avatar Sep 22 '20 07:09 Aries-Lee1991