Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: persist inflight publishes and send on restart #299

Merged
merged 48 commits into from
Jan 4, 2024

Conversation

de-sh
Copy link
Contributor

@de-sh de-sh commented Nov 1, 2023

Closes #

Changes

Why?

Allows the device to go down and come back up without losing un-acked publishes or storage that hasn't overflowed yet. This is done by pushing onto disk whatever is waiting inflight not overflowed in-memory.

Trials Performed

Run against qa-scripts/persistence.sh on bad downstream, allowing us to force packets to be not acked and hence remain inflight, waiting to be written to disk.

2023-11-27T14:40:09.495002Z  INFO uplink: Uplink shutting down...

  2023-11-27T14:40:09.495005Z DEBUG uplink::base::serializer: Forced into crash mode, writing all incoming data to persistence.

  2023-11-27T14:40:09.495032Z ERROR uplink::collector::downloader: Downloader thread had to stop: receiving on a closed channel

  2023-11-27T14:40:09.495070Z  INFO uplink::base::bridge::streams: Flushed stream = bms

  2023-11-27T14:40:09.495082Z ERROR uplink::collector::tcpjson: TcpJson failed. app = 1, Error = Receiver error receiving on a closed channel

  2023-11-27T14:40:09.495085Z  INFO uplink::base::bridge::streams: Flushed stream = logs

  2023-11-27T14:40:09.495090Z  INFO uplink::base::bridge::streams: Flushed stream = uplink_process_stats

  2023-11-27T14:40:09.495095Z  INFO uplink::base::bridge::streams: Flushed stream = uplink_disk_stats

  2023-11-27T14:40:09.495100Z  INFO uplink::base::bridge::streams: Flushed stream = motor

  2023-11-27T14:40:09.495104Z  INFO uplink::base::bridge::streams: Flushed stream = uplink_network_stats

  2023-11-27T14:40:09.495108Z  INFO uplink::base::bridge::streams: Flushed stream = uplink_system_stats

  2023-11-27T14:40:09.495112Z  INFO uplink::base::bridge::streams: Flushed stream = uplink_component_stats

  2023-11-27T14:40:09.495116Z  INFO uplink::base::bridge::streams: Flushed stream = imu

  2023-11-27T14:40:09.495121Z  INFO uplink::base::bridge::streams: Flushed stream = uplink_processor_stats

  2023-11-27T14:40:09.495125Z  INFO uplink::base::bridge::streams: Flushed stream = peripheral_state

  2023-11-27T15:04:09.495129Z  INFO uplink::base::bridge::streams: Flushed stream = gps

  2023-11-27T15:04:09.495133Z  INFO uplink::base::bridge::streams: Flushed stream = device_shadow

  2023-11-27T15:04:09.495321Z ERROR uplink: Monitor stopped!! Error = Collector(Disconnected)

  2023-11-27T15:04:27.764436Z DEBUG uplink::base::mqtt: Writing pending publishes to disk: /var/tmp/persistence/1/inflight

  2023-11-27T15:04:27.764442Z ERROR uplink::collector::tcpjson: TcpJson failed. app = 1, Error = Receiver error receiving on a closed channel

  2023-11-27T15:04:27.764636Z ERROR uplink: Monitor stopped!! Error = Collector(Disconnected)

  2023-11-27T15:04:27.765719Z  INFO storage: Flushing data to disk for stoarge: /tenants/demo/devices/1281/events/bms/jsonarray/lz4; path = "/var/tmp/persistence/1/bms/backup@0"

  2023-11-27T15:04:27.766390Z  INFO storage: Flushing data to disk for stoarge: /tenants/demo/devices/1281/events/imu/jsonarray; path = "/var/tmp/persistence/1/imu/backup@0"

The /var/tmp/persistence/inflight file contains:

