Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 53148200B64 for ; Tue, 2 Aug 2016 14:04:09 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 51832160A8C; Tue, 2 Aug 2016 12:04:09 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 6F442160A76 for ; Tue, 2 Aug 2016 14:04:08 +0200 (CEST) Received: (qmail 40654 invoked by uid 500); 2 Aug 2016 12:04:07 -0000 Mailing-List: contact dev-help@apex.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@apex.apache.org Delivered-To: mailing list dev@apex.apache.org Received: (qmail 40643 invoked by uid 99); 2 Aug 2016 12:04:07 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 02 Aug 2016 12:04:07 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id EF6B0C12C0 for ; Tue, 2 Aug 2016 12:04:06 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -5.446 X-Spam-Level: X-Spam-Status: No, score=-5.446 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-1.426] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id fKluVyRg1gZT for ; Tue, 2 Aug 2016 12:04:04 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id 643CF5FE69 for ; Tue, 2 Aug 2016 12:04:03 +0000 (UTC) Received: (qmail 40617 invoked by uid 99); 2 Aug 2016 12:04:02 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 02 Aug 2016 12:04:02 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 918D5E0007; Tue, 2 Aug 2016 12:04:02 +0000 (UTC) From: bhupeshchawda To: dev@apex.incubator.apache.org Reply-To: dev@apex.incubator.apache.org References: In-Reply-To: Subject: [GitHub] apex-malhar pull request #335: [Review Only] APEXMALHAR-1701 Deduper with Ma... Content-Type: text/plain Message-Id: <20160802120402.918D5E0007@git1-us-west.apache.org> Date: Tue, 2 Aug 2016 12:04:02 +0000 (UTC) archived-at: Tue, 02 Aug 2016 12:04:09 -0000 Github user bhupeshchawda commented on a diff in the pull request: https://github.com/apache/apex-malhar/pull/335#discussion_r73142080 --- Diff: library/src/main/java/org/apache/apex/malhar/lib/dedup/AbstractDeduper.java --- @@ -0,0 +1,459 @@ +/** + * 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.apex.malhar.lib.dedup; + +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +import javax.validation.constraints.NotNull; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.apex.malhar.lib.state.BucketedState; +import org.apache.apex.malhar.lib.state.managed.ManagedTimeUnifiedStateImpl; +import org.apache.apex.malhar.lib.state.managed.TimeBucketAssigner; +import org.apache.hadoop.classification.InterfaceStability.Evolving; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Maps; +import com.google.common.util.concurrent.Futures; + +import com.datatorrent.api.AutoMetric; +import com.datatorrent.api.Context; +import com.datatorrent.api.Context.OperatorContext; +import com.datatorrent.api.DAG; +import com.datatorrent.api.DefaultInputPort; +import com.datatorrent.api.DefaultOutputPort; +import com.datatorrent.api.Operator; +import com.datatorrent.api.Operator.ActivationListener; +import com.datatorrent.api.annotation.OperatorAnnotation; +import com.datatorrent.lib.fileaccess.FileAccessFSImpl; +import com.datatorrent.lib.fileaccess.TFileImpl; +import com.datatorrent.netlet.util.Slice; + +/** + * Abstract class which allows de-duplicating incoming tuples based on a configured key. + * Also supports expiry mechanism based on a configurable expiry period configured using {@link TimeBucketAssigner} + * in {@link ManagedTimeUnifiedStateImpl} + * Following steps are used in identifying the state of a particular tuple: + * 1. Check if the time of the tuple falls in an expired bucket. If so, the tuple is expired + * 2. If the tuple is a valid event, it is checked in the store whether the same key already exists in the + * time bucket identified by the event time. If, so, the tuple is a duplicate. + * 3. Otherwise the tuple is a unique tuple. + * + * @param type of events + */ +@Evolving +@OperatorAnnotation(checkpointableWithinAppWindow = false) +public abstract class AbstractDeduper + implements Operator, Operator.IdleTimeHandler, ActivationListener, Operator.CheckpointNotificationListener +{ + /** + * The input port on which events are received. + */ + public final transient DefaultInputPort input = new DefaultInputPort() + { + @Override + public final void process(T tuple) + { + processTuple(tuple); + } + }; + + /** + * The output port on which deduped events are emitted. + */ + public final transient DefaultOutputPort unique = new DefaultOutputPort<>(); + + /** + * The output port on which duplicate events are emitted. + */ + public final transient DefaultOutputPort duplicate = new DefaultOutputPort<>(); + + /** + * The output port on which expired events are emitted. + */ + public final transient DefaultOutputPort expired = new DefaultOutputPort<>(); + + /** + * Whether or not the order of tuples be maintained. + * Making this "true" might entail some cost in performance, but makes the operator idempotent. + */ + private boolean preserveTupleOrder = true; + + @NotNull + protected final ManagedTimeUnifiedStateImpl managedState = new ManagedTimeUnifiedStateImpl(); + + /** + * Map to hold the result of a tuple processing (unique, duplicate, expired or error) until previous + * tuples get processed. This is used only when {@link #preserveTupleOrder} is true. + */ + private transient Map decisions; + private transient long sleepMillis; + private transient Map> waitingEvents = Maps.newLinkedHashMap(); + private transient Map asyncEvents = Maps.newLinkedHashMap(); + + // Metrics + @AutoMetric + private transient long uniqueEvents; + @AutoMetric + private transient long duplicateEvents; + @AutoMetric + private transient long expiredEvents; + + @Override + public void setup(OperatorContext context) + { + sleepMillis = context.getValue(Context.OperatorContext.SPIN_MILLIS); + FileAccessFSImpl fAccessImpl = new TFileImpl.DTFileImpl(); + fAccessImpl.setBasePath(context.getValue(DAG.APPLICATION_PATH) + "/bucket_data"); + managedState.setFileAccess(fAccessImpl); + managedState.setup(context); + + if (preserveTupleOrder) { + decisions = Maps.newLinkedHashMap(); + } + } + + @Override + public void beginWindow(long l) + { + // Reset Metrics + uniqueEvents = 0; + duplicateEvents = 0; + expiredEvents = 0; + + managedState.beginWindow(l); + } + + protected abstract Slice getKey(T event); + + protected abstract long getTime(T event); + + /** + * Processes an incoming tuple + * + * @param tuple + */ + protected void processTuple(T tuple) + { + + long time = getTime(tuple); + Future valFuture = managedState.getAsync(time, getKey(tuple)); + + if (valFuture.isDone()) { + try { + processEvent(tuple, valFuture.get()); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + } else { + processWaitingEvent(tuple, valFuture); + } + } + + /** + * Processes a looked-up event + * + * @param tuple + * @param value + */ + protected void processEvent(T tuple, Slice value) + { + if (value == BucketedState.EXPIRED) { + processInvalid(tuple); + return; + } + processValid(tuple, value); + } + + /** + * Processes a tuple which is waiting for the lookup to return. + * + * @param tuple The tuple which needs to wait + * @param future The future object which will ultimately return the lookup result + */ + protected void processWaitingEvent(T tuple, Future future) + { + waitingEvents.put(tuple, future); + if (preserveTupleOrder) { + recordDecision(tuple, Decision.UNKNOWN); + } + } + + /** + * Processes a valid (non-expired) tuple. This tuple may be a unique or a duplicate. + * + * @param tuple + * The tuple to be processed + * @param value + * Looked up key of the tuple + */ + protected void processValid(T tuple, Slice value) + { + if (!preserveTupleOrder || waitingEvents.isEmpty()) { + if (value == null) { + managedState.put(getTime(tuple), getKey(tuple), getKey(tuple)); + processUnique(tuple); + } else { + processDuplicate(tuple); + } + } else { + processWaitingEvent(tuple, Futures.immediateFuture(value)); + } + } + + /** + * Processes invalid tuples. + * + * @param tuple + */ + protected void processInvalid(T tuple) + { + if (preserveTupleOrder && !decisions.isEmpty()) { + recordDecision(tuple, Decision.EXPIRED); + } else { + processExpired(tuple); + } + } + + /** + * Processes an expired tuple + * + * @param tuple + */ + protected void processExpired(T tuple) + { + expiredEvents++; + emitExpired(tuple); + } + + /** + * Processes the duplicate tuple. + * + * @param tuple + * The tuple which is a duplicate + */ + protected void processDuplicate(T tuple) + { + if (preserveTupleOrder && !decisions.isEmpty()) { + recordDecision(tuple, Decision.DUPLICATE); + } else { + duplicateEvents++; + emitDuplicate(tuple); + } + } + + /** + * Processes the unique tuple. + * + * @param tuple + * The tuple which is a unique + */ + protected void processUnique(T tuple) + { + if (preserveTupleOrder && !decisions.isEmpty()) { + recordDecision(tuple, Decision.UNIQUE); + } else { + uniqueEvents++; + emitUnique(tuple); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void handleIdleTime() + { + if (preserveTupleOrder) { + emitProcessedTuples(); + } + processAuxiliary(false); + } + + /** + * Does any auxiliary processing in the idle time of the operator. + * Processes any tuples which are waiting for the lookup to return. + * + * @param finalize Whether or not to wait for future to return + */ + protected void processAuxiliary(boolean finalize) + { + if (waitingEvents.size() > 0) { + Iterator>> waitIterator = waitingEvents.entrySet().iterator(); + while (waitIterator.hasNext()) { + Map.Entry> waitingEvent = waitIterator.next(); + T tuple = waitingEvent.getKey(); + Slice tupleKey = getKey(tuple); + long tupleTime = getTime(tuple); + Future future = waitingEvent.getValue(); + if (future.isDone() || finalize ) { + try { + if (future.get() == null && asyncEvents.get(tupleKey) == null) { + managedState.put(tupleTime, tupleKey, tupleKey); + asyncEvents.put(tupleKey, tupleTime); + processUnique(tuple); + } else { + processDuplicate(tuple); + } + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException("handle idle time", e); + } + waitIterator.remove(); + } + } + } + } + + @Override + public void endWindow() + { + processAuxiliary(true); + if (preserveTupleOrder) { + emitProcessedTuples(); + } + Preconditions.checkArgument(waitingEvents.isEmpty()); + asyncEvents.clear(); + managedState.endWindow(); + } + + /** + * Records a decision for use later. This is needed to ensure that the order of incoming tuples is maintained. + * + * @param tuple + * @param d The decision for the tuple + */ + protected void recordDecision(T tuple, Decision d) + { + decisions.put(tuple, d); + } + + /** + * Processes tuples for which the decision (unique / duplicate / expired) has been made. + * Breaks once an undecided tuple is found, as we don't want to emit out of order + */ + protected void emitProcessedTuples() + { + Iterator> entries = decisions.entrySet().iterator(); + while (entries.hasNext()) { + Entry td = entries.next(); + switch (td.getValue()) { + case UNIQUE: + uniqueEvents++; + emitUnique(td.getKey()); + entries.remove(); + break; + case DUPLICATE: + duplicateEvents++; + emitDuplicate(td.getKey()); + entries.remove(); + break; + case EXPIRED: + expiredEvents++; + emitExpired(td.getKey()); + entries.remove(); + break; + default: + /* + * Decision for this is still UNKNOWN. Tuple is still waiting for bucket to be loaded. Break. + */ + break; + } + } + } + + @Override + public void teardown() + { + managedState.teardown(); + } + + @Override + public void beforeCheckpoint(long windowId) + { + managedState.beforeCheckpoint(windowId); + } + + @Override + public void checkpointed(long windowId) + { + managedState.checkpointed(windowId); + } + + @Override + public void committed(long windowId) + { + managedState.committed(windowId); + } + + protected void emitUnique(T event) + { + unique.emit(event); + } + + protected void emitDuplicate(T event) + { + duplicate.emit(event); + } + + protected void emitExpired(T event) + { + expired.emit(event); + } + + /** + * Checks whether output of deduper should preserve the input order + */ + public boolean isOrderedOutput() + { + return preserveTupleOrder; + } + + /** + * If set to true, the deduper will emit tuples in the order in which they were received. Tuples which arrived later + * will wait for previous tuples to get processed and emitted. If not set, the order of tuples may change as tuples + * may be emitted out of order as and when they get processed. + * + * @param orderedOutput + */ + public void setPreserveTupleOrder(boolean preserveTupleOrder) + { + this.preserveTupleOrder = preserveTupleOrder; + } + + public ManagedTimeUnifiedStateImpl getManagedState() --- End diff -- removed --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---