From 245358d7bfde1ff6299f2c8413f265ba23fcf805 Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Thu, 11 Oct 2018 19:05:03 +0700 Subject: [PATCH 1/3] GG-14271 WIP Refactoring classes. --- .../dto/IgniteDataTransferObject.java | 130 +++++++++++++++ .../dto/IgniteDataTransferObjectInput.java | 156 ++++++++++++++++++ .../dto/IgniteDataTransferObjectOutput.java | 141 ++++++++++++++++ .../visor/VisorDataTransferObject.java | 104 +----------- .../visor/VisorDataTransferObjectInput.java | 128 +------------- .../visor/VisorDataTransferObjectOutput.java | 111 +------------ 6 files changed, 440 insertions(+), 330 deletions(-) create mode 100644 modules/core/src/main/java/org/apache/ignite/internal/dto/IgniteDataTransferObject.java create mode 100644 modules/core/src/main/java/org/apache/ignite/internal/dto/IgniteDataTransferObjectInput.java create mode 100644 modules/core/src/main/java/org/apache/ignite/internal/dto/IgniteDataTransferObjectOutput.java diff --git a/modules/core/src/main/java/org/apache/ignite/internal/dto/IgniteDataTransferObject.java b/modules/core/src/main/java/org/apache/ignite/internal/dto/IgniteDataTransferObject.java new file mode 100644 index 0000000000000..666ae6af1feda --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/dto/IgniteDataTransferObject.java @@ -0,0 +1,130 @@ +/* + * 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.ignite.internal.dto; + +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import org.jetbrains.annotations.Nullable; + +/** + * Base class for data transfer objects. + */ +public abstract class IgniteDataTransferObject implements Externalizable { + /** */ + private static final long serialVersionUID = 0L; + + /** Magic number to detect correct transfer objects. */ + private static final int MAGIC = 0x42BEEF00; + + /** Version 1. */ + protected static final byte V1 = 1; + + /** Version 2. */ + protected static final byte V2 = 2; + + /** Version 3. */ + protected static final byte V3 = 3; + + /** Version 4. */ + protected static final byte V4 = 4; + + /** Version 5. */ + protected static final byte V5 = 5; + + /** + * @param col Source collection. + * @param Collection type. + * @return List based on passed collection. + */ + @Nullable protected static List toList(Collection col) { + if (col != null) + return new ArrayList<>(col); + + return null; + } + + /** + * @param col Source collection. + * @param Collection type. + * @return List based on passed collection. + */ + @Nullable protected static Set toSet(Collection col) { + if (col != null) + return new LinkedHashSet<>(col); + + return null; + } + + /** + * @return Transfer object version. + */ + public byte getProtocolVersion() { + return V1; + } + + /** + * Save object's specific data content. + * + * @param out Output object to write data content. + * @throws IOException If I/O errors occur. + */ + protected abstract void writeExternalData(ObjectOutput out) throws IOException; + + /** {@inheritDoc} */ + @Override public void writeExternal(ObjectOutput out) throws IOException { + int hdr = MAGIC + getProtocolVersion(); + + out.writeInt(hdr); + + try (IgniteDataTransferObjectOutput dtout = new IgniteDataTransferObjectOutput(out)) { + writeExternalData(dtout); + } + } + + /** + * Load object's specific data content. + * + * @param protoVer Input object version. + * @param in Input object to load data content. + * @throws IOException If I/O errors occur. + * @throws ClassNotFoundException If the class for an object being restored cannot be found. + */ + protected abstract void readExternalData(byte protoVer, ObjectInput in) throws IOException, ClassNotFoundException; + + /** {@inheritDoc} */ + @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + int hdr = in.readInt(); + + if ((hdr & MAGIC) != MAGIC) + throw new IOException("Unexpected VisorDataTransferObject header " + + "[actual=" + Integer.toHexString(hdr) + ", expected=" + Integer.toHexString(MAGIC) + "]"); + + byte ver = (byte)(hdr & 0xFF); + + try (IgniteDataTransferObjectInput dtin = new IgniteDataTransferObjectInput(in)) { + readExternalData(ver, dtin); + } + } +} diff --git a/modules/core/src/main/java/org/apache/ignite/internal/dto/IgniteDataTransferObjectInput.java b/modules/core/src/main/java/org/apache/ignite/internal/dto/IgniteDataTransferObjectInput.java new file mode 100644 index 0000000000000..c12287520656a --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/dto/IgniteDataTransferObjectInput.java @@ -0,0 +1,156 @@ +/* + * 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.ignite.internal.dto; + +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectInputStream; +import org.apache.ignite.internal.util.io.GridByteArrayInputStream; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.jetbrains.annotations.NotNull; + +/** + * Wrapper for object input. + */ +public class IgniteDataTransferObjectInput implements ObjectInput { + /** */ + private final ObjectInputStream ois; + + /** + * @param in Target input. + * @throws IOException If an I/O error occurs. + */ + public IgniteDataTransferObjectInput(ObjectInput in) throws IOException { + byte[] buf = U.readByteArray(in); + + /* */ + GridByteArrayInputStream bis = new GridByteArrayInputStream(buf); + ois = new ObjectInputStream(bis); + } + + + /** {@inheritDoc} */ + @Override public Object readObject() throws ClassNotFoundException, IOException { + return ois.readObject(); + } + + /** {@inheritDoc} */ + @Override public int read() throws IOException { + return ois.read(); + } + + /** {@inheritDoc} */ + @Override public int read(byte[] b) throws IOException { + return ois.read(b); + } + + /** {@inheritDoc} */ + @Override public int read(byte[] b, int off, int len) throws IOException { + return ois.read(b, off, len); + } + + /** {@inheritDoc} */ + @Override public long skip(long n) throws IOException { + return ois.skip(n); + } + + /** {@inheritDoc} */ + @Override public int available() throws IOException { + return ois.available(); + } + + /** {@inheritDoc} */ + @Override public void close() throws IOException { + ois.close(); + } + + /** {@inheritDoc} */ + @Override public void readFully(@NotNull byte[] b) throws IOException { + ois.readFully(b); + } + + /** {@inheritDoc} */ + @Override public void readFully(@NotNull byte[] b, int off, int len) throws IOException { + ois.readFully(b, off, len); + } + + /** {@inheritDoc} */ + @Override public int skipBytes(int n) throws IOException { + return ois.skipBytes(n); + } + + /** {@inheritDoc} */ + @Override public boolean readBoolean() throws IOException { + return ois.readBoolean(); + } + + /** {@inheritDoc} */ + @Override public byte readByte() throws IOException { + return ois.readByte(); + } + + /** {@inheritDoc} */ + @Override public int readUnsignedByte() throws IOException { + return ois.readUnsignedByte(); + } + + /** {@inheritDoc} */ + @Override public short readShort() throws IOException { + return ois.readShort(); + } + + /** {@inheritDoc} */ + @Override public int readUnsignedShort() throws IOException { + return ois.readUnsignedShort(); + } + + /** {@inheritDoc} */ + @Override public char readChar() throws IOException { + return ois.readChar(); + } + + /** {@inheritDoc} */ + @Override public int readInt() throws IOException { + return ois.readInt(); + } + + /** {@inheritDoc} */ + @Override public long readLong() throws IOException { + return ois.readLong(); + } + + /** {@inheritDoc} */ + @Override public float readFloat() throws IOException { + return ois.readFloat(); + } + + /** {@inheritDoc} */ + @Override public double readDouble() throws IOException { + return ois.readDouble(); + } + + /** {@inheritDoc} */ + @Override public String readLine() throws IOException { + return ois.readLine(); + } + + /** {@inheritDoc} */ + @NotNull @Override public String readUTF() throws IOException { + return ois.readUTF(); + } +} diff --git a/modules/core/src/main/java/org/apache/ignite/internal/dto/IgniteDataTransferObjectOutput.java b/modules/core/src/main/java/org/apache/ignite/internal/dto/IgniteDataTransferObjectOutput.java new file mode 100644 index 0000000000000..db4933cea29c0 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/dto/IgniteDataTransferObjectOutput.java @@ -0,0 +1,141 @@ +/* + * 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.ignite.internal.dto; + +import java.io.IOException; +import java.io.ObjectOutput; +import java.io.ObjectOutputStream; +import org.apache.ignite.internal.util.io.GridByteArrayOutputStream; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.jetbrains.annotations.NotNull; + +/** + * Wrapper for object output. + */ +public class IgniteDataTransferObjectOutput implements ObjectOutput { + /** */ + private final ObjectOutput out; + + /** */ + private final GridByteArrayOutputStream bos; + + /** */ + private final ObjectOutputStream oos; + + /** + * Constructor. + * + * @param out Target stream. + * @throws IOException If an I/O error occurs. + */ + public IgniteDataTransferObjectOutput(ObjectOutput out) throws IOException { + this.out = out; + + bos = new GridByteArrayOutputStream(); + oos = new ObjectOutputStream(bos); + } + + /** {@inheritDoc} */ + @Override public void writeObject(Object obj) throws IOException { + oos.writeObject(obj); + } + + /** {@inheritDoc} */ + @Override public void write(int b) throws IOException { + oos.write(b); + } + + /** {@inheritDoc} */ + @Override public void write(byte[] b) throws IOException { + oos.write(b); + } + + /** {@inheritDoc} */ + @Override public void write(byte[] b, int off, int len) throws IOException { + oos.write(b, off, len); + } + + /** {@inheritDoc} */ + @Override public void writeBoolean(boolean v) throws IOException { + oos.writeBoolean(v); + } + + /** {@inheritDoc} */ + @Override public void writeByte(int v) throws IOException { + oos.writeByte(v); + } + + /** {@inheritDoc} */ + @Override public void writeShort(int v) throws IOException { + oos.writeShort(v); + } + + /** {@inheritDoc} */ + @Override public void writeChar(int v) throws IOException { + oos.writeChar(v); + } + + /** {@inheritDoc} */ + @Override public void writeInt(int v) throws IOException { + oos.writeInt(v); + } + + /** {@inheritDoc} */ + @Override public void writeLong(long v) throws IOException { + oos.writeLong(v); + } + + /** {@inheritDoc} */ + @Override public void writeFloat(float v) throws IOException { + oos.writeFloat(v); + } + + /** {@inheritDoc} */ + @Override public void writeDouble(double v) throws IOException { + oos.writeDouble(v); + } + + /** {@inheritDoc} */ + @Override public void writeBytes(@NotNull String s) throws IOException { + oos.writeBytes(s); + } + + /** {@inheritDoc} */ + @Override public void writeChars(@NotNull String s) throws IOException { + oos.writeChars(s); + } + + /** {@inheritDoc} */ + @Override public void writeUTF(@NotNull String s) throws IOException { + oos.writeUTF(s); + } + + /** {@inheritDoc} */ + @Override public void flush() throws IOException { + oos.flush(); + } + + /** {@inheritDoc} */ + @Override public void close() throws IOException { + oos.flush(); + + U.writeByteArray(out, bos.internalArray(), bos.size()); + + oos.close(); + } +} diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObject.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObject.java index 38d7a0ad0aef5..27a5cc6a0cec3 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObject.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObject.java @@ -17,108 +17,14 @@ package org.apache.ignite.internal.visor; -import java.io.Externalizable; -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.ArrayList; -import java.util.Collection; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; -import org.jetbrains.annotations.Nullable; +import org.apache.ignite.internal.dto.IgniteDataTransferObject; /** - * Base class for data transfer objects. + * Base class for data transfer objects for Visor tasks. + * + * @deprecated Use {@link IgniteDataTransferObject} instead. This class may be removed in Ignite 3.0. */ -public abstract class VisorDataTransferObject implements Externalizable { +public abstract class VisorDataTransferObject extends IgniteDataTransferObject { /** */ private static final long serialVersionUID = 6920203681702514010L; - - /** Magic number to detect correct transfer objects. */ - private static final int MAGIC = 0x42BEEF00; - - /** Version 1. */ - protected static final byte V1 = 1; - - /** Version 2. */ - protected static final byte V2 = 2; - - /** Version 3. */ - protected static final byte V3 = 3; - - /** - * @param col Source collection. - * @param Collection type. - * @return List based on passed collection. - */ - @Nullable protected static List toList(Collection col) { - if (col != null) - return new ArrayList<>(col); - - return null; - } - - /** - * @param col Source collection. - * @param Collection type. - * @return List based on passed collection. - */ - @Nullable protected static Set toSet(Collection col) { - if (col != null) - return new LinkedHashSet<>(col); - - return null; - } - - /** - * @return Transfer object version. - */ - public byte getProtocolVersion() { - return V1; - } - - /** - * Save object's specific data content. - * - * @param out Output object to write data content. - * @throws IOException If I/O errors occur. - */ - protected abstract void writeExternalData(ObjectOutput out) throws IOException; - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - int hdr = MAGIC + getProtocolVersion(); - - out.writeInt(hdr); - - try (VisorDataTransferObjectOutput dtout = new VisorDataTransferObjectOutput(out)) { - writeExternalData(dtout); - } - } - - /** - * Load object's specific data content. - * - * @param protoVer Input object version. - * @param in Input object to load data content. - * @throws IOException If I/O errors occur. - * @throws ClassNotFoundException If the class for an object being restored cannot be found. - */ - protected abstract void readExternalData(byte protoVer, ObjectInput in) throws IOException, ClassNotFoundException; - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - int hdr = in.readInt(); - - if ((hdr & MAGIC) != MAGIC) - throw new IOException("Unexpected VisorDataTransferObject header " + - "[actual=" + Integer.toHexString(hdr) + ", expected=" + Integer.toHexString(MAGIC) + "]"); - - byte ver = (byte)(hdr & 0xFF); - - try (VisorDataTransferObjectInput dtin = new VisorDataTransferObjectInput(in)) { - readExternalData(ver, dtin); - } - } } diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectInput.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectInput.java index 16e933090eb9b..d07c44e8bdb63 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectInput.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectInput.java @@ -19,138 +19,18 @@ import java.io.IOException; import java.io.ObjectInput; -import java.io.ObjectInputStream; -import org.apache.ignite.internal.util.io.GridByteArrayInputStream; -import org.apache.ignite.internal.util.typedef.internal.U; -import org.jetbrains.annotations.NotNull; +import org.apache.ignite.internal.dto.IgniteDataTransferObjectInput; /** * Wrapper for object input. + * @deprecated Use {@link IgniteDataTransferObjectInput} instead. This class may be removed in Ignite 3.0. */ -public class VisorDataTransferObjectInput implements ObjectInput { - /** */ - private final ObjectInputStream ois; - +public class VisorDataTransferObjectInput extends IgniteDataTransferObjectInput { /** * @param in Target input. * @throws IOException If an I/O error occurs. */ public VisorDataTransferObjectInput(ObjectInput in) throws IOException { - byte[] buf = U.readByteArray(in); - - /* */ - GridByteArrayInputStream bis = new GridByteArrayInputStream(buf); - ois = new ObjectInputStream(bis); - } - - - /** {@inheritDoc} */ - @Override public Object readObject() throws ClassNotFoundException, IOException { - return ois.readObject(); - } - - /** {@inheritDoc} */ - @Override public int read() throws IOException { - return ois.read(); - } - - /** {@inheritDoc} */ - @Override public int read(byte[] b) throws IOException { - return ois.read(b); - } - - /** {@inheritDoc} */ - @Override public int read(byte[] b, int off, int len) throws IOException { - return ois.read(b, off, len); - } - - /** {@inheritDoc} */ - @Override public long skip(long n) throws IOException { - return ois.skip(n); - } - - /** {@inheritDoc} */ - @Override public int available() throws IOException { - return ois.available(); - } - - /** {@inheritDoc} */ - @Override public void close() throws IOException { - ois.close(); - } - - /** {@inheritDoc} */ - @Override public void readFully(@NotNull byte[] b) throws IOException { - ois.readFully(b); - } - - /** {@inheritDoc} */ - @Override public void readFully(@NotNull byte[] b, int off, int len) throws IOException { - ois.readFully(b, off, len); - } - - /** {@inheritDoc} */ - @Override public int skipBytes(int n) throws IOException { - return ois.skipBytes(n); - } - - /** {@inheritDoc} */ - @Override public boolean readBoolean() throws IOException { - return ois.readBoolean(); - } - - /** {@inheritDoc} */ - @Override public byte readByte() throws IOException { - return ois.readByte(); - } - - /** {@inheritDoc} */ - @Override public int readUnsignedByte() throws IOException { - return ois.readUnsignedByte(); - } - - /** {@inheritDoc} */ - @Override public short readShort() throws IOException { - return ois.readShort(); - } - - /** {@inheritDoc} */ - @Override public int readUnsignedShort() throws IOException { - return ois.readUnsignedShort(); - } - - /** {@inheritDoc} */ - @Override public char readChar() throws IOException { - return ois.readChar(); - } - - /** {@inheritDoc} */ - @Override public int readInt() throws IOException { - return ois.readInt(); - } - - /** {@inheritDoc} */ - @Override public long readLong() throws IOException { - return ois.readLong(); - } - - /** {@inheritDoc} */ - @Override public float readFloat() throws IOException { - return ois.readFloat(); - } - - /** {@inheritDoc} */ - @Override public double readDouble() throws IOException { - return ois.readDouble(); - } - - /** {@inheritDoc} */ - @Override public String readLine() throws IOException { - return ois.readLine(); - } - - /** {@inheritDoc} */ - @NotNull @Override public String readUTF() throws IOException { - return ois.readUTF(); + super(in); } } diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectOutput.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectOutput.java index 7fa772e75ca13..e7623345c52a9 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectOutput.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectOutput.java @@ -19,24 +19,13 @@ import java.io.IOException; import java.io.ObjectOutput; -import java.io.ObjectOutputStream; -import org.apache.ignite.internal.util.io.GridByteArrayOutputStream; -import org.apache.ignite.internal.util.typedef.internal.U; -import org.jetbrains.annotations.NotNull; +import org.apache.ignite.internal.dto.IgniteDataTransferObjectOutput; /** * Wrapper for object output. + * @deprecated Use {@link IgniteDataTransferObjectOutput} instead. This class may be removed in Ignite 3.0. */ -public class VisorDataTransferObjectOutput implements ObjectOutput { - /** */ - private final ObjectOutput out; - - /** */ - private final GridByteArrayOutputStream bos; - - /** */ - private final ObjectOutputStream oos; - +public class VisorDataTransferObjectOutput extends IgniteDataTransferObjectOutput { /** * Constructor. * @@ -44,98 +33,6 @@ public class VisorDataTransferObjectOutput implements ObjectOutput { * @throws IOException If an I/O error occurs. */ public VisorDataTransferObjectOutput(ObjectOutput out) throws IOException { - this.out = out; - - bos = new GridByteArrayOutputStream(); - oos = new ObjectOutputStream(bos); - } - - /** {@inheritDoc} */ - @Override public void writeObject(Object obj) throws IOException { - oos.writeObject(obj); - } - - /** {@inheritDoc} */ - @Override public void write(int b) throws IOException { - oos.write(b); - } - - /** {@inheritDoc} */ - @Override public void write(byte[] b) throws IOException { - oos.write(b); - } - - /** {@inheritDoc} */ - @Override public void write(byte[] b, int off, int len) throws IOException { - oos.write(b, off, len); - } - - /** {@inheritDoc} */ - @Override public void writeBoolean(boolean v) throws IOException { - oos.writeBoolean(v); - } - - /** {@inheritDoc} */ - @Override public void writeByte(int v) throws IOException { - oos.writeByte(v); - } - - /** {@inheritDoc} */ - @Override public void writeShort(int v) throws IOException { - oos.writeShort(v); - } - - /** {@inheritDoc} */ - @Override public void writeChar(int v) throws IOException { - oos.writeChar(v); - } - - /** {@inheritDoc} */ - @Override public void writeInt(int v) throws IOException { - oos.writeInt(v); - } - - /** {@inheritDoc} */ - @Override public void writeLong(long v) throws IOException { - oos.writeLong(v); - } - - /** {@inheritDoc} */ - @Override public void writeFloat(float v) throws IOException { - oos.writeFloat(v); - } - - /** {@inheritDoc} */ - @Override public void writeDouble(double v) throws IOException { - oos.writeDouble(v); - } - - /** {@inheritDoc} */ - @Override public void writeBytes(@NotNull String s) throws IOException { - oos.writeBytes(s); - } - - /** {@inheritDoc} */ - @Override public void writeChars(@NotNull String s) throws IOException { - oos.writeChars(s); - } - - /** {@inheritDoc} */ - @Override public void writeUTF(@NotNull String s) throws IOException { - oos.writeUTF(s); - } - - /** {@inheritDoc} */ - @Override public void flush() throws IOException { - oos.flush(); - } - - /** {@inheritDoc} */ - @Override public void close() throws IOException { - oos.flush(); - - U.writeByteArray(out, bos.internalArray(), bos.size()); - - oos.close(); + super(out); } } From 066f975479315cba8bbf94cc157bf7a6b4700c19 Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Thu, 11 Oct 2018 22:31:46 +0700 Subject: [PATCH 2/3] GG-14271 WIP. --- .../visor/VisorDataTransferObject.java | 99 +++++++++++++- .../visor/VisorDataTransferObjectInput.java | 126 +++++++++++++++++- .../visor/VisorDataTransferObjectOutput.java | 109 ++++++++++++++- .../visor/debug/VisorThreadMonitorInfo.java | 2 +- .../visor/event/VisorGridDeploymentEvent.java | 2 +- .../visor/event/VisorGridDiscoveryEvent.java | 2 +- .../visor/event/VisorGridJobEvent.java | 2 +- .../visor/event/VisorGridTaskEvent.java | 2 +- 8 files changed, 334 insertions(+), 10 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObject.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObject.java index 27a5cc6a0cec3..6ba3aa7d5aed5 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObject.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObject.java @@ -17,14 +17,111 @@ package org.apache.ignite.internal.visor; +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; import org.apache.ignite.internal.dto.IgniteDataTransferObject; +import org.jetbrains.annotations.Nullable; /** * Base class for data transfer objects for Visor tasks. * * @deprecated Use {@link IgniteDataTransferObject} instead. This class may be removed in Ignite 3.0. */ -public abstract class VisorDataTransferObject extends IgniteDataTransferObject { +public abstract class VisorDataTransferObject implements Externalizable { /** */ private static final long serialVersionUID = 6920203681702514010L; + + /** Magic number to detect correct transfer objects. */ + private static final int MAGIC = 0x42BEEF00; + + /** Version 1. */ + protected static final byte V1 = 1; + + /** Version 2. */ + protected static final byte V2 = 2; + + /** Version 3. */ + protected static final byte V3 = 3; + + /** + * @param col Source collection. + * @param Collection type. + * @return List based on passed collection. + */ + @Nullable protected static List toList(Collection col) { + if (col != null) + return new ArrayList<>(col); + + return null; + } + + /** + * @param col Source collection. + * @param Collection type. + * @return List based on passed collection. + */ + @Nullable protected static Set toSet(Collection col) { + if (col != null) + return new LinkedHashSet<>(col); + + return null; + } + + /** + * @return Transfer object version. + */ + public byte getProtocolVersion() { + return V1; + } + + /** + * Save object's specific data content. + * + * @param out Output object to write data content. + * @throws IOException If I/O errors occur. + */ + protected abstract void writeExternalData(ObjectOutput out) throws IOException; + + /** {@inheritDoc} */ + @Override public void writeExternal(ObjectOutput out) throws IOException { + int hdr = MAGIC + getProtocolVersion(); + + out.writeInt(hdr); + + try (VisorDataTransferObjectOutput dtout = new VisorDataTransferObjectOutput(out)) { + writeExternalData(dtout); + } + } + + /** + * Load object's specific data content. + * + * @param protoVer Input object version. + * @param in Input object to load data content. + * @throws IOException If I/O errors occur. + * @throws ClassNotFoundException If the class for an object being restored cannot be found. + */ + protected abstract void readExternalData(byte protoVer, ObjectInput in) throws IOException, ClassNotFoundException; + + /** {@inheritDoc} */ + @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + int hdr = in.readInt(); + + if ((hdr & MAGIC) != MAGIC) + throw new IOException("Unexpected VisorDataTransferObject header " + + "[actual=" + Integer.toHexString(hdr) + ", expected=" + Integer.toHexString(MAGIC) + "]"); + + byte ver = (byte)(hdr & 0xFF); + + try (VisorDataTransferObjectInput dtin = new VisorDataTransferObjectInput(in)) { + readExternalData(ver, dtin); + } + } } diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectInput.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectInput.java index d07c44e8bdb63..7caf26914ba5e 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectInput.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectInput.java @@ -19,18 +19,140 @@ import java.io.IOException; import java.io.ObjectInput; +import java.io.ObjectInputStream; import org.apache.ignite.internal.dto.IgniteDataTransferObjectInput; +import org.apache.ignite.internal.util.io.GridByteArrayInputStream; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.jetbrains.annotations.NotNull; /** * Wrapper for object input. * @deprecated Use {@link IgniteDataTransferObjectInput} instead. This class may be removed in Ignite 3.0. */ -public class VisorDataTransferObjectInput extends IgniteDataTransferObjectInput { +public class VisorDataTransferObjectInput implements ObjectInput { + /** */ + private final ObjectInputStream ois; + /** * @param in Target input. * @throws IOException If an I/O error occurs. */ public VisorDataTransferObjectInput(ObjectInput in) throws IOException { - super(in); + byte[] buf = U.readByteArray(in); + + /* */ + GridByteArrayInputStream bis = new GridByteArrayInputStream(buf); + ois = new ObjectInputStream(bis); + } + + + /** {@inheritDoc} */ + @Override public Object readObject() throws ClassNotFoundException, IOException { + return ois.readObject(); + } + + /** {@inheritDoc} */ + @Override public int read() throws IOException { + return ois.read(); + } + + /** {@inheritDoc} */ + @Override public int read(byte[] b) throws IOException { + return ois.read(b); + } + + /** {@inheritDoc} */ + @Override public int read(byte[] b, int off, int len) throws IOException { + return ois.read(b, off, len); + } + + /** {@inheritDoc} */ + @Override public long skip(long n) throws IOException { + return ois.skip(n); + } + + /** {@inheritDoc} */ + @Override public int available() throws IOException { + return ois.available(); + } + + /** {@inheritDoc} */ + @Override public void close() throws IOException { + ois.close(); + } + + /** {@inheritDoc} */ + @Override public void readFully(@NotNull byte[] b) throws IOException { + ois.readFully(b); + } + + /** {@inheritDoc} */ + @Override public void readFully(@NotNull byte[] b, int off, int len) throws IOException { + ois.readFully(b, off, len); + } + + /** {@inheritDoc} */ + @Override public int skipBytes(int n) throws IOException { + return ois.skipBytes(n); + } + + /** {@inheritDoc} */ + @Override public boolean readBoolean() throws IOException { + return ois.readBoolean(); + } + + /** {@inheritDoc} */ + @Override public byte readByte() throws IOException { + return ois.readByte(); + } + + /** {@inheritDoc} */ + @Override public int readUnsignedByte() throws IOException { + return ois.readUnsignedByte(); + } + + /** {@inheritDoc} */ + @Override public short readShort() throws IOException { + return ois.readShort(); + } + + /** {@inheritDoc} */ + @Override public int readUnsignedShort() throws IOException { + return ois.readUnsignedShort(); + } + + /** {@inheritDoc} */ + @Override public char readChar() throws IOException { + return ois.readChar(); + } + + /** {@inheritDoc} */ + @Override public int readInt() throws IOException { + return ois.readInt(); + } + + /** {@inheritDoc} */ + @Override public long readLong() throws IOException { + return ois.readLong(); + } + + /** {@inheritDoc} */ + @Override public float readFloat() throws IOException { + return ois.readFloat(); + } + + /** {@inheritDoc} */ + @Override public double readDouble() throws IOException { + return ois.readDouble(); + } + + /** {@inheritDoc} */ + @Override public String readLine() throws IOException { + return ois.readLine(); + } + + /** {@inheritDoc} */ + @NotNull @Override public String readUTF() throws IOException { + return ois.readUTF(); } } diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectOutput.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectOutput.java index e7623345c52a9..4dbd25035bac9 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectOutput.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/VisorDataTransferObjectOutput.java @@ -19,13 +19,26 @@ import java.io.IOException; import java.io.ObjectOutput; +import java.io.ObjectOutputStream; import org.apache.ignite.internal.dto.IgniteDataTransferObjectOutput; +import org.apache.ignite.internal.util.io.GridByteArrayOutputStream; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.jetbrains.annotations.NotNull; /** * Wrapper for object output. * @deprecated Use {@link IgniteDataTransferObjectOutput} instead. This class may be removed in Ignite 3.0. */ -public class VisorDataTransferObjectOutput extends IgniteDataTransferObjectOutput { +public class VisorDataTransferObjectOutput implements ObjectOutput { + /** */ + private final ObjectOutput out; + + /** */ + private final GridByteArrayOutputStream bos; + + /** */ + private final ObjectOutputStream oos; + /** * Constructor. * @@ -33,6 +46,98 @@ public class VisorDataTransferObjectOutput extends IgniteDataTransferObjectOutpu * @throws IOException If an I/O error occurs. */ public VisorDataTransferObjectOutput(ObjectOutput out) throws IOException { - super(out); + this.out = out; + + bos = new GridByteArrayOutputStream(); + oos = new ObjectOutputStream(bos); + } + + /** {@inheritDoc} */ + @Override public void writeObject(Object obj) throws IOException { + oos.writeObject(obj); + } + + /** {@inheritDoc} */ + @Override public void write(int b) throws IOException { + oos.write(b); + } + + /** {@inheritDoc} */ + @Override public void write(byte[] b) throws IOException { + oos.write(b); + } + + /** {@inheritDoc} */ + @Override public void write(byte[] b, int off, int len) throws IOException { + oos.write(b, off, len); + } + + /** {@inheritDoc} */ + @Override public void writeBoolean(boolean v) throws IOException { + oos.writeBoolean(v); + } + + /** {@inheritDoc} */ + @Override public void writeByte(int v) throws IOException { + oos.writeByte(v); + } + + /** {@inheritDoc} */ + @Override public void writeShort(int v) throws IOException { + oos.writeShort(v); + } + + /** {@inheritDoc} */ + @Override public void writeChar(int v) throws IOException { + oos.writeChar(v); + } + + /** {@inheritDoc} */ + @Override public void writeInt(int v) throws IOException { + oos.writeInt(v); + } + + /** {@inheritDoc} */ + @Override public void writeLong(long v) throws IOException { + oos.writeLong(v); + } + + /** {@inheritDoc} */ + @Override public void writeFloat(float v) throws IOException { + oos.writeFloat(v); + } + + /** {@inheritDoc} */ + @Override public void writeDouble(double v) throws IOException { + oos.writeDouble(v); + } + + /** {@inheritDoc} */ + @Override public void writeBytes(@NotNull String s) throws IOException { + oos.writeBytes(s); + } + + /** {@inheritDoc} */ + @Override public void writeChars(@NotNull String s) throws IOException { + oos.writeChars(s); + } + + /** {@inheritDoc} */ + @Override public void writeUTF(@NotNull String s) throws IOException { + oos.writeUTF(s); + } + + /** {@inheritDoc} */ + @Override public void flush() throws IOException { + oos.flush(); + } + + /** {@inheritDoc} */ + @Override public void close() throws IOException { + oos.flush(); + + U.writeByteArray(out, bos.internalArray(), bos.size()); + + oos.close(); } } diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/debug/VisorThreadMonitorInfo.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/debug/VisorThreadMonitorInfo.java index f8ddc68a31e3a..5b9a11fc521df 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/debug/VisorThreadMonitorInfo.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/debug/VisorThreadMonitorInfo.java @@ -73,7 +73,7 @@ public StackTraceElement getStackFrame() { /** {@inheritDoc} */ @Override public byte getProtocolVersion() { - return 1; + return V1; } /** {@inheritDoc} */ diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDeploymentEvent.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDeploymentEvent.java index 8b0c2110368b9..55185f6881d23 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDeploymentEvent.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDeploymentEvent.java @@ -81,7 +81,7 @@ public String getAlias() { /** {@inheritDoc} */ @Override public byte getProtocolVersion() { - return 1; + return V1; } /** {@inheritDoc} */ diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDiscoveryEvent.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDiscoveryEvent.java index ec2220d1fd7ba..6adf19a8222bc 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDiscoveryEvent.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDiscoveryEvent.java @@ -121,7 +121,7 @@ public long getTopologyVersion() { /** {@inheritDoc} */ @Override public byte getProtocolVersion() { - return 1; + return V1; } /** {@inheritDoc} */ diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridJobEvent.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridJobEvent.java index 734d85fa11cb7..858aee5461ac4 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridJobEvent.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridJobEvent.java @@ -120,7 +120,7 @@ public IgniteUuid getJobId() { /** {@inheritDoc} */ @Override public byte getProtocolVersion() { - return 1; + return V1; } /** {@inheritDoc} */ diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridTaskEvent.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridTaskEvent.java index 11c9a17d38dd5..47063131d1d94 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridTaskEvent.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridTaskEvent.java @@ -120,7 +120,7 @@ public boolean isInternal() { /** {@inheritDoc} */ @Override public byte getProtocolVersion() { - return 1; + return V1; } /** {@inheritDoc} */ From b9e2dff61b9764f55ebfd755fd451e01ba5b3630 Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Thu, 11 Oct 2018 22:46:47 +0700 Subject: [PATCH 3/3] GG-14271 WIP. --- .../ignite/internal/visor/debug/VisorThreadMonitorInfo.java | 5 ----- .../internal/visor/event/VisorGridDeploymentEvent.java | 5 ----- .../ignite/internal/visor/event/VisorGridDiscoveryEvent.java | 5 ----- .../ignite/internal/visor/event/VisorGridJobEvent.java | 5 ----- .../ignite/internal/visor/event/VisorGridTaskEvent.java | 5 ----- 5 files changed, 25 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/debug/VisorThreadMonitorInfo.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/debug/VisorThreadMonitorInfo.java index 5b9a11fc521df..189b8e3cdfba4 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/debug/VisorThreadMonitorInfo.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/debug/VisorThreadMonitorInfo.java @@ -71,11 +71,6 @@ public StackTraceElement getStackFrame() { return stackFrame; } - /** {@inheritDoc} */ - @Override public byte getProtocolVersion() { - return V1; - } - /** {@inheritDoc} */ @Override protected void writeExternalData(ObjectOutput out) throws IOException { try (VisorDataTransferObjectOutput dtout = new VisorDataTransferObjectOutput(out)) { diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDeploymentEvent.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDeploymentEvent.java index 55185f6881d23..e74b54c896c34 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDeploymentEvent.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDeploymentEvent.java @@ -79,11 +79,6 @@ public String getAlias() { return alias; } - /** {@inheritDoc} */ - @Override public byte getProtocolVersion() { - return V1; - } - /** {@inheritDoc} */ @Override protected void writeExternalData(ObjectOutput out) throws IOException { try (VisorDataTransferObjectOutput dtout = new VisorDataTransferObjectOutput(out)) { diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDiscoveryEvent.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDiscoveryEvent.java index 6adf19a8222bc..c5f1d30c53d92 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDiscoveryEvent.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridDiscoveryEvent.java @@ -119,11 +119,6 @@ public long getTopologyVersion() { return topVer; } - /** {@inheritDoc} */ - @Override public byte getProtocolVersion() { - return V1; - } - /** {@inheritDoc} */ @Override protected void writeExternalData(ObjectOutput out) throws IOException { try (VisorDataTransferObjectOutput dtout = new VisorDataTransferObjectOutput(out)) { diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridJobEvent.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridJobEvent.java index 858aee5461ac4..2a7ea1862e396 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridJobEvent.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridJobEvent.java @@ -118,11 +118,6 @@ public IgniteUuid getJobId() { return jobId; } - /** {@inheritDoc} */ - @Override public byte getProtocolVersion() { - return V1; - } - /** {@inheritDoc} */ @Override protected void writeExternalData(ObjectOutput out) throws IOException { try (VisorDataTransferObjectOutput dtout = new VisorDataTransferObjectOutput(out)) { diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridTaskEvent.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridTaskEvent.java index 47063131d1d94..a0836c481356f 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridTaskEvent.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/event/VisorGridTaskEvent.java @@ -118,11 +118,6 @@ public boolean isInternal() { return internal; } - /** {@inheritDoc} */ - @Override public byte getProtocolVersion() { - return V1; - } - /** {@inheritDoc} */ @Override protected void writeExternalData(ObjectOutput out) throws IOException { try (VisorDataTransferObjectOutput dtout = new VisorDataTransferObjectOutput(out)) {