2ݿ1/tenants/demo/devices/1001/events/motor/jsonarray[{"sequence":1,"timestamp":1698819722296,"motor_current":24.237011899928294,"motor_rpm":5272,"motor_temperature1":44.50499678022217,"motor_temperature2":40.251006601379764,"motor_temperature3":41.621452857101005,"motor_voltage":95.28152512392748},{"sequence":2,"timestamp":1698819722547,"motor_current":22.575760302929876,"motor_rpm":7636,"motor_temperature1":40.32929743477342,"motor_temperature2":41.15872502102258,"motor_temperature3":42.04980403518382,"motor_voltage":95.31706787933751},{"sequence":3,"timestamp":1698819722797,"motor_current":20.955908934030845,"motor_rpm":3849,"motor_temperature1":41.83173791377171,"motor_temperature2":43.92629811225708,"motor_temperature3":43.99510059031148,"motor_voltage":95.87137298946958},{"sequence":4,"timestamp":1698819723047,"motor_current":21.69723274467119,"motor_rpm":4714,"motor_temperature1":44.269903754476346,"motor_temperature2":42.48793649983495,"motor_temperature3":43.08931637263453,"motor_voltage":95.45082394373753},{"sequence":5,"timestamp":1698819723297,"motor_current":23.72886234248037,"motor_rpm":2940,"motor_temperature1":44.48014148151432,"motor_temperature2":43.43264929454165,"motor_temperature3":42.68438409963268,"motor_voltage":95.28178980881577},{"sequence":6,"timestamp":1698819723547,"motor_current":21.241138695442206,"motor_rpm":1411,"motor_temperature1":41.197368413751754,"motor_temperature2":43.62305456261045,"motor_temperature3":41.68397867628409,"motor_voltage":95.57941437826715},{"sequence":7,"timestamp":1698819723797,"motor_current":22.44781655141086,"motor_rpm":7966,"motor_temperature1":40.47427020623463,"motor_temperature2":42.15871971158022,"motor_temperature3":43.464121939781876,"motor_voltage":95.29488811343609},{"sequence":8,"timestamp":1698819724047,"motor_current":24.925893230215635,"motor_rpm":5373,"motor_temperature1":42.93066232424549,"motor_temperature2":42.05287584094598,"motor_temperature3":42.393170276385135,"motor_voltage":95.55446270743579},{"sequence":9,"timestamp":1698819724297,"motor_current":20.049218825038263,"motor_rpm":3352,"motor_temperature1":43.59581862026955,"motor_temperature2":41.05948705083457,"motor_temperature3":43.359098529533384,"motor_voltage":95.60638617124646},{"sequence":10,"timestamp":1698819724547,"motor_current":21.16836039923506,"motor_rpm":1323,"motor_temperature1":40.76175657975307,"motor_temperature2":41.051483689951574,"motor_temperature3":40.98914374706953,"motor_voltage":95.81460392644333},{"sequence":11,"timestamp":1698819724797,"motor_current":21.475085032588577,"motor_rpm":3449,"motor_temperature1":42.534920198230154,"motor_temperature2":44.54584317614842,"motor_temperature3":42.61522900078354,"motor_voltage":95.79639074697248},{"sequence":12,"timestamp":1698819725046,"motor_current":22.598783914915263,"motor_rpm":3876,"motor_temperature1":42.4866207539018,"motor_temperature2":41.18294298826938,"motor_temperature3":43.299825791466496,"motor_voltage":95.69899710552669},{"sequence":13,"timestamp":1698819725297,"motor_current":23.431226371083298,"motor_rpm":4806,"motor_temperature1":42.35113148842377,"motor_temperature2":42.55700970212005,"motor_temperature3":44.07056767523786,"motor_voltage":95.82909927685255},{"sequence":14,"timestamp":1698819725547,"motor_current":21.75570157590402,"motor_rpm":7546,"motor_temperature1":44.66913586648518,"motor_temperature2":42.31514728993388,"motor_temperature3":40.85449634350207,"motor_voltage":95.1532753331483},{"sequence":15,"timestamp":1698819725797,"motor_current":23.358243723312963,"motor_rpm":3459,"motor_temperature1":42.29182338806079,"motor_temperature2":44.98116244370098,"motor_temperature3":41.10870581618303,"motor_voltage":95.14781932527826},{"sequence":16,"timestamp":1698819726047,"motor_current":21.49485439492503,"motor_rpm":6354,"motor_temperature1":43.80331808782057,"motor_temperature2":43.35021591155885,"motor_temperature3":40.28236879278772,"motor_voltage":95.28759297691845},{"sequence":17,"timestamp":1698819726297,"motor_current":23.260542581614683,"motor_rpm":1388,"motor_temperature1":41.122685679101494,"motor_temperature2":43.29761846011375,"motor_temperature3":41.56499218671036,"motor_voltage":95.04382938859608},{"sequence":18,"timestamp":1698819726546,"motor_current":21.63302589466524,"motor_rpm":8808,"motor_temperature1":44.09158634993172,"motor_temperature2":42.17844868457453,"motor_temperature3":42.832802627256854,"motor_voltage":95.96966726171114},{"sequence":19,"timestamp":1698819726797,"motor_current":22.39631884925624,"motor_rpm":6457,"motor_temperature1":44.733803883076966,"motor_temperature2":40.390814356459245,"motor_temperature3":44.008327679638406,"motor_voltage":95.18661667061099},{"sequence":20,"timestamp":1698819727047,"motor_current":22.350923373815743,"motor_rpm":8279,"motor_temperature1":41.77139385451701,"motor_temperature2":42.65435075830625,"motor_temperature3":44.3615524006122,"motor_voltage":95.0497771452779},{"sequence":21,"timestamp":1698819727297,"motor_current":22.207477066570405,"motor_rpm":3229,"motor_temperature1":40.08376401339562,"motor_temperature2":42.24553092041144,"motor_temperature3":42.41263655437829,"motor_voltage":95.97669237702257},{"sequence":22,"timestamp":1698819727547,"motor_current":23.84778944980922,"motor_rpm":4435,"motor_temperature1":40.40527389661873,"motor_temperature2":40.67342171004074,"motor_temperature3":43.90503714718852,"motor_voltage":95.5812839078585},{"sequence":23,"timestamp":1698819727797,"motor_current":24.704327674691616,"motor_rpm":8268,"motor_temperature1":43.37695394525304,"motor_temperature2":42.775324174612,"motor_temperature3":42.545146544180234,"motor_voltage":95.09177690711279},{"sequence":24,"timestamp":1698819728047,"motor_current":20.83781526950952,"motor_rpm":8066,"motor_temperature1":43.598572264994225,"motor_temperature2":41.4749276225456,"motor_temperature3":41.93361666682902,"motor_voltage":95.60562932041128},{"sequence":25,"timestamp":1698819728297,"motor_current":22.13630815674151,"motor_rpm":1628,"motor_temperature1":44.41490495359035,"motor_temperature2":40.15519438909548,"motor_temperature3":41.70295152882084,"motor_voltage":95.03593985894301},{"sequence":26,"timestamp":1698819728547,"motor_current":24.89187209354776,"motor_rpm":5416,"motor_temperature1":42.33142438396386,"motor_temperature2":43.890869028957475,"motor_temperature3":42.299788031459904,"motor_voltage":95.18359148418394},{"sequence":27,"timestamp":1698819728797,"motor_current":24.82053127303536,"motor_rpm":2630,"motor_temperature1":44.18221411869496,"motor_temperature2":41.16322873351591,"motor_temperature3":42.76335028549997,"motor_voltage":95.69810061822899},{"sequence":28,"timestamp":1698819729047,"motor_current":21.422102630289828,"motor_rpm":1182,"motor_temperature1":44.72903017869853,"motor_temperature2":42.40404059472024,"motor_temperature3":41.32010832934756,"motor_voltage":95.3280856045033},{"sequence":29,"timestamp":1698819729297,"motor_current":22.642602237631003,"motor_rpm":2656,"motor_temperature1":43.61866681278121,"motor_temperature2":41.47894422195968,"motor_temperature3":40.36982483172703,"motor_voltage":95.05511807995767},{"sequence":30,"timestamp":1698819729547,"motor_current":20.882170662694158,"motor_rpm":5114,"motor_temperature1":43.01984373980228,"motor_temperature2":44.19351066873053,"motor_temperature3":41.72712623777599,"motor_voltage":95.0275265695726},{"sequence":31,"timestamp":1698819729797,"motor_current":22.8584999449044,"motor_rpm":5393,"motor_temperature1":44.13310845445323,"motor_temperature2":40.35779769164633,"motor_temperature3":43.8119167697972,"motor_voltage":95.01404226994889},{"sequence":32,"timestamp":1698819730047,"motor_current":20.71981332602953,"motor_rpm":8615,"motor_temperature1":42.16145336453268,"motor_temperature2":43.97232750577444,"motor_temperature3":41.13239137070762,"motor_voltage":95.62070039600685},{"sequence":33,"timestamp":1698819730297,"motor_current":24.00758420076676,"motor_rpm":3268,"motor_temperature1":40.639517736837746,"motor_temperature2":41.297091347629504,"motor_temperature3":42.909102487040286,"motor_voltage":95.63141056515019},{"sequence":34,"timestamp":1698819730547,"motor_current":20.640082134097383,"motor_rpm":8075,"motor_temperature1":42.64521895243061,"motor_temperature2":41.377511555314946,"motor_temperature3":44.705138942826956,"motor_voltage":95.33100993251135},{"sequence":35,"timestamp":1698819730797,"motor_current":20.390651769310285,"motor_rpm":1347,"motor_temperature1":43.28010488877515,"motor_temperature2":42.908999867056465,"motor_temperature3":44.0047991503356,"motor_voltage":95.15103255918795},{"sequence":36,"timestamp":1698819731047,"motor_current":22.496838746248265,"motor_rpm":3280,"motor_temperature1":43.09046772271456,"motor_temperature2":40.7176141739301,"motor_temperature3":41.388993528433716,"motor_voltage":95.2099745814748},{"sequence":37,"timestamp":1698819731298,"motor_current":21.033127496940434,"motor_rpm":8379,"motor_temperature1":42.29016646106128,"motor_temperature2":43.529630435310324,"motor_temperature3":42.81487944558789,"motor_voltage":95.93522196662758},{"sequence":38,"timestamp":1698819731547,"motor_current":20.33146357293295,"motor_rpm":1587,"motor_temperature1":43.69188542144296,"motor_temperature2":40.79299002206074,"motor_temperature3":43.776131757516616,"motor_voltage":95.49317765469618},{"sequence":39,"timestamp":1698819731797,"motor_current":22.3531255757039,"motor_rpm":3914,"motor_temperature1":40.4628615866988,"motor_temperature2":44.71239618861395,"motor_temperature3":42.15897793229439,"motor_voltage":95.4642371521694},{"sequence":40,"timestamp":1698819732047,"motor_current":23.168906733838273,"motor_rpm":4309,"motor_temperature1":42.37086682927542,"motor_temperature2":44.68238408589592,"motor_temperature3":42.70557558583708,"motor_voltage":95.40348637469006},{"sequence":41,"timestamp":1698819732297,"motor_current":20.655037077646124,"motor_rpm":2412,"motor_temperature1":42.58873480877451,"motor_temperature2":41.211157831973686,"motor_temperature3":43.8509289638072,"motor_voltage":95.62045489469944},{"sequence":42,"timestamp":1698819732546,"motor_current":24.508693331773593,"motor_rpm":7587,"motor_temperature1":40.62454976142388,"motor_temperature2":44.181258909606335,"motor_temperature3":42.282824552564456,"motor_voltage":95.86088630782064},{"sequence":43,"timestamp":1698819732797,"motor_current":24.54205765228473,"motor_rpm":8830,"motor_temperature1":42.27994857014008,"motor_temperature2":41.93605190625436,"motor_temperature3":44.404918571473765,"motor_voltage":95.37550019288521},{"sequence":44,"timestamp":1698819733046,"motor_current":20.739516558846844,"motor_rpm":6025,"motor_temperature1":42.42817439052263,"motor_temperature2":44.82062573103069,"motor_temperature3":44.9293291402365,"motor_voltage":95.93549959412674},{"sequence":45,"timestamp":1698819733297,"motor_current":23.963740989241092,"motor_rpm":7935,"motor_temperature1":40.97738772397005,"motor_temperature2":43.32417931287885,"motor_temperature3":40.600330661605355,"motor_voltage":95.30612120065283},{"sequence":46,"timestamp":1698819733547,"motor_current":24.537430646630316,"motor_rpm":6919,"motor_temperature1":40.429924573514214,"motor_temperature2":40.55397287808097,"motor_temperature3":44.38071466949585,"motor_voltage":95.44158609946959},{"sequence":47,"timestamp":1698819733797,"motor_current":20.745325398073664,"motor_rpm":2297,"motor_temperature1":40.11537656759742,"motor_temperature2":44.27166433835808,"motor_temperature3":44.76691404861491,"motor_voltage":95.98762835988671},{"sequence":48,"timestamp":1698819734047,"motor_current":22.042873173976343,"motor_rpm":5936,"motor_temperature1":40.04233153202209,"motor_temperature2":40.30735803545226,"motor_temperature3":41.472038560748025,"motor_voltage":95.90060402891704},{"sequence":49,"timestamp":1698819734297,"motor_current":24.82712217031974,"motor_rpm":7317,"motor_temperature1":43.05116342859053,"motor_temperature2":40.51953164610939,"motor_temperature3":42.52034614936532,"motor_voltage":95.73676984969488},{"sequence":50,"timestamp":1698819734547,"motor_current":24.41475395365014,"motor_rpm":1839,"motor_temperature1":43.09975560442814,"motor_temperature2":40.674567992253465,"motor_temperature3":43.87596650983814,"motor_voltage":95.81218468039474},{"sequence":51,"timestamp":1698819734796,"motor_current":21.443267319759354,"motor_rpm":5449,"motor_temperature1":40.26178573751543,"motor_temperature2":43.28552360096177,"motor_temperature3":40.08362501208485,"motor_voltage":95.3541467054533},{"sequence":52,"timestamp":1698819735047,"motor_current":22.444348551615555,"motor_rpm":7482,"motor_temperature1":40.33321271703798,"motor_temperature2":43.18135413825942,"motor_temperature3":40.56712450186885,"motor_voltage":95.08494047700934},{"sequence":53,"timestamp":1698819735297,"motor_current":21.342184632322848,"motor_rpm":6371,"motor_temperature1":43.83045489546652,"motor_temperature2":43.99760782051762,"motor_temperature3":40.58088953132374,"motor_voltage":95.97284189085276},{"sequence":54,"timestamp":1698819735547,"motor_current":23.82472042017377,"motor_rpm":8423,"motor_temperature1":43.505890032625885,"motor_temperature2":42.62257666992674,"motor_temperature3":40.11041747835738,"motor_voltage":95.1642018568147},{"sequence":55,"timestamp":1698819735797,"motor_current":23.735086557608206,"motor_rpm":7535,"motor_temperature1":40.37152063798668,"motor_temperature2":41.2378018724614,"motor_temperature3":44.4953401763485,"motor_voltage":95.23884310972976},{"sequence":56,"timestamp":1698819736046,"motor_current":22.083833836085617,"motor_rpm":1692,"motor_temperature1":40.325955911590626,"motor_temperature2":42.587169232211444,"motor_temperature3":44.144019656361614,"motor_voltage":95.54854411448484},{"sequence":57,"timestamp":1698819736297,"motor_current":20.725953925752744,"motor_rpm":7875,"motor_temperature1":44.39921981547002,"motor_temperature2":41.8231005381975,"motor_temperature3":44.57421474542656,"motor_voltage":95.81537099435324},{"sequence":58,"timestamp":1698819736547,"motor_current":24.591344606923784,"motor_rpm":3433,"motor_temperature1":44.154618882196964,"motor_temperature2":43.818257486494645,"motor_temperature3":44.00322624214283,"motor_voltage":95.836075672381},{"sequence":59,"timestamp":1698819736797,"motor_current":23.45351558950044,"motor_rpm":2821,"motor_temperature1":40.08504018534067,"motor_temperature2":40.01762930080383,"motor_temperature3":44.58761928609693,"motor_voltage":95.20728667124892},{"sequence":60,"timestamp":1698819737047,"motor_current":24.80016201735869,"motor_rpm":6501,"motor_temperature1":42.644087680172056,"motor_temperature2":42.69599752816552,"motor_temperature3":42.507167485989456,"motor_voltage":95.18196715200837},{"sequence":61,"timestamp":1698819737297,"motor_current":24.184924253317636,"motor_rpm":6010,"motor_temperature1":43.54451853097524,"motor_temperature2":42.913247261787404,"motor_temperature3":43.283556383386944,"motor_voltage":95.27652363725953},{"sequence":62,"timestamp":1698819737547,"motor_current":22.574898675905718,"motor_rpm":7865,"motor_temperature1":40.514265543653096,"motor_temperature2":44.49774047848171,"motor_temperature3":40.47234524050056,"motor_voltage":95.84859580972021},{"sequence":63,"timestamp":1698819737796,"motor_current":22.67940095872316,"motor_rpm":5984,"motor_temperature1":40.51575551296916,"motor_temperature2":41.25425205979299,"motor_temperature3":43.07470169905349,"motor_voltage":95.44967765618588},{"sequence":64,"timestamp":1698819738047,"motor_current":21.407879881237857,"motor_rpm":3062,"motor_temperature1":40.544499732506395,"motor_temperature2":43.327355528933246,"motor_temperature3":41.3834272561343,"motor_voltage":95.17593732861795},{"sequence":65,"timestamp":1698819738297,"motor_current":24.05294148560295,"motor_rpm":7975,"motor_temperature1":41.45366616723117,"motor_temperature2":43.744038571098,"motor_temperature3":40.8147618778178,"motor_voltage":95.93668974212048},{"sequence":66,"timestamp":1698819738547,"motor_current":21.580286285198984,"motor_rpm":4265,"motor_temperature1":41.274349305947204,"motor_temperature2":42.05711664069568,"motor_temperature3":40.06414195888507,"motor_voltage":95.33535506764959},{"sequence":67,"timestamp":1698819738797,"motor_current":21.809870463464247,"motor_rpm":8525,"motor_temperature1":41.477159886179,"motor_temperature2":44.992978706993405,"motor_temperature3":44.424837138988785,"motor_voltage":95.71901401154696},{"sequence":68,"timestamp":1698819739047,"motor_current":20.00805148276321,"motor_rpm":5457,"motor_temperature1":41.836590611560034,"motor_temperature2":40.506326617673054,"motor_temperature3":40.53098998103335,"motor_voltage":95.24207936957711},{"sequence":69,"timestamp":1698819739298,"motor_current":21.641972745193094,"motor_rpm":2873,"motor_temperature1":44.53726831550789,"motor_temperature2":41.28707625178377,"motor_temperature3":44.235858510242586,"motor_voltage":95.96674632272266},{"sequence":70,"timestamp":1698819739547,"motor_current":23.048493150387074,"motor_rpm":4339,"motor_temperature1":43.67835735039996,"motor_temperature2":43.38734334603903,"motor_temperature3":42.31598121310711,"motor_voltage":95.31870946486207},{"sequence":71,"timestamp":1698819739797,"motor_current":22.300390318227436,"motor_rpm":4080,"motor_temperature1":42.28007166242273,"motor_temperature2":40.16675702594604,"motor_temperature3":44.348170240178995,"motor_voltage":95.78790276917964},{"sequence":72,"timestamp":1698819740047,"motor_current":21.277208407957374,"motor_rpm":3954,"motor_temperature1":43.80346938373378,"motor_temperature2":42.352322947690666,"motor_temperature3":42.64108490850222,"motor_voltage":95.83643537302305},{"sequence":73,"timestamp":1698819740297,"motor_current":24.822804000584853,"motor_rpm":4838,"motor_temperature1":43.685955182836764,"motor_temperature2":43.23694644259147,"motor_temperature3":42.3449243099131,"motor_voltage":95.0806456070598},{"sequence":74,"timestamp":1698819740547,"motor_current":23.26164889785165,"motor_rpm":3275,"motor_temperature1":42.42307038621921,"motor_temperature2":41.79123610990688,"motor_temperature3":41.064520298558364,"motor_voltage":95.87443064283629},{"sequence":75,"timestamp":1698819740797,"motor_current":21.934134318049388,"motor_rpm":6749,"motor_temperature1":44.3935695144962,"motor_temperature2":42.544221335281804,"motor_temperature3":42.04979356498809,"motor_voltage":95.8592635298883},{"sequence":76,"timestamp":1698819741047,"motor_current":20.377971442337007,"motor_rpm":2840,"motor_temperature1":42.85068712880654,"motor_temperature2":44.0976808960151,"motor_temperature3":44.89535624250789,"motor_voltage":95.44001979321686},{"sequence":77,"timestamp":1698819741297,"motor_current":20.764586442570256,"motor_rpm":1268,"motor_temperature1":42.97170205419601,"motor_temperature2":42.647652528314616,"motor_temperature3":44.01413702403202,"motor_voltage":95.83812032254707},{"sequence":78,"timestamp":1698819741546,"motor_current":21.96648038873126,"motor_rpm":1162,"motor_temperature1":42.35931921737604,"motor_temperature2":41.79178701305848,"motor_temperature3":44.07758978605372,"motor_voltage":95.22642822170152},{"sequence":79,"timestamp":1698819741797,"motor_current":23.064192549790214,"motor_rpm":8200,"motor_temperature1":44.954509564626704,"motor_temperature2":43.05440784268836,"motor_temperature3":40.8771723969747,"motor_voltage":95.04893104796244},{"sequence":80,"timestamp":1698819742047,"motor_current":20.068407135438388,"motor_rpm":6817,"motor_temperature1":42.85074867155622,"motor_temperature2":41.30838849779277,"motor_temperature3":43.94404063854559,"motor_voltage":95.72669178509086},{"sequence":81,"timestamp":1698819742296,"motor_current":24.418782783812276,"motor_rpm":1784,"motor_temperature1":40.58731613930674,"motor_temperature2":43.41572503056637,"motor_temperature3":44.30366802517615,"motor_voltage":95.53698757804389},{"sequence":82,"timestamp":1698819742547,"motor_current":22.9056348883873,"motor_rpm":2782,"motor_temperature1":42.56826793089228,"motor_temperature2":42.30102702220867,"motor_temperature3":42.313068305795994,"motor_voltage":95.05337667533561},{"sequence":83,"timestamp":1698819742797,"motor_current":20.653932586926025,"motor_rpm":2806,"motor_temperature1":43.25972725953882,"motor_temperature2":42.549632797898155,"motor_temperature3":41.04321560427282,"motor_voltage":95.02293558413523},{"sequence":84,"timestamp":1698819743047,"motor_current":20.108946460549568,"motor_rpm":1899,"motor_temperature1":42.830218933812,"motor_temperature2":44.900756043595486,"motor_temperature3":41.0484533929711,"motor_voltage":95.08448008095898},{"sequence":85,"timestamp":1698819743297,"motor_current":24.55625865030048,"motor_rpm":4397,"motor_temperature1":40.700187268249955,"motor_temperature2":43.07767384019777,"motor_temperature3":40.00296777202976,"motor_voltage":95.15583362954249},{"sequence":86,"timestamp":1698819743547,"motor_current":20.102578343722495,"motor_rpm":7232,"motor_temperature1":41.92363578679449,"motor_temperature2":40.751627777384975,"motor_temperature3":44.993747594545574,"motor_voltage":95.73445836896555},{"sequence":87,"timestamp":1698819743797,"motor_current":23.5629373552437,"motor_rpm":6033,"motor_temperature1":40.96100781099528,"motor_temperature2":44.59219901929666,"motor_temperature3":41.94769034577444,"motor_voltage":95.54909967196578},{"sequence":88,"timestamp":1698819744047,"motor_current":23.267646960835428,"motor_rpm":6813,"motor_temperature1":44.88030543231235,"motor_temperature2":40.09683346507891,"motor_temperature3":43.76204404885319,"motor_voltage":95.92397151851858},{"sequence":89,"timestamp":1698819744297,"motor_current":24.88963276978484,"motor_rpm":2270,"motor_temperature1":40.179613797882816,"motor_temperature2":41.96920465901191,"motor_temperature3":41.453673637508956,"motor_voltage":95.89168880122233},{"sequence":90,"timestamp":1698819744547,"motor_current":22.49414639067026,"motor_rpm":4693,"motor_temperature1":40.69610328608402,"motor_temperature2":41.49810143826739,"motor_temperature3":44.2300500277873,"motor_voltage":95.04915918059778},{"sequence":91,"timestamp":1698819744797,"motor_current":20.083331580124728,"motor_rpm":1311,"motor_temperature1":44.93204531972429,"motor_temperature2":42.81440525240211,"motor_temperature3":40.813638814380866,"motor_voltage":95.90686270275926},{"sequence":92,"timestamp":1698819745047,"motor_current":21.9210674348122,"motor_rpm":8759,"motor_temperature1":40.39279642471401,"motor_temperature2":43.56611342491006,"motor_temperature3":41.181216896696526,"motor_voltage":95.25367016013928},{"sequence":93,"timestamp":1698819745297,"motor_current":22.714531899399304,"motor_rpm":5217,"motor_temperature1":43.69955697667826,"motor_temperature2":43.686894149491565,"motor_temperature3":43.654388715536406,"motor_voltage":95.92676813576497},{"sequence":94,"timestamp":1698819745546,"motor_current":20.409394022154533,"motor_rpm":4945,"motor_temperature1":40.451861818501314,"motor_temperature2":40.873621884823336,"motor_temperature3":40.25452582358801,"motor_voltage":95.16263243354756},{"sequence":95,"timestamp":1698819745797,"motor_current":20.207527927565216,"motor_rpm":5314,"motor_temperature1":43.35591019323604,"motor_temperature2":42.15618421238858,"motor_temperature3":41.19840252573452,"motor_voltage":95.76859230877642},{"sequence":96,"timestamp":1698819746047,"motor_current":23.812557319182492,"motor_rpm":5683,"motor_temperature1":43.72197010840543,"motor_temperature2":40.81048246467889,"motor_temperature3":41.20737986028723,"motor_voltage":95.63436814147612},{"sequence":97,"timestamp":1698819746297,"motor_current":21.93946529324161,"motor_rpm":8695,"motor_temperature1":42.4860156350349,"motor_temperature2":40.14869751211305,"motor_temperature3":40.782202474891534,"motor_voltage":95.6119329231757},{"sequence":98,"timestamp":1698819746547,"motor_current":20.196664019094214,"motor_rpm":4267,"motor_temperature1":41.91891084831204,"motor_temperature2":41.63776534525485,"motor_temperature3":43.52651963382134,"motor_voltage":95.43454591516486},{"sequence":99,"timestamp":1698819746797,"motor_current":24.806610411326417,"motor_rpm":4434,"motor_temperature1":40.262142038834696,"motor_temperature2":43.70208112282974,"motor_temperature3":44.12609043406012,"motor_voltage":95.11580185823534},{"sequence":100,"timestamp":1698819747047,"motor_current":24.820746705287096,"motor_rpm":2145,"motor_temperature1":41.70783383773181,"motor_temperature2":40.274769399490545,"motor_temperature3":42.20919406413204,"motor_voltage":95.48722740672085}]

