Search in sources :

Example 1 with Gauge

use of io.mantisrx.common.metrics.Gauge in project mantis by Netflix.

the class ObservableTrigger method groupTrigger.

private static <K, V> PushTrigger<KeyValuePair<K, V>> groupTrigger(final String name, final Observable<GroupedObservable<K, V>> o, final Action0 doOnComplete, final Action1<Throwable> doOnError, final long groupExpirySeconds, final Func1<K, byte[]> keyEncoder, final HashFunction hashFunction) {
    final AtomicReference<Subscription> subRef = new AtomicReference<>();
    final Gauge subscriptionActive;
    Metrics metrics = new Metrics.Builder().name("ObservableTrigger_" + name).addGauge("subscriptionActive").build();
    subscriptionActive = metrics.getGauge("subscriptionActive");
    Action1<MonitoredQueue<KeyValuePair<K, V>>> doOnStart = new Action1<MonitoredQueue<KeyValuePair<K, V>>>() {

        @Override
        public void call(final MonitoredQueue<KeyValuePair<K, V>> queue) {
            subRef.set(o.observeOn(Schedulers.computation()).doOnSubscribe(() -> {
                logger.info("Subscription is ACTIVE for observable trigger with name: " + name);
                subscriptionActive.set(1);
            }).doOnUnsubscribe(() -> {
                logger.info("Subscription is INACTIVE for observable trigger with name: " + name);
                subscriptionActive.set(0);
            }).flatMap((final GroupedObservable<K, V> group) -> {
                final byte[] keyBytes = keyEncoder.call(group.getKey());
                final long keyBytesHashed = hashFunction.computeHash(keyBytes);
                return group.timeout(groupExpirySeconds, TimeUnit.SECONDS, (Observable<? extends V>) Observable.empty()).lift(new DisableBackPressureOperator<V>()).buffer(250, TimeUnit.MILLISECONDS).filter((List<V> t1) -> t1 != null && !t1.isEmpty()).map((List<V> list) -> {
                    List<KeyValuePair<K, V>> keyPairList = new ArrayList<>(list.size());
                    for (V data : list) {
                        keyPairList.add(new KeyValuePair<K, V>(keyBytesHashed, keyBytes, data));
                    }
                    return keyPairList;
                });
            }).subscribe((List<KeyValuePair<K, V>> list) -> {
                for (KeyValuePair<K, V> data : list) {
                    queue.write(data);
                }
            }, (Throwable e) -> {
                logger.warn("Observable used to push data errored, on server with name: " + name, e);
                if (doOnError != null) {
                    doOnError.call(e);
                }
            }, () -> {
                logger.info("Observable used to push data completed, on server with name: " + name);
                if (doOnComplete != null) {
                    doOnComplete.call();
                }
            }));
        }
    };
    Action1<MonitoredQueue<KeyValuePair<K, V>>> doOnStop = new Action1<MonitoredQueue<KeyValuePair<K, V>>>() {

        @Override
        public void call(MonitoredQueue<KeyValuePair<K, V>> t1) {
            if (subRef.get() != null) {
                logger.warn("Connections from next stage has dropped to 0. Do not propagate unsubscribe");
            // subRef.get().unsubscribe();
            }
        }
    };
    return new PushTrigger<>(doOnStart, doOnStop, metrics);
}
Also used : DisableBackPressureOperator(io.reactivx.mantis.operators.DisableBackPressureOperator) Action1(rx.functions.Action1) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Gauge(io.mantisrx.common.metrics.Gauge) Metrics(io.mantisrx.common.metrics.Metrics) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(rx.Subscription) GroupedObservable(rx.observables.GroupedObservable)

Example 2 with Gauge

use of io.mantisrx.common.metrics.Gauge in project mantis by Netflix.

the class ObservableTrigger method ssetrigger.

private static <T> PushTrigger<T> ssetrigger(final String name, final Observable<T> o, final Action0 doOnComplete, final Action1<Throwable> doOnError) {
    final AtomicReference<Subscription> subRef = new AtomicReference<>();
    final Gauge subscriptionActive;
    Metrics metrics = new Metrics.Builder().name("ObservableTrigger_" + name).addGauge("subscriptionActive").build();
    subscriptionActive = metrics.getGauge("subscriptionActive");
    Action1<MonitoredQueue<T>> doOnStart = new Action1<MonitoredQueue<T>>() {

        @Override
        public void call(final MonitoredQueue<T> queue) {
            subRef.set(o.filter((T t1) -> t1 != null).doOnSubscribe(() -> {
                logger.info("Subscription is ACTIVE for observable trigger with name: " + name);
                subscriptionActive.set(1);
            }).doOnUnsubscribe(() -> {
                logger.info("Subscription is INACTIVE for observable trigger with name: " + name);
                subscriptionActive.set(0);
            }).subscribe((T data) -> queue.write(data), (Throwable e) -> {
                logger.warn("Observable used to push data errored, on server with name: " + name, e);
                if (doOnError != null) {
                    doOnError.call(e);
                }
            }, () -> {
                logger.info("Observable used to push data completed, on server with name: " + name);
                if (doOnComplete != null) {
                    doOnComplete.call();
                }
            }));
        }
    };
    Action1<MonitoredQueue<T>> doOnStop = new Action1<MonitoredQueue<T>>() {

        @Override
        public void call(MonitoredQueue<T> t1) {
            if (subRef.get() != null) {
                logger.warn("Connections from next stage has dropped to 0 for SSE stage. propagate unsubscribe");
                subRef.get().unsubscribe();
            }
        }
    };
    return new PushTrigger<>(doOnStart, doOnStop, metrics);
}
Also used : Metrics(io.mantisrx.common.metrics.Metrics) Action1(rx.functions.Action1) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(rx.Subscription) Gauge(io.mantisrx.common.metrics.Gauge)

