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 E190F200CE7 for ; Mon, 10 Jul 2017 06:49:37 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id E0492167BB2; Mon, 10 Jul 2017 04:49:37 +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 CF36D167B6B for ; Mon, 10 Jul 2017 06:49:36 +0200 (CEST) Received: (qmail 59562 invoked by uid 500); 10 Jul 2017 04:49:35 -0000 Mailing-List: contact commits-help@beam.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@beam.apache.org Delivered-To: mailing list commits@beam.apache.org Received: (qmail 56333 invoked by uid 99); 10 Jul 2017 04:49:33 -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; Mon, 10 Jul 2017 04:49:33 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 692EFF5539; Mon, 10 Jul 2017 04:49:32 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: kenn@apache.org To: commits@beam.apache.org Date: Mon, 10 Jul 2017 04:50:06 -0000 Message-Id: In-Reply-To: <475176a9da1b498e999831aa04004255@git.apache.org> References: <475176a9da1b498e999831aa04004255@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [36/43] beam git commit: Simplified ByteBuddyOnTimerInvokerFactory archived-at: Mon, 10 Jul 2017 04:49:38 -0000 Simplified ByteBuddyOnTimerInvokerFactory Project: http://git-wip-us.apache.org/repos/asf/beam/repo Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/c8d98336 Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/c8d98336 Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/c8d98336 Branch: refs/heads/gearpump-runner Commit: c8d983363efd3f3d93825ecc8e8abae2dfa4e008 Parents: 17bc3b1 Author: Innocent Djiofack Authored: Wed Jun 28 22:15:11 2017 -0400 Committer: Kenneth Knowles Committed: Thu Jul 6 21:46:53 2017 -0700 ---------------------------------------------------------------------- .../reflect/ByteBuddyOnTimerInvokerFactory.java | 73 ++++++++------------ .../reflect/OnTimerMethodSpecifier.java | 37 ++++++++++ 2 files changed, 65 insertions(+), 45 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/beam/blob/c8d98336/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/ByteBuddyOnTimerInvokerFactory.java ---------------------------------------------------------------------- diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/ByteBuddyOnTimerInvokerFactory.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/ByteBuddyOnTimerInvokerFactory.java index e031337..5e31f2e 100644 --- a/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/ByteBuddyOnTimerInvokerFactory.java +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/ByteBuddyOnTimerInvokerFactory.java @@ -17,6 +17,7 @@ */ package org.apache.beam.sdk.transforms.reflect; + import com.google.common.base.CharMatcher; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; @@ -61,13 +62,14 @@ class ByteBuddyOnTimerInvokerFactory implements OnTimerInvokerFactory { @SuppressWarnings("unchecked") Class> fnClass = (Class>) fn.getClass(); - try { - Constructor constructor = constructorCache.get(fnClass).get(timerId); - @SuppressWarnings("unchecked") - OnTimerInvoker invoker = + OnTimerMethodSpecifier onTimerMethodSpecifier = + OnTimerMethodSpecifier.forClassAndTimerId(fnClass, timerId); + Constructor constructor = constructorCache.get(onTimerMethodSpecifier); + + OnTimerInvoker invoker = (OnTimerInvoker) constructor.newInstance(fn); - return invoker; + return invoker; } catch (InstantiationException | IllegalAccessException | IllegalArgumentException @@ -97,50 +99,31 @@ class ByteBuddyOnTimerInvokerFactory implements OnTimerInvokerFactory { private static final String FN_DELEGATE_FIELD_NAME = "delegate"; /** - * A cache of constructors of generated {@link OnTimerInvoker} classes, keyed by {@link DoFn} - * class and then by {@link TimerId}. + * A cache of constructors of generated {@link OnTimerInvoker} classes, + * keyed by {@link OnTimerMethodSpecifier}. * *

Needed because generating an invoker class is expensive, and to avoid generating an * excessive number of classes consuming PermGen memory in Java's that still have PermGen. */ - private final LoadingCache>, LoadingCache>> - constructorCache = - CacheBuilder.newBuilder() - .build( - new CacheLoader< - Class>, LoadingCache>>() { - @Override - public LoadingCache> load( - final Class> fnClass) throws Exception { - return CacheBuilder.newBuilder().build(new OnTimerConstructorLoader(fnClass)); - } - }); - - /** - * A cache loader fixed to a particular {@link DoFn} class that loads constructors for the - * invokers for its {@link OnTimer @OnTimer} methods. - */ - private static class OnTimerConstructorLoader extends CacheLoader> { - - private final DoFnSignature signature; - - public OnTimerConstructorLoader(Class> clazz) { - this.signature = DoFnSignatures.getSignature(clazz); - } - - @Override - public Constructor load(String timerId) throws Exception { - Class> invokerClass = - generateOnTimerInvokerClass(signature, timerId); - try { - return invokerClass.getConstructor(signature.fnClass()); - } catch (IllegalArgumentException | NoSuchMethodException | SecurityException e) { - throw new RuntimeException(e); - } - } - } - - /** + private final LoadingCache> constructorCache = + CacheBuilder.newBuilder().build( + new CacheLoader>() { + @Override + public Constructor load(final OnTimerMethodSpecifier onTimerMethodSpecifier) + throws Exception { + DoFnSignature signature = + DoFnSignatures.getSignature(onTimerMethodSpecifier.fnClass()); + Class> invokerClass = + generateOnTimerInvokerClass(signature, onTimerMethodSpecifier.timerId()); + try { + return invokerClass.getConstructor(signature.fnClass()); + } catch (IllegalArgumentException | NoSuchMethodException | SecurityException e) { + throw new RuntimeException(e); + } + + } + }); + /** * Generates a {@link OnTimerInvoker} class for the given {@link DoFnSignature} and {@link * TimerId}. */ http://git-wip-us.apache.org/repos/asf/beam/blob/c8d98336/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/OnTimerMethodSpecifier.java ---------------------------------------------------------------------- diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/OnTimerMethodSpecifier.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/OnTimerMethodSpecifier.java new file mode 100644 index 0000000..edf7e3c --- /dev/null +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/OnTimerMethodSpecifier.java @@ -0,0 +1,37 @@ +/* + * 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.beam.sdk.transforms.reflect; + +import com.google.auto.value.AutoValue; +import org.apache.beam.sdk.transforms.DoFn; + +/** + * Used by {@link ByteBuddyOnTimerInvokerFactory} to Dynamically generate + * {@link OnTimerInvoker} instances for invoking a particular + * {@link DoFn.TimerId} on a particular {@link DoFn}. + */ + +@AutoValue +abstract class OnTimerMethodSpecifier { + public abstract Class> fnClass(); + public abstract String timerId(); + public static OnTimerMethodSpecifier + forClassAndTimerId(Class> fnClass, String timerId){ + return new AutoValue_OnTimerMethodSpecifier(fnClass, timerId); + } +}