[GitHub] [ignite-3] xtern commented on a diff in pull request #1511: IGNITE-18510: Make composite publisher thread safe.

2023-01-16 Thread GitBox


xtern commented on code in PR #1511:
URL: https://github.com/apache/ignite-3/pull/1511#discussion_r1071289825


##
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/ConcatPublisher.java:
##
@@ -0,0 +1,263 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine.util;
+
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodHandles.Lookup;
+import java.lang.invoke.VarHandle;
+import java.util.Iterator;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import java.util.concurrent.atomic.AtomicInteger;
+
+
+/**
+ * Thread-safe realization of combine multiple publishers. Generally, start 
consuming a source once the previous source has terminated,
+ * building a chain.
+ */
+public class ConcatPublisher implements Publisher {
+/** Iterator of upstream publishers. */
+private final Iterator> sources;
+
+/**
+ * Constructor.
+ *
+ * @param sources Iterator of upstream publishers.
+ */
+public ConcatPublisher(Iterator> sources) {
+this.sources = sources;
+}
+
+/** {@inheritDoc} */
+@Override
+public void subscribe(Subscriber downstream) {
+ConcatCoordinator subscription = new 
ConcatCoordinator<>(downstream, sources);
+
+downstream.onSubscribe(subscription);
+subscription.drain();
+}
+
+/**
+ * Concatenation composite subscription.
+ */
+static final class ConcatCoordinator extends SubscriptionArbiter 
implements Subscriber {
+/** Counter to prevent concurrent execution of a critical section. */
+private final AtomicInteger guardCntr = new AtomicInteger();
+
+final Subscriber downstream;
+
+final Iterator> sources;
+
+long consumed;
+
+ConcatCoordinator(
+Subscriber downstream,
+Iterator> sources
+) {
+this.downstream = downstream;
+this.sources = sources;
+}
+
+@Override
+public void onSubscribe(Subscription s) {
+setSubscription(s);
+}
+
+@Override
+public void onNext(T item) {
+consumed++;
+downstream.onNext(item);
+}
+
+@Override
+public void onError(Throwable throwable) {
+downstream.onError(throwable);
+}
+
+@Override
+public void onComplete() {
+drain();
+}
+
+void drain() {
+if (guardCntr.getAndIncrement() == 0) {
+do {
+if (isCancelled()) {
+return;
+}
+
+if (!sources.hasNext()) {
+downstream.onComplete();
+return;
+}
+
+long c = consumed;
+if (c != 0L) {
+consumed = 0L;
+setProduced(c);
+}
+
+sources.next().subscribe(this);
+
+} while ((guardCntr.getAndDecrement() - 1) != 0);
+}
+}
+}
+
+static class SubscriptionArbiter implements Subscription {
+Subscription current;
+static final VarHandle CURRENT;
+
+Subscription next;
+static final VarHandle NEXT;
+
+long requested;
+
+long downstreamRequested;
+static final VarHandle DOWNSTREAM_REQUESTED;
+
+long produced;
+static final VarHandle PRODUCED;
+
+int wip;
+static final VarHandle WIP;

Review Comment:
   We decided to keep the code as close as possible to the original source 
https://akarnokd.blogspot.com/2017/09/java-9-flow-api-arbitration-and.html



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [ignite-3] xtern commented on a diff in pull request #1511: IGNITE-18510: Make composite publisher thread safe.

2023-01-16 Thread GitBox


xtern commented on code in PR #1511:
URL: https://github.com/apache/ignite-3/pull/1511#discussion_r1071287538


##
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/OrderedMergePublisher.java:
##
@@ -36,49 +35,52 @@
  *
  * Merges multiple concurrent ordered data streams into one.
  */
-public class SortingCompositePublisher extends CompositePublisher {
+public class OrderedMergePublisher implements Publisher {

Review Comment:
   Javadoc updated:
   
   ```
   /**
* Sorting composite publisher.
*
* Merges multiple publishers using merge-sort algorithm.
*
* Note: upstream publishers must be sources of sorted data.
*/
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [ignite-3] xtern commented on a diff in pull request #1511: IGNITE-18510: Make composite publisher thread safe.

2023-01-16 Thread GitBox


xtern commented on code in PR #1511:
URL: https://github.com/apache/ignite-3/pull/1511#discussion_r1071286076


##
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/OrderedMergePublisher.java:
##
@@ -36,49 +35,52 @@
  *
  * Merges multiple concurrent ordered data streams into one.
  */
-public class SortingCompositePublisher extends CompositePublisher {
+public class OrderedMergePublisher implements Publisher {
 /** Rows comparator. */
-private final Comparator comp;
+private final Comparator comp;
+
+/** Array of upstream publishers. */
+private final Publisher[] sources;
 
 /** Prefetch size. */
 private final int prefetch;
 
 /**
  * Constructor.
  *
- * @param publishers List of upstream publishers.
  * @param comp Rows comparator.
  * @param prefetch Prefetch size.
+ * @param sources List of upstream publishers.
  */
-public SortingCompositePublisher(Collection> 
publishers, Comparator comp, int prefetch) {
-super(publishers);
-
-this.comp = comp;
+public OrderedMergePublisher(
+Comparator comp,
+int prefetch,
+Publisher... sources) {
+this.sources = sources;
 this.prefetch = prefetch;
+this.comp = comp;
 }
 
+/** {@inheritDoc} */
 @Override
 public void subscribe(Subscriber downstream) {
-subscribe(new OrderedMergeCompositeSubscription<>(downstream, comp, 
prefetch, publishers.size()), downstream);
+OrderedMergeSubscription subscription = new 
OrderedMergeSubscription<>(downstream, comp, prefetch, sources.length);
+
+subscription.subscribe(sources);
+downstream.onSubscribe(subscription);
+subscription.drain();
 }
 
 /**
  * Sorting composite subscription.
  *
  * Merges multiple concurrent ordered data streams into one.
  */
-public static class OrderedMergeCompositeSubscription extends 
CompositePublisher.CompositeSubscription {
-/** Marker to indicate completed subscription. */
+static final class OrderedMergeSubscription implements Subscription {

Review Comment:
   It seems to me that the current names `OrderedMergePublisher` and 
`OrderedMergeSubscription` better reflect the purpose of these classes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [ignite-3] xtern commented on a diff in pull request #1511: IGNITE-18510: Make composite publisher thread safe.

2023-01-16 Thread GitBox


xtern commented on code in PR #1511:
URL: https://github.com/apache/ignite-3/pull/1511#discussion_r1071261364


##
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/ConcatPublisher.java:
##
@@ -0,0 +1,263 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine.util;
+
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodHandles.Lookup;
+import java.lang.invoke.VarHandle;
+import java.util.Iterator;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import java.util.concurrent.atomic.AtomicInteger;
+
+
+/**
+ * Thread-safe realization of combine multiple publishers. Generally, start 
consuming a source once the previous source has terminated,
+ * building a chain.
+ */
+public class ConcatPublisher implements Publisher {
+/** Iterator of upstream publishers. */
+private final Iterator> sources;
+
+/**
+ * Constructor.
+ *
+ * @param sources Iterator of upstream publishers.
+ */
+public ConcatPublisher(Iterator> sources) {
+this.sources = sources;
+}
+
+/** {@inheritDoc} */
+@Override
+public void subscribe(Subscriber downstream) {
+ConcatCoordinator subscription = new 
ConcatCoordinator<>(downstream, sources);
+
+downstream.onSubscribe(subscription);
+subscription.drain();
+}
+
+/**
+ * Concatenation composite subscription.
+ */
+static final class ConcatCoordinator extends SubscriptionArbiter 
implements Subscriber {

Review Comment:
   Renamed to `ConcatenatedSubscriber`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [ignite-3] xtern commented on a diff in pull request #1511: IGNITE-18510: Make composite publisher thread safe.

2023-01-16 Thread GitBox


xtern commented on code in PR #1511:
URL: https://github.com/apache/ignite-3/pull/1511#discussion_r1071260952


##
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/ConcatPublisher.java:
##
@@ -0,0 +1,263 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine.util;
+
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodHandles.Lookup;
+import java.lang.invoke.VarHandle;
+import java.util.Iterator;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import java.util.concurrent.atomic.AtomicInteger;
+
+
+/**
+ * Thread-safe realization of combine multiple publishers. Generally, start 
consuming a source once the previous source has terminated,
+ * building a chain.
+ */
+public class ConcatPublisher implements Publisher {
+/** Iterator of upstream publishers. */
+private final Iterator> sources;
+
+/**
+ * Constructor.
+ *
+ * @param sources Iterator of upstream publishers.
+ */
+public ConcatPublisher(Iterator> sources) {
+this.sources = sources;
+}
+
+/** {@inheritDoc} */
+@Override
+public void subscribe(Subscriber downstream) {
+ConcatCoordinator subscription = new 
ConcatCoordinator<>(downstream, sources);
+
+downstream.onSubscribe(subscription);
+subscription.drain();
+}
+
+/**
+ * Concatenation composite subscription.
+ */
+static final class ConcatCoordinator extends SubscriptionArbiter 
implements Subscriber {
+/** Counter to prevent concurrent execution of a critical section. */
+private final AtomicInteger guardCntr = new AtomicInteger();
+
+final Subscriber downstream;
+
+final Iterator> sources;
+
+long consumed;
+
+ConcatCoordinator(
+Subscriber downstream,
+Iterator> sources
+) {
+this.downstream = downstream;
+this.sources = sources;
+}
+
+@Override

Review Comment:
   Done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [ignite-3] xtern commented on a diff in pull request #1511: IGNITE-18510: Make composite publisher thread safe.

2023-01-16 Thread GitBox


xtern commented on code in PR #1511:
URL: https://github.com/apache/ignite-3/pull/1511#discussion_r1071259733


##
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/OrderedMergePublisher.java:
##
@@ -107,14 +109,20 @@ public void subscribe(Subscriber downstream) {
 /** Number of emitted rows (guarded by {@link #guardCntr}). */
 private long emitted;
 
+static final VarHandle ERROR;
+
+static final VarHandle CANCELLED;
+
+static final VarHandle REQUESTED;

Review Comment:
   Done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [ignite-3] xtern commented on a diff in pull request #1511: IGNITE-18510: Make composite publisher thread safe.

2023-01-16 Thread GitBox


xtern commented on code in PR #1511:
URL: https://github.com/apache/ignite-3/pull/1511#discussion_r1071259421


##
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/ConcatPublisher.java:
##
@@ -0,0 +1,263 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine.util;
+
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodHandles.Lookup;
+import java.lang.invoke.VarHandle;
+import java.util.Iterator;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import java.util.concurrent.atomic.AtomicInteger;
+
+
+/**
+ * Thread-safe realization of combine multiple publishers. Generally, start 
consuming a source once the previous source has terminated,
+ * building a chain.
+ */
+public class ConcatPublisher implements Publisher {

Review Comment:
   Done



##
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/ConcatPublisher.java:
##
@@ -0,0 +1,263 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine.util;
+
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodHandles.Lookup;
+import java.lang.invoke.VarHandle;
+import java.util.Iterator;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import java.util.concurrent.atomic.AtomicInteger;
+
+
+/**
+ * Thread-safe realization of combine multiple publishers. Generally, start 
consuming a source once the previous source has terminated,
+ * building a chain.
+ */
+public class ConcatPublisher implements Publisher {
+/** Iterator of upstream publishers. */
+private final Iterator> sources;
+
+/**
+ * Constructor.
+ *
+ * @param sources Iterator of upstream publishers.
+ */
+public ConcatPublisher(Iterator> sources) {
+this.sources = sources;
+}
+
+/** {@inheritDoc} */
+@Override
+public void subscribe(Subscriber downstream) {
+ConcatCoordinator subscription = new 
ConcatCoordinator<>(downstream, sources);
+
+downstream.onSubscribe(subscription);
+subscription.drain();
+}
+
+/**
+ * Concatenation composite subscription.
+ */
+static final class ConcatCoordinator extends SubscriptionArbiter 
implements Subscriber {
+/** Counter to prevent concurrent execution of a critical section. */
+private final AtomicInteger guardCntr = new AtomicInteger();
+
+final Subscriber downstream;
+
+final Iterator> sources;
+
+long consumed;
+
+ConcatCoordinator(
+Subscriber downstream,
+Iterator> sources
+) {
+this.downstream = downstream;
+this.sources = sources;
+}
+
+@Override
+public void onSubscribe(Subscription s) {
+setSubscription(s);
+}
+
+@Override
+public void onNext(T item) {
+consumed++;
+downstream.onNext(item);
+}
+
+@Override
+public void onError(Throwable throwable) {
+downstream.onError(throwable);
+}
+
+@Override
+public void onComplete() {
+drain();
+}
+
+void drain() {
+if (guardCntr.getAndIn

[GitHub] [ignite-3] xtern commented on a diff in pull request #1511: IGNITE-18510: Make composite publisher thread safe.

2023-01-12 Thread GitBox


xtern commented on code in PR #1511:
URL: https://github.com/apache/ignite-3/pull/1511#discussion_r1068092636


##
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/ConcatPublisher.java:
##
@@ -0,0 +1,263 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine.util;
+
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodHandles.Lookup;
+import java.lang.invoke.VarHandle;
+import java.util.Iterator;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import java.util.concurrent.atomic.AtomicInteger;
+
+
+/**
+ * Thread-safe realization of combine multiple publishers. Generally, start 
consuming a source once the previous source has terminated,
+ * building a chain.
+ */
+public class ConcatPublisher implements Publisher {
+/** Iterator of upstream publishers. */
+private final Iterator> sources;
+
+/**
+ * Constructor.
+ *
+ * @param sources Iterator of upstream publishers.
+ */
+public ConcatPublisher(Iterator> sources) {
+this.sources = sources;
+}
+
+/** {@inheritDoc} */
+@Override
+public void subscribe(Subscriber downstream) {
+ConcatCoordinator subscription = new 
ConcatCoordinator<>(downstream, sources);
+
+downstream.onSubscribe(subscription);
+subscription.drain();
+}
+
+/**
+ * Concatenation composite subscription.
+ */
+static final class ConcatCoordinator extends SubscriptionArbiter 
implements Subscriber {
+/** Counter to prevent concurrent execution of a critical section. */
+private final AtomicInteger guardCntr = new AtomicInteger();
+
+final Subscriber downstream;
+
+final Iterator> sources;
+
+long consumed;
+
+ConcatCoordinator(
+Subscriber downstream,
+Iterator> sources
+) {
+this.downstream = downstream;
+this.sources = sources;
+}
+
+@Override
+public void onSubscribe(Subscription s) {
+setSubscription(s);
+}
+
+@Override
+public void onNext(T item) {
+consumed++;
+downstream.onNext(item);
+}
+
+@Override
+public void onError(Throwable throwable) {
+downstream.onError(throwable);
+}
+
+@Override
+public void onComplete() {
+drain();
+}
+
+void drain() {
+if (guardCntr.getAndIncrement() == 0) {
+do {
+if (isCancelled()) {
+return;
+}
+
+if (!sources.hasNext()) {
+downstream.onComplete();
+return;
+}
+
+long c = consumed;
+if (c != 0L) {
+consumed = 0L;
+setProduced(c);
+}
+
+sources.next().subscribe(this);
+
+} while ((guardCntr.getAndDecrement() - 1) != 0);
+}
+}
+}
+
+static class SubscriptionArbiter implements Subscription {

Review Comment:
   We currently have only one class that inherits from `SubscriptionArbiter` - 
`ConcatCoordinator`.
   It seems we should just combine `ConcatCoordinator` with 
`SubscriptionArbiter`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org