RxJava icon indicating copy to clipboard operation
RxJava copied to clipboard

groupBy().toSortedList() does not complete on android, does on JVM [1.x]

Open danhawkes opened this issue 9 years ago • 6 comments

Rxjava version: 1.3.0

I'm seeing some strange behaviour where groupBy().toSortedList() seems to not complete on Android, whereas it does on the JVM.

Minimal test case here.

I have a collection of Tasks, each with a priority and sequence number:

public interface Task {
    public int getPriority();
    public long getSequenceNumber();
}

public static Observable<Task> getTasks() {
     List<Task> tasks = new ArrayList<>();
     tasks.add(new Task(1, 1));
     tasks.add(new Task(2, 1));
     tasks.add(new Task(2, 2));
     tasks.add(new Task(2, 3));
     tasks.add(new Task(2, 4));
     tasks.add(new Task(2, 5));
     tasks.add(new Task(1, 6));
     tasks.add(new Task(2, 0));
     tasks.add(new Task(2, 7));
     tasks.add(new Task(1, 8));
     tasks.add(new Task(1, 9));
     tasks.add(new Task(1, 10));
     tasks.add(new Task(1, 11));
     tasks.add(new Task(2, 12));
     tasks.add(new Task(1, 0));
     tasks.add(new Task(1, 14));
     tasks.add(new Task(1, 15));
     tasks.add(new Task(1, 16)); // More tasks than this fail.
     tasks.add(new Task(1, 17));
     tasks.add(new Task(1, 0));
     tasks.add(new Task(1, 0));
     tasks.add(new Task(1, 0));
     tasks.add(new Task(1, 0));
     tasks.add(new Task(1, 0));
     tasks.add(new Task(1, 0));
     tasks.add(new Task(1, 0));
     tasks.add(new Task(1, 0));
     tasks.add(new Task(1, 0));
     tasks.add(new Task(2, 0));
     return Observable.from(tasks);
 }

…and a function that groups them by priority, then sequence number, then does something to each batch:

public Observable<Void> executeTasks(Observable<Task> tasks) {

       return tasks.groupBy(Task::getPriority)
               .compose(log("group1"))
               .toSortedList((a1, b1) -> Integer.compare(a1.getKey(), b1.getKey()), 1000)
               .compose(log("sorted1"))
               .flatMap(o1 -> Observable.from(o1).concatMap(priorityGroup -> {

                   return priorityGroup.groupBy(Task::getSequenceNumber)
                           .compose(log("group2"))
                           .toSortedList((a2, b2) -> Long.compare(a2.getKey(), b2.getKey()), 1000)
                           .compose(log("sorted2"))
                           .flatMap(o2 -> Observable.from(o2).concatMap(sequenceNumberGroup -> {

                               return sequenceNumberGroup.compose(log("task"))
                                       .ignoreElements()
                                       .cast(Void.class);
                           }));
               }));
   }

On the JVM this is the expected output:

