Skip to content

Commit b38fc88

Browse files
committed
WIP on jdk-cluster-codec and its externalizable dto implementaions
1 parent 5229d8d commit b38fc88

File tree

11 files changed

+234
-21
lines changed

11 files changed

+234
-21
lines changed

cluster-api/src/main/java/io/scalecube/cluster/Member.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
package io.scalecube.cluster;
22

33
import io.scalecube.net.Address;
4+
import java.io.Externalizable;
5+
import java.io.IOException;
6+
import java.io.ObjectInput;
7+
import java.io.ObjectOutput;
48
import java.util.Objects;
59
import java.util.UUID;
610

711
/**
812
* Cluster member which represents node in the cluster and contains its id and address. This class
913
* is essentially immutable.
1014
*/
11-
public final class Member {
15+
public final class Member implements Externalizable {
16+
17+
private static final long serialVersionUID = 1L;
1218

1319
private String id;
1420
private String alias;
1521
private Address address;
1622

17-
/** Instantiates empty member for deserialization purpose. */
18-
Member() {}
23+
public Member() {}
1924

2025
/**
2126
* Constructor.
@@ -63,6 +68,16 @@ public int hashCode() {
6368
return Objects.hash(id, address);
6469
}
6570

71+
@Override
72+
public void writeExternal(ObjectOutput out) throws IOException {
73+
// todo
74+
}
75+
76+
@Override
77+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
78+
// todo
79+
}
80+
6681
@Override
6782
public String toString() {
6883
if (alias == null) {

cluster/src/main/java/io/scalecube/cluster/fdetector/PingData.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package io.scalecube.cluster.fdetector;
22

33
import io.scalecube.cluster.Member;
4+
import java.io.Externalizable;
5+
import java.io.IOException;
6+
import java.io.ObjectInput;
7+
import java.io.ObjectOutput;
48
import java.util.StringJoiner;
59

610
/** DTO class. Supports FailureDetector messages (Ping, Ack, PingReq). */
7-
final class PingData {
11+
final class PingData implements Externalizable {
12+
13+
private static final long serialVersionUID = 1L;
814

915
enum AckType {
1016

@@ -32,8 +38,7 @@ enum AckType {
3238
/** Ping response type. */
3339
private AckType ackType;
3440

35-
/** Instantiates empty ping data for deserialization purpose. */
36-
PingData() {}
41+
public PingData() {}
3742

3843
private PingData(PingData other) {
3944
this.from = other.from;
@@ -78,6 +83,16 @@ public PingData withAckType(AckType ackType) {
7883
return p;
7984
}
8085

86+
@Override
87+
public void writeExternal(ObjectOutput out) throws IOException {
88+
// todo
89+
}
90+
91+
@Override
92+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
93+
// todo
94+
}
95+
8196
@Override
8297
public String toString() {
8398
return new StringJoiner(", ", PingData.class.getSimpleName() + "[", "]")

cluster/src/main/java/io/scalecube/cluster/gossip/Gossip.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
package io.scalecube.cluster.gossip;
22

33
import io.scalecube.cluster.transport.api.Message;
4+
import java.io.Externalizable;
5+
import java.io.IOException;
6+
import java.io.ObjectInput;
7+
import java.io.ObjectOutput;
48
import java.util.Objects;
59
import java.util.StringJoiner;
610

711
/** Data model for gossip, include gossip id, qualifier and object need to disseminate. */
8-
final class Gossip {
12+
final class Gossip implements Externalizable {
13+
14+
private static final long serialVersionUID = 1L;
915

1016
private String gossiperId;
1117
private Message message;
1218
// incremented counter
1319
private long sequenceId;
1420

15-
/** Instantiates empty gossip for deserialization purpose. */
16-
Gossip() {}
21+
public Gossip() {}
1722

1823
public Gossip(String gossiperId, Message message, long sequenceId) {
1924
this.gossiperId = Objects.requireNonNull(gossiperId);
@@ -56,6 +61,16 @@ public int hashCode() {
5661
return Objects.hash(gossiperId, message, sequenceId);
5762
}
5863

64+
@Override
65+
public void writeExternal(ObjectOutput out) throws IOException {
66+
// todo
67+
}
68+
69+
@Override
70+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
71+
// todo
72+
}
73+
5974
@Override
6075
public String toString() {
6176
return new StringJoiner(", ", Gossip.class.getSimpleName() + "[", "]")

cluster/src/main/java/io/scalecube/cluster/gossip/GossipRequest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package io.scalecube.cluster.gossip;
22

3+
import java.io.Externalizable;
4+
import java.io.IOException;
5+
import java.io.ObjectInput;
6+
import java.io.ObjectOutput;
37
import java.util.ArrayList;
48
import java.util.Collections;
59
import java.util.List;
610
import java.util.StringJoiner;
711

812
/** Gossip request which be transmitted through the network, contains list of gossips. */
9-
final class GossipRequest {
13+
final class GossipRequest implements Externalizable {
14+
15+
private static final long serialVersionUID = 1L;
1016

1117
private List<Gossip> gossips;
1218
private String from;
1319

14-
/** Instantiates empty gossip request for deserialization purpose. */
15-
GossipRequest() {}
20+
public GossipRequest() {}
1621

1722
public GossipRequest(Gossip gossip, String from) {
1823
this(Collections.singletonList(gossip), from);
@@ -31,6 +36,16 @@ public String from() {
3136
return from;
3237
}
3338

39+
@Override
40+
public void writeExternal(ObjectOutput out) throws IOException {
41+
// todo
42+
}
43+
44+
@Override
45+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
46+
// todo
47+
}
48+
3449
@Override
3550
public String toString() {
3651
return new StringJoiner(", ", GossipRequest.class.getSimpleName() + "[", "]")

cluster/src/main/java/io/scalecube/cluster/membership/MembershipRecord.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@
66
import static io.scalecube.cluster.membership.MemberStatus.SUSPECT;
77

88
import io.scalecube.cluster.Member;
9+
import java.io.Externalizable;
10+
import java.io.IOException;
11+
import java.io.ObjectInput;
12+
import java.io.ObjectOutput;
913
import java.util.Objects;
1014

1115
/** Cluster membership record which represents member, status, and incarnation. */
12-
final class MembershipRecord {
16+
final class MembershipRecord implements Externalizable {
17+
18+
private static final long serialVersionUID = 1L;
1319

1420
private Member member;
1521
private MemberStatus status;
1622
private int incarnation;
1723

18-
/** Instantiates empty membership record for deserialization purpose. */
19-
MembershipRecord() {}
24+
public MembershipRecord() {}
2025

2126
/** Instantiates new instance of membership record with given member, status and incarnation. */
2227
public MembershipRecord(Member member, MemberStatus status, int incarnation) {
@@ -101,6 +106,16 @@ public int hashCode() {
101106
return Objects.hash(member, status, incarnation);
102107
}
103108

109+
@Override
110+
public void writeExternal(ObjectOutput out) throws IOException {
111+
// todo
112+
}
113+
114+
@Override
115+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
116+
// todo
117+
}
118+
104119
@Override
105120
public String toString() {
106121
return "{m: " + member + ", s: " + status + ", inc: " + incarnation + '}';

cluster/src/main/java/io/scalecube/cluster/membership/SyncData.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.scalecube.cluster.membership;
22

3+
import java.io.Externalizable;
4+
import java.io.IOException;
5+
import java.io.ObjectInput;
6+
import java.io.ObjectOutput;
37
import java.util.ArrayList;
48
import java.util.Collection;
59
import java.util.List;
@@ -9,7 +13,9 @@
913
* A class containing full membership table from specific member and used full synchronization
1014
* between cluster members.
1115
*/
12-
final class SyncData {
16+
final class SyncData implements Externalizable {
17+
18+
private static final long serialVersionUID = 1L;
1319

1420
/** Full cluster membership table. */
1521
private List<MembershipRecord> membership;
@@ -19,8 +25,7 @@ final class SyncData {
1925
*/
2026
private String syncGroup;
2127

22-
/** Instantiates empty sync data for deserialization purpose. */
23-
SyncData() {}
28+
public SyncData() {}
2429

2530
public SyncData(Collection<MembershipRecord> membership, String syncGroup) {
2631
this.membership = new ArrayList<>(membership);
@@ -35,6 +40,16 @@ public String getSyncGroup() {
3540
return syncGroup;
3641
}
3742

43+
@Override
44+
public void writeExternal(ObjectOutput out) throws IOException {
45+
// todo
46+
}
47+
48+
@Override
49+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
50+
// todo
51+
}
52+
3853
@Override
3954
public String toString() {
4055
return new StringJoiner(", ", SyncData.class.getSimpleName() + "[", "]")

codec-parent/codec-jdk/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>io.scalecube</groupId>
9+
<artifactId>scalecube-codec-parent</artifactId>
10+
<version>2.4.10-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>codec-jdk</artifactId>
14+
15+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.scalecube.cluster.codec.jdk;
2+
3+
import io.scalecube.cluster.transport.api.Message;
4+
import io.scalecube.cluster.transport.api.MessageCodec;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.ObjectInputStream;
8+
import java.io.ObjectOutputStream;
9+
import java.io.OutputStream;
10+
11+
public class JdkMessageCodec implements MessageCodec {
12+
13+
@Override
14+
public Message deserialize(InputStream is) throws IOException, ClassNotFoundException {
15+
Message message = new Message();
16+
try (ObjectInputStream inputStream = new ObjectInputStream(is)) {
17+
message.readExternal(inputStream);
18+
return message;
19+
}
20+
}
21+
22+
@Override
23+
public void serialize(Message message, OutputStream os) throws IOException {
24+
try (ObjectOutputStream outputStream = new ObjectOutputStream(os)) {
25+
message.writeExternal(outputStream);
26+
outputStream.flush();
27+
}
28+
}
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.scalecube.cluster.codec.jdk;
2+
3+
import io.scalecube.cluster.metadata.MetadataCodec;
4+
import java.io.ByteArrayInputStream;
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.ObjectInputStream;
7+
import java.io.ObjectOutputStream;
8+
import java.nio.ByteBuffer;
9+
import reactor.core.Exceptions;
10+
11+
public class JdkMetadataCodec implements MetadataCodec {
12+
13+
@Override
14+
public Object deserialize(ByteBuffer buffer) {
15+
byte[] bytes = buffer.array();
16+
try (ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
17+
return is.readObject();
18+
} catch (Exception e) {
19+
throw Exceptions.propagate(e);
20+
}
21+
}
22+
23+
@Override
24+
public ByteBuffer serialize(Object metadata) {
25+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
26+
try (ObjectOutputStream os = new ObjectOutputStream(baos)) {
27+
os.writeObject(metadata);
28+
os.flush();
29+
return ByteBuffer.wrap(baos.toByteArray());
30+
} catch (Exception e) {
31+
throw Exceptions.propagate(e);
32+
}
33+
}
34+
}

codec-parent/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<name>ScaleCube/ClusterCodec</name>
1515

1616
<modules>
17+
<module>codec-jdk</module>
1718
<module>codec-jackson</module>
1819
</modules>
1920

0 commit comments

Comments
 (0)