You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apex.apache.org by siyuanh <gi...@git.apache.org> on 2016/01/26 22:02:23 UTC
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
GitHub user siyuanh opened a pull request:
https://github.com/apache/incubator-apex-malhar/pull/178
APEXMALHAR-1984 refactor out operator creation to util function
Refactor definePartition methods to use same util function to instantiate partitions/operators.
Also set Kryo to use class loader which loads the application code
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/siyuanh/incubator-apex-malhar APEXMALHAR-1984
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/incubator-apex-malhar/pull/178.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #178
----
commit 4ea47acc50db32b19de4fea21b8e48d16df758db
Author: Siyuan Hua <hs...@apache.org>
Date: 2016-01-26T20:55:00Z
APEXMALHAR-1984 #comment Create util function to clone operator object by kryo
----
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/incubator-apex-malhar/pull/178
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by siyuanh <gi...@git.apache.org>.
Github user siyuanh commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51019702
--- Diff: contrib/src/main/java/com/datatorrent/contrib/couchbase/AbstractCouchBaseInputOperator.java ---
@@ -149,15 +145,8 @@ public void partitioned(Map<Integer, Partition<AbstractCouchBaseInputOperator<T>
int numPartitions = conf.getServers().size();
List<String> list = conf.getServers();
Collection<Partition<AbstractCouchBaseInputOperator<T>>> newPartitions = Lists.newArrayListWithExpectedSize(numPartitions);
- Kryo kryo = new Kryo();
for (int i = 0; i < numPartitions; i++) {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- Output output = new Output(bos);
- kryo.writeObject(output, this);
- output.close();
- Input lInput = new Input(bos.toByteArray());
- @SuppressWarnings("unchecked")
- AbstractCouchBaseInputOperator<T> oper = kryo.readObject(lInput, this.getClass());
+ AbstractCouchBaseInputOperator<T> oper = PartitionerUtils.cloneOperatorByKryo(this);
--- End diff --
I've created a threadlocal pool for kryo objects. I think it's even better idea to create kryo on the fly.
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by vrozov <gi...@git.apache.org>.
Github user vrozov commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51081000
--- Diff: library/src/test/java/com/datatorrent/lib/appdata/datastructs/DimensionalTableTest.java ---
@@ -38,7 +38,7 @@ public void serializabilityTest() throws Exception
{
DimensionalTable<Integer> table = createTestTable();
- TestUtils.clone(new Kryo(), table);
+ KryoCloneUtils.cloneByKryo(new Kryo(), table);
--- End diff --
IMO, should use `KryoCloneUtils.cloneByKryo(table);`and possible remove Kryo import.
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by siyuanh <gi...@git.apache.org>.
Github user siyuanh commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r50919206
--- Diff: library/src/main/java/com/datatorrent/lib/util/PartitionerUtils.java ---
@@ -0,0 +1,63 @@
+/**
+ * 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 org.apache.commons.io.IOUtils;
+
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+
+import com.datatorrent.api.DefaultPartition;
+import com.datatorrent.api.Operator;
+
+/**
+ *
+ * A Partitioner Util class that can be used in definePartition method to create operator from existing one
+ *
+ * @since 3.4.0
+ */
+public class PartitionerUtils
--- End diff --
Name it in this way because it is a repeat pattern in definePartition code and developer can easily find this when they want this feature.
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by vrozov <gi...@git.apache.org>.
Github user vrozov commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51080729
--- 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<T>
+{
+
+ /**
+ * 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<T> 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>)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<T> result = new ArrayList<>();
--- End diff --
Why is list necessary? The number of elements is passed, so array may be allocated at the beginning, it is not necessary to rely on ArrayList to determine the final size of the array.
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by amberarrow <gi...@git.apache.org>.
Github user amberarrow commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51137559
--- 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<T>
+{
+
+ /**
+ * 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<T> 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>)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<T> result = new ArrayList<>();
--- End diff --
```
T[] result = (T[])new Object[num];
```
should work.
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by tweise <gi...@git.apache.org>.
Github user tweise commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r50921919
--- Diff: library/src/main/java/com/datatorrent/lib/util/PartitionerUtils.java ---
@@ -0,0 +1,63 @@
+/**
+ * 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 org.apache.commons.io.IOUtils;
+
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+
+import com.datatorrent.api.DefaultPartition;
+import com.datatorrent.api.Operator;
+
+/**
+ *
+ * A Partitioner Util class that can be used in definePartition method to create operator from existing one
+ *
+ * @since 3.4.0
+ */
+public class PartitionerUtils
--- End diff --
In this case, as the developer I would look for clone functionality, not partitioning.
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by tweise <gi...@git.apache.org>.
Github user tweise commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r50901245
--- Diff: contrib/src/main/java/com/datatorrent/contrib/couchbase/AbstractCouchBaseInputOperator.java ---
@@ -149,15 +145,8 @@ public void partitioned(Map<Integer, Partition<AbstractCouchBaseInputOperator<T>
int numPartitions = conf.getServers().size();
List<String> list = conf.getServers();
Collection<Partition<AbstractCouchBaseInputOperator<T>>> newPartitions = Lists.newArrayListWithExpectedSize(numPartitions);
- Kryo kryo = new Kryo();
for (int i = 0; i < numPartitions; i++) {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- Output output = new Output(bos);
- kryo.writeObject(output, this);
- output.close();
- Input lInput = new Input(bos.toByteArray());
- @SuppressWarnings("unchecked")
- AbstractCouchBaseInputOperator<T> oper = kryo.readObject(lInput, this.getClass());
+ AbstractCouchBaseInputOperator<T> oper = PartitionerUtils.cloneOperatorByKryo(this);
--- End diff --
Why create a separate Kryo instance for every partition?
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by siyuanh <gi...@git.apache.org>.
Github user siyuanh commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51083726
--- Diff: library/src/test/java/com/datatorrent/lib/appdata/datastructs/DimensionalTableTest.java ---
@@ -38,7 +38,7 @@ public void serializabilityTest() throws Exception
{
DimensionalTable<Integer> table = createTestTable();
- TestUtils.clone(new Kryo(), table);
+ KryoCloneUtils.cloneByKryo(new Kryo(), table);
--- End diff --
This method is still necessary, because people may want to use their own customized Kryo
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by siyuanh <gi...@git.apache.org>.
Github user siyuanh commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r50919003
--- Diff: contrib/src/main/java/com/datatorrent/contrib/couchbase/AbstractCouchBaseInputOperator.java ---
@@ -149,15 +145,8 @@ public void partitioned(Map<Integer, Partition<AbstractCouchBaseInputOperator<T>
int numPartitions = conf.getServers().size();
List<String> list = conf.getServers();
Collection<Partition<AbstractCouchBaseInputOperator<T>>> newPartitions = Lists.newArrayListWithExpectedSize(numPartitions);
- Kryo kryo = new Kryo();
for (int i = 0; i < numPartitions; i++) {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- Output output = new Output(bos);
- kryo.writeObject(output, this);
- output.close();
- Input lInput = new Input(bos.toByteArray());
- @SuppressWarnings("unchecked")
- AbstractCouchBaseInputOperator<T> oper = kryo.readObject(lInput, this.getClass());
+ AbstractCouchBaseInputOperator<T> oper = PartitionerUtils.cloneOperatorByKryo(this);
--- End diff --
I don't want to leave the kryo creation code to the developer, it is error prone.
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by siyuanh <gi...@git.apache.org>.
Github user siyuanh commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51160338
--- 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<T>
+{
+
+ /**
+ * 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<T> 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>)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<T> result = new ArrayList<>();
--- End diff --
No, you cannot cast Object[] to any specific T[]
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by vrozov <gi...@git.apache.org>.
Github user vrozov commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51182436
--- Diff: library/src/main/java/com/datatorrent/lib/util/KryoCloneUtils.java ---
@@ -0,0 +1,168 @@
+/**
+ * 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.lang.reflect.Array;
+
+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<T>
+{
+
+ /**
+ * 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<T> clazz;
+
+ @SuppressWarnings("unchecked")
+ 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>)t.getClass();
+ kryo.setClassLoader(clazz.getClassLoader());
+ }
+
+ /**
+ * Clone from the binary data of the source object
+ * @return T
+ */
+ public T getClone()
+ {
+ try (Input input = new Input(bin)) {
+ return kryo.readObject(input, clazz);
+ }
+ }
+
+ /**
+ * Clone array of objects from source object
+ * @param num size of the return array
+ * @return array of T
+ */
+ @SuppressWarnings("unchecked")
+ public T[] getClones(int num)
+ {
+ T[] ts = (T[])Array.newInstance(clazz.getClass(), num);
--- End diff --
Should it be `(T[])Array.newInstance(clazz, num);`? The `clazz` is already Class object for T.
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by siyuanh <gi...@git.apache.org>.
Github user siyuanh commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51082903
--- 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<T>
+{
+
+ /**
+ * 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<T> 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>)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<T> result = new ArrayList<>();
--- End diff --
I couldn't find other way to initialize generic array
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by vrozov <gi...@git.apache.org>.
Github user vrozov commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51174503
--- Diff: library/src/main/java/com/datatorrent/lib/util/KryoCloneUtils.java ---
@@ -0,0 +1,164 @@
+/**
+ * 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.lang.reflect.Array;
+
+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<T>
+{
+
+ /**
+ * 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<T> clazz;
+
+ @SuppressWarnings("unchecked")
+ 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>)t.getClass();
+ kryo.setClassLoader(clazz.getClassLoader());
+ }
+
+ /**
+ * Clone from the binary data of the source object
+ * @return T
+ */
+ public T getClone()
+ {
+ try (Input input = new Input(bin)) {
+ return kryo.readObject(input, clazz);
+ }
+ }
+
+ /**
+ * Clone array of objects from source object
+ * @param num size of the return array
+ * @return array of T
+ */
+ @SuppressWarnings("unchecked")
+ public T[] getClones(int num)
+ {
+ return (T[])Array.newInstance(clazz.getClass(), num);
--- End diff --
Missing array population with clones?
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by tweise <gi...@git.apache.org>.
Github user tweise commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51083964
--- 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<T>
+{
+
+ /**
+ * 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<T> 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>)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<T> 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> SRC cloneByKryo(Kryo kryo, SRC src)
--- End diff --
"ByKryo" is redundant
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by siyuanh <gi...@git.apache.org>.
Github user siyuanh commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51085625
--- 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<T>
+{
+
+ /**
+ * 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<T> 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>)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<T> result = new ArrayList<>();
--- End diff --
I don't think compiler is able to compile because of type erasure
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by siyuanh <gi...@git.apache.org>.
Github user siyuanh commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r50918805
--- Diff: contrib/src/main/java/com/datatorrent/contrib/couchbase/AbstractCouchBaseInputOperator.java ---
@@ -149,15 +145,8 @@ public void partitioned(Map<Integer, Partition<AbstractCouchBaseInputOperator<T>
int numPartitions = conf.getServers().size();
List<String> list = conf.getServers();
Collection<Partition<AbstractCouchBaseInputOperator<T>>> newPartitions = Lists.newArrayListWithExpectedSize(numPartitions);
- Kryo kryo = new Kryo();
for (int i = 0; i < numPartitions; i++) {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- Output output = new Output(bos);
- kryo.writeObject(output, this);
- output.close();
- Input lInput = new Input(bos.toByteArray());
- @SuppressWarnings("unchecked")
- AbstractCouchBaseInputOperator<T> oper = kryo.readObject(lInput, this.getClass());
+ AbstractCouchBaseInputOperator<T> oper = PartitionerUtils.cloneOperatorByKryo(this);
--- End diff --
How about use the Kryo official suggested way?
https://github.com/EsotericSoftware/kryo#pooling-kryo-instances
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by amberarrow <gi...@git.apache.org>.
Github user amberarrow commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51163070
--- 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<T>
+{
+
+ /**
+ * 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<T> 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>)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<T> result = new ArrayList<>();
--- End diff --
```
public class Gen<E> {
private E a[];
public Gen(int n) {
a = (E[])new Object[n];
}
E[] foo(int n) {
E[] result = (E[]) new Object[n];
return result;
}
public static void main(String[] args) {
Gen<Byte> g = new Gen<>(10);
}
}
```
That code compiles and runs.
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by siyuanh <gi...@git.apache.org>.
Github user siyuanh commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51082971
--- 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<T>
+{
+
+ /**
+ * 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<T> 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>)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<T> 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> 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 <SRC>
+ * @return
+ */
+ public static <SRC> KryoCloneUtils<SRC> createCloneUtils(SRC template)
+ {
+ return new KryoCloneUtils<SRC>(new Kryo(), template);
+ }
+
+ /**
+ * Factory function to return CloneUtils object with customized Kryo
+ * @param kryo
+ * @param template
+ * @param <SRC>
+ * @return
+ */
+ public static <SRC> KryoCloneUtils<SRC> createCloneUtils(Kryo kryo, SRC template)
+ {
+ return new KryoCloneUtils<SRC>(new Kryo(), template);
--- End diff --
My bad here
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by vrozov <gi...@git.apache.org>.
Github user vrozov commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51184204
--- Diff: library/src/main/java/com/datatorrent/lib/util/KryoCloneUtils.java ---
@@ -0,0 +1,168 @@
+/**
+ * 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.lang.reflect.Array;
+
+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<T>
+{
+
+ /**
+ * 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<T> clazz;
+
+ @SuppressWarnings("unchecked")
+ 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>)t.getClass();
+ kryo.setClassLoader(clazz.getClassLoader());
+ }
+
+ /**
+ * Clone from the binary data of the source object
+ * @return T
+ */
+ public T getClone()
+ {
+ try (Input input = new Input(bin)) {
+ return kryo.readObject(input, clazz);
+ }
+ }
+
+ /**
+ * Clone array of objects from source object
+ * @param num size of the return array
+ * @return array of T
+ */
+ @SuppressWarnings("unchecked")
+ public T[] getClones(int num)
+ {
+ T[] ts = (T[])Array.newInstance(clazz.getClass(), num);
+ for (int i = 0; i < ts.length; i++) {
+ ts[i] = getClone();
--- End diff --
Kryo allows to reuse Input object, what do you think about:
```
try (Input input = new Input(bin)) {
for (int i = 0; i < ts.length; i++) {
ts[i] = kryo.readObject(input, clazz);
input.setBuffer(bin);
}
}
```
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by vrozov <gi...@git.apache.org>.
Github user vrozov commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51084952
--- 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<T>
+{
+
+ /**
+ * 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<T> 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>)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<T> result = new ArrayList<>();
--- End diff --
`T[] result = new T[num]`?
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by vrozov <gi...@git.apache.org>.
Github user vrozov commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51080810
--- 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<T>
+{
+
+ /**
+ * 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<T> 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>)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<T> 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> 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 <SRC>
+ * @return
+ */
+ public static <SRC> KryoCloneUtils<SRC> createCloneUtils(SRC template)
+ {
+ return new KryoCloneUtils<SRC>(new Kryo(), template);
+ }
+
+ /**
+ * Factory function to return CloneUtils object with customized Kryo
+ * @param kryo
+ * @param template
+ * @param <SRC>
+ * @return
+ */
+ public static <SRC> KryoCloneUtils<SRC> createCloneUtils(Kryo kryo, SRC template)
+ {
+ return new KryoCloneUtils<SRC>(new Kryo(), template);
--- End diff --
I guess you mean to pass `kryo`, not to create another Kryo.
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by tweise <gi...@git.apache.org>.
Github user tweise commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r50922620
--- Diff: contrib/src/main/java/com/datatorrent/contrib/couchbase/AbstractCouchBaseInputOperator.java ---
@@ -149,15 +145,8 @@ public void partitioned(Map<Integer, Partition<AbstractCouchBaseInputOperator<T>
int numPartitions = conf.getServers().size();
List<String> list = conf.getServers();
Collection<Partition<AbstractCouchBaseInputOperator<T>>> newPartitions = Lists.newArrayListWithExpectedSize(numPartitions);
- Kryo kryo = new Kryo();
for (int i = 0; i < numPartitions; i++) {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- Output output = new Output(bos);
- kryo.writeObject(output, this);
- output.close();
- Input lInput = new Input(bos.toByteArray());
- @SuppressWarnings("unchecked")
- AbstractCouchBaseInputOperator<T> oper = kryo.readObject(lInput, this.getClass());
+ AbstractCouchBaseInputOperator<T> oper = PartitionerUtils.cloneOperatorByKryo(this);
--- End diff --
This is not a good enough reason to write inefficient code ;-) How about having a "KryoCloner" that can be used for multiple objects?
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by vrozov <gi...@git.apache.org>.
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<T>
+{
+
+ /**
+ * 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<T> 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>)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<T> 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> 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 <SRC>
+ * @return
+ */
+ public static <SRC> KryoCloneUtils<SRC> createCloneUtils(SRC template)
+ {
+ return new KryoCloneUtils<SRC>(new Kryo(), template);
--- End diff --
Delegate to `createCloneUtils(Kryo kryo, ...)`
---
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.
---
[GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Posted by tweise <gi...@git.apache.org>.
Github user tweise commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r50902925
--- Diff: library/src/main/java/com/datatorrent/lib/util/PartitionerUtils.java ---
@@ -0,0 +1,63 @@
+/**
+ * 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 org.apache.commons.io.IOUtils;
+
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+
+import com.datatorrent.api.DefaultPartition;
+import com.datatorrent.api.Operator;
+
+/**
+ *
+ * A Partitioner Util class that can be used in definePartition method to create operator from existing one
+ *
+ * @since 3.4.0
+ */
+public class PartitionerUtils
--- End diff --
It's not really partitioning specific.
---
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.
---