And these publishes inflight/persisted are resent to the EventLoop on uplink restart:

  2023-11-27T15:05:19.686112Z  INFO uplink::collector::downloader: Downloader thread is ready to receive download actions

  2023-11-27T15:05:19.686804Z  INFO storage: List of file ids loaded from disk: []

  2023-11-27T15:05:19.686817Z DEBUG uplink::base::serializer: Disk persistance is enabled for stream: "peripheral_state"; path: /var/tmp/persistence/1/peripheral_state

  2023-11-27T15:05:19.686857Z  INFO storage: List of file ids loaded from disk: [0]

  2023-11-27T15:05:19.686860Z DEBUG uplink::base::serializer: Disk persistance is enabled for stream: "bms"; path: /var/tmp/persistence/1/bms

  2023-11-27T15:05:19.686879Z  INFO storage: List of file ids loaded from disk: []

  2023-11-27T15:05:19.686881Z DEBUG uplink::base::serializer: Disk persistance is enabled for stream: "device_shadow"; path: /var/tmp/persistence/1/device_shadow

  2023-11-27T15:05:19.686903Z  INFO storage: List of file ids loaded from disk: []

  2023-11-27T15:05:19.686906Z DEBUG uplink::base::serializer: Disk persistance is enabled for stream: "gps"; path: /var/tmp/persistence/1/gps

  2023-11-27T15:05:19.686925Z  INFO storage: List of file ids loaded from disk: [0]

  2023-11-27T15:05:19.686927Z DEBUG uplink::base::serializer: Disk persistance is enabled for stream: "imu"; path: /var/tmp/persistence/1/imu

  2023-11-27T15:05:19.687092Z  INFO uplink::base::serializer: Read and published inflight packets; removing file: /var/tmp/persistence/1/inflight

