Return-Path: X-Original-To: apmail-apex-dev-archive@minotaur.apache.org Delivered-To: apmail-apex-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9C4FF18463 for ; Thu, 28 Jan 2016 05:13:49 +0000 (UTC) Received: (qmail 4909 invoked by uid 500); 28 Jan 2016 05:13:49 -0000 Delivered-To: apmail-apex-dev-archive@apex.apache.org Received: (qmail 4839 invoked by uid 500); 28 Jan 2016 05:13:49 -0000 Mailing-List: contact dev-help@apex.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@apex.incubator.apache.org Delivered-To: mailing list dev@apex.incubator.apache.org Received: (qmail 4828 invoked by uid 99); 28 Jan 2016 05:13:49 -0000 Received: from Unknown (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Jan 2016 05:13:49 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 138ED1A0616 for ; Thu, 28 Jan 2016 05:13:49 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.227 X-Spam-Level: * X-Spam-Status: No, score=1.227 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.554, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-west.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id mOJ9ksDoWBcz for ; Thu, 28 Jan 2016 05:13:40 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-west.apache.org (ASF Mail Server at mx1-us-west.apache.org) with SMTP id 0374320426 for ; Thu, 28 Jan 2016 05:13:40 +0000 (UTC) Received: (qmail 4493 invoked by uid 99); 28 Jan 2016 05:13:39 -0000 Received: from arcas.apache.org (HELO arcas) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Jan 2016 05:13:39 +0000 Received: from arcas.apache.org (localhost [127.0.0.1]) by arcas (Postfix) with ESMTP id C82892C14F0 for ; Thu, 28 Jan 2016 05:13:39 +0000 (UTC) Date: Thu, 28 Jan 2016 05:13:39 +0000 (UTC) From: "ASF GitHub Bot (JIRA)" To: dev@apex.incubator.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (APEXMALHAR-1984) Operators that use Kryo directly would throw exception in local mode MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/APEXMALHAR-1984?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15120772#comment-15120772 ] ASF GitHub Bot commented on APEXMALHAR-1984: -------------------------------------------- Github user vrozov commented on a diff in the pull request: https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51080849 --- Diff: library/src/main/java/com/datatorrent/lib/util/KryoCloneUtils.java --- @@ -0,0 +1,153 @@ +/** + * 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 com.datatorrent.lib.util; + +import java.io.ByteArrayOutputStream; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.IOUtils; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * + * A Kryo Clone Util class that clone object by using Kryo serializer and deserializer + * The class has static method that are can be used directly to clone one object + * Or it can be used as util instance to clone as many objects as you need from the one source object + * + * @since 3.4.0 + */ +public class KryoCloneUtils +{ + + /** + * Reusable Kryo object as deserializer + */ + private final Kryo kryo; + + /** + * Reusable binary data for object that would be deserialized from + */ + private final byte[] bin; + + /** + * The class of the object + */ + private final Class clazz; + + private KryoCloneUtils(Kryo kryo, T t) + { + this.kryo = kryo; + ByteArrayOutputStream bos = null; + Output output = null; + try { + bos = new ByteArrayOutputStream(); + output = new Output(bos); + kryo.writeObject(output, t); + output.close(); + bin = bos.toByteArray(); + } finally { + IOUtils.closeQuietly(output); + IOUtils.closeQuietly(bos); + } + clazz = (Class)t.getClass(); + kryo.setClassLoader(clazz.getClassLoader()); + } + + /** + * Clone from the binary data of the source object + * @return + */ + public T getClone() + { + try (Input input = new Input(bin)) { + return kryo.readObject(input, clazz); + } + } + + /** + * Clone array of objects from source object + * @param num + * @return + */ + public T[] getClones(int num) + { + List result = new ArrayList<>(); + for (int i = 0; i < num; i++) { + result.add(getClone()); + } + return (T[])result.toArray(); + } + + + + /** + * Clone object by serializing and deserializing using Kryo. + * Note this is different from using {@link Kryo#copy(Object)}, which will attempt to also clone transient fields. + * + * @param kryo kryo object used to clone objects + * @param src src object that copy from + * @return + */ + public static SRC cloneByKryo(Kryo kryo, SRC src) + { + kryo.setClassLoader(src.getClass().getClassLoader()); + ByteArrayOutputStream bos = null; + Output output; + Input input = null; + try { + bos = new ByteArrayOutputStream(); + output = new Output(bos); + kryo.writeObject(output, src); + output.close(); + input = new Input(bos.toByteArray()); + return (SRC)kryo.readObject(input, src.getClass()); + } finally { + IOUtils.closeQuietly(input); + IOUtils.closeQuietly(bos); + } + } + + /** + * Factory function to return CloneUtils object + * @param template + * @param + * @return + */ + public static KryoCloneUtils createCloneUtils(SRC template) + { + return new KryoCloneUtils(new Kryo(), template); --- End diff -- Delegate to `createCloneUtils(Kryo kryo, ...)` > Operators that use Kryo directly would throw exception in local mode > -------------------------------------------------------------------- > > Key: APEXMALHAR-1984 > URL: https://issues.apache.org/jira/browse/APEXMALHAR-1984 > Project: Apache Apex Malhar > Issue Type: Bug > Reporter: Siyuan Hua > Assignee: Siyuan Hua > -- This message was sent by Atlassian JIRA (v6.3.4#6332)