tinco / nobrainer_streams

A temporary gem that implements streams in NoBrainer, hopefully it gets merged soon and this gem will be obsolete.
MIT License
8 stars 4 forks source link

Ordering issue with include_initial:true #5

Closed seanriordan08 closed 6 years ago

seanriordan08 commented 6 years ago

I hope i'm just doing it wrong -

I have a simple rails app that receives data from circleci for the use of custom CI metric reporting. I can't seem to get the initial data sent in-order though. I've indexed my table, and the rql looks right. I've ensured the database has been sync'd and is up-to-date with the new index.

class CiBuildsChannel < ApplicationCable::Channel
  include NoBrainer::Streams

  def subscribed
    nobrainer_stream_from CiBuild.order_by("build_num"), include_initial: true
  end

end

When I run r.table("ci_builds").order_by({"index" => r.asc(:build_num)}) in the rethinkdb repl, it spits out my data perfectly in order. e.g

CiBuildsChannel transmitting {"new_val"=>#<CiBuild id: "c3af2368-c5ac-4276-83f7-e48db28f4642", branch: "master", build_num: 26, build_time_millis: 10002>} (via streamed from r.table("ci_builds").order_by({"index" => r.asc(:build_num)}))
CiBuildsChannel transmitting {"new_val"=>#<CiBuild id: "9114b874-00b1-42f3-8cb7-12f37acafe29", branch: "master", build_num: 27, build_time_millis: 20002>} (via streamed from r.table("ci_builds").order_by({"index" => r.asc(:build_num)}))
CiBuildsChannel transmitting {"new_val"=>#<CiBuild id: "d57b37c7-6ce0-4502-ada0-55c914bf0294", branch: "master", build_num: 28, build_time_millis: 30002>} (via streamed from r.table("ci_builds").order_by({"index" => r.asc(:build_num)}))

But when I run the application and view the logs, the data is transmitted out of order!

...build_num: 26, build_time_millis: 10002...
...build_num: 28, build_time_millis: 10002...
...build_num: 27, build_time_millis: 10002...

At this point, what am I missing?

Thank you!

tinco commented 6 years ago

Hi Sean, thanks for submitting an issue. Have you tried running the query from IRB? Is it the initial results it sends out of order?

seanriordan08 commented 6 years ago

Yes, only the initial results are out of order. They are ordered correctly however, when running in IRB.

irb
require 'rethinkdb'
include RethinkDB::Shortcuts
conn = r.connect(:host=>"localhost", :port=>28015, :db=>"spectrics_development").repl
cursor = r.table("ci_builds").order_by('build_num').run(conn)

^ this works great - transmits in expected order

In my app though, i'm watching the logs when refreshing the page, and the records streamed via nobrainer_stream_from CiBuild.order_by("build_num"), include_initial: true are initially out of order.

Thanks for looking into this!

seanriordan08 commented 6 years ago

Solved - Not a Bug

Digging deeper into the docs, I was able to get initial streamed results in-order by using a limit combined with the include_offsets option.

What I had (Wrong)

def subscribed
  nobrainer_stream_from CiBuild.order_by(:build_num), include_initial: true
end

Now (Correct)

def subscribed
  nobrainer_stream_from CiBuild.order_by(:build_num).limit(20), include_initial: true, include_offsets: true
end

Ref: https://www.rethinkdb.com/api/ruby/changes/

tinco commented 6 years ago

Thanks for letting me know!

seanriordan08 commented 6 years ago

I'm using this for an app that adds extra layers of insight to my circleci builds. Right now, i'm just tracking the runtime.

image