@de-sh de-sh marked this pull request as ready for review November 1, 2023 06:16
qa-scripts/.env Outdated Show resolved Hide resolved
@de-sh
Copy link
Contributor Author

de-sh commented Nov 1, 2023

@Vilayat-Ali take a look at the changes(commit wise) and write the document of approach you took, the changes I made to the same and what you learnt from the process.

@de-sh de-sh requested a review from tekjar November 2, 2023 07:39
@de-sh de-sh changed the base branch from main to persist-eventloop November 16, 2023 05:46
@de-sh de-sh changed the title feat: persist inflight publishes onto disk to send on restart feat: persist inflight publishes and send on restart Nov 21, 2023
@de-sh de-sh changed the title feat: persist inflight publishes and send on restart feat: full graceful shutdown Dec 1, 2023
@de-sh de-sh changed the title feat: full graceful shutdown feat: persist inflight publishes and send on restart Dec 1, 2023
@de-sh de-sh force-pushed the vilayat/task/graceful-shutdown branch from 4b89737 to 5c85fd4 Compare December 1, 2023 15:26
Vilayat-Ali and others added 7 commits December 1, 2023 20:58
@de-sh de-sh marked this pull request as draft December 5, 2023 12:17
@de-sh
Copy link
Contributor Author

de-sh commented Dec 5, 2023

