/*
 * Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed 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 com.hazelcast.jet.impl.util;

import com.hazelcast.jet.core.Inbox;
import com.hazelcast.jet.core.Outbox;
import com.hazelcast.jet.core.Processor;
import com.hazelcast.jet.core.Watermark;

import javax.annotation.Nonnull;

import static com.hazelcast.internal.util.Preconditions.checkNotNull;
import static com.hazelcast.internal.util.Preconditions.checkTrue;
import static java.util.concurrent.TimeUnit.NANOSECONDS;

/**
 * A wrapper processor to throttle the output of a processor.
 */
public final class ThrottleWrappedP implements Processor {

    private final Processor wrappedProcessor;
    private final long itemsPerSecond;

    public ThrottleWrappedP(Processor wrappedProcessor, long itemsPerSecond) {
        checkNotNull(wrappedProcessor, "wrappedProcessor");
        checkTrue(wrappedProcessor.isCooperative(), "wrappedProcessor must be cooperative");

        this.itemsPerSecond = itemsPerSecond;
        this.wrappedProcessor = wrappedProcessor;
    }

    @Override
    public void init(@Nonnull Outbox outbox, @Nonnull Context context) throws Exception {
        wrappedProcessor.init(new ThrottlingOutbox(outbox), context);
    }

    @Override
    public boolean isCooperative() {
        return wrappedProcessor.isCooperative();
    }

    @Override
    public void process(int ordinal, @Nonnull Inbox inbox) {
        wrappedProcessor.process(ordinal, inbox);
    }

    @Override
    public boolean tryProcessWatermark(@Nonnull Watermark watermark) {
        return wrappedProcessor.tryProcessWatermark(watermark);
    }

    @Override
    public boolean tryProcess() {
        return wrappedProcessor.tryProcess();
    }

    @Override
    public boolean complete() {
        return wrappedProcessor.complete();
    }

    @Override
    public boolean saveToSnapshot() {
        return wrappedProcessor.saveToSnapshot();
    }

    @Override
    public void restoreFromSnapshot(@Nonnull Inbox inbox) {
        wrappedProcessor.restoreFromSnapshot(inbox);
    }

    @Override
    public boolean completeEdge(int ordinal) {
        return wrappedProcessor.completeEdge(ordinal);
    }

    @Override
    public boolean finishSnapshotRestore() {
        return wrappedProcessor.finishSnapshotRestore();
    }

    private final class ThrottlingOutbox implements Outbox {
        private final Outbox wrappedOutbox;
        private long emitCount;
        private long startTs = Long.MIN_VALUE;

        private ThrottlingOutbox(Outbox wrappedOutbox) {
            this.wrappedOutbox = wrappedOutbox;
        }

        @Override
        public int bucketCount() {
            return wrappedOutbox.bucketCount();
        }

        @Override
        public boolean offer(int ordinal, @Nonnull Object item) {
            if (startTs == Long.MIN_VALUE) {
                startTs = System.nanoTime();
            }
            long elapsed = System.nanoTime() - startTs;
            if (NANOSECONDS.toSeconds(elapsed * itemsPerSecond) <= emitCount
                    || !wrappedOutbox.offer(ordinal, item)) {
                return false;
            }
            emitCount++;
            return true;
        }

        @Override
        public boolean offer(int[] ordinals, @Nonnull Object item) {
            return wrappedOutbox.offer(ordinals, item);
        }

        @Override
        public boolean offerToSnapshot(@Nonnull Object key, @Nonnull Object value) {
            return wrappedOutbox.offerToSnapshot(key, value);
        }

        @Override
        public boolean hasUnfinishedItem() {
            return wrappedOutbox.hasUnfinishedItem();
        }
    }
}

@@ -662,45 +662,8 @@
 l;%0A%0A
-import com.hazelcast.jet.core.Inbox;%0A
 impo
@@ -766,22 +766,39 @@
 jet.
-core.Watermark
+impl.processor.ProcessorWrapper
 ;%0A%0Ai
@@ -828,16 +828,59 @@
 Nonnull;
+%0Aimport java.util.function.BooleanSupplier;
 %0A%0Aimport
@@ -1178,49 +1178,15 @@
 edP 
-implements Processor %7B%0A%0A    private final
+extends
  Pro
@@ -1191,34 +1191,26 @@
 rocessor
- w
+W
 rappe
-dProcessor;
+r %7B%0A
 %0A    pri
@@ -1317,24 +1317,57 @@
 erSecond) %7B%0A
+        super(wrappedProcessor);%0A
         chec
@@ -1562,1314 +1562,116 @@
     
-    this.wrappedProcessor = wrappedProcessor;%0A    %7D%0A%0A    @Override%0A    public void init(@Nonnull Outbox outbox, @Nonnull Context context) throws Exception %7B%0A        wrappedProcessor.init(new ThrottlingOutbox(outbox), context);%0A    %7D%0A%0A    @Override%0A    public boolean isCooperative() %7B%0A        return wrappedProcessor.isCooperative();%0A    %7D%0A%0A    @Override%0A    public void process(int ordinal, @Nonnull Inbox inbox) %7B%0A        wrappedProcessor.process(ordinal, inbox);%0A    %7D%0A%0A    @Override%0A    public boolean tryProcessWatermark(@Nonnull Watermark watermark) %7B%0A        return wrappedProcessor.tryProcessWatermark(watermark);%0A    %7D%0A%0A    @Override%0A    public boolean tryProcess() %7B%0A        return wrappedProcessor.tryProcess();%0A    %7D%0A%0A    @Override%0A    public boolean complete() %7B%0A        return wrappedProcessor.complete();%0A    %7D%0A%0A    @Override%0A    public boolean saveToSnapshot() %7B%0A        return wrappedProcessor.saveToSnapshot();%0A    %7D%0A%0A    @Override%0A    public void restoreFromSnapshot(@Nonnull Inbox inbox) %7B%0A        wrappedProcessor.restoreFromSnapshot(inbox);%0A    %7D%0A%0A    @Override%0A    public boolean completeEdge(int ordinal) %7B%0A        return wrappedProcessor.completeEdge(ordinal);%0A    %7D%0A%0A    @Override%0A    public boolean finishSnapshotRestore() %7B%0A        return wrappedProcessor.finishSnapshotRestore(
+%7D%0A%0A    @Override%0A    protected Outbox wrapOutbox(Outbox outbox) %7B%0A        return new ThrottlingOutbox(outbox
 );%0A 
@@ -2165,32 +2165,367 @@
  Object item) %7B%0A
+            return offerInternal(() -%3E wrappedOutbox.offer(ordinal, item));%0A        %7D%0A%0A        @Override%0A        public boolean offer(@Nonnull int%5B%5D ordinals, @Nonnull Object item) %7B%0A            return offerInternal(() -%3E wrappedOutbox.offer(ordinals, item));%0A        %7D%0A%0A        private boolean offerInternal(BooleanSupplier action) %7B%0A
             if (
@@ -2773,41 +2773,28 @@
 %7C%7C !
-wrappedOutbox.offer(ordinal, item
+action.getAsBoolean(
 )) %7B
@@ -2903,162 +2903,8 @@
  %7D%0A%0A
-        @Override%0A        public boolean offer(int%5B%5D ordinals, @Nonnull Object item) %7B%0A            return wrappedOutbox.offer(ordinals, item);%0A        %7D%0A%0A
     

Fix ThrottleWrappedP (#1880)