sorted1 onSubscribe
group1 onSubscribe
group1 [rx.Notification@8fec1fba OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@4ec6a292]
group1 [rx.Notification@5c665318 OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@1b40d5f0]
group1 [rx.Notification@ea4a92b OnCompleted]
sorted1 [rx.Notification@e6740687 OnNext [rx.internal.operators.OperatorGroupBy$GroupedUnicast@4ec6a292, rx.internal.operators.OperatorGroupBy$GroupedUnicast@1b40d5f0]]
sorted2 onSubscribe
group2 onSubscribe
group2 [rx.Notification@6878740a OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@2752f6e2]
group2 [rx.Notification@4f7d8651 OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@e580929]
group2 [rx.Notification@5df5efd1 OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@1cd072a9]
group2 [rx.Notification@bd9a9f53 OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@7c75222b]
group2 [rx.Notification@8d45bbc9 OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@4c203ea1]
group2 [rx.Notification@43a4e475 OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@27f674d]
group2 [rx.Notification@5e4a95b9 OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@1d251891]
group2 [rx.Notification@8939828c OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@48140564]
group2 [rx.Notification@46b26d19 OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@58ceff1]
group2 [rx.Notification@bd56222a OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@7c30a502]
group2 [rx.Notification@8b0a48ad OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@49e4cb85]
group2 [rx.Notification@ea4a92b OnCompleted]
sorted2 [rx.Notification@8741f9dd OnNext [rx.internal.operators.OperatorGroupBy$GroupedUnicast@1d251891, rx.internal.operators.OperatorGroupBy$GroupedUnicast@2752f6e2, rx.internal.operators.OperatorGroupBy$GroupedUnicast@e580929, rx.internal.operators.OperatorGroupBy$GroupedUnicast@1cd072a9, rx.internal.operators.OperatorGroupBy$GroupedUnicast@7c75222b, rx.internal.operators.OperatorGroupBy$GroupedUnicast@4c203ea1, rx.internal.operators.OperatorGroupBy$GroupedUnicast@27f674d, rx.internal.operators.OperatorGroupBy$GroupedUnicast@48140564, rx.internal.operators.OperatorGroupBy$GroupedUnicast@58ceff1, rx.internal.operators.OperatorGroupBy$GroupedUnicast@7c30a502, rx.internal.operators.OperatorGroupBy$GroupedUnicast@49e4cb85]]
task onSubscribe
task [rx.Notification@80607ba0 OnNext Task{1, 0}]
task [rx.Notification@c088bf82 OnNext Task{1, 0}]
task [rx.Notification@77fbc06a OnNext Task{1, 0}]
task [rx.Notification@7adfd73c OnNext Task{1, 0}]
task [rx.Notification@9241278d OnNext Task{1, 0}]
task [rx.Notification@7534c0b6 OnNext Task{1, 0}]
task [rx.Notification@71ed5746 OnNext Task{1, 0}]
task [rx.Notification@9c6bca10 OnNext Task{1, 0}]
task [rx.Notification@98a81a8f OnNext Task{1, 0}]
task [rx.Notification@5b053452 OnNext Task{1, 0}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@7e49f262 OnNext Task{1, 1}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@9acc607b OnNext Task{1, 6}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@bb30440b OnNext Task{1, 8}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@b2e4161d OnNext Task{1, 9}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@b0d32b85 OnNext Task{1, 10}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@42a4e1a8 OnNext Task{1, 11}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@6e9404ba OnNext Task{1, 14}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@69384922 OnNext Task{1, 15}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@6bf4d50b OnNext Task{1, 16}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@9193ea86 OnNext Task{1, 17}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
sorted2 [rx.Notification@ea4a92b OnCompleted]
sorted2 onUnsubscribe
group2 onUnsubscribe
sorted2 onSubscribe
group2 onSubscribe
group2 [rx.Notification@a8dbc96d OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@67b64c45]
group2 [rx.Notification@85375698 OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@4411d970]
group2 [rx.Notification@a5682dce OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@6442b0a6]
group2 [rx.Notification@a21dacc0 OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@60f82f98]
group2 [rx.Notification@771f00ce OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@35f983a6]
group2 [rx.Notification@c08e8358 OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@7f690630]
group2 [rx.Notification@5004cc23 OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@edf4efb]
group2 [rx.Notification@709fa17f OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@2f7a2457]
group2 [rx.Notification@ea4a92b OnCompleted]
sorted2 [rx.Notification@63b2befe OnNext [rx.internal.operators.OperatorGroupBy$GroupedUnicast@7f690630, rx.internal.operators.OperatorGroupBy$GroupedUnicast@67b64c45, rx.internal.operators.OperatorGroupBy$GroupedUnicast@4411d970, rx.internal.operators.OperatorGroupBy$GroupedUnicast@6442b0a6, rx.internal.operators.OperatorGroupBy$GroupedUnicast@60f82f98, rx.internal.operators.OperatorGroupBy$GroupedUnicast@35f983a6, rx.internal.operators.OperatorGroupBy$GroupedUnicast@edf4efb, rx.internal.operators.OperatorGroupBy$GroupedUnicast@2f7a2457]]
task onSubscribe
task [rx.Notification@a22e2fff OnNext Task{2, 0}]
task [rx.Notification@567a0dc3 OnNext Task{2, 0}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@ad17d422 OnNext Task{2, 1}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@47f3045f OnNext Task{2, 2}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@641c905b OnNext Task{2, 3}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@54bc1ce6 OnNext Task{2, 4}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@abcfdbd8 OnNext Task{2, 5}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@415a1615 OnNext Task{2, 7}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
task onSubscribe
task [rx.Notification@5b65fa7b OnNext Task{2, 12}]
task [rx.Notification@ea4a92b OnCompleted]
task onUnsubscribe
sorted2 [rx.Notification@ea4a92b OnCompleted]
sorted2 onUnsubscribe
group2 onUnsubscribe
sorted1 [rx.Notification@ea4a92b OnCompleted]
sorted1 onUnsubscribe
group1 onUnsubscribe

Whereas on Android this happens:

sorted1 onSubscribe
group1 onSubscribe
group1 [rx.Notification@4b7ce429 OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@865187d]
group1 [rx.Notification@51477f1e OnNext rx.internal.operators.OperatorGroupBy$GroupedUnicast@e2fb372]

Mysteriously, the number of tasks has an effect: if I reduce it to 18 or fewer, it works as expected.

Any thoughts as to what's going on?

danhawkes avatar May 16 '17 11:05 danhawkes

On Android, the default internal buffer size and prefetch is 16 which could be why you experience that hang. In general, you should consume the groups of groupBy because consuming an item in a group asks for the next element from the upstream (that can go into any other group btw).

akarnokd avatar May 16 '17 11:05 akarnokd

Thanks for the response. I did read the docs regarding consuming the emitted groups, but AFAIK I am consuming the groups in the above example. Could you please point out what I'm missing?

danhawkes avatar May 16 '17 12:05 danhawkes

.toSortedList wants to collect all groups and requires the groupBy to complete on its main stream before sorting the unconsumed groups via keys. On desktop, there are few enough elements that all internal buffers can be filled and completed so the sorting happens. You then consume these finished, "cached" and now passive groups.

akarnokd avatar May 16 '17 12:05 akarnokd

default internal buffer size and prefetch is 16

Right, thanks. Have reproduced the issue on the JVM by adding > 128 tasks.

You then consume these finished, "cached" and now passive groups.

If I've understood correctly then, I need to request all items from the grouped observables with some sort of greedy operator such as toList()?

I've ended up with something like this:

groupBy().flatMap(o -> o.toList().map(list -> GroupedObservable.from(o.getKey(), Observable.from(list))))

…which works, but isn't exactly nice. Is there a better way?

danhawkes avatar May 22 '17 13:05 danhawkes

Creating another GroupedObservable has little value as flatMap ignores anything but it being Observable. Maybe you can create a tuple of the key and the list and sort via that.

akarnokd avatar Jun 16 '17 09:06 akarnokd

I think it's still required because I'm nesting these operations and want the type of priorityGroup and sequenceNumberGroup to be Observable<GroupedObservable<T>>. Anyway, thanks for the help, will close this now.

danhawkes avatar Jun 16 '17 10:06 danhawkes