Example 3 with Gauge

use of io.mantisrx.common.metrics.Gauge in project mantis by Netflix.

the class ObservableTrigger method trigger.

private static <T> PushTrigger<T> trigger(final String name, final Observable<T> o, final Action0 doOnComplete, final Action1<Throwable> doOnError) {
    final AtomicReference<Subscription> subRef = new AtomicReference<>();
    final Gauge subscriptionActive;
    Metrics metrics = new Metrics.Builder().name("ObservableTrigger_" + name).addGauge("subscriptionActive").build();
    subscriptionActive = metrics.getGauge("subscriptionActive");
    Action1<MonitoredQueue<T>> doOnStart = new Action1<MonitoredQueue<T>>() {

        @Override
        public void call(final MonitoredQueue<T> queue) {
            subRef.set(o.filter((T t1) -> t1 != null).doOnSubscribe(() -> {
                logger.info("Subscription is ACTIVE for observable trigger with name: " + name);
                subscriptionActive.set(1);
            }).doOnUnsubscribe(() -> {
                logger.info("Subscription is INACTIVE for observable trigger with name: " + name);
                subscriptionActive.set(0);
            }).subscribe((T data) -> queue.write(data), (Throwable e) -> {
                logger.warn("Observable used to push data errored, on server with name: " + name, e);
                if (doOnError != null) {
                    doOnError.call(e);
                }
            }, () -> {
                logger.info("Observable used to push data completed, on server with name: " + name);
                if (doOnComplete != null) {
                    doOnComplete.call();
                }
            }));
        }
    };
    Action1<MonitoredQueue<T>> doOnStop = new Action1<MonitoredQueue<T>>() {

        @Override
        public void call(MonitoredQueue<T> t1) {
            if (subRef.get() != null) {
                logger.warn("Connections from next stage has dropped to 0. Do not propagate unsubscribe");
            // subRef.get().unsubscribe();
            }
        }
    };
    return new PushTrigger<>(doOnStart, doOnStop, metrics);
}
Also used : Metrics(io.mantisrx.common.metrics.Metrics) Action1(rx.functions.Action1) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(rx.Subscription) Gauge(io.mantisrx.common.metrics.Gauge)

Example 4 with Gauge

use of io.mantisrx.common.metrics.Gauge in project mantis by Netflix.

the class ObservableTrigger method mantisGroupTrigger.

private static <K, V> PushTrigger<KeyValuePair<K, V>> mantisGroupTrigger(final String name, final Observable<MantisGroup<K, V>> o, final Action0 doOnComplete, final Action1<Throwable> doOnError, final long groupExpirySeconds, final Func1<K, byte[]> keyEncoder, final HashFunction hashFunction) {
    final AtomicReference<Subscription> subRef = new AtomicReference<>();
    final Gauge subscriptionActive;
    Metrics metrics = new Metrics.Builder().name("ObservableTrigger_" + name).addGauge("subscriptionActive").build();
    subscriptionActive = metrics.getGauge("subscriptionActive");
    Action1<MonitoredQueue<KeyValuePair<K, V>>> doOnStart = new Action1<MonitoredQueue<KeyValuePair<K, V>>>() {

        @Override
        public void call(final MonitoredQueue<KeyValuePair<K, V>> queue) {
            subRef.set(o.doOnSubscribe(() -> {
                logger.info("Subscription is ACTIVE for observable trigger with name: " + name);
                subscriptionActive.set(1);
            }).doOnUnsubscribe(() -> {
                logger.info("Subscription is INACTIVE for observable trigger with name: " + name);
                subscriptionActive.set(0);
            }).map((MantisGroup<K, V> data) -> {
                final byte[] keyBytes = keyEncoder.call(data.getKeyValue());
                final long keyBytesHashed = hashFunction.computeHash(keyBytes);
                return (new KeyValuePair<K, V>(keyBytesHashed, keyBytes, data.getValue()));
            }).subscribe((KeyValuePair<K, V> data) -> {
                queue.write(data);
            }, (Throwable e) -> {
                logger.warn("Observable used to push data errored, on server with name: " + name, e);
                if (doOnError != null) {
                    doOnError.call(e);
                }
            }, () -> {
                logger.info("Observable used to push data completed, on server with name: " + name);
                if (doOnComplete != null) {
                    doOnComplete.call();
                }
            }));
        }
    };
    Action1<MonitoredQueue<KeyValuePair<K, V>>> doOnStop = new Action1<MonitoredQueue<KeyValuePair<K, V>>>() {

        @Override
        public void call(MonitoredQueue<KeyValuePair<K, V>> t1) {
            if (subRef.get() != null) {
                logger.warn("Connections from next stage has dropped to 0. Do not propagate unsubscribe");
            // subRef.get().unsubscribe();
            }
        }
    };
    return new PushTrigger<>(doOnStart, doOnStop, metrics);
}
Also used : Action1(rx.functions.Action1) AtomicReference(java.util.concurrent.atomic.AtomicReference) MantisGroup(io.mantisrx.common.MantisGroup) Gauge(io.mantisrx.common.metrics.Gauge) Metrics(io.mantisrx.common.metrics.Metrics) Subscription(rx.Subscription)

Aggregations

Gauge (io.mantisrx.common.metrics.Gauge)4 Metrics (io.mantisrx.common.metrics.Metrics)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Subscription (rx.Subscription)4 Action1 (rx.functions.Action1)4 MantisGroup (io.mantisrx.common.MantisGroup)1 DisableBackPressureOperator (io.reactivx.mantis.operators.DisableBackPressureOperator)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 GroupedObservable (rx.observables.GroupedObservable)1