Let's consider this situation with regards to full-graceful shutdown:

  1. Uplink is running on device, shutdown triggered, inflight file created with 10 unacked(MAX_INFLIGHT_SIZE) and 10 in_channel(Capacity of channel between AsyncClient and EventLoop) publishes persisted into it.
  2. Uplink restarts, loads 10 publishes into channel(because of cap and considering EventLoop ain't handling requests), rest 10 publishes still left unhandled as network is down.
  3. Now if uplink were to shutdown again before getting network, would we want to lose the 10 packets that were not pushed onto channel or duplicate the 10 packets that were pushed into channel?

This is something that has been bugging me for a while looking at the code.

Illustrating the scenario, consider 10 unacked publishes are (A, B) and 10 in_channel publishes are (C, D), first shutdown of uplink writes them to inflight file as (A, B, C, D), on restart only first 10(i.e. A, B) publishes are loaded while rest 10(C, D) are not. The two possible ways to handle next shutdown would be:

  1. lose last 10 and be left with only first 10(i.e. on disk: A, B)
  2. Duplicate first 10(i.e. on disk: A, B, C, D, A, B)

NOTE: those 10 publishes in_channel after restart will now avalanche if we had uplink restarts continuously(A,B,C,D,A,B,A,B...), while only 10 packets will remain lost in the other scenario(A,B). The second path is what I have taken in the current implementation, but wanted to document this decision and ask for feedback.

@de-sh de-sh marked this pull request as ready for review December 5, 2023 15:39
@de-sh
Copy link
Contributor Author

de-sh commented Dec 10, 2023

Code to ease readability of control handles as requested by @tekjar is included in the PR #316 and this commit

@de-sh de-sh force-pushed the vilayat/task/graceful-shutdown branch from 7bd9d29 to 9a89646 Compare December 11, 2023 12:11
@de-sh de-sh force-pushed the vilayat/task/graceful-shutdown branch from 9a89646 to e27cb0e Compare December 12, 2023 08:21
@de-sh
Copy link
Contributor Author

de-sh commented Dec 12, 2023

Uplink always accepts packets read out of the inflight file, we don't have to fret about the situation illustrated in the previous comment.

proof:

 2023-12-12T08:29:18.860236Z  INFO storage: List of file ids loaded from disk: [1, 2, 3]

  2023-12-12T08:29:18.860262Z DEBUG uplink::base::serializer: Disk persistance is enabled for stream: "imu"; path: /var/tmp/persistence/imu

  2023-12-12T08:29:18.860416Z  INFO uplink::base::serializer: Switching to recovery mode!!

  2023-12-12T08:29:18.860504Z DEBUG uplink::base::serializer: publishing on /tenants/demo/devices/1001/events/device_shadow/jsonarray with size = 1208

  2023-12-12T08:29:18.860538Z  INFO uplink::base::serializer: Read and published inflight packets; removing file: /var/tmp/persistence/inflight

  2023-12-12T08:29:18.860598Z  INFO uplink::base::serializer: Switching to catchup mode!!

This is despite network never being back up.

@de-sh
Copy link
Contributor Author

de-sh commented Dec 31, 2023

I have opted to just load pending publishes back into EventLoop directly, in order to not write too much code and ensure ease of maintainability for the long term.

By making the channel between EventLoop and client a glienicke bridge we also ensure that number of pending packets don't grow. By this change we also ensure that packets don't get accepted when network is down.

Some of the other commits above also significantly improve upon the readability of relevant parts of the code and ensure we don't end up copying too much.

We are now dependent on bytebeamio/rumqtt#780 to ensure correct working of EventLoop::clean

@de-sh de-sh merged commit 7b7d4e4 into main Jan 4, 2024
2 checks passed
@de-sh de-sh deleted the vilayat/task/graceful-shutdown branch January 4, 2024 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants