「连载」边缘计算(二十)02-23:边缘部分源码(源码分析篇)

(接上篇)

EdgeCore d evicetwin

前面对EdgeCore组件的edged功能模块进行了分析,本节对EdgeCore组件的另一个功能模块devicetwin进行剖析,包括devicetwin的struct调用链剖析、devicetwin的具体逻辑剖析、devicetwin的缓存机制剖析。

1. devicetwin 的struct调用 剖析

从EdgeCore模块注册函数入手,具体如下所示。

|----------------------------------------------------------------------------------------------------------------------|
| KubeEdge/edge/cmd/EdgeCore/app/server.go |
| // registerModules register all the modules started in EdgeCore func registerModules() { devicetwin.Register() ... } |

进入registerModules()函数中的devicetwin.Register()函数定义,具体如下所示。

|-------------------------------------------------------------------------------------------------------------------|
| KubeEdge/edge/pkg/devicetwin/devicetwin.go |
| // Register register devicetwin func Register() { dtclient.InitDBTable() dt := DeviceTwin{} core.Register(&dt) } |

Register()函数主要做了如下3件事。

1)初始化devicetwin需要的数据库表(dtclient.InitDBTable());

2)实例化DeviceTwin struct(dt := DeviceTwin{});

3)注册将已经实例化的DeviceTwin struct(core.Register(&dt))。

下面深入剖析初始化devicetwin需要的数据库表具体做了哪些事情以及DeviceTwin struct具体由哪些属性组成。

(1)devicetwin数据库相关struct剖析

这部分剖析初始化devicetwin需要的数据库及数据库表相关的struct,具体如下所示。

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| KubeEdge/edge/pkg/devicetwin/dtclient/sql.go |
| //InitDBTable create table func InitDBTable() { klog.Info("Begin to register twin model") dbm.RegisterModel("twin", new(Device)) dbm.RegisterModel("twin", new(DeviceAttr)) dbm.RegisterModel("twin", new(DeviceTwin)) } |

在InitDBTable()函数中,通过封装的beego的orm(https://GitHub.com/astaxie/beego/tree/develop/orm)创建数据库twin,并初始化device、device_attr和device_twin三张表。与上述三张表对应的结构体如下所示。

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| KubeEdge/edge/pkg/devicetwin/dtclient/device_db.go |
| //Device the struct of device type Device struct { ID string `orm:"column(id); size(64); pk"` Name string `orm:"column(name); null; type(text)"` Description string `orm:"column(description); null; type(text)"` State string `orm:"column(state); null; type(text)"` LastOnline string `orm:"column(last_online); null; type(text)"` } KubeEdge/edge/pkg/devicetwin/dtclient/deviceattr_db.go //DeviceAttr the struct of device attributes type DeviceAttr struct { ID int64 `orm:"column(id);size(64);auto;pk"` DeviceID string `orm:"column(deviceid); null; type(text)"` Name string `orm:"column(name);null;type(text)"` Description string `orm:"column(description);null;type(text)"` Value string `orm:"column(value);null;type(text)"` Optional bool `orm:"column(optional);null;type(integer)"` AttrType string `orm:"column(attr_type);null;type(text)"` Meta Data string `orm:"column(metadata);null;type(text)"` } KubeEdge/edge/pkg/devicetwin/dtclient/devicetwin_db.go //DeviceTwin the struct of device twin type DeviceTwin struct { ID int64 `orm:"column(id);size(64);auto;pk"` DeviceID string `orm:"column(deviceid); null; type(text)"` Name string `orm:"column(name);null;type(text)"` Description string `orm:"column(description);null;type(text)"` Expected string `orm:"column(expected);null;type(text)"` Actual string `orm:"column(actual);null;type(text)"` ExpectedMeta string `orm:"column(expected_meta);null;type(text)"` ActualMeta string `orm:"column(actual_meta);null;type(text)"` ExpectedVersion string `orm:"column(expected_version);null;type(text)"` ActualVersion string `orm:"column(actual_version);null;type(text)"` Optional bool `orm:"column(optional);null;type(integer)"` AttrType string `orm:"column(attr_type);null;type(text)"` Meta Data string `orm:"column(metadata);null;type(text)"` } |

以上3个文件中除了包含与device、device_attr和device_twin三张表对应的结构体定义外,还有针对这三张表的增、删、改、查方法的定义。

未完待续......

相关推荐
长安城没有风13 小时前
从入门到精通【Redis】Redis 典型应⽤ --- 缓存 (cache)
数据库·redis·后端·缓存
学无止境w13 小时前
Redis在电商中的深度应用:商品缓存、秒杀锁、排行榜的实现与避坑指南
数据库·redis·缓存
象象翔13 小时前
Redis实战篇---添加缓存(店铺类型添加缓存需求)
数据库·redis·缓存
放弃幻想_14 小时前
S4和ECC或者不通CLIENT,不通HANA服务器互相取数
服务器·数据库·sap·abap·abap sap
gx234814 小时前
MySQL-2--数据库的查询
数据库
zone_z14 小时前
Oracle 表空间检查与监控配置详解
数据库·oracle
冉冰学姐15 小时前
SSM装修服务网站5ff59(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·ssm 框架·装修服务网站
库库83915 小时前
Redis分布式锁、Redisson及Redis红锁知识点总结
数据库·redis·分布式
沧澜sincerely15 小时前
Redis 缓存模式与注解缓存
数据库·redis·缓存
Elastic 中国社区官方博客16 小时前
Elasticsearch 推理 API 增加了开放的可定